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 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 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; } }
FONT *FONT_init( char *name ) { FONT *font = ( FONT * ) calloc( 1, sizeof( FONT ) ); strcpy( font->name, name ); font->program = PROGRAM_init( name ); font->program->vertex_shader = SHADER_init( name, GL_VERTEX_SHADER ); SHADER_compile( font->program->vertex_shader, "uniform mediump mat4 MODELVIEWPROJECTIONMATRIX;" "attribute mediump vec2 POSITION;" "attribute lowp vec2 TEXCOORD0;" "varying lowp vec2 texcoord0;" "void main( void ) {" "texcoord0 = TEXCOORD0;" "gl_Position = MODELVIEWPROJECTIONMATRIX * vec4( POSITION.x, POSITION.y, 0.0, 1.0 ); }", 0 ); font->program->fragment_shader = SHADER_init( name, GL_FRAGMENT_SHADER ); SHADER_compile( font->program->fragment_shader, "uniform sampler2D DIFFUSE;" "uniform lowp vec4 COLOR;" "varying lowp vec2 texcoord0;" "void main( void ) {" "lowp vec4 color = texture2D( DIFFUSE, texcoord0 );" "color.x = COLOR.x;" "color.y = COLOR.y;" "color.z = COLOR.z;" "color.w *= COLOR.w;" "gl_FragColor = color; }", 0 ); PROGRAM_link( font->program, 0 ); return font; }
/*! 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 ); }