From 8bd56f42aa9abaae62985c8837afc42cc75f2a42 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 1 May 2011 08:34:13 -0400 Subject: [PATCH] first lighting attempt; specular not working --- lighting/f_shader.glsl | 28 +++++++++++++++++++++++++++- lighting/test.cc | 21 +++++++++++++-------- lighting/v_shader.glsl | 9 ++++++++- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/lighting/f_shader.glsl b/lighting/f_shader.glsl index db724ca..e1fdafb 100644 --- a/lighting/f_shader.glsl +++ b/lighting/f_shader.glsl @@ -1,5 +1,31 @@ +varying vec3 eye_pos_i; +varying vec3 normal_i; +varying vec4 diffuse_i; + void main(void) { - gl_FragColor = gl_Color; + vec3 n, lightDir; + vec4 color; + float NdotL, RdotEye; + + lightDir = vec3(-0.1, 0, -0.9); + color = vec4(0.2, 0.2, 0.2, 1.0); /* ambient light */ + n = normalize(normal_i); + + NdotL = max(dot(n, -lightDir), 0.0); + + if (NdotL > 0.0) + { + /* diffuse component */ + color += diffuse_i * NdotL; + /* specular component */ + RdotEye = dot(normalize(eye_pos_i), normalize(reflect(-lightDir, n))); + if (RdotEye > 0.0) + { + color += pow(RdotEye, 96.5); + } + } + + gl_FragColor = color; } diff --git a/lighting/test.cc b/lighting/test.cc index 73db8bb..1c98886 100644 --- a/lighting/test.cc +++ b/lighting/test.cc @@ -15,13 +15,13 @@ using namespace std; #define STRIDE(a, b) ((unsigned long)(b) - (unsigned long)(a)) #define OFFSET(a, b) ((const GLvoid *)STRIDE((a), (b))) -const GLfloat data[][2][3] = { - {{-1.0, -0.6, 0.0}, {1, 0, 0}}, - {{-0.2, -0.6, 0.0}, {0, 1, 0}}, - {{-0.6, 0.6, 0.0}, {0, 0, 1}}, - {{0.2, -0.6, 0.0}, {1, 0, 0}}, - {{1.0, -0.6, 0.0}, {0, 1, 0}}, - {{0.6, 0.6, -10.0}, {0, 0, 1}} +const GLfloat data[][3][3] = { + {{-1.0, -0.6, 0.0}, {1, 0, 0}, {0, 0, 1}}, + {{-0.2, -0.6, 0.0}, {0, 1, 0}, {0, 0, 1}}, + {{-0.6, 0.6, 0.0}, {0, 0, 1}, {0, 0, 1}}, + {{0.2, -0.6, 0.0}, {1, 0, 0}, {0, 0.7071, 0.7071}}, + {{1.0, -0.6, 0.0}, {0, 1, 0}, {0, 0.7071, 0.7071}}, + {{0.6, 0.6, -10.0}, {0, 0, 1}, {0, 0.7071, 0.7071}} }; const GLushort indices[] = { 0, 1, 2, 3, 4, 5 @@ -29,7 +29,8 @@ const GLushort indices[] = { GLuint program, vs, fs, data_vbo, index_vbo; enum Locations { LOC_POSITION, - LOC_COLOR + LOC_COLOR, + LOC_NORMAL }; char * loadFile(const char *fname) @@ -154,6 +155,7 @@ bool init(int width, int height) glBindAttribLocation(program, LOC_POSITION, "pos"); glBindAttribLocation(program, LOC_COLOR, "color"); + glBindAttribLocation(program, LOC_NORMAL, "normal"); glLinkProgram(program); @@ -183,10 +185,13 @@ void display(void) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_vbo); glEnableVertexAttribArray(LOC_POSITION); glEnableVertexAttribArray(LOC_COLOR); + glEnableVertexAttribArray(LOC_NORMAL); glVertexAttribPointer(LOC_POSITION, 3, GL_FLOAT, GL_FALSE, STRIDE(&data[0], &data[1]), OFFSET(&data[0], &data[0][0])); glVertexAttribPointer(LOC_COLOR, 3, GL_FLOAT, GL_FALSE, STRIDE(&data[0], &data[1]), OFFSET(&data[0], &data[0][1])); + glVertexAttribPointer(LOC_NORMAL, 3, GL_FLOAT, GL_FALSE, + STRIDE(&data[0], &data[1]), OFFSET(&data[0], &data[0][2])); glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(indices[0]), GL_UNSIGNED_SHORT, OFFSET(&indices, &indices[0])); SDL_GL_SwapBuffers(); diff --git a/lighting/v_shader.glsl b/lighting/v_shader.glsl index c76673f..4881a17 100644 --- a/lighting/v_shader.glsl +++ b/lighting/v_shader.glsl @@ -1,9 +1,16 @@ attribute vec3 pos; attribute vec3 color; +attribute vec3 normal; + +varying vec3 eye_pos_i; +varying vec3 normal_i; +varying vec4 diffuse_i; void main(void) { gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1); - gl_FrontColor = vec4(color, 1); + eye_pos_i = vec3(gl_Position.x, gl_Position.y, gl_Position.z); + normal_i = gl_NormalMatrix * normal; + diffuse_i = vec4(color, 1); }