Last active
December 29, 2015 12:39
-
-
Save sharavsambuu/7671924 to your computer and use it in GitHub Desktop.
blinn-phong reference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GLint uloc_MV, uloc_P, uloc_texDiff, uloc_texNorm, uloc_texSpec, uloc_lightPos, uloc_lightDir, uloc_lightDiff, uloc_lightSpec, uloc_amb, uloc_viewDir; | |
void GLdummyShaders() { | |
const GLchar* vertsrc = "#version 150 core\n" | |
"in vec3 inPos;\n" | |
"in vec2 inTexCoord;\n" | |
"out vec3 pos;\n" | |
"out vec2 texCoord;\n" | |
"uniform mat3 MV;\n" | |
"uniform mat4 P;\n" | |
"void main() {\n" | |
" pos = MV*inPos;\n" | |
" texCoord = inTexCoord;\n" | |
" gl_Position = P*vec4(pos, 1.0);\n" | |
"}"; | |
const GLchar* fragsrc = "#version 150 core\n" | |
"out vec4 outColor;\n" | |
"smooth in vec3 pos;\n" | |
"in vec2 texCoord;\n" | |
"uniform sampler2D texDiff;\n" | |
"uniform sampler2D texNorm;\n" | |
"uniform sampler2D texSpec;\n" | |
"uniform vec3 lightPos;\n" | |
"uniform vec3 lightDir;\n" | |
"uniform vec3 lightDiff;\n" | |
"uniform vec3 lightSpec;\n" | |
"uniform vec3 amb;\n" | |
"uniform vec3 viewDir;\n" | |
"void main() {\n" | |
" float dist = length(lightPos - pos);\n" | |
" vec3 norm = texture(texNorm, texCoord).xyz;\n" | |
" vec3 diff = max(0.0, dot(lightDir, norm))*lightDiff*texture(texDiff, texCoord).rgb;\n" | |
" vec3 spec = pow(dot(norm, normalize(lightDir+viewDir)), texture(texSpec, texCoord).r)*lightSpec;\n" | |
" outColor = vec4(amb + diff + spec, 1.0);\n" | |
"}"; | |
int vertid = glCreateShader(GL_VERTEX_SHADER); | |
int fragid = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(vertid, 1, &vertsrc, NULL); | |
glCompileShader(vertid); | |
glShaderSource(fragid, 1, &fragsrc, NULL); | |
glCompileShader(fragid); | |
int progid = glCreateProgram(); | |
glAttachShader(progid, vertid); | |
glAttachShader(progid, fragid); | |
glBindAttribLocation(progid, 0, "inPos"); | |
glBindAttribLocation(progid, 1, "inTexCoord"); | |
glLinkProgram(progid); | |
uloc_MV = glGetUniformLocation(progid, "MV"); | |
uloc_P = glGetUniformLocation(progid, "P"); | |
uloc_texDiff = glGetUniformLocation(progid, "texDiff"); | |
uloc_texNorm = glGetUniformLocation(progid, "texNorm"); | |
uloc_texSpec = glGetUniformLocation(progid, "texSpec"); | |
uloc_lightPos = glGetUniformLocation(progid, "lightPos"); | |
uloc_lightDir = glGetUniformLocation(progid, "lightDir"); | |
uloc_lightDiff = glGetUniformLocation(progid, "lightDiff"); | |
uloc_lightSpec = glGetUniformLocation(progid, "lightSpec"); | |
uloc_amb = glGetUniformLocation(progid, "amb"); | |
uloc_viewDir = glGetUniformLocation(progid, "viewDir"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment