Guest User

Untitled

a guest
Oct 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. char Shader::vertex[] =
  2.     "\n"
  3.     "#version 120\n"
  4.     "\n"
  5.     "attribute vec4 InPosition;"
  6.     "attribute vec3 InNormal;"
  7.     "attribute vec4 InMaterialAmbient;"
  8.     "uniform mat4 cl_ModelViewMatrix;"
  9.     "uniform mat4 cl_ModelViewProjectionMatrix;"
  10.     "uniform mat3 cl_NormalMatrix;"
  11.     "varying vec3 WorldSpaceNormal; \n"
  12.     "varying vec3 WorldSpacePosition; \n"
  13.     "varying vec3 ObjPos;\n"
  14.     "varying vec4 MaterialAmbient;\n"
  15.     "\n"
  16.     "void main()\n"
  17.     "{\n"
  18.     "   gl_Position = cl_ModelViewProjectionMatrix * InPosition;\n"
  19.     "   WorldSpaceNormal = normalize( cl_NormalMatrix * InNormal );\n"
  20.     "   WorldSpacePosition = InPosition.xyz;\n"
  21.     "   MaterialAmbient = InMaterialAmbient;\n"
  22.     "   ObjPos = vec3(cl_ModelViewMatrix * InPosition);\n"
  23.     "}\n"
  24.     ;
  25.  
  26. char Shader::fragment[] =
  27.     "\n"
  28.     "#version 120\n"
  29.     "\n"
  30.     "varying vec3 WorldSpaceNormal; \n"
  31.     "varying vec3 WorldSpacePosition; \n"
  32.     "varying vec3 ObjPos;\n"
  33.     "varying vec4 MaterialAmbient;\n"
  34.     "\n"
  35.     "uniform float MaterialShininess;\n"
  36.     "uniform vec4 MaterialEmission;\n"
  37.     "uniform vec4 MaterialSpecular;\n"
  38.     "\n"
  39.     "uniform vec4 LightPosition;\n"
  40.     "uniform vec4 LightHalfVector;\n"
  41.     "uniform vec4 LightSpecular;\n"
  42.     "uniform vec4 LightDiffuse;\n"
  43.     "\n"
  44.     "void main()\n"
  45.     "{\n"
  46.     "   vec3 texture_color = MaterialAmbient.rgb;\n"
  47.     "\n"
  48.     "   vec3 eye = -normalize(ObjPos); \n"
  49.     "   vec4 diff = vec4(0); \n"
  50.     "   vec4 spec = vec4(0); \n"
  51.     "\n"
  52.     "   float nDotL = max(0.0, dot(WorldSpaceNormal, LightPosition.xyz)); \n"
  53.     "   float pf; \n"
  54.     "   if (nDotL == 0.0)\n"
  55.     "   {\n"
  56.     "       pf = 0.0; \n"
  57.     "   }else\n"
  58.     "   {\n"
  59.     "           float nDotHV = max(0.0, dot(WorldSpaceNormal, LightHalfVector.xyz));\n"
  60.     "           pf = pow(nDotHV, MaterialShininess);\n"
  61.     "   }\n"
  62.     "   \n"
  63.     "   spec += LightSpecular * pf; \n"
  64.     "   diff += LightDiffuse * nDotL;\n"
  65.     "\n"
  66.     "   vec4 final_texture_color = vec4(texture_color,1.0);\n"
  67.     "\n"
  68.     "   const vec4 ambient_light = vec4(0.05, 0.05, 0.05, 1.0);\n"
  69.     "   gl_FragColor = ambient_light * final_texture_color + (diff + MaterialEmission) * final_texture_color +spec * MaterialSpecular;\n"
  70.     "\n"
  71.     "   gl_FragColor.a = MaterialAmbient.a;\n"
  72.  
  73.     "}\n"
  74.     ;
  75.  
  76. Shader::Shader(CL_GraphicContext &gc)
  77. {
  78.     CL_ShaderObject vertex_shader(gc, cl_shadertype_vertex, vertex);
  79.     if(!vertex_shader.compile())
  80.     {
  81.         throw CL_Exception(cl_format("Unable to compile vertex shader object: %1", vertex_shader.get_info_log()));
  82.     }
  83.  
  84.     CL_ShaderObject fragment_shader(gc, cl_shadertype_fragment, fragment);
  85.     if(!fragment_shader.compile())
  86.     {
  87.         throw CL_Exception(cl_format("Unable to compile fragment shader object: %1", fragment_shader.get_info_log()));
  88.     }
  89.  
  90.     program_object = CL_ProgramObject(gc);
  91.     program_object.attach(vertex_shader);
  92.     program_object.attach(fragment_shader);
  93.     program_object.bind_attribute_location(0, "InPosition");
  94.     program_object.bind_attribute_location(1, "InNormal");
  95.     program_object.bind_attribute_location(2, "InMaterialAmbient");
  96.     if (!program_object.link())
  97.     {
  98.         throw CL_Exception(cl_format("Unable to link program object: %1", program_object.get_info_log()));
  99.     }
  100.  
  101.     material_shininess = 64.0f;
  102.     material_emission = CL_Vec4f(0.0f, 0.0f, 0.0f, 1.0f);
  103.     //material_ambient =  CL_Vec4f(0.9f, 0.2f, 0.2f, 1.0f);
  104.     material_specular = CL_Vec4f(0.0f, 0.0f, 0.0f, 1.0f);
  105.  
  106.     light_position = CL_Vec4f(0.0f, 0.0f, 1.0f, 0.0f);
  107.     light_specular = CL_Vec4f(0.7f, 0.7f, 0.7f, 1.0f);
  108.     light_diffuse = CL_Vec4f(0.7f, 0.7f, 0.7f, 1.0f);
  109.  
  110.  
  111. }
  112.  
  113. void Shader::Set(CL_GraphicContext &gc)
  114. {
  115.         program_object.set_uniform1f("MaterialShininess", material_shininess);
  116.         program_object.set_uniform4f("MaterialEmission", material_emission);
  117.         program_object.set_uniform4f("MaterialSpecular", material_specular);
  118.         //program_object.set_uniform4f("MaterialAmbient", material_ambient);
  119.         program_object.set_uniform4f("LightPosition", light_position);
  120.         CL_Vec4f light_halfvector(0.0f, 0.0f, 1.0f, 0.0f);
  121.         light_halfvector += light_position;
  122.         light_halfvector.normalize3();
  123.         program_object.set_uniform4f("LightHalfVector", light_halfvector);
  124.         program_object.set_uniform4f("LightSpecular", light_specular);
  125.         program_object.set_uniform4f("LightDiffuse", light_diffuse);
  126. }
  127.  
  128. void Shader::Use(CL_GraphicContext &gc)
  129. {
  130.     gc.set_program_object(program_object);
  131. }
Add Comment
Please, Sign In to add comment