"//uniform mat3 u_inv_modelview;\n"
"uniform mat4 u_inv_modelview;\n"
"uniform vec3 u_eyePosition;\n"
+ "uniform bool u_useTexture;\n"
"\n"
"uniform material u_material;\n"
"uniform light u_light[8];\n"
"\n"
"attribute vec4 a_position;\n"
"attribute vec4 a_texcoord;\n"
- "attribute vec4 a_color;\n"
"attribute vec3 a_normal;\n"
"\n"
"varying vec2 v_texcoord;\n"
"varying vec4 v_color;\n"
"\n"
- "vec4 pointLight(int i)\n"
+ "vec4\n"
+ "pointLight(int i)\n"
"{\n"
" vec3 lightdir, n, h, eye, distance, vertexPos;\n"
" vec4 color = vec4(c_zero, c_zero, c_zero, c_zero);\n"
" color.a = u_material.diffuse.a;\n"
"\n"
" v_color = color;\n"
- " v_texcoord = a_texcoord.xy;\n"
+ " if (u_useTexture == true)\n"
+ " {\n"
+ " v_texcoord = a_texcoord.xy;\n"
+ " }\n"
+ " else\n"
+ " {\n"
+ " v_texcoord = vec2(0, 0);\n"
+ " }\n"
" gl_Position = u_mvp * a_position;\n"
"}\n";
static const char strFragmentShader_light[] =
"precision highp float;\n"
"uniform sampler2D u_tex2d;\n"
+ "uniform bool u_useTexture;\n"
"varying vec2 v_texcoord;\n"
"varying vec4 v_color;\n"
"void main()\n"
"{\n"
- " gl_FragColor.rgba = texture2D(u_tex2d, v_texcoord).bgra * v_color;\n"
+ " if (u_useTexture == true)\n"
+ " {\n"
+ " gl_FragColor.rgba = texture2D(u_tex2d, v_texcoord).bgra * v_color;\n"
+ " }\n"
+ " else\n"
+ " {\n"
+ " gl_FragColor.rgba = v_color;\n"
+ " }\n"
+ "}\n";
+
+ static const char strVertexShader_PerPixellight[] =
+ "uniform mat4 u_mvp;\n"
+ "uniform mat4 u_modelview;\n"
+ "uniform mat4 u_inv_modelview;\n"
+ "uniform bool u_useTexture;\n"
+ "\n"
+ "attribute vec4 a_position;\n"
+ "attribute vec2 a_texcoord;\n"
+ "attribute vec3 a_normal;\n"
+ "\n"
+ "varying vec4 v_position;\n"
+ "varying vec2 v_texcoord;\n"
+ "varying vec3 v_normal;\n"
+ "\n"
+ "void\n"
+ "main()\n"
+ "{\n"
+ " v_position = u_modelview * a_position;\n"
+ " v_normal = (u_inv_modelview * vec4(a_normal, 0.0)).xyz;\n"
+ " if (u_useTexture == true)\n"
+ " {\n"
+ " v_texcoord = a_texcoord.xy;\n"
+ " }\n"
+ " else\n"
+ " {\n"
+ " v_texcoord = vec2(0, 0);\n"
+ " }\n"
+ " gl_Position = u_mvp * a_position;\n"
"}\n";
static const char strFragmentShader_PerPixellight[] =
" float shininess;\n"
"};\n"
"\n"
+ "const float c_zero = 0.0;\n"
+ "const float c_one = 1.0;\n"
+ "\n"
"uniform vec3 u_eyePosition;\n"
- "uniform int u_lightCount;\n"
- "uniform sampler2D u_tex2d;\n"
+ "\n"
"uniform material u_material;\n"
"uniform light u_light[8];\n"
+ "uniform int u_lightCount;\n"
+ "\n"
+ "uniform sampler2D u_tex2d;\n"
+ "uniform bool u_useTexture;\n"
"\n"
"varying vec4 v_position;\n"
"varying vec2 v_texcoord;\n"
"varying vec3 v_normal;\n"
"\n"
- "vec4 pointLight(int i)\n"
+ "vec4\n"
+ "pointLight(int i)\n"
"{\n"
- " vec4 result = vec4(0.0, 0.0, 0.0, 0.0);\n"
- " vec4 diffuse = vec4(0.0, 0.0, 0.0, 0.0);\n"
- " vec4 ambient = vec4(0.0, 0.0, 0.0, 0.0);\n"
- " vec4 specular = vec4(0.0, 0.0, 0.0, 0.0);\n"
- " vec3 normal = normalize(v_normal);\n"
- " vec3 lightdir;\n"
- " vec3 v = normalize(u_eyePosition - v_position.xyz);\n"
- " vec3 r = reflect(-v, normal);\n"
+ " vec3 lightdir, n, h, eye, distance;\n"
+ " vec4 color = vec4(c_zero, c_zero, c_zero, c_zero);\n"
+ " float ndotl, ndoth, attenuation;\n"
"\n"
- " float att = 0.0;\n"
+ " n = normalize(v_normal);\n"
"\n"
" lightdir = u_light[i].position.xyz - v_position.xyz;\n"
- " lightdir = normalize(lightdir);\n"
"\n"
- " float denum = (u_light[i].attenuation.x + u_light[i].attenuation.y*u_light[i].attenuation.x + u_light[i].attenuation.z*u_light[i].attenuation.z );\n"
+ " // compute distance attenuation\n"
+ " distance.x = c_one;\n"
+ " distance.z = dot(lightdir, lightdir);\n"
+ " distance.y = sqrt(distance.z);\n"
"\n"
- " if (denum > 0.0)\n"
- " att = 1.0 / denum;\n"
+ " attenuation = c_one / dot(distance, u_light[i].attenuation);"
"\n"
- " ambient = u_light[i].ambient * u_material.ambient;\n"
- " diffuse += u_light[i].diffuse *u_material.diffuse *texture2D(u_tex2d, v_texcoord).bgra* max(0.0, dot(lightdir, normal));\n"
- " specular = u_light[i].specular * u_material.specular * pow(max(dot(r, lightdir), 0.0), u_material.shininess );\n"
+ " if (attenuation > c_zero)\n"
+ " {\n"
+ " lightdir = normalize(lightdir);\n"
+ "\n"
+ " color = u_light[i].ambient * u_material.ambient;\n"
+ "\n"
+ " ndotl = max(c_zero, dot(n, lightdir));\n"
+ " if (u_useTexture == true)\n"
+ " {\n"
+ " color += ndotl * u_light[i].diffuse * u_material.diffuse * texture2D(u_tex2d, v_texcoord).bgra;\n"
+ " }\n"
+ " else\n"
+ " {\n"
+ " color += ndotl * u_light[i].diffuse * u_material.diffuse;\n"
+ " }\n"
+ "\n"
+ " eye = normalize(u_eyePosition - v_position.xyz);\n"
+ " h = normalize(lightdir + eye);\n"
+ " ndoth = dot(n, h);\n"
"\n"
- " result = ambient + diffuse + specular;\n"
+ " if (ndoth > c_zero)\n"
+ " {\n"
+ " color += pow(ndoth, u_material.shininess) * u_material.specular * u_light[i].specular;\n"
+ " }\n"
"\n"
- "if (att > 0.0)\n"
- " result*=att;\n"
+ " color *= attenuation;\n"
+ " }\n"
"\n"
- " return result;\n"
- " \n"
+ " return color;\n"
"}\n"
"\n"
- "vec4 spotLight(int i)\n"
+ "vec4\n"
+ "spotLight(int i)\n"
"{\n"
- " vec4 result = vec4(0.0, 0.0, 0.0, 0.0);\n"
- " vec4 diffuse = vec4(0.0, 0.0, 0.0, 0.0);\n"
- " vec4 ambient = vec4(0.0, 0.0, 0.0, 0.0);\n"
- " vec4 specular = vec4(0.0, 0.0, 0.0, 0.0);\n"
- "\n"
- " vec3 normal = normalize(v_normal);\n"
- " vec3 lightdir;\n"
+ " vec3 lightdir, n, h, eye, distance;\n"
+ " vec4 color = vec4(c_zero, c_zero, c_zero, c_zero);\n"
+ " float ndotl, ndoth, attenuation, spot_factor;\n"
"\n"
- " vec3 v = normalize(u_eyePosition - v_position.xyz);\n"
- " vec3 r = reflect(-v, normal);\n"
- " vec3 d = normalize(u_light[i].direction.xyz);\n"
+ " n = normalize(v_normal);\n"
"\n"
- " float att = 0.0;\n"
- " float spotEffect = 0.0;\n"
+ " distance = u_light[i].position.xyz - v_position.xyz;\n"
"\n"
- " lightdir = u_light[i].position.xyz - v_position.xyz;\n"
- " lightdir = normalize(lightdir);\n"
+ " lightdir = normalize(distance);\n"
"\n"
- " spotEffect = dot( -lightdir, d);\n"
+ " // compute spot factor\n"
+ " spot_factor = dot(-lightdir, normalize(u_light[i].direction));\n"
"\n"
- " float denum = (u_light[i].attenuation.x + u_light[i].attenuation.y*u_light[i].attenuation.x + u_light[i].attenuation.z*u_light[i].attenuation.z );\n"
- " if (denum > 0.0)\n"
- " att = 1.0 / denum;\n"
+ " if (spot_factor >= cos(radians(u_light[i].cutoff)))\n"
+ " {\n"
+ " // compute distance attenuation\n"
+ " distance.z = dot(distance, distance);\n"
+ " distance.y = sqrt(distance.z);\n"
+ " distance.x = c_one;\n"
"\n"
- " if (spotEffect >= cos(radians(u_light[i].cutoff)))\n"
- " spotEffect = pow(spotEffect, u_light[i].exponent);\n"
+ " attenuation = pow(spot_factor, u_light[i].exponent) / dot(distance, u_light[i].attenuation);\n"
+ " }\n"
" else\n"
- " spotEffect = 0.0;\n"
+ " attenuation = c_zero;\n"
"\n"
- " att*= spotEffect;\n"
+ " if (attenuation > c_zero)\n"
+ " {\n"
+ " color = u_light[i].ambient * u_material.ambient;\n"
"\n"
+ " ndotl = max(c_zero, dot(n, lightdir));\n"
+ " if (u_useTexture == true)\n"
+ " {\n"
+ " color += ndotl * u_light[i].diffuse * u_material.diffuse * texture2D(u_tex2d, v_texcoord).bgra;\n"
+ " }\n"
+ " else\n"
+ " {\n"
+ " color += ndotl * u_light[i].diffuse * u_material.diffuse;\n"
+ " }\n"
"\n"
- " if (att > 0.0)\n"
- " {\n"
- " diffuse += u_light[i].diffuse *u_material.diffuse * texture2D(u_tex2d, v_texcoord).bgra * max(0.0, dot(lightdir, normal));\n"
- " ambient = u_light[i].ambient * u_material.ambient;\n"
- " specular = u_light[i].specular * u_material.specular * pow(max(dot(r, lightdir), 0.0), u_material.shininess );\n"
- " }\n"
+ " eye = normalize(u_eyePosition - v_position.xyz);\n"
+ " h = normalize(lightdir + eye);\n"
+ " ndoth = dot(n, h);\n"
"\n"
- " result = (ambient + diffuse + specular) * att;\n"
+ " if (ndoth > c_zero)\n"
+ " {\n"
+ " color += pow(ndoth, u_material.shininess) * u_material.specular * u_light[i].specular;\n"
+ " }\n"
"\n"
- " return result;\n"
+ " color *= attenuation;\n"
+ " }\n"
+ "\n"
+ " return color;\n"
"}\n"
"\n"
- "vec4 directionalLight(int i)\n"
+ "vec4\n"
+ "directionalLight(int i)\n"
"{\n"
- " vec4 result = vec4(0.0, 0.0, 0.0, 0.0);\n"
- " vec4 diffuse = vec4(0.0, 0.0, 0.0, 0.0);\n"
- " vec4 ambient = vec4(0.0, 0.0, 0.0, 0.0);\n"
- " vec4 specular = vec4(0.0, 0.0, 0.0, 0.0);\n"
- " \n"
- " vec3 normal = normalize(v_normal);\n"
- " vec3 lightdir = normalize(u_light[i].position.xyz);\n"
- " vec3 v = normalize(u_eyePosition - v_position.xyz);\n"
- " vec3 r = reflect(-v, normal);\n"
- " \n"
- " diffuse = u_light[i].diffuse * u_material.diffuse * texture2D(u_tex2d, v_texcoord).bgra * max(0.0, dot(lightdir, normal));\n"
- " ambient = u_light[i].ambient * u_material.ambient;\n"
- " specular = u_light[i].specular * u_material.specular * pow(max(dot(r, lightdir), 0.0), u_material.shininess);\n"
- " \n"
- " result = ambient + diffuse + specular;\n"
- " \n"
- " return result;\n"
+ " vec3 lightdir, n, h, eye;\n"
+ " vec4 color;\n"
+ " float ndotl, ndoth;\n"
+ "\n"
+ " n = normalize(v_normal);\n"
+ "\n"
+ " lightdir = normalize(u_light[i].position.xyz);\n"
+ "\n"
+ " color = u_light[i].ambient * u_material.ambient;\n"
+ "\n"
+ " ndotl = max(c_zero, dot(n, lightdir));\n"
+ " if (u_useTexture == true)\n"
+ " {\n"
+ " color += ndotl * u_light[i].diffuse * u_material.diffuse * texture2D(u_tex2d, v_texcoord).bgra;\n"
+ " }\n"
+ " else\n"
+ " {\n"
+ " color += ndotl * u_light[i].diffuse * u_material.diffuse;\n"
+ " }\n"
+ "\n"
+ " eye = normalize(u_eyePosition - v_position.xyz);\n"
+ " h = normalize(lightdir + eye);\n"
+ " ndoth = dot(n, h);\n"
+ "\n"
+ " if (ndoth > c_zero)\n"
+ " {\n"
+ " color += pow(ndoth, u_material.shininess) * u_material.specular * u_light[i].specular;\n"
+ " }\n"
+ "\n"
+ " return color;\n"
"}\n"
"\n"
"void main()\n"
"{\n"
- " vec4 res_color = vec4(0,0,0,1);\n"
- " if (u_lightCount > 0 )\n"
+ " vec4 color = vec4(c_zero, c_zero, c_zero, c_zero);\n"
+ "\n"
+ " for (int i = 0; i < u_lightCount; i++)\n"
" {\n"
- " for (int i = 0; i < u_lightCount; i++)\n"
- " {\n"
- " if (u_light[i].type == 0)\n"
- " res_color += pointLight(i);\n"
- " else if (u_light[i].type == 1)\n"
- " res_color += spotLight(i);\n"
- " else if (u_light[i].type == 2)\n"
- " res_color += directionalLight(i);\n"
- " }\n"
- " res_color += u_material.emissive;\n"
- " res_color.a = u_material.diffuse.a;\n"
+ " if (u_light[i].type == 0)\n"
+ " color += pointLight(i);\n"
+ " else if (u_light[i].type == 1)\n"
+ " color += spotLight(i);\n"
+ " else if (u_light[i].type == 2)\n"
+ " color += directionalLight(i);\n"
" }\n"
- " gl_FragColor.rgba = res_color;\n"
- "}";
- static const char strVertexShader_PerPixellight[] =
- "uniform mat4 u_mvp;\n"
- "uniform mat4 u_modelview;\n"
- "//uniform mat3 u_inv_modelview;\n"
- "uniform mat4 u_inv_modelview;\n"
"\n"
- "attribute vec4 a_position;\n"
- "attribute vec2 a_texcoord;\n"
- "attribute vec3 a_normal;\n"
- "varying vec4 v_position;\n"
- "varying vec2 v_texcoord;\n"
- "varying vec3 v_normal;\n"
+ " color += u_material.emissive;\n"
+ "// color += u_material.emissive + u_material.ambient * u_scene_ambient; //todo\n"
"\n"
- "void\n"
- "main()\n"
- "{\n"
- " v_position = u_modelview * a_position;\n"
- " vec4 normal = u_inv_modelview * vec4(a_normal, 0.0);\n"
- " v_normal = normal.xyz;\n"
- " v_texcoord = a_texcoord.xy;\n"
- " gl_Position = u_mvp * a_position;\n"
- "}\n";
+ " if (u_useTexture == true)\n"
+ " {\n"
+ " color.a = u_material.diffuse.a * texture2D(u_tex2d, v_texcoord).a;\n"
+ " }\n"
+ " else\n"
+ " {\n"
+ " color.a = u_material.diffuse.a;\n"
+ " }\n"
+ "\n"
+ " gl_FragColor = color;\n"
+ "}";
Shader vertexColor, fragmentColor;
Shader vertexUniformColor, fragmentUniformColor;
"\n"
"attribute vec4 a_position;\n"
"attribute vec4 a_texcoord;\n"
- "attribute vec4 a_color;\n"
"attribute vec3 a_normal;\n"
"\n"
"varying vec2 v_texcoord;\n"
"void main()\n"
"{\n"
" gl_FragColor.rgba = texture2D(u_tex2d, v_texcoord).bgra * v_color;\n"
+ " gl_FragColor.rgba = texture2D(u_tex2d, v_texcoord).bgra * v_color;\n"
"}\n";
Shader vertexColor, fragmentColor;