void draw_scene( void ) { glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); GFX_set_matrix_mode( PROJECTION_MATRIX ); GFX_load_identity(); GFX_set_perspective( 45.0f, ( float )viewport_matrix[ 2 ] / ( float )viewport_matrix[ 3 ], 0.1f, 100.0f, -90.0f ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); GFX_translate( 14.0f, -12.0f, 7.0f ); GFX_rotate( 48.5f, 0.0f, 0.0f, 1.0f ); GFX_rotate( 72.0, 1.0f, 0.0f, 0.0f ); mat4_invert( GFX_get_modelview_matrix() ); glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, texture->tid ); mat4 projector_matrix_copy; mat4_copy_mat4( &projector_matrix_copy, &projector_matrix ); unsigned int 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 ); mat4_copy_mat4( &projector_matrix, &projector_matrix_copy ); mat4_translate( &projector_matrix, &projector_matrix, &objmesh->location ); OBJ_draw_mesh( obj, i ); GFX_pop_matrix(); ++i; } }
void GFX_start( void ) { memset( &gfx, 0, sizeof( GFX ) ); #ifdef __IPHONE_4_0 printf("\nGL_VENDOR: %s\n", ( char * )glGetString( GL_VENDOR ) ); printf("GL_RENDERER: %s\n" , ( char * )glGetString( GL_RENDERER ) ); printf("GL_VERSION: %s\n" , ( char * )glGetString( GL_VERSION ) ); printf("GL_EXTENSIONS: %s\n" , ( char * )glGetString( GL_EXTENSIONS ) ); #else __android_log_print( ANDROID_LOG_INFO, "", "\nGL_VENDOR: %s\n", ( char * )glGetString( GL_VENDOR ) ); __android_log_print( ANDROID_LOG_INFO, "", "GL_RENDERER: %s\n" , ( char * )glGetString( GL_RENDERER ) ); __android_log_print( ANDROID_LOG_INFO, "", "GL_VERSION: %s\n" , ( char * )glGetString( GL_VERSION ) ); __android_log_print( ANDROID_LOG_INFO, "", "GL_EXTENSIONS: %s\n" , ( char * )glGetString( GL_EXTENSIONS ) ); #endif glHint( GL_GENERATE_MIPMAP_HINT, GL_NICEST ); glHint( GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES, GL_NICEST ); glEnable( GL_DEPTH_TEST ); glEnable( GL_CULL_FACE ); glDisable( GL_DITHER ); glDepthMask( GL_TRUE ); glDepthFunc( GL_LESS ); glDepthRangef( 0.0f, 1.0f ); glCullFace ( GL_BACK ); glFrontFace( GL_CCW ); glClearStencil( 0 ); glStencilMask( 0xFFFFFFFF ); glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); glClear( GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); #ifndef __IPHONE_4_0 glBindVertexArrayOES = ( PFNGLBINDVERTEXARRAYOESPROC ) eglGetProcAddress("glBindVertexArrayOES" ); glGenVertexArraysOES = ( PFNGLGENVERTEXARRAYSOESPROC ) eglGetProcAddress("glGenVertexArraysOES" ); glDeleteVertexArraysOES = ( PFNGLDELETEVERTEXARRAYSOESPROC ) eglGetProcAddress("glDeleteVertexArraysOES"); #endif GFX_set_matrix_mode( TEXTURE_MATRIX ); GFX_load_identity(); GFX_set_matrix_mode( PROJECTION_MATRIX ); GFX_load_identity(); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); }
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(45.0f, (float)viewport_matrix[2] / (float)viewport_matrix[3], 0.1f, 100.0f, 0.0f); GFX_set_matrix_mode(MODELVIEW_MATRIX); GFX_load_identity(); GFX_translate(0.0f, -14.0f, 3.0f); GFX_rotate(90.0, 1.0f, 0.0f, 0.0f); mat4_invert(GFX_get_modelview_matrix()); }
void templateAppInit(int width, int height) { GFX_start(); glViewport(0.0f, 0.0f, width, height); GFX_set_matrix_mode(PROJECTION_MATRIX); GFX_load_identity(); GFX_set_perspective(80.0f, (float)width / (float)height, 0.1f, 100.0f, -90.0f); obj = OBJ_load(OBJ_FILE, 1); unsigned int i = 0; while (i != obj->n_objmesh) { OBJ_optimize_mesh(obj, i, 128); OBJ_build_mesh(obj, i); OBJ_free_mesh_vertex_data(obj, i); ++i; } i = 0; while (i != obj->n_texture) { OBJ_build_texture(obj, i, obj->texture_path, TEXTURE_MIPMAP | TEXTURE_16_BITS, TEXTURE_FILTER_2X, 0.0f); ++i; } i = 0; while (i != obj->n_objmaterial) { OBJ_build_material(obj, i, NULL); ++i; } program = PROGRAM_create((char *)"default", VERTEX_SHADER, FRAGMENT_SHADER, 1, 0, program_bind_attrib_location, NULL); PROGRAM_draw(program); glUniform1i(PROGRAM_get_uniform_location(program, (char *)"DIFFUSE"), 1); }
void templateAppInit(int width, int height) { GFX_start(); glViewport(0.0f, 0.0f, width, height); GFX_set_matrix_mode(PROJECTION_MATRIX); { float half_width = (float)width * 0.5f, half_height = (float)height *0.5f; GFX_load_identity(); GFX_set_orthographic_2d(-half_width, half_width, -half_height, half_height); GFX_translate(-half_width, -half_height, 0.0f); glDisable(GL_DEPTH_TEST); glDepthMask(GL_FALSE); } program = PROGRAM_init((char *)"default"); program->vertex_shader = SHADER_init(VERTEX_SHADER, GL_VERTEX_SHADER); program->fragment_shader = SHADER_init(FRAGMENT_SHADER, GL_FRAGMENT_SHADER); m = mopen(VERTEX_SHADER, 1); if (m) { if (!SHADER_compile(program->vertex_shader, (char *)m->buffer, DEBUG_SHADERS)) exit(1); } m = mclose(m); m = mopen(FRAGMENT_SHADER, 1); if (m) { if (!SHADER_compile(program->fragment_shader, (char *)m->buffer, DEBUG_SHADERS)) exit(2); } m = mclose(m); if (!PROGRAM_link(program, DEBUG_SHADERS)) exit(3); }
void draw_scene( void ) { glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); GFX_translate( 14.0f, -12.0f, 7.0f ); GFX_rotate( 48.5f, 0.0f, 0.0f, 1.0f ); GFX_rotate( 72.0, 1.0f, 0.0f, 0.0f ); mat4_invert( GFX_get_modelview_matrix() ); unsigned int 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; } }
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 ); //创建一个关键矩阵,并在Y轴上设置一个向后的偏移量,以便能够看到模型 GFX_load_identity(); { vec3 e = { 0.0f, -4.0f, 0.0f }, c = { 0.0f, 0.0f, 0.0f }, u = { 0.0f, 0.0f, 1.0f }; GFX_look_at( &e, &c, &u ); } //绑定VAO glBindVertexArrayOES( objmesh->vao ); if (auto_rotate) rot_angle.z += 2.0f; GFX_rotate(rot_angle.x, 1.0f, 0.0f, 0.0f); GFX_rotate(rot_angle.z, 0.0f, 0.0f, 1.0f); //设置绘制的着色器程序(自动使用glUseProgram绑定到着色器程序,然后发送program_draw_callback中执行指针,调用该函数) PROGRAM_draw( program ); //绘图语句 glDrawElements( GL_TRIANGLES, objmesh->objtrianglelist[ 0 ].n_indice_array, GL_UNSIGNED_SHORT, ( void * )NULL ); }
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 ) { 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 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 = { 0.0, -3.0f, 0.0f }, c = { 0.0f, 0.0f, 0.0f }, u = { 0.0f, 0.0f, 1.0f }; GFX_look_at( &e, &c, &u ); } unsigned int i = 0; while( i != obj->n_objmesh ) { GFX_push_matrix(); if( auto_rotate ) rot_angle.z += 2.0f; GFX_rotate( rot_angle.x, 1.0f, 0.0f, 0.0f ); GFX_rotate( rot_angle.z, 0.0f, 0.0f, 1.0f ); 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 = { 0.0f, -4.0f, 0.0f }, c = { 0.0f, 0.0f, 0.0f }, u = { 0.0f, 0.0f, 1.0f }; GFX_look_at( &e, &c, &u ); } glBindVertexArrayOES( objmesh->vao ); if( auto_rotate ) rot_angle.z += 2.0f; GFX_rotate( rot_angle.x, 1.0f, 0.0f, 0.0f ); GFX_rotate( rot_angle.z, 0.0f, 0.0f, 1.0f ); PROGRAM_draw( program ); glDrawElements( GL_TRIANGLES, objmesh->objtrianglelist[ 0 ].n_indice_array, GL_UNSIGNED_SHORT, ( void * )NULL ); }
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 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 templateAppDraw( void ) { glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); GFX_set_matrix_mode( PROJECTION_MATRIX ); GFX_load_identity(); GFX_set_perspective( 45.0f, ( float )viewport_matrix[ 2 ] / ( float )viewport_matrix[ 3 ], 0.1f, 100.0f, -90.0f ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); GFX_translate( 14.0f, -12.0f, 7.0f ); GFX_rotate( 48.5f, 0.0f, 0.0f, 1.0f ); GFX_rotate( 72.0, 1.0f, 0.0f, 0.0f ); mat4_invert( GFX_get_modelview_matrix() ); unsigned int 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; } }
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 templateAppInit(int width, int height) { GFX_start(); glViewport(0.0f, 0.0f, width, height); GFX_set_matrix_mode(PROJECTION_MATRIX); GFX_load_identity(); GFX_set_orthographic((float)height / (float)width, 15.0f, (float)width / (float)height, 1.0f, 100.0f, -90.0f); load_game(); }
void templateAppInit( int width, int height ) { atexit( templateAppExit ); GFX_start(); init_physic_world(); gContactAddedCallback = contact_added_callback; glViewport( 0.0f, 0.0f, width, height ); GFX_set_matrix_mode( PROJECTION_MATRIX ); GFX_load_identity(); GFX_set_perspective( 45.0f, ( float )width / ( float )height, 0.1f, 100.0f, -90.0f ); obj = OBJ_load( OBJ_FILE, 1 ); unsigned int i = 0; while( i != obj->n_objmesh ) { OBJ_build_mesh( obj, i ); OBJMESH *objmesh = &obj->objmesh[ i ]; if( !strcmp( objmesh->name, "Cube" ) ) { objmesh->rotation.x = objmesh->rotation.y = objmesh->rotation.z = 35.0f; add_rigid_body( objmesh, 1.0f ); } else add_rigid_body( objmesh, 0.0f ); OBJ_free_mesh_vertex_data( obj, i ); ++i; } program = PROGRAM_create( ( char * )"default", VERTEX_SHADER, FRAGMENT_SHADER, 1, 0, program_bind_attrib_location, NULL ); PROGRAM_draw( program ); }
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( 45.0f, ( float )viewport_matrix[ 2 ] / ( float )viewport_matrix[ 3 ], 0.1f, 100.0f, 0.0f ); GFX_set_matrix_mode( MODELVIEW_MATRIX ); GFX_load_identity(); GFX_translate( 0.0f, -14.0f, 3.0f ); GFX_rotate( 90.0, 1.0f, 0.0f, 0.0f ); mat4_invert( GFX_get_modelview_matrix() ); GFX_push_matrix(); if( auto_rotate ) rot_angle.z += 1.0f; GFX_rotate( rot_angle.x, 1.0f, 0.0f, 0.0f ); GFX_rotate( rot_angle.z, 0.0f, 0.0f, 1.0f ); if( MD5_draw_action( md5, 1.0f / 60.0f ) ) { MD5_set_pose( md5, idle->pose ); } MD5_draw( md5 ); GFX_pop_matrix(); }
void templateAppInit(int width, int height) { GFX_start(); glViewport(0.0f, 0.0f, width, height); GFX_set_matrix_mode(PROJECTION_MATRIX); GFX_load_identity(); GFX_set_perspective(45.0f, (float)width / (float)height, 0.1f, 100.0f, 0.0f); program = PROGRAM_create((char *)"default", VERTEX_SHADER, FRAGMENT_SHADER, 1, DEBUG_SHADERS, NULL, program_draw_callback); obj = OBJ_load(OBJ_FILE, 1); unsigned char *vertex_array = NULL, *vertex_start = NULL; unsigned int i = 0, index = 0, stride = 0, size = 0; objmesh = &obj->objmesh[0]; size = objmesh->n_objvertexdata * sizeof(vec3) * sizeof(vec3) * sizeof(vec2); vertex_array = (unsigned char *)malloc(size); vertex_start = vertex_array; while (i != objmesh->n_objvertexdata) { index = objmesh->objvertexdata[i].vertex_index; memcpy(vertex_array, &obj->indexed_vertex[index], sizeof(vec3)); vertex_array += sizeof(vec3); memcpy(vertex_array, &obj->indexed_normal[index], sizeof(vec3)); vertex_array += sizeof(vec3); memcpy(vertex_array, &obj->indexed_uv[objmesh->objvertexdata[i].uv_index], sizeof(vec2)); vertex_array += sizeof(vec2); ++i; } glGenBuffers(1, &objmesh->vbo); glBindBuffer(GL_ARRAY_BUFFER, objmesh->vbo); glBufferData(GL_ARRAY_BUFFER, size, vertex_start, GL_STATIC_DRAW); free(vertex_start); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &objmesh->objtrianglelist[0].vbo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, objmesh->objtrianglelist[0].vbo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, objmesh->objtrianglelist[0].n_indice_array * sizeof(unsigned short), objmesh->objtrianglelist[0].indice_array, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); unsigned char attribute; stride = sizeof(vec3) + sizeof(vec3) + sizeof(vec2); glGenVertexArraysOES(1, &objmesh->vao); glBindVertexArrayOES(objmesh->vao); glBindBuffer(GL_ARRAY_BUFFER, objmesh->vbo); attribute = PROGRAM_get_vertex_attrib_location(program, (char *)"POSITION"); glEnableVertexAttribArray(attribute); glVertexAttribPointer(attribute, 3, GL_FLOAT, GL_FALSE, stride, (void *)NULL); attribute = PROGRAM_get_vertex_attrib_location(program, (char *)"NORMAL"); glEnableVertexAttribArray(attribute); glVertexAttribPointer(attribute, 3, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET(sizeof(vec3))); attribute = PROGRAM_get_vertex_attrib_location(program, (char *)"TEXCOORD0"); glEnableVertexAttribArray(attribute); glVertexAttribPointer(attribute, 2, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET(sizeof(vec3) + sizeof(vec3))); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, objmesh->objtrianglelist[0].vbo); glBindVertexArrayOES(0); texture = TEXTURE_create(obj->objmaterial[0].map_diffuse, obj->objmaterial[0].map_diffuse, 1, TEXTURE_MIPMAP, TEXTURE_FILTER_2X, 0.0f); }
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() ); }
void templateAppInit( int width, int height ) { atexit( templateAppExit ); GFX_start(); glViewport( 0.0f, 0.0f, width, height ); GFX_set_matrix_mode( PROJECTION_MATRIX ); { GFX_load_identity(); //设置为透视投影 GFX_set_perspective( 45.0f, ( float )width / ( float )height, 0.01f, 100.0f, 0.0f ); //角度越大,视角范围越宽广,相反,角度越小媒,投影也越窄 glDisable( GL_CULL_FACE ); } program = PROGRAM_init( ( char * )"default" ); program->vertex_shader = SHADER_init( VERTEX_SHADER, GL_VERTEX_SHADER ); program->fragment_shader = SHADER_init( FRAGMENT_SHADER, GL_FRAGMENT_SHADER ); m = mopen( VERTEX_SHADER, 1 ); if( m ) { if( !SHADER_compile( program->vertex_shader, ( char * )m->buffer, DEBUG_SHADERS ) ) exit( 1 ); } m = mclose( m ); m = mopen( FRAGMENT_SHADER, 1 ); if( m ) { if( !SHADER_compile( program->fragment_shader, ( char * )m->buffer, DEBUG_SHADERS ) ) exit( 2 ); } m = mclose( m ); if( !PROGRAM_link( program, DEBUG_SHADERS ) ) exit( 3 ); }
void draw_scene(void) { glBindFramebuffer(GL_FRAMEBUFFER, main_buffer); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glViewport(0, 0, viewport_matrix[2], viewport_matrix[3]); GFX_set_matrix_mode(PROJECTION_MATRIX); GFX_load_identity(); GFX_set_perspective(45.0f, (float)viewport_matrix[2] / (float)viewport_matrix[3], 0.1f, 100.0f, -90.0f); GFX_set_matrix_mode(MODELVIEW_MATRIX); GFX_load_identity(); GFX_translate(14.0f, -12.0f, 7.0f); GFX_rotate(48.5f, 0.0f, 0.0f, 1.0f); GFX_rotate(72.0, 1.0f, 0.0f, 0.0f); mat4_invert(GFX_get_modelview_matrix()); mat4 projector_matrix_copy; mat4_copy_mat4(&projector_matrix_copy, &projector_matrix); unsigned int i = 0; PROGRAM *program = OBJ_get_program(obj, "lighting", 0); while (i != obj->n_objmaterial) { obj->objmaterial[i].program = program; ++i; } glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, depth_texture); 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); mat4_copy_mat4(&projector_matrix, &projector_matrix_copy); mat4_translate(&projector_matrix, &projector_matrix, &objmesh->location); OBJ_draw_mesh(obj, i); GFX_pop_matrix(); ++i; } }
void templateAppDraw( void ) { glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); GFX_set_matrix_mode( PROJECTION_MATRIX ); GFX_load_identity(); GFX_set_perspective( 45.0f, ( float )viewport_matrix[ 2 ] / ( float )viewport_matrix[ 3 ], 0.1f, 100.0f, -90.0f ); draw_scene(); }
void templateAppInit(int width, int height) { GFX_start(); glViewport(0.0f, 0.0f, width, height); GFX_set_matrix_mode(PROJECTION_MATRIX); GFX_load_identity(); GFX_set_perspective(45.0f, (float)width / (float)height, 0.1f, 100.0f, -90.0f); obj = OBJ_load(OBJ_FILE, 1); unsigned int i = 0; while (i != obj->n_objmesh) { OBJ_build_mesh(obj, i); OBJ_free_mesh_vertex_data(obj, i); ++i; } i = 0; while (i != obj->n_texture) { OBJ_build_texture(obj, i, obj->texture_path, TEXTURE_MIPMAP, TEXTURE_FILTER_2X, 0.0f); ++i; } i = 0; while (i != obj->n_objmaterial) { MEMORY *fragment_shader = mopen((char *)"fragment.glsl", 1); MEMORY *vertex_shader = mopen((char *)"vertex.glsl", 1); OBJMATERIAL *objmaterial = &obj->objmaterial[i]; OBJ_build_material(obj, i, NULL); if (objmaterial->dissolve == 1.0f) minsert(fragment_shader, (char *)"#define SOLID_OBJECT\n", 0); else if (!objmaterial->dissolve) minsert(fragment_shader, (char *)"#define ALPHA_TESTED_OBJECT\n", 0); else minsert(fragment_shader, (char *)"#define TRANSPARENT_OBJECT\n", 0); if (objmaterial->illumination_model) { minsert(vertex_shader, (char *)"#define LIGHTING_SHADER\n", 0); minsert(fragment_shader, (char *)"#define LIGHTING_SHADER\n", 0); } objmaterial->program = PROGRAM_init(objmaterial->name); objmaterial->program->vertex_shader = SHADER_init((char *)"vertex", GL_VERTEX_SHADER); objmaterial->program->fragment_shader = SHADER_init((char *)"fragment", GL_FRAGMENT_SHADER); SHADER_compile(objmaterial->program->vertex_shader, (char *)vertex_shader->buffer, 1); SHADER_compile(objmaterial->program->fragment_shader, (char *)fragment_shader->buffer, 1); PROGRAM_set_bind_attrib_location_callback(objmaterial->program, program_bind_attrib_location); PROGRAM_link(objmaterial->program, 1); OBJ_set_draw_callback_material(obj, i, material_draw_callback); mclose(fragment_shader); mclose(vertex_shader); ++i; } }
void templateAppInit(int width, int height) { GFX_start(); glViewport(0.0f, 0.0f, width, height); GFX_set_matrix_mode(PROJECTION_MATRIX); GFX_load_identity(); GFX_set_perspective(45.0f, (float)width / (float)height, 0.1f, 100.0f, -90.0f); obj = OBJ_load(OBJ_FILE, 1); unsigned int i = 0; while (i != obj->n_objmesh) { OBJ_build_mesh(obj, i); OBJ_free_mesh_vertex_data(obj, i); ++i; } program = PROGRAM_create((char *)"default", VERTEX_SHADER, FRAGMENT_SHADER, 1, 0, program_bind_attrib_location, NULL); PROGRAM_draw(program); }
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 templateAppInit(int width, int height) { GFX_start(); glViewport(0.0f, 0.0f, width, height); GFX_set_matrix_mode(PROJECTION_MATRIX); GFX_load_identity(); GFX_set_perspective(80.0f, (float)width / (float)height, 1.0f, 100.0f, -90.0f); obj = OBJ_load(OBJ_FILE, 1); unsigned int i = 0; while (i != obj->n_objmesh) { if (strstr(obj->objmesh[i].name, "maze")) { navigation = NAVIGATION_init((char *)"maze"); navigation->navigationconfiguration.agent_height = 2.0f; navigation->navigationconfiguration.agent_radius = 0.4f; if (NAVIGATION_build(navigation, obj, i)) { console_print("Navigation generated.\n"); } else { console_print("Unable to create the navigation mesh."); } } OBJ_optimize_mesh(obj, i, 128); OBJ_build_mesh2(obj, i); OBJ_free_mesh_vertex_data(obj, i); ++i; } init_physic_world(); load_physic_world(); player = OBJ_get_mesh(obj, "player", 0); player->btrigidbody->setAngularFactor(0.0f); maze = OBJ_get_mesh(obj, "maze", 0); distance = maze->radius * 2.0f; i = 0; while (i != obj->n_texture) { OBJ_build_texture(obj, i, obj->texture_path, TEXTURE_MIPMAP | TEXTURE_16_BITS, TEXTURE_FILTER_2X, 0.0f); ++i; } i = 0; while (i != obj->n_objmaterial) { OBJ_build_material(obj, i, NULL); ++i; } program = PROGRAM_create((char *)"default", VERTEX_SHADER, FRAGMENT_SHADER, 1, 0, program_bind_attrib_location, NULL); }
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(); vec3 e = { 0.0f, -6.0f, 1.35f }, c = { 0.0f, -5.0f, 1.35f}, u = { 0.0f, 0.0f, 1.0f}; GFX_look_at(&e, &c, &u); unsigned int i = 0; while (i != obj->n_objmesh) { OBJMATERIAL *objmaterial = obj->objmesh[i].objtrianglelist[0].objmaterial; if (objmaterial->dissolve == 1.0f) { GFX_push_matrix(); GFX_translate(obj->objmesh[i].location.x, obj->objmesh[i].location.y, obj->objmesh[i].location.z); OBJ_draw_mesh(obj, i); GFX_pop_matrix(); } ++i; } glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); i = 0; while (i != obj->n_objmesh) { OBJMATERIAL *objmaterial = obj->objmesh[i].objtrianglelist[0].objmaterial; if (objmaterial->dissolve != 1.0f) { GFX_push_matrix(); GFX_translate(obj->objmesh[i].location.x, obj->objmesh[i].location.y, obj->objmesh[i].location.z); glCullFace(GL_FRONT); OBJ_draw_mesh(obj, i); glCullFace(GL_BACK); OBJ_draw_mesh(obj, i); GFX_pop_matrix(); } ++i; } 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(); { 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; } }