void material_draw_callback(void *ptr) { OBJMATERIAL *objmaterial = (OBJMATERIAL *) ptr; PROGRAM *program = objmaterial->program; unsigned int i = 0; while (i != program->uniform_count) { if (!strcmp(program->uniform_array[i].name, "DIFFUSE")) { glUniform1i(program->uniform_array[i].location, 1); } else if (!strcmp(program->uniform_array[i].name, "MODELVIEWPROJECTIONMATRIX")) { glUniformMatrix4fv(program->uniform_array[i].location, 1, GL_FALSE, (float *)GFX_get_modelview_projection_matrix()); } else if (!strcmp(program->uniform_array[i].name, "DISSOLVE")) { glUniform1f(program->uniform_array[i].location, objmaterial->dissolve); } else if (!strcmp(program->uniform_array[i].name, "AMBIENT_COLOR")) { glUniform3fv(program->uniform_array[i].location, 1, (float *)&objmaterial->ambient); } else if (!strcmp(program->uniform_array[i].name, "DIFFUSE_COLOR")) { glUniform3fv(program->uniform_array[i].location, 1, (float *)&objmaterial->diffuse); } else if (!strcmp(program->uniform_array[i].name, "SPECULAR_COLOR")) { glUniform3fv(program->uniform_array[i].location, 1, (float *)&objmaterial->specular); } else if (!strcmp(program->uniform_array[i].name, "SHININESS")) { glUniform1f(program->uniform_array[i].location, objmaterial->specular_exponent * 0.128f); } else if (!strcmp(program->uniform_array[i].name, "MODELVIEWMATRIX")) { glUniformMatrix4fv(program->uniform_array[i].location, 1, GL_FALSE, (float *)GFX_get_modelview_matrix()); } else if (!strcmp(program->uniform_array[i].name, "PROJECTIONMATRIX")) { glUniformMatrix4fv(program->uniform_array[i].location, 1, GL_FALSE, (float *)GFX_get_projection_matrix()); } else if (!strcmp(program->uniform_array[i].name, "NORMALMATRIX")) { glUniformMatrix3fv(program->uniform_array[i].location, 1, GL_FALSE, (float *)GFX_get_normal_matrix()); } else if (!strcmp(program->uniform_array[i].name, "LIGHTPOSITION")) { vec3 position = { 0.0f, -3.0f, 10.0f }; vec3 eyeposition = { 0.0f, 0.0f, 0.0f }; vec3_multiply_mat4(&eyeposition, &position, &gfx.modelview_matrix[gfx.modelview_matrix_index - 1]); glUniform3fv(program->uniform_array[i].location, 1, (float *)&eyeposition); } ++i; } }
void material_draw(void *ptr) { OBJMATERIAL *objmaterial = (OBJMATERIAL *) ptr; PROGRAM *program = objmaterial->program; unsigned int i = 0; while (i != program->uniform_count) { if (program->uniform_array[i].constant) { ++i; continue; } else if (!strcmp(program->uniform_array[i].name, "DIFFUSE")) { glUniform1i(program->uniform_array[i].location, 1); program->uniform_array[i].constant = 1; } else if (!strcmp(program->uniform_array[i].name, "BUMP")) { glUniform1i(program->uniform_array[i].location, 4); program->uniform_array[i].constant = 1; } // Matrix Data else if (!strcmp(program->uniform_array[i].name, "MODELVIEWMATRIX")) { glUniformMatrix4fv(program->uniform_array[i].location, 1, GL_FALSE, (float *)GFX_get_modelview_matrix()); } else if (!strcmp(program->uniform_array[i].name, "PROJECTIONMATRIX")) { glUniformMatrix4fv(program->uniform_array[i].location, 1, GL_FALSE, (float *)GFX_get_projection_matrix()); program->uniform_array[i].constant = 1; } else if (!strcmp(program->uniform_array[i].name, "NORMALMATRIX")) { glUniformMatrix3fv(program->uniform_array[i].location, 1, GL_FALSE, (float *)GFX_get_normal_matrix()); } // Material Data else if (!strcmp(program->uniform_array[i].name, "MATERIAL.ambient")) { glUniform4fv(program->uniform_array[i].location, 1, (float *)&objmaterial->ambient); program->uniform_array[i].constant = 1; } else if (!strcmp(program->uniform_array[i].name, "MATERIAL.diffuse")) { glUniform4fv(program->uniform_array[i].location, 1, (float *)&objmaterial->diffuse); } else if (!strcmp(program->uniform_array[i].name, "MATERIAL.specular")) { glUniform4fv(program->uniform_array[i].location, 1, (float *)&objmaterial->specular); } else if (!strcmp(program->uniform_array[i].name, "MATERIAL.shininess")) { glUniform1f(program->uniform_array[i].location, objmaterial->specular_exponent * 0.128f); program->uniform_array[i].constant = 1; } // Lamp Data else if (!strcmp(program->uniform_array[i].name, "LIGHT_FS.color")) { glUniform4fv(program->uniform_array[i].location, 1, (float *)&light->color); program->uniform_array[i].constant = 1; } else if (!strcmp(program->uniform_array[i].name, "LIGHT_VS.direction")) { vec3 direction; LIGHT_get_direction_in_eye_space(light, &gfx.modelview_matrix[gfx.modelview_matrix_index - 1], &direction); glUniform3fv(program->uniform_array[i].location, 1, (float *)&direction); program->uniform_array[i].constant = 1; } ++i; } }
void program_draw_callback( void *ptr ) { PROGRAM *curr_program = ( PROGRAM * )ptr; unsigned int i = 0; while( i != curr_program->uniform_count ) { if( !strcmp( curr_program->uniform_array[ i ].name, "MODELVIEWMATRIX" ) ) { glUniformMatrix4fv( curr_program->uniform_array[ i ].location, 1, GL_FALSE, ( float * )GFX_get_modelview_matrix() ); } else if( !strcmp( curr_program->uniform_array[ i ].name, "PROJECTIONMATRIX" ) ) { glUniformMatrix4fv( curr_program->uniform_array[ i ].location, 1, GL_FALSE, ( float * )GFX_get_projection_matrix() ); } else if( !strcmp( curr_program->uniform_array[ i ].name, "NORMALMATRIX" ) ) { glUniformMatrix3fv( curr_program->uniform_array[ i ].location, 1, GL_FALSE, ( float * )GFX_get_normal_matrix() ); } else if( !strcmp( curr_program->uniform_array[ i ].name, "LIGHTPOSITION" ) ) { // In eye space, far is Z vec3 l = { 0.0f, 0.0f, 0.0f }; glUniform3fv( curr_program->uniform_array[ i ].location, 1, ( float * )&l ); } ++i; } }
void program_draw( void *ptr ) { unsigned int i = 0; PROGRAM *program = ( PROGRAM * )ptr; while( i != program->uniform_count ) { if( program->uniform_array[ i ].constant ) { ++i; continue; } else if( !strcmp( program->uniform_array[ i ].name, "MODELVIEWPROJECTIONMATRIX" ) ) { glUniformMatrix4fv( program->uniform_array[ i ].location, 1, GL_FALSE, ( float * )GFX_get_modelview_projection_matrix() ); } else if( !strcmp( program->uniform_array[ i ].name, "DIFFUSE" ) ) { glUniform1i( program->uniform_array[ i ].location, 1 ); program->uniform_array[ i ].constant = 1; } else if( !strcmp( program->uniform_array[ i ].name, "BUMP" ) ) { glUniform1i( program->uniform_array[ i ].location, 4 ); program->uniform_array[ i ].constant = 1; } // Matrix Data else if( !strcmp( program->uniform_array[ i ].name, "MODELVIEWMATRIX" ) ) { glUniformMatrix4fv( program->uniform_array[ i ].location, 1, GL_FALSE, ( float * )GFX_get_modelview_matrix() ); } else if( !strcmp( program->uniform_array[ i ].name, "PROJECTIONMATRIX" ) ) { glUniformMatrix4fv( program->uniform_array[ i ].location, 1, GL_FALSE, ( float * )GFX_get_projection_matrix() ); program->uniform_array[ i ].constant = 1; } else if( !strcmp( program->uniform_array[ i ].name, "NORMALMATRIX" ) ) { glUniformMatrix3fv( program->uniform_array[ i ].location, 1, GL_FALSE, ( float * )GFX_get_normal_matrix() ); } // Material Data else if( !strcmp( program->uniform_array[ i ].name, "MATERIAL.ambient" ) ) { glUniform4fv( program->uniform_array[ i ].location, 1, ( float * )&objmesh->current_material->ambient ); program->uniform_array[ i ].constant = 1; } else if( !strcmp( program->uniform_array[ i ].name, "MATERIAL.diffuse" ) ) { glUniform4fv( program->uniform_array[ i ].location, 1, ( float * )&objmesh->current_material->diffuse ); } else if( !strcmp( program->uniform_array[ i ].name, "MATERIAL.specular" ) ) { glUniform4fv( program->uniform_array[ i ].location, 1, ( float * )&objmesh->current_material->specular ); } else if( !strcmp( program->uniform_array[ i ].name, "MATERIAL.shininess" ) ) { glUniform1f( program->uniform_array[ i ].location, objmesh->current_material->specular_exponent * 0.128f ); program->uniform_array[ i ].constant = 1; } // Lamp Data else if( !strcmp( program->uniform_array[ i ].name, "LIGHT_FS.color" ) ) { glUniform4fv( program->uniform_array[ i ].location, 1, ( float * )&light->color ); program->uniform_array[ i ].constant = 1; } else if( !strcmp( program->uniform_array[ i ].name, "LIGHT_VS.position" ) ) { vec4 position; static float rot_angle = 0.0f; light->position.x = 7.5f * cosf( rot_angle * DEG_TO_RAD ); light->position.y = 7.5f * sinf( rot_angle * DEG_TO_RAD ); rot_angle += 0.25f; LIGHT_get_position_in_eye_space( light, &gfx.modelview_matrix[ gfx.modelview_matrix_index - 1 ], &position ); glUniform3fv( program->uniform_array[ i ].location, 1, ( float * )&position ); } ++i; } }
//作为供Program结构使用的回调,每次调用Program_draw时都会自动触发该函数; //循环检查着色器中所有的uniform变量,并获取对每个变量的控制,可将回调链接到 //一个或者多个着色器,并在统一位置处理所有的uniform变量更新 void program_draw_callback( void *ptr ) { PROGRAM *curr_program = ( PROGRAM * )ptr; unsigned int i = 0; while( i != curr_program->uniform_count ) { // 处理投影矩阵 if(!strcmp(curr_program->uniform_array[i].name, "MODELVIEWMATRIX")) { glUniformMatrix4fv(curr_program->uniform_array[i].location, 1, GL_FALSE, (float *)GFX_get_modelview_matrix()); } // 处理投影矩阵 else if(!strcmp(curr_program->uniform_array[i].name, "PROJECTIONMATRIX")) { glUniformMatrix4fv(curr_program->uniform_array[i].location, 1, GL_FALSE, (float *) GFX_get_projection_matrix()); } // 处理法线矩阵(仅包含旋转,不包括位置,所以3*3) else if(!strcmp(curr_program->uniform_array[i].name, "NORMALMATRIX")) { glUniformMatrix3fv(curr_program->uniform_array[i].location, 1, GL_FALSE, (float *) GFX_get_normal_matrix()); } else if(!strcmp(curr_program->uniform_array[i].name, "LIGHTPOSITION")) { vec3 light = {0.0f, 0.0f, 0.0f}; // 暂时设置光源位置为屏幕的中心点 glUniformMatrix3fv(curr_program->uniform_array[i].location, 1, GL_FALSE, (float *) &light); } ++i; } }
void program_draw( void *ptr ) { unsigned int i = 0; PROGRAM *program = ( PROGRAM * )ptr; while( i != program->uniform_count ) { if( program->uniform_array[ i ].constant ) { ++i; continue; } else if( !strcmp( program->uniform_array[ i ].name, "MODELVIEWPROJECTIONMATRIX" ) ) { glUniformMatrix4fv( program->uniform_array[ i ].location, 1, GL_FALSE, ( float * )GFX_get_modelview_projection_matrix() ); } else if( !strcmp( program->uniform_array[ i ].name, "DIFFUSE" ) ) { glUniform1i( program->uniform_array[ i ].location, 1 ); program->uniform_array[ i ].constant = 1; } else if( !strcmp( program->uniform_array[ i ].name, "BUMP" ) ) { glUniform1i( program->uniform_array[ i ].location, 4 ); program->uniform_array[ i ].constant = 1; } // Matrix Data else if( !strcmp( program->uniform_array[ i ].name, "MODELVIEWMATRIX" ) ) { glUniformMatrix4fv( program->uniform_array[ i ].location, 1, GL_FALSE, ( float * )GFX_get_modelview_matrix() ); } else if( !strcmp( program->uniform_array[ i ].name, "PROJECTIONMATRIX" ) ) { glUniformMatrix4fv( program->uniform_array[ i ].location, 1, GL_FALSE, ( float * )GFX_get_projection_matrix() ); program->uniform_array[ i ].constant = 1; } else if( !strcmp( program->uniform_array[ i ].name, "NORMALMATRIX" ) ) { glUniformMatrix3fv( program->uniform_array[ i ].location, 1, GL_FALSE, ( float * )GFX_get_normal_matrix() ); } // Material Data else if( !strcmp( program->uniform_array[ i ].name, "MATERIAL.ambient" ) ) { glUniform4fv( program->uniform_array[ i ].location, 1, ( float * )&objmesh->current_material->ambient ); program->uniform_array[ i ].constant = 1; } else if( !strcmp( program->uniform_array[ i ].name, "MATERIAL.diffuse" ) ) { glUniform4fv( program->uniform_array[ i ].location, 1, ( float * )&objmesh->current_material->diffuse ); program->uniform_array[ i ].constant = 1; } else if( !strcmp( program->uniform_array[ i ].name, "MATERIAL.specular" ) ) { glUniform4fv( program->uniform_array[ i ].location, 1, ( float * )&objmesh->current_material->specular ); program->uniform_array[ i ].constant = 1; } else if( !strcmp( program->uniform_array[ i ].name, "MATERIAL.shininess" ) ) { glUniform1f( program->uniform_array[ i ].location, objmesh->current_material->specular_exponent * 0.128f ); program->uniform_array[ i ].constant = 1; } ++i; } char tmp[ MAX_CHAR ] = {""}; sprintf( tmp, "LAMP_FS.color" ); glUniform4fv( PROGRAM_get_uniform_location( program, tmp ), 1, ( float * )&lamp->color ); if( lamp->type == 0 ) { vec3 direction; sprintf( tmp, "LAMP_VS.direction" ); LAMP_get_direction_in_eye_space( lamp, &gfx.modelview_matrix[ gfx.modelview_matrix_index - 1 ], &direction ); glUniform3fv( PROGRAM_get_uniform_location( program, tmp ), 1, ( float * )&direction ); } }