void templateAppDraw(void) { static const float POSITION[8] = { 0.0f, 0.0f, // Down left (pivot point) 1.0f, 0.0f, // Up left 0.0f, 1.0f, // Down right 1.0f, 1.0f // Up right }; static const float COLOR[16] = { 1.0f, 0.0f, 0.0f, 1.0f, // Red 0.0f, 1.0f, 0.0f, 1.0f, // Green 0.0f, 0.0f, 1.0f, 1.0f, // Blue 1.0f, 1.0f, 0.0f, 1.0f // Yellow }; glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); GFX_set_matrix_mode(MODELVIEW_MATRIX); GFX_load_identity(); GFX_scale(100.0f, 100.0f, 0.0f); if (program->pid) { char attribute, uniform; glUseProgram(program->pid); uniform = PROGRAM_get_uniform_location(program, (char *)"MODELVIEWPROJECTIONMATRIX"); glUniformMatrix4fv(uniform, 1 /* How many 4x4 matrix */ , GL_FALSE /* Transpose the matrix? */ , (float *)GFX_get_modelview_projection_matrix()); attribute = PROGRAM_get_vertex_attrib_location(program, (char *)"POSITION"); glEnableVertexAttribArray(attribute); glVertexAttribPointer(attribute, 2, GL_FLOAT, GL_FALSE, 0, POSITION); attribute = PROGRAM_get_vertex_attrib_location(program, (char *)"COLOR"); glEnableVertexAttribArray(attribute); glVertexAttribPointer(attribute, 4, GL_FLOAT, GL_FALSE, 0, COLOR); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } }
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 templateAppDraw(void) { glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); GFX_set_matrix_mode(MODELVIEW_MATRIX); GFX_load_identity(); if (touche_delta.x) { vec3 forward = { 0.0f, 1.0f, 0.0f } , direction; float r = rotz * DEG_TO_RAD, c = cosf(r), s = sinf(r); direction.x = c * forward.x - s * forward.y; direction.y = s * forward.x + c * forward.y; eye_location.x += direction.x * -touche_delta.x; eye_location.y += direction.y * -touche_delta.x; } GFX_translate(eye_location.x, eye_location.y, eye_location.z); GFX_rotate(rotz, 0.0f, 0.0f, 1.0f); GFX_rotate(90.0f, 1.0f, 0.0f, 0.0f); mat4_invert(GFX_get_modelview_matrix()); unsigned int i = 0; while (i != obj->n_objmesh) { OBJMESH *objmesh = &obj->objmesh[i]; GFX_push_matrix(); GFX_translate(objmesh->location.x, objmesh->location.y, objmesh->location.z); glUniformMatrix4fv(program->uniform_array[0].location, 1, GL_FALSE, (float *)GFX_get_modelview_projection_matrix()); OBJ_draw_mesh(obj, i); GFX_pop_matrix(); ++i; } }
void templateAppDraw(void) { glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); GFX_set_matrix_mode(MODELVIEW_MATRIX); GFX_load_identity(); { vec3 e = { 10.4f, -9.8f, 5.5f }, c = { -3.4f, 2.8f, 0.0f}, u = { 0.0f, 0.0f, 1.0f}; GFX_look_at(&e, &c, &u); } unsigned int i = 0; while (i != obj->n_objmesh) { OBJMESH *objmesh = &obj->objmesh[i]; GFX_push_matrix(); mat4 mat; objmesh->btrigidbody->getWorldTransform().getOpenGLMatrix((float *)&mat); GFX_multiply_matrix(&mat); glUniformMatrix4fv(program->uniform_array[0].location, 1, GL_FALSE, (float *)GFX_get_modelview_projection_matrix()); OBJ_draw_mesh(obj, i); GFX_pop_matrix(); ++i; } dynamicsworld->stepSimulation(1.0f / 60.0f); }
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() ); } ++i; } }
void templateAppDraw( void ) { glClear( GL_DEPTH_BUFFER_BIT ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); GFX_look_at( &eye, ¢er, &up ); unsigned int i = 0; while( i != obj->n_objmesh ) { OBJMESH *objmesh = &obj->objmesh[ i ]; GFX_push_matrix(); GFX_translate( objmesh->location.x, objmesh->location.y, objmesh->location.z ); glUniformMatrix4fv( PROGRAM_get_uniform_location( program, ( char * )"MODELVIEWPROJECTIONMATRIX" ), 1, GL_FALSE, ( float * )GFX_get_modelview_projection_matrix() ); OBJ_draw_mesh( obj, i ); GFX_pop_matrix(); ++i; } dynamicsworld->stepSimulation( 1.0f / 60.0f ); }
void program_draw( void *ptr ) { PROGRAM *program = ( PROGRAM * )ptr; unsigned int i = 0; while( i != program->uniform_count ) { if( !program->uniform_array[ i ].constant && !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, "MODELVIEWPROJECTIONMATRIX" ) ) { glUniformMatrix4fv( program->uniform_array[ i ].location, 1, GL_FALSE, ( float * )GFX_get_modelview_projection_matrix() ); } /* Check if you are dealing with the TEXTUREMATRIX uniform. */ else if( !strcmp( program->uniform_array[ i ].name, "TEXTUREMATRIX" ) ) { static vec2 scroll = { 0.0f, 0.0f }; GFX_set_matrix_mode( TEXTURE_MATRIX ); GFX_push_matrix(); scroll.x += 0.0025f; scroll.y += 0.0025f; GFX_translate( scroll.x, scroll.y, 0.0f ); glUniformMatrix4fv( program->uniform_array[ i ].location, 1, GL_FALSE, ( float * )GFX_get_texture_matrix() ); GFX_pop_matrix(); GFX_set_matrix_mode( MODELVIEW_MATRIX ); } ++i; } }
void draw_scene_from_projector(void) { GFX_set_matrix_mode(PROJECTION_MATRIX); GFX_load_identity(); GFX_set_perspective(light->spot_fov, (float)viewport_matrix[2] / (float)viewport_matrix[3], 1.0f, 20.0f, -90.0f); GFX_set_matrix_mode(MODELVIEW_MATRIX); GFX_load_identity(); GFX_look_at((vec3 *) & light->position, ¢er, &up_axis); projector_matrix.m[0].x = 0.5f; projector_matrix.m[0].y = 0.0f; projector_matrix.m[0].z = 0.0f; projector_matrix.m[0].w = 0.0f; projector_matrix.m[1].x = 0.0f; projector_matrix.m[1].y = 0.5f; projector_matrix.m[1].z = 0.0f; projector_matrix.m[1].w = 0.0f; projector_matrix.m[2].x = 0.0f; projector_matrix.m[2].y = 0.0f; projector_matrix.m[2].z = 0.5f; projector_matrix.m[2].w = 0.0f; projector_matrix.m[3].x = 0.5f; projector_matrix.m[3].y = 0.5f; projector_matrix.m[3].z = 0.5f; projector_matrix.m[3].w = 1.0f; mat4_multiply_mat4(&projector_matrix, &projector_matrix, GFX_get_modelview_projection_matrix()); glBindFramebuffer(GL_FRAMEBUFFER, shadowmap_buffer); glViewport(0, 0, shadowmap_width, shadowmap_height); glClear(GL_DEPTH_BUFFER_BIT); glCullFace(GL_FRONT); unsigned int i = 0; PROGRAM *program = OBJ_get_program(obj, "writedepth", 0); while (i != obj->n_objmaterial) { obj->objmaterial[i].program = program; ++i; } i = 0; while (i != obj->n_objmesh) { objmesh = &obj->objmesh[i]; GFX_push_matrix(); GFX_translate(objmesh->location.x, objmesh->location.y, objmesh->location.z); OBJ_draw_mesh(obj, i); GFX_pop_matrix(); ++i; } glCullFace(GL_BACK); }
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; } ++i; } }
void templateAppDraw(void) { glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); GFX_set_matrix_mode(MODELVIEW_MATRIX); GFX_load_identity(); { vec3 e = { 10.4f, -9.8f, 5.5f }, c = { -3.4f, 2.8f, 0.0f}, u = { 0.0f, 0.0f, 1.0f}; GFX_look_at(&e, &c, &u); } unsigned int i = 0; while (i != obj->n_objmesh) { OBJMESH *objmesh = &obj->objmesh[i]; GFX_push_matrix(); GFX_translate(objmesh->location.x, objmesh->location.y, objmesh->location.z); glUniformMatrix4fv(program->uniform_array[0].location, 1, GL_FALSE, (float *)GFX_get_modelview_projection_matrix()); OBJ_draw_mesh(obj, i); GFX_pop_matrix(); ++i; } }
void draw_scene_from_projector( void ) { GFX_set_matrix_mode( PROJECTION_MATRIX ); GFX_load_identity(); GFX_set_perspective( light->spot_fov, ( float )viewport_matrix[ 2 ] / ( float )viewport_matrix[ 3 ], 1.0f, 20.0f, -90.0f ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); GFX_look_at( ( vec3 * )&light->position, ¢er, &up_axis ); projector_matrix.m[ 0 ].x = 0.5f; projector_matrix.m[ 0 ].y = 0.0f; projector_matrix.m[ 0 ].z = 0.0f; projector_matrix.m[ 0 ].w = 0.0f; projector_matrix.m[ 1 ].x = 0.0f; projector_matrix.m[ 1 ].y = 0.5f; projector_matrix.m[ 1 ].z = 0.0f; projector_matrix.m[ 1 ].w = 0.0f; projector_matrix.m[ 2 ].x = 0.0f; projector_matrix.m[ 2 ].y = 0.0f; projector_matrix.m[ 2 ].z = 0.5f; projector_matrix.m[ 2 ].w = 0.0f; projector_matrix.m[ 3 ].x = 0.5f; projector_matrix.m[ 3 ].y = 0.5f; projector_matrix.m[ 3 ].z = 0.5f; projector_matrix.m[ 3 ].w = 1.0f; mat4_multiply_mat4( &projector_matrix, &projector_matrix, GFX_get_modelview_projection_matrix() ); }
/*! Helper function to debug draw the navigation mesh. Call this function at the end of your rendering loop to overlay the navigation mesh on top of your current scene. \param[in] navigation A valid NAVIGATION structure pointer. */ void NAVIGATION_draw( NAVIGATION *navigation ) { if( !navigation->program ) { navigation->program = PROGRAM_init( navigation->name ); navigation->program->vertex_shader = SHADER_init( navigation->name, GL_VERTEX_SHADER ); SHADER_compile( navigation->program->vertex_shader, "uniform highp mat4 MODELVIEWPROJECTIONMATRIX;" "attribute highp vec3 POSITION;" "void main( void ) {" "gl_Position = MODELVIEWPROJECTIONMATRIX * vec4( POSITION, 1.0 ); }", 0 ); navigation->program->fragment_shader = SHADER_init( navigation->name, GL_FRAGMENT_SHADER ); SHADER_compile( navigation->program->fragment_shader, "void main( void ) {" "gl_FragColor = vec4( 0.25, 0.5, 1.0, 0.65 ); }", 0 ); PROGRAM_link( navigation->program, 0 ); } char vertex_attribute = PROGRAM_get_vertex_attrib_location( navigation->program, ( char * )"POSITION" ); glBindVertexArrayOES( 0 ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); glEnable( GL_BLEND ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); PROGRAM_draw( navigation->program ); glUniformMatrix4fv( PROGRAM_get_uniform_location( navigation->program, ( char * )"MODELVIEWPROJECTIONMATRIX"), 1, GL_FALSE, ( float * )GFX_get_modelview_projection_matrix() ); glEnableVertexAttribArray( vertex_attribute ); unsigned int j = 0; while( j != navigation->dtnavmesh->getMaxTiles() ) { dtMeshTile *_dtMeshTile = navigation->dtnavmesh->getTile( j ); if( !_dtMeshTile->header ) { continue; } unsigned int k = 0; while( k != _dtMeshTile->header->polyCount ) { dtPoly *_dtPoly = &_dtMeshTile->polys[ k ]; if( _dtPoly->type == DT_POLYTYPE_OFFMESH_CONNECTION ) { continue; } else { dtPolyDetail* pd = &_dtMeshTile->detailMeshes[ k ]; unsigned int l = 0; while( l != pd->triCount ) { vec3 v[ 3 ]; const unsigned char *t = &_dtMeshTile->detailTris[ ( pd->triBase + l ) << 2 ]; int m = 2; while( m != -1 ) { if( t[ m ] < _dtPoly->vertCount ) { memcpy( &v[ m ], &_dtMeshTile->verts[ _dtPoly->verts[ t[ m ] ] * 3 ], sizeof( vec3 ) ); recast_to_vec3( &v[ m ] ); } else { memcpy( &v[ m ], &_dtMeshTile->detailVerts[ ( pd->vertBase + t[ m ] - _dtPoly->vertCount ) * 3 ], sizeof( vec3 ) ); recast_to_vec3( &v[ m ] ); } --m; } glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, ( float * )v ); glDrawArrays( GL_TRIANGLES, 0, 3 ); ++l; } } ++k; } ++j; } glDisable( GL_BLEND ); }
void templateAppDraw(void) { glClear(GL_DEPTH_BUFFER_BIT); if (restart_game) { templateAppExit(); load_game(); restart_game = 0; } GFX_set_matrix_mode(MODELVIEW_MATRIX); GFX_load_identity(); if (momo) { eye.x = eye.x * 0.98f + momo->location.x * 0.02f; center.x = eye.x = CLAMP(eye.x, -2.0f, 3.5f); } GFX_look_at(&eye, ¢er, &up); unsigned int i = 0; while (i != obj->n_objmesh) { OBJMESH *objmesh = &obj->objmesh[i]; GFX_push_matrix(); if (objmesh->btrigidbody) { mat4 mat; objmesh->btrigidbody->getWorldTransform().getOpenGLMatrix((float *)&mat); objmesh->location.x = mat.m[3].x; GFX_multiply_matrix(&mat); } else GFX_translate(objmesh->location.x, objmesh->location.y, objmesh->location.z); glUniformMatrix4fv(PROGRAM_get_uniform_location(program, (char *)"MODELVIEWPROJECTIONMATRIX"), 1, GL_FALSE, (float *)GFX_get_modelview_projection_matrix()); OBJ_draw_mesh(obj, i); GFX_pop_matrix(); ++i; } dynamicsworld->stepSimulation(1.0f / 60.0f); if (momo && (momo->btrigidbody->getLinearVelocity().length() > 20.0f || momo->btrigidbody->getActivationState() == ISLAND_SLEEPING)) get_next_momo(); if (!momo || !banana) { gameover->visible = 1; gameover->location.x = eye.x; gameover->location.z = eye.z; } }
void templateAppDraw( void ) { glClearColor( 0.5f, 0.5f, 0.5f, 1.0f ); glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); if( view_delta.x || view_delta.y ) { if( view_delta.y ) next_rotz -= view_delta.y; if( view_delta.x ) { next_rotx -= view_delta.x; next_rotx = CLAMP( next_rotx, 0.0f, 90.0f ); } view_delta.x = view_delta.y = 0.0f; } rotx = rotx * 0.9f + next_rotx * 0.1f; rotz = rotz * 0.9f + next_rotz * 0.1f; eye.x = center.x + distance * cosf( rotx * DEG_TO_RAD ) * sinf( rotz * DEG_TO_RAD ); eye.y = center.y - distance * cosf( rotx * DEG_TO_RAD ) * cosf( rotz * DEG_TO_RAD ); eye.z = center.z + distance * sinf( rotx * DEG_TO_RAD ); rotx = rotx * 0.9f + next_rotx * 0.1f; rotz = rotz * 0.9f + next_rotz * 0.1f; center.x = maze->location.x; center.y = maze->location.y; center.z = maze->location.z; GFX_look_at( &eye, ¢er, &up ); if( double_tap ) { vec3 location; if( GFX_unproject( view_location.x, viewport_matrix[ 3 ] - view_location.y, 1.0f, GFX_get_modelview_matrix(), GFX_get_projection_matrix(), viewport_matrix, &location.x, &location.y, &location.z ) ) { btVector3 ray_from( eye.x, eye.y, eye.z ), ray_to( location.x + eye.x, location.y + eye.y, location.z + eye.z ); btCollisionWorld::ClosestRayResultCallback collision_ray( ray_from, ray_to ); dynamicsworld->rayTest( ray_from, ray_to, collision_ray ); if( collision_ray.hasHit() && collision_ray.m_collisionObject == maze->btrigidbody ) { collision_ray.m_hitNormalWorld.normalize(); if( collision_ray.m_hitNormalWorld.z() == 1.0f ) { navigationpath_player.start_location.x = player->location.x; navigationpath_player.start_location.y = player->location.y; navigationpath_player.start_location.z = player->location.z; navigationpath_player.end_location.x = collision_ray.m_hitPointWorld.x(); navigationpath_player.end_location.y = collision_ray.m_hitPointWorld.y(); navigationpath_player.end_location.z = collision_ray.m_hitPointWorld.z(); if( NAVIGATION_get_path( navigation, &navigationpath_player, &navigationpathdata_player ) ) { player_next_point = 1; unsigned int i = 0; while( i != navigationpathdata_player.path_point_count + 1 ) { console_print( "%d: %f %f %f\n", i, navigationpathdata_player.path_point_array[ i ].x, navigationpathdata_player.path_point_array[ i ].y, navigationpathdata_player.path_point_array[ i ].z ); ++i; } printf( "\n" ); } } } } double_tap = 0; } if( navigationpathdata_player.path_point_count ) { move_entity( player, &navigationpathdata_player, &player_next_point, 3.0f ); } PROGRAM_draw( program ); glUniform1i( PROGRAM_get_uniform_location( program, ( char * )"DIFFUSE" ), 1 ); unsigned int i = 0; while( i != obj->n_objmesh ) { OBJMESH *objmesh = &obj->objmesh[ i ]; GFX_push_matrix(); mat4 mat; objmesh->btrigidbody->getWorldTransform().getOpenGLMatrix( ( float * )&mat ); memcpy( &objmesh->location, ( vec3 * )&mat.m[ 3 ], sizeof( vec3 ) ); GFX_multiply_matrix( &mat ); glUniformMatrix4fv( PROGRAM_get_uniform_location( program, ( char * )"MODELVIEWPROJECTIONMATRIX" ), 1, GL_FALSE, ( float * )GFX_get_modelview_projection_matrix() ); OBJ_draw_mesh( obj, i ); GFX_pop_matrix(); ++i; } NAVIGATION_draw( navigation ); dynamicsworld->stepSimulation( 1.0f / 60.0f ); }
void templateAppDraw( void ) { glClearColor( 0.5f, 0.5f, 0.5f, 1.0f ); glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); GFX_set_matrix_mode( PROJECTION_MATRIX ); GFX_load_identity(); GFX_set_perspective( 80.0f, ( float )viewport_matrix[ 2 ] / ( float )viewport_matrix[ 3 ], 1.0f, 1000.0f, -90.0f ); if( game_over == 2 ) { templateAppExit(); load_game(); game_over = 0; } GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); if( view_delta.x || view_delta.y ) { if( view_delta.y ) next_rotz -= view_delta.y; if( view_delta.x ) { next_rotx -= view_delta.x; next_rotx = CLAMP( next_rotx, 0.0f, 90.0f ); } view_delta.x = view_delta.y = 0.0f; } rotx = rotx * 0.9f + next_rotx * 0.1f; rotz = rotz * 0.9f + next_rotz * 0.1f; eye.x = center.x + distance * cosf( rotx * DEG_TO_RAD ) * sinf( rotz * DEG_TO_RAD ); eye.y = center.y - distance * cosf( rotx * DEG_TO_RAD ) * cosf( rotz * DEG_TO_RAD ); eye.z = center.z + distance * sinf( rotx * DEG_TO_RAD ); rotx = rotx * 0.9f + next_rotx * 0.1f; rotz = rotz * 0.9f + next_rotz * 0.1f; center.x = maze->location.x; center.y = maze->location.y; center.z = maze->location.z; GFX_look_at( &eye, ¢er, &up ); if( double_tap ) { vec3 location; if( GFX_unproject( view_location.x, viewport_matrix[ 3 ] - view_location.y, 1.0f, GFX_get_modelview_matrix(), GFX_get_projection_matrix(), viewport_matrix, &location.x, &location.y, &location.z ) ) { btVector3 ray_from( eye.x, eye.y, eye.z ), ray_to( location.x + eye.x, location.y + eye.y, location.z + eye.z ); btCollisionWorld::ClosestRayResultCallback collision_ray( ray_from, ray_to ); dynamicsworld->rayTest( ray_from, ray_to, collision_ray ); if( collision_ray.hasHit() && collision_ray.m_collisionObject == maze->btrigidbody ) { collision_ray.m_hitNormalWorld.normalize(); if( collision_ray.m_hitNormalWorld.z() == 1.0f ) { navigationpath_player.start_location.x = player->location.x; navigationpath_player.start_location.y = player->location.y; navigationpath_player.start_location.z = player->location.z; navigationpath_player.end_location.x = collision_ray.m_hitPointWorld.x(); navigationpath_player.end_location.y = collision_ray.m_hitPointWorld.y(); navigationpath_player.end_location.z = collision_ray.m_hitPointWorld.z(); if( NAVIGATION_get_path( navigation, &navigationpath_player, &navigationpathdata_player ) ) { player_next_point = 1; unsigned int i = 0; while( i != navigationpathdata_player.path_point_count + 1 ) { console_print( "%d: %f %f %f\n", i, navigationpathdata_player.path_point_array[ i ].x, navigationpathdata_player.path_point_array[ i ].y, navigationpathdata_player.path_point_array[ i ].z ); ++i; } printf( "\n" ); } } } } double_tap = 0; } if( navigationpathdata_player.path_point_count ) { vec3 color = { 0.0f, 0.0f, 1.0f }; draw_navigation_points( &navigationpathdata_player, &color ); move_entity( player, &navigationpathdata_player, &player_next_point, 3.0f ); } static unsigned int start_time = get_milli_time(); if( get_milli_time() - start_time > 1000 ) { navigationpath_enemy.start_location.x = enemy->location.x; navigationpath_enemy.start_location.y = enemy->location.y; navigationpath_enemy.start_location.z = enemy->location.z; navigationpath_enemy.end_location.x = player->location.x; navigationpath_enemy.end_location.y = player->location.y; navigationpath_enemy.end_location.z = player->location.z; NAVIGATION_get_path( navigation, &navigationpath_enemy, &navigationpathdata_enemy ); enemy_next_point = 1; start_time = get_milli_time(); } if( navigationpathdata_enemy.path_point_count ) { vec3 color = { 1.0f, 0.0f, 0.0f }; move_entity( enemy, &navigationpathdata_enemy, &enemy_next_point, 4.0f ); draw_navigation_points( &navigationpathdata_enemy, &color ); } PROGRAM_draw( program ); glUniform1i( PROGRAM_get_uniform_location( program, ( char * )"DIFFUSE" ), 1 ); unsigned int i = 0; while( i != obj->n_objmesh ) { OBJMESH *objmesh = &obj->objmesh[ i ]; GFX_push_matrix(); mat4 mat; objmesh->btrigidbody->getWorldTransform().getOpenGLMatrix( ( float * )&mat ); memcpy( &objmesh->location, ( vec3 * )&mat.m[ 3 ], sizeof( vec3 ) ); GFX_multiply_matrix( &mat ); glUniformMatrix4fv( PROGRAM_get_uniform_location( program, ( char * )"MODELVIEWPROJECTIONMATRIX" ), 1, GL_FALSE, ( float * )GFX_get_modelview_projection_matrix() ); OBJ_draw_mesh( obj, i ); GFX_pop_matrix(); ++i; } if( !game_over ) dynamicsworld->stepSimulation( 1.0f / 60.0f ); else { GFX_set_matrix_mode( PROJECTION_MATRIX ); GFX_load_identity(); float half_width = ( float )viewport_matrix[ 2 ] * 0.5f, half_height = ( float )viewport_matrix[ 3 ] * 0.5f; GFX_set_orthographic_2d( -half_width, half_width, -half_height, half_height ); GFX_rotate( -90.0f, 0.0f, 0.0f, 1.0f ); GFX_translate( -half_height, -half_width, 0.0f ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); vec4 color = { 0.0f, 0.0f, 0.0f, 1.0f }; char msg[ MAX_CHAR ] = {"GAME OVER!"}; if( !font ) { font = FONT_init( ( char * )"foo.ttf" ); FONT_load( font, font->name, 1, 64.0f, 512, 512, 32, 96 ); } float posx = ( viewport_matrix[ 3 ] * 0.5f ) - ( FONT_length( font, msg ) * 0.5f ), posy = viewport_matrix[ 2 ] - font->font_size; FONT_print( font, posx + 4.0f, posy - 4.0f, msg, &color ); color.y = 1.0f; FONT_print( font, posx, posy, msg, &color ); } }
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 ); } }
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; } }
void draw_navigation_points( NAVIGATIONPATHDATA *navigationpathdata, vec3 *color ) { unsigned int i = 0; while( i != navigationpathdata->path_point_count + 1 ) { navigationpathdata->path_point_array[ i ].z = 1.0f; ++i; } glBindVertexArrayOES( 0 ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); glEnable( GL_BLEND ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); if( !path_point ) { path_point = PROGRAM_create( ( char * )"path_point", ( char * )"point_vert.glsl", ( char * )"point_frag.glsl", 1, 0, program_bind_attrib_location, NULL ); } PROGRAM_draw( path_point ); glUniformMatrix4fv( PROGRAM_get_uniform_location( path_point, ( char * )"MODELVIEWPROJECTIONMATRIX" ), 1, GL_FALSE, ( float * )GFX_get_modelview_projection_matrix() ); glUniform3fv( PROGRAM_get_uniform_location( path_point, ( char * )"COLOR" ), 1, ( float * )color ); glEnableVertexAttribArray( 0 ); glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, navigationpathdata->path_point_array ); glDrawArrays( GL_POINTS, 0, navigationpathdata->path_point_count + 1 ); glDrawArrays( GL_LINE_STRIP, 0, navigationpathdata->path_point_count + 1 ); glDisable( GL_BLEND ); }
void FONT_print( FONT *font, float x, float y, char *text, vec4 *color ) { char vertex_attribute = PROGRAM_get_vertex_attrib_location( font->program, ( char * )"POSITION" ), texcoord_attribute = PROGRAM_get_vertex_attrib_location( font->program, ( char * )"TEXCOORD0" ); glBindVertexArrayOES( 0 ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); glDisable( GL_CULL_FACE ); glDisable( GL_DEPTH_TEST ); glDepthMask( GL_FALSE ); glEnable( GL_BLEND ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); PROGRAM_draw( font->program ); glUniformMatrix4fv( PROGRAM_get_uniform_location( font->program, ( char * )"MODELVIEWPROJECTIONMATRIX"), 1, GL_FALSE, ( float * )GFX_get_modelview_projection_matrix() ); glUniform1i( PROGRAM_get_uniform_location( font->program, ( char * )"DIFFUSE" ), 0 ); if( color ) glUniform4fv( PROGRAM_get_uniform_location( font->program, ( char * )"COLOR" ), 1, ( float * )color ); glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, font->tid ); glEnableVertexAttribArray( vertex_attribute ); glEnableVertexAttribArray( texcoord_attribute ); while( *text ) { if( *text >= font->first_character && *text <= ( font->first_character + font->count_character ) ) { vec2 vert[ 4 ]; vec2 uv[ 4 ]; stbtt_aligned_quad quad; stbtt_bakedchar *bakedchar = font->character_data + ( *text - font->first_character ); int round_x = STBTT_ifloor( x + bakedchar->xoff ); int round_y = STBTT_ifloor( y - bakedchar->yoff ); quad.x0 = ( float )round_x; quad.y0 = ( float )round_y; quad.x1 = ( float )round_x + bakedchar->x1 - bakedchar->x0; quad.y1 = ( float )round_y - bakedchar->y1 + bakedchar->y0; quad.s0 = bakedchar->x0 / ( float )font->texture_width; quad.t0 = bakedchar->y0 / ( float )font->texture_width; quad.s1 = bakedchar->x1 / ( float )font->texture_height; quad.t1 = bakedchar->y1 / ( float )font->texture_height; x += bakedchar->xadvance; vert[ 0 ].x = quad.x1; vert[ 0 ].y = quad.y0; uv [ 0 ].x = quad.s1; uv [ 0 ].y = quad.t0; vert[ 1 ].x = quad.x0; vert[ 1 ].y = quad.y0; uv [ 1 ].x = quad.s0; uv [ 1 ].y = quad.t0; vert[ 2 ].x = quad.x1; vert[ 2 ].y = quad.y1; uv [ 2 ].x = quad.s1; uv [ 2 ].y = quad.t1; vert[ 3 ].x = quad.x0; vert[ 3 ].y = quad.y1; uv [ 3 ].x = quad.s0; uv [ 3 ].y = quad.t1; glVertexAttribPointer( vertex_attribute, 2, GL_FLOAT, GL_FALSE, 0, ( float * )&vert[ 0 ] ); glVertexAttribPointer( texcoord_attribute, 2, GL_FLOAT, GL_FALSE, 0, ( float * )&uv[ 0 ] ); glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 ); } ++text; } glEnable( GL_CULL_FACE ); glEnable( GL_DEPTH_TEST ); glDepthMask( GL_TRUE ); glDisable( GL_BLEND ); }
void templateAppDraw(void) { glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); GFX_set_matrix_mode(MODELVIEW_MATRIX); GFX_load_identity(); if (view_delta.x || view_delta.y) { if (view_delta.y) next_rotz -= view_delta.y; if (view_delta.x) { next_rotx -= view_delta.x; next_rotx = CLAMP(next_rotx, 0.0f, 90.0f); } view_delta.x = view_delta.y = 0.0f; } rotx = rotx * 0.9f + next_rotx * 0.1f; rotz = rotz * 0.9f + next_rotz * 0.1f; eye.x = center.x + distance * cosf(rotx * DEG_TO_RAD) * sinf(rotz * DEG_TO_RAD); eye.y = center.y - distance * cosf(rotx * DEG_TO_RAD) * cosf(rotz * DEG_TO_RAD); eye.z = center.z + distance * sinf(rotx * DEG_TO_RAD); rotx = rotx * 0.9f + next_rotx * 0.1f; rotz = rotz * 0.9f + next_rotz * 0.1f; center.x = maze->location.x; center.y = maze->location.y; center.z = maze->location.z; GFX_look_at(&eye, ¢er, &up); PROGRAM_draw(program); glUniform1i(PROGRAM_get_uniform_location(program, (char *)"DIFFUSE"), 1); unsigned int i = 0; while (i != obj->n_objmesh) { OBJMESH *objmesh = &obj->objmesh[i]; GFX_push_matrix(); mat4 mat; objmesh->btrigidbody->getWorldTransform().getOpenGLMatrix((float *)&mat); memcpy(&objmesh->location, (vec3 *) & mat.m[3], sizeof(vec3)); GFX_multiply_matrix(&mat); glUniformMatrix4fv(PROGRAM_get_uniform_location(program, (char *)"MODELVIEWPROJECTIONMATRIX"), 1, GL_FALSE, (float *)GFX_get_modelview_projection_matrix()); OBJ_draw_mesh(obj, i); GFX_pop_matrix(); ++i; } NAVIGATION_draw(navigation); dynamicsworld->stepSimulation(1.0f / 60.0f); }
void templateAppDraw( void ) { static const float POSITION[ 12 ] = { -0.5f, 0.0f, -0.5f, // Bottom left 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.5f // Top right }; static const float COLOR[ 16 ] = { 1.0f, 0.0f, 0.0f, 1.0f, // Red 0.0f, 1.0f, 0.0f, 1.0f, // Green 0.0f, 0.0f, 1.0f, 1.0f, // Blue 1.0f, 1.0f, 0.0f, 1.0f // Yellow }; glClearColor( 0.5f, 0.5f, 0.5f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); vec3 e = { 0.0f, -3.0f, 0.0f }, c = { 0.0f, 0.0f, 0.0f }, u = { 0.0f, 0.0f, 1.0f }; GFX_look_at( &e, &c, &u ); static float y = 0.0f; y += 0.1f; //GFX_translate( 0.0f, y, 0.0f ); GFX_rotate( y * 50.0f, 1.0f, 1.0f, 1.0f ); if( program->pid ) { char attribute, uniform; glUseProgram( program->pid ); uniform = PROGRAM_get_uniform_location( program, ( char * )"MODELVIEWPROJECTIONMATRIX" ); glUniformMatrix4fv( uniform, 1 /* How many 4x4 matrix */, GL_FALSE /* Transpose the matrix? */, ( float * )GFX_get_modelview_projection_matrix() ); attribute = PROGRAM_get_vertex_attrib_location( program, ( char * )"POSITION" ); glEnableVertexAttribArray( attribute ); glVertexAttribPointer( attribute, 3, GL_FLOAT, GL_FALSE, 0, POSITION ); attribute = PROGRAM_get_vertex_attrib_location( program, ( char * )"COLOR" ); glEnableVertexAttribArray( attribute ); glVertexAttribPointer( attribute, 4, GL_FLOAT, GL_FALSE, 0, COLOR ); glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 ); } }