When a key is pressed, changeVertColor() is called to edit the verts array. After the verts array is edited, the the event loop in main updates the vbo containing the vertex information to be drawn to the screen.
void changeVertColor(int v_index, GLfloat verts[])
{
int r_index = v_index * 5 + 2;
verts[r_index] = (double)rand() / RAND_MAX;
verts[r_index + 1] = (double)rand() / RAND_MAX;
verts[r_index + 2] = (double)rand() / RAND_MAX;
printf("\tnew color: (%f, %f, %f)\n", verts[r_index], verts[r_index+1], verts[r_index+2]);
}
Per-fragment lighting that uses the halfway vector to compute specularity.
const GLchar* BP_fragmentSource =
"#version 150 core\n"
"in vec3 Color;"
"in vec3 normal;"
"in vec3 pos;"
"in vec3 lightDir;"
"out vec4 outColor;"
//lighting constant(s)
"const float ambient = .3;"
"void main() {"
" vec3 h = normalize(lightDir - pos);"
" vec3 diffuseC = Color*max(dot(lightDir,normal),0.0);"
" vec3 ambC = Color*ambient;"
" vec3 reflectDir = reflect(-lightDir,normal);"
" vec3 viewDir = normalize(-pos);"
" float spec = max(dot(h, normal),0.0);"
" if (dot(lightDir,normal) <= 0.0)spec = 0;"
" vec3 specC = vec3(1.0,1.0,1.0)*pow(spec,4);"
" outColor = vec4(diffuseC+ambC+specC, 1.0);"
"}";
Per-vertex lighting that uses the Phong lighting model.
const GLchar* G_vertexSource =
"#version 150 core\n"
"const vec3 inColor = vec3(0.f,0.7f,0.f);"
"const vec3 inlightDir = normalize(vec3(1,0,0));"
"in vec3 position;"
"in vec3 inNormal;"
"out vec4 Color;"
"uniform mat4 model;"
"uniform mat4 view;"
"uniform mat4 proj;"
//lighting constant(s)
"const float ambient = .3;"
"void main() {"
//transform into correct space
" gl_Position = proj * view * model * vec4(position,1.0);"
//calc pos and normal for shading
" vec4 pos4 = (view * model * vec4(position,1.0));"
" vec3 pos = pos4.xyz/pos4.w;"
" vec4 norm4 = transpose(inverse(view*model)) * vec4(inNormal,0.0);"
" vec3 normal = norm4.xyz;"
" vec3 lightDir = (view * vec4(inlightDir,0)).xyz;"
//calc per vertex shading
" vec3 diffuseC = inColor*max(dot(lightDir,normal),0.0);"
" vec3 ambC = inColor*ambient;"
" vec3 reflectDir = reflect(-lightDir,normal);"
" vec3 viewDir = normalize(-pos);"
" float spec = max(dot(reflectDir,viewDir),0.0);"
" if (dot(lightDir,normal) <= 0.0)spec = 0;"
" vec3 specC = vec3(1.0,1.0,1.0)*pow(spec,4);"
" Color = vec4(diffuseC+ambC+specC, 1.0);"
"}";
When a key is pressed, main calls onKeyUp() to process the KeyboardEvent. Translation is done only in the image plane using the arrow keys. Rotation is done around each of the 3 model axes (x,y,z) using the W-S, A-D, and Q-E key pairings to rotate one or the other direction around each axis separately.
void onKeyUp(SDL_KeyboardEvent & event)
{
if (!saveOutput)
{
switch (event.keysym.sym)
{
//////////////////////////////
//TRANSLATION WITH ARROW KEYS
//////////////////////////////
case SDLK_UP: //position z direction
printf("Up arrow pressed - translate positive z\n");
cur_z_position += 0.1;
break;
case SDLK_DOWN: //negative z direction
printf("Down arrow pressed - translate negative z\n");
cur_z_position -= 0.1;
break;
case SDLK_RIGHT: //positive y direction
printf("Right arrow pressed - translate positive y\n");
cur_y_position += 0.1;
break;
case SDLK_LEFT: //negative y direction
printf("Left arrow pressed - translate negative y\n");
cur_y_position -= 0.1;
break;
//////////////////////////
//ROTATION WITH WSDAEQ KEYS
//////////////////////////
case SDLK_w: //about y-axis (left hand rule - into screen)
printf("W key pressed - y rotate left hand rule - into screen\n");
cur_y_rotation -= 0.1;
break;
case SDLK_s: //about x-axis (left hand rule - out of screen)
printf("S key pressed - y rotate right hand rule - into screen\n");
cur_y_rotation += 0.1;
break;
case SDLK_d: //about z-axis (right hand rule - into screen)
printf("D key pressed - z rotate right hand rule - into screen\n");
cur_z_rotation += 0.1;
break;
case SDLK_a: //about z-axis (left hand rule - into screen)
printf("A key pressed - z rotate right hand rule - out of screen\n");
cur_z_rotation -= 0.1;
break;
case SDLK_e: //about x-axis (left hand rule - down)
printf("E key pressed - x rotate left hand rule - down\n");
cur_x_rotation -= 0.1;
break;
case SDLK_q: //about x-axis (right hand rule - down)
printf("Q key pressed - x rotate right hand rule - down\n");
cur_x_rotation += 0.1;
break;
default:
break;
}//END switch
}
}