DefaultShader.cpp
#include "DefaultShader.h"
const char defaultShader::vShader[] =
"varying vec3 eyeNormal;\n"
"\n"
"void main() {\n"
" gl_Position = ftransform();\n"
" eyeNormal = normalize(gl_NormalMatrix*gl_Normal);\n"
" gl_FrontColor = vec4(0.0, 0.0, 1.0, 1.0);\n"
" gl_BackColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
" gl_TexCoord[0] = gl_MultiTexCoord0*4.0;\n"
"}\n\0";
const char defaultShader::fShader[] =
"varying vec3 eyeNormal;\n"
"\n"
"void main(){ \n"
"\n"
"vec3 lightDirection = vec3( 0.5, 0.5, 0.5 );\n"
"vec3 halfVector = normalize( lightDirection + vec3( 0.0, 0.0, 1.0 ));\n"
"vec4 col = step(0.5,mod(gl_TexCoord[0],1.0));\n"
"vec3 color = (abs(1.0-(col.r+col.g))==0.0) ? gl_Color.xyz : vec3(1.0);\n"
"\n"
"vec3 normal = normalize(eyeNormal);\n"
"float dif = abs(dot(normal, lightDirection));\n"
"dif = min( dif+0.2, 1.0);\n"
"float spec = clamp( abs( dot( normal, halfVector) ), 0.0, 1.0);\n"
"spec = pow(spec, 24.0);\n"
"\n"
"gl_FragColor = vec4( color*dif+spec, 1.0);\n"
"}\n\0";
defaultShader::defaultShader() : m_program(0), m_fShader(0), m_vShader(0) {
if (glShadingLanguageSupport) {
m_program = glCreateProgramObject();
m_vShader = glCreateShaderObject( GL_VERTEX_SHADER);
m_fShader = glCreateShaderObject( GL_FRAGMENT_SHADER);
glAttachObject( m_program, m_vShader);
glAttachObject( m_program, m_fShader);
const char *temp = vShader;
glShaderSource( m_vShader, 1, &temp, NULL);
temp = fShader;
glShaderSource( m_fShader, 1, &temp, NULL);
GLint success;
glCompileShader( m_vShader);
glGetObjectParameteriv( m_vShader, GL_COMPILE_STATUS, &success);
if (! success) {
char log[256];
glGetInfoLog( m_vShader, 256, NULL, log);
}
glCompileShader( m_fShader);
glGetObjectParameteriv( m_fShader, GL_COMPILE_STATUS, &success);
if (! success) {
char log[256];
glGetInfoLog( m_fShader, 256, NULL, log);
}
glLinkProgram( m_program);
glGetObjectParameteriv( m_program, GL_LINK_STATUS, &success);
if (! success) {
char log[256];
glGetInfoLog( m_program, 256, NULL, log);
}
}
}
void defaultShader::bind() {
if (glShadingLanguageSupport)
glUseProgramObject(m_program);
}
void defaultShader::setShapeDependentState() {
}
void defaultShader::unbind() {
if (glShadingLanguageSupport)
glUseProgramObject(0);
}