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);
}
Пример #2
0
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;
	}
}
Пример #3
0
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 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 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());
}
Пример #6
0
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 );
}
Пример #7
0
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;
    }
}
Пример #9
0
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 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 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, &center, &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_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);
}
Пример #13
0
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();
}
Пример #14
0
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, &center, &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 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;
    }
}
Пример #16
0
void templateAppDraw( void ) {

	if( game_state == 2 ) {

		free_level();
		load_level();
	}
   
	glClearColor( 1.0f, 1.0f, 1.0f, 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 ],
						 0.1f,
						 50.0f,
						 -90.0f );

	GFX_set_matrix_mode( MODELVIEW_MATRIX );
	GFX_load_identity();

	/* Linearly interpolate the accelerometer values to get a smooth transition. */
	next_accelerometer.x = accelerometer.x * 0.1f + next_accelerometer.x * 0.9f;
	next_accelerometer.y = accelerometer.y * 0.1f + next_accelerometer.y * 0.9f;

	/* Assign the current Y rotation of the accelerometer to the Z rotation of the camera,
	 * multiplied by the accelerometer sensitivity factor. */
	rotz += next_accelerometer.y * sensitivity;

	/* The forward vector of the ball. */
	vec3 forward = { 0.0f, 1.0f, 0.0f },
			/* The current direction vector of the ball. Basically, this is the
			forward vector rotated by the cameraÕs Z rotation. */
		 direction;

	/* If the game is running, let the user move the ball. */
	if( !game_state ) {
		/* Pre-calculate a few variables before rotating the forward vector by the cameraÕs Z rotation. */
		float r = rotz * DEG_TO_RAD,
			  c = cosf( r ),
			  s = sinf( r );

		/* Rotate the forward vector and store the result into the
		direction variable. Because both vectors are already normalized,
		thereÕs no need to re-normalize them again. */
		direction.x = c * forward.y - s * forward.x;
		direction.y = s * forward.y + c * forward.x;

		float speed = CLAMP( ( -next_accelerometer.x * sensitivity ) * ball_speed,
							   -ball_speed,
							    ball_speed );

		/* Assign the direction vector multiplied by the current speed to the
		angular velocity of the ball. */
		player->btrigidbody->setAngularVelocity( btVector3( direction.x * speed,
															direction.y * speed,
														    0.0f ) );
		/* Activate the rigid body to make sure that the angular velocity
		will be applied. */
		player->btrigidbody->setActivationState( ACTIVE_TAG );
	}

	next_eye.x = player->location.x + 
				 distance * 
				 cosf( rotx * DEG_TO_RAD ) * 
				 sinf( rotz * DEG_TO_RAD );

	next_eye.y = player->location.y - 
				 distance *
				 cosf( rotx * DEG_TO_RAD ) *
				 cosf( rotz * DEG_TO_RAD );

	next_eye.z = player->location.z +
				 distance *
				 sinf( rotx * DEG_TO_RAD );

	player->location.z += player->dimension.z;

	btVector3 p1( player->location.x,
				  player->location.y,
				  player->location.z ),

			  p2( next_eye.x,
				  next_eye.y,
				  next_eye.z );

	ClosestNotMeRayResultCallback back_ray( player->btrigidbody,
											p1,
											p2 );

	dynamicsworld->rayTest( p1,
							p2,
							back_ray );

	if( back_ray.hasHit() ) { 

		back_ray.m_hitNormalWorld.normalize();

		next_eye.x =  back_ray.m_hitPointWorld.x() +
					( back_ray.m_hitNormalWorld.x() * 0.1f );

		next_eye.y =  back_ray.m_hitPointWorld.y() +
					( back_ray.m_hitNormalWorld.y() * 0.1f );

		next_eye.z =  back_ray.m_hitPointWorld.z() +
					( back_ray.m_hitNormalWorld.z()* 0.1f );
	}

	eye.x = next_eye.x * 0.05f + eye.x * 0.95f;
	eye.y = next_eye.y * 0.05f + eye.y * 0.95f;
	eye.z = next_eye.z * 0.05f + eye.z * 0.95f;

	/* Calculate the direction vector from the player to the current eye location. */
	vec3_diff( &direction, &player->location, &eye );
	/* Normalize the direction vector. */
	vec3_normalize( &direction, &direction );

	AUDIO_set_listener( &eye, &direction, &up );


	GFX_look_at( &eye,
				 &player->location,
				 &up );
	
	build_frustum( frustum,
				  GFX_get_modelview_matrix(),
				  GFX_get_projection_matrix() );	


	unsigned int i = 0;

	while( i != obj->n_objmesh ) {

		OBJMESH *objmesh = &obj->objmesh[ i ];

		objmesh->distance = sphere_distance_in_frustum( frustum, 
														&objmesh->location,
														objmesh->radius );

		if( objmesh->distance && objmesh->visible )
		{
			GFX_push_matrix();
			/* Check if the current objmesh name contains Ògem.Ó
			 * If yes, donÕt ask Bullet for the transformation matrix and handle the position and
			 * rotation manually. */
			if( strstr( objmesh->name, "gem" ) ) {

				GFX_translate( objmesh->location.x, 
							   objmesh->location.y, 
							   objmesh->location.z );
				
				objmesh->rotation.z += 1.0f;

				GFX_rotate( objmesh->rotation.z, 0.0f, 0.0f, 1.0f );
			}
			
			else if( objmesh->btrigidbody )
			{
				mat4 mat;

				objmesh->btrigidbody->getWorldTransform().getOpenGLMatrix( ( float * )&mat );
				
				memcpy( &objmesh->location, ( vec3 * )&mat.m[ 3 ], sizeof( vec3 ) );

				GFX_multiply_matrix( &mat );				
			}
			else
			{
				GFX_translate( objmesh->location.x, 
							   objmesh->location.y, 
							   objmesh->location.z );
			}

			OBJ_draw_mesh( obj, i );

			GFX_pop_matrix();
		}
		
		++i;
	}
	
	dynamicsworld->stepSimulation( 1.0f / 60.0f );

	
	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 font_color = { 0.0f, 0.0f, 0.0f, 1.0f };

	char gem_str  [ MAX_CHAR ] = {""},
		 time_str [ MAX_CHAR ] = {""},
		 level_str[ MAX_CHAR ] = {""};

	if( game_state ) { 
	
		sprintf( level_str, "Level Clear!" );

		FONT_print( font_big,
					viewport_matrix[ 3 ] * 0.5f - FONT_length( font_big, level_str ) * 0.5f + 4.0f,
					viewport_matrix[ 2 ] - font_big->font_size * 1.5f - 4.0f,
					level_str,
					&font_color );

		/* Yellow. */
		font_color.x = 1.0f;
		font_color.y = 1.0f;
		font_color.z = 0.0f;

		FONT_print( font_big,
					viewport_matrix[ 3 ] * 0.5f - FONT_length( font_big, level_str ) * 0.5f,
					viewport_matrix[ 2 ] - font_big->font_size * 1.5f,
		level_str,
		&font_color );   
	}

	font_color.x = 0.0f;
	font_color.y = 0.0f;
	font_color.z = 0.0f;

	sprintf( gem_str, "Gem Points:%02d", gem_points );
	sprintf( time_str, "Game Time:%02.2f", game_time * 0.1f );

	FONT_print( font_small,
				viewport_matrix[ 3 ] - FONT_length( font_small, gem_str ) - 6.0f,
				( font_small->font_size * 0.5f ),
				gem_str,
				&font_color );
	
	FONT_print( font_small,
				8.0f,
				( font_small->font_size * 0.5f ),
				time_str,
				&font_color );

	font_color.x = 1.0f;
	font_color.y = 1.0f;
	font_color.z = 0.0f;

	FONT_print( font_small,
				viewport_matrix[ 3 ] - FONT_length( font_small, gem_str ) - 8.0f,
				( font_small->font_size * 0.5f ),
				gem_str,
				&font_color );
	
	FONT_print( font_small,
				6.0f,
				( font_small->font_size * 0.5f ),
				time_str,
				&font_color );

	if( !game_state ) game_time += SOUND_get_time( background_sound );
}
Пример #17
0
void templateAppDraw( void ) {

	if( game_state == 2 ) {

		free_level();
		load_level();
	}
   
	glClearColor( 1.0f, 1.0f, 1.0f, 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 ],
						 0.1f,
						 50.0f,
						 0.0f );

	GFX_set_matrix_mode( MODELVIEW_MATRIX );
	GFX_load_identity();


	if(turn_left())
		{
			next_rotz+=1.2;

		}
	if(turn_right())

	{
	         next_rotz-=1.2;
	}

	vec3 forward = { 0.0f, 1.0f, 0.0f },direction;

	if( !game_state )
	{
		//Pre-calculate a few variables before rotating the forward vector by the camera's Z rotation.
		float r = rotz * DEG_TO_RAD, c = cosf( r ), s = sinf( r );

		//Rotate the forward vector and store the result into the direction variable. Because
		//both vectors are already normalized, there's no need to re-normalize them one more time.

		direction.x = c * forward.y - s * forward.x;
		direction.y = s * forward.y + c * forward.x;


		//Calculate the current angular velocity that is relevant to the
		//accelerometer value that we are using as the force factor.
		//Then clamp the result to make sure that the speed is between the minimum and
		//maximum ball speed thresholds.
		float speed = CLAMP(   -maximum_speed,
		                       -maximum_speed,
								maximum_speed );



			player->btrigidbody->setAngularVelocity( btVector3( direction.x * speed,direction.y * speed,0.0f ) );


			//Activate the rigid body to make sure that the angular velocity will be applied.
			player->btrigidbody->setActivationState( ACTIVE_TAG );
		}
	/*
	if( view_delta.x || view_delta.y )
	{

		if( view_delta.x ) next_rotz -= view_delta.x;

		if( view_delta.y )
		{
			next_roty += view_delta.y;
			next_roty = CLAMP( next_roty, -180.0f, -90.0f );
		}

		view_delta.x =
		view_delta.y = 0.0f;
	}


	if( move_delta.z )
	{
		vec3 direction;

		float r = rotz * DEG_TO_RAD,
				      c = cosf( r ),
				      s = sinf( r );

		direction.x =  s * move_delta.y + c * move_delta.x;
		direction.y =  c * move_delta.y - s * move_delta.x;

		player->btrigidbody->setAngularVelocity( 2*btVector3( -direction.y * ( move_delta.z * 6.7f ),
															  -direction.x * ( move_delta.z * 6.7f ),
															  0.0f ) );


		player->btrigidbody->setActivationState( ACTIVE_TAG );
	}*/

	//console_print("player_x: %3.f player_y: %3.f\n",player->location.x,player->location.y);

	next_eye.x = player->location.x +
					 distance *
					 cosf( roty * DEG_TO_RAD ) *
					 sinf( rotz * DEG_TO_RAD );

	next_eye.y = player->location.y -
					 distance *
					 cosf( roty * DEG_TO_RAD ) *
					 cosf( rotz * DEG_TO_RAD );


	next_eye.z = player->location.z +
					       distance *
					      sinf( roty * DEG_TO_RAD );
		

	btVector3 p1(   player->location.x,
					 player->location.y,
					 player->location.z ),

			     p2( next_eye.x,
					 next_eye.y,
					 next_eye.z );

	ClosestNotMeRayResultCallback back_ray( player->btrigidbody,
											   p1,
											   p2 );
	dynamicsworld->rayTest( p1,
							p2,
							back_ray );

	if( back_ray.hasHit() )
	{

		back_ray.m_hitNormalWorld.normalize();

		next_eye.x =   back_ray.m_hitPointWorld.x() +
					   ( back_ray.m_hitNormalWorld.x() * 0.1f );

		next_eye.y =   back_ray.m_hitPointWorld.y() +
					   ( back_ray.m_hitNormalWorld.y()* 0.1f );

		next_eye.z =   back_ray.m_hitPointWorld.z() +
					   ( back_ray.m_hitNormalWorld.z()* 0.1f );
	}



	roty = roty * 0.9f + next_roty * 0.1f;
	rotz = rotz * 0.9f + next_rotz * 0.1f;

	eye.x = eye.x * 0.95f + next_eye.x * 0.05f;
	eye.y = eye.y * 0.95f + next_eye.y * 0.05f;
	eye.z = eye.z * 0.95f + next_eye.z * 0.05f;

	player->location.z += player->dimension.z * 0.5f;

	/*
	 * needed for directional audio
	 */
	vec3 player_location={player->location.x,player->location.y,player->location.z};
	AUDIO_set_listener( &eye, &player_location, &up );


	GFX_look_at( &eye,
				 &player->location,
				 &up );
	
	build_frustum( frustum,
				  GFX_get_modelview_matrix(),
				  GFX_get_projection_matrix() );	


	unsigned int i = 0;

	while( i != obj->n_objmesh ) {

		OBJMESH *objmesh = &obj->objmesh[ i ];

		objmesh->distance = sphere_distance_in_frustum( frustum, 
														&objmesh->location,
														objmesh->radius );

		if( objmesh->distance && objmesh->visible )
		{
			GFX_push_matrix();

			if( strstr( objmesh->name, "gem" ) ) {

				GFX_translate( objmesh->location.x, 
							   objmesh->location.y, 
							   objmesh->location.z );
				
				objmesh->rotation.z += 1.0f;

				GFX_rotate( objmesh->rotation.z, 0.0f, 0.0f, 1.0f );
			}
			
			else if( objmesh->btrigidbody )
			{
				mat4 mat;

				objmesh->btrigidbody->getWorldTransform().getOpenGLMatrix( ( float * )&mat );
				
				memcpy( &objmesh->location, ( vec3 * )&mat.m[ 3 ], sizeof( vec3 ) );

				GFX_multiply_matrix( &mat );				
			}
			else
			{
				GFX_translate( objmesh->location.x, 
							   objmesh->location.y, 
							   objmesh->location.z );
			}

			OBJ_draw_mesh( obj, i );

			GFX_pop_matrix();
		}
		
		++i;
	}
	if(!game_state )
	{
	dynamicsworld->stepSimulation( 1.0f / 60.0f );
	}
	
	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( 0.0f, 0.0f, 0.0f, 1.0f );

	GFX_translate( -half_width, -half_height, 0.0f );



	GFX_set_matrix_mode( MODELVIEW_MATRIX );

	GFX_load_identity();
	
	vec4 font_color = { 0.0f, 0.0f, 0.0f, 1.0f };

	char gem_str  [ MAX_CHAR ] = {""},
		 time_str [ MAX_CHAR ] = {""},
		 level_str[ MAX_CHAR ] = {""},
		 pause_str [ MAX_CHAR ] = {""};

	if( game_state == 3 )
	{
		sprintf( pause_str, "Touch to Resume" );

				FONT_print( font_big,
							viewport_matrix[ 2 ] * 0.5f - FONT_length( font_big, pause_str ) * 0.5f + 4.0f,
							viewport_matrix[ 3 ] * 0.7f - font_big->font_size * 1.5f - 4.0f,
							pause_str,
							&font_color );

				/* Yellow. */
				font_color.x = 1.0f;
				font_color.y = 1.0f;
				font_color.z = 0.0f;

				FONT_print( font_big,
							viewport_matrix[ 2 ] * 0.5f - FONT_length( font_big, pause_str ) * 0.5f,
							viewport_matrix[ 3 ] * 0.7f - font_big->font_size * 1.5f,
							pause_str,
							&font_color );
	}
	if( game_state == 1 )
	{
	
		sprintf( level_str, "Level Clear!" );

		FONT_print( font_big,
					viewport_matrix[ 2 ] * 0.5f - FONT_length( font_big, level_str ) * 0.5f + 4.0f,
					viewport_matrix[ 3 ] - font_big->font_size * 1.5f - 4.0f,
					level_str,
					&font_color );

		/* Yellow. */
		font_color.x = 1.0f;
		font_color.y = 1.0f;
		font_color.z = 0.0f;

		FONT_print( font_big,
					viewport_matrix[ 2 ] * 0.5f - FONT_length( font_big, level_str ) * 0.5f,
					viewport_matrix[ 3 ] - font_big->font_size * 1.5f,
					level_str,
					&font_color );

	}

	font_color.x = 0.0f;
	font_color.y = 0.0f;
	font_color.z = 0.0f;

	sprintf( gem_str, "Gem Points:%02d", gem_points );
	sprintf( time_str, "Game Time:%02.2f", game_time * 0.1f );

	FONT_print( font_small,
				viewport_matrix[ 2 ] - FONT_length( font_small, gem_str ) - 6.0f,
				( font_small->font_size * 0.5f ),
				gem_str,
				&font_color );

	FONT_print( font_small,
				8.0f,
				( font_small->font_size * 0.5f ),
				time_str,
				&font_color );

	font_color.x = 1.0f;
	font_color.y = 1.0f;
	font_color.z = 0.0f;

	FONT_print( font_small,
				viewport_matrix[ 2 ] - FONT_length( font_small, gem_str ) - 8.0f,
				( font_small->font_size * 0.5f ),
				gem_str,
				&font_color );
	
	FONT_print( font_small,
				6.0f,
				( font_small->font_size * 0.5f ),
				time_str,
				&font_color );


	if( !game_state ) game_time += SOUND_get_time( background_sound );

}
Пример #18
0
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( 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 ) {
	
		OBJ_optimize_mesh( obj, i, 128 );

		/* OBJ_build_mesh2 is another version of the OBJ_build_mesh that do not 
		   use VAO (only pure glDraw calls), at the time of writing this book mixing
		   direct rendering using glDraw commands (as you will do in this chapter) with
		   VAO cause issues on some Android drivers. */
		OBJ_build_mesh2( obj, i );
		
		OBJ_free_mesh_vertex_data( obj, i );

		++i;
	}
	
	
	init_physic_world();
	
	load_physic_world();
	

	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 );
}
Пример #19
0
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.1f,
						 100.0f,
						 0.0f );

	obj = OBJ_load( OBJ_FILE, 1 );

	unsigned int i = 0;

	while( i != obj->n_objmesh ) {

		console_print( "%s: %d: GL_TRIANGLES\n",
					   obj->objmesh[ i ].name,
					   obj->objmesh[ i ].objtrianglelist[ 0 ].n_indice_array );

		OBJ_optimize_mesh( obj, i, 128 );

		console_print( "%s: %d: GL_TRIANGLE_STRIP\n",
					   obj->objmesh[ i ].name,
					   obj->objmesh[ i ].objtrianglelist[ 0 ].n_indice_array );

		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_16_BITS_5551,
						   TEXTURE_FILTER_2X,
						   0.0f );
		++i;
	}	


	i = 0;

	while( i != obj->n_objmaterial ) {
	
		OBJ_build_material( obj,
							i,
							PROGRAM_create( ( char * )"default",
											VERTEX_SHADER,
											FRAGMENT_SHADER,
											1,
											1,
											program_bind_attrib_location,
											NULL ) );
		
		OBJ_set_draw_callback_material( obj, i, material_draw_callback );
		
		++i;
	}
}
Пример #20
0
//获取obj文件,再获取该文件的网格
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.1f,
						 100.0f,
						 0.0f );
    //新建一个可自动加载、编译和链接着色器程序的着色器程序
	program = PROGRAM_create( ( char * )"default",
							  VERTEX_SHADER,
							  FRAGMENT_SHADER,
							  1,
							  DEBUG_SHADERS,
							  NULL,   //属性回调函数
							  program_draw_callback );   //绘制回调函数

    //加载obj文件
	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 ]; //里面包含多个obj文件的实体

    //计算顶点数据数组的总大小,以便可以分配为VBO构建GLES友好的顶点数据数组所需的内存大小
	size = objmesh->n_objvertexdata * sizeof( vec3 ) * sizeof( vec3 );

	vertex_array = ( unsigned char * ) malloc( size );
	vertex_start = vertex_array;

    //根据索引的顶点位置以及objmesh结构中包含的顶点法线构建顶点数据数组
	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 );

		++i;
	}
    
    //请求驱动程序为VBO创建一个新的缓冲区索引,并使其处于活动状态
	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 );

    //----------------------------------------------------
    
    //为第一个objmesh三角形列表创建一个新id,并使当前索引处于活动状态
	glGenBuffers( 1, &objmesh->objtrianglelist[ 0 ].vbo );
	
    
	glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 
				  objmesh->objtrianglelist[ 0 ].vbo );

    //以类似发送数组缓冲区的方式,将该索引数组发送到GPU
	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 );


    //创建VAO
    //计算不同顶点数据之间的顶点数组跨度
	unsigned char attribute;
				  stride = sizeof( vec3 )+
						   sizeof( vec3 );

    //创建一个新的VAO索引,并使其保持活动状态
	glGenVertexArraysOES( 1, &objmesh->vao );

	glBindVertexArrayOES( objmesh->vao );

    
    //开始构建VAO列表绑定,其中包括相关的调用来设置数组缓冲区
	glBindBuffer( GL_ARRAY_BUFFER, objmesh->vbo );

    //在VAO列表中包括POSITION顶点属性调用
	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 ) ) );
    
    //在关闭VAO列表之前绑定数组元素缓冲区(索引)
	glBindBuffer( GL_ELEMENT_ARRAY_BUFFER,
				  objmesh->objtrianglelist[ 0 ].vbo );					   

    //停用当前的VAO,其次对前面所调用的类似数组的所有命令进行编译,然后将它们与VAO索引相关亮
	glBindVertexArrayOES( 0 );
}
Пример #21
0
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,
				 &center,
				 &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 );
	}
}