first lighting attempt; specular not working
This commit is contained in:
parent
4e40f6af5c
commit
8bd56f42aa
@ -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;
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user