Exemplo n.º 1
0
void shader_program_file_destroy(shader_program_file_t *pfile)
{
	IF_FAILED(pfile);
	
	TRACE_MSG("release shader program file; program_id = %i \n", pfile->program.program_id);
	
	if(pfile->shaders_bit & SHADER_VERTEX_BIT) {
		shader_delete(&pfile->vertex_shader);
	}
	
	if(pfile->shaders_bit & SHADER_FRAGMENT_BIT) {
		shader_delete(&pfile->fragment_shader);
	}
	
	if(pfile->shaders_bit & SHADER_GEOMETRY_BIT) {
		shader_delete(&pfile->geometry_shader);
	}
	
	if(pfile->program.program_id) {
		shader_program_release(&pfile->program);
	}
	
	pfile->shaders_bit = 0;
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
	int i;
	Light light;
	const char *filename;
	Camera cam;
	Vec3 position = {0, 0, 150e9};
	Vec3 up =  {0, 1, 0};
	Vec3 target = {0, 0, 0};
	Shader *shader_light;
	Shader *shader_simple;
	Mesh *mesh;
	Renderable planet;
	if (argc < 2)
		filename = STRINGIFY(ROOT_PATH) "/data/teapot.ply";
	else
		filename = argv[1];

	solsys = solsys_load(STRINGIFY(ROOT_PATH) "/data/sol.ini");
	if (solsys == NULL)
		return 1;

	mesh = mesh_import(filename);
	if (mesh == NULL)
		return 1;
	for (i = 0; i < mesh->num_vertices; i++) /* Blow up the teapot */
	{
		mesh->vertex[i].x = mesh->vertex[i].x * 100;
		mesh->vertex[i].y = mesh->vertex[i].y * 100;
		mesh->vertex[i].z = mesh->vertex[i].z * 100;
	}

	cam.fov = M_PI/4;
	cam.left = 0;
	cam.bottom = 0;
	cam.width = 1024;
	cam.height = 768;
	cam.zNear = 1e6;
	cam.zFar = 4.5e15;
	init_allegro(&cam);
	cam_lookat(&cam, position, target, up);

	glewInit();

	shader_light = shader_create(STRINGIFY(ROOT_PATH) "/data/lighting.v.glsl", 
	                             STRINGIFY(ROOT_PATH) "/data/lighting.f.glsl");
	if (shader_light == NULL)
		return 1;
	
	shader_simple = shader_create(STRINGIFY(ROOT_PATH) "/data/simple.v.glsl", 
	                              STRINGIFY(ROOT_PATH) "/data/simple.f.glsl");
	if (shader_simple == NULL)
		return 1;

	glmProjectionMatrix = glmNewMatrixStack();
	glmViewMatrix = glmNewMatrixStack();
	glmModelMatrix = glmNewMatrixStack();

	light.position = light_pos;
	memcpy(light.ambient, light_ambient, sizeof(light_ambient));
	memcpy(light.diffuse, light_diffuse, sizeof(light_diffuse));
	memcpy(light.specular, light_specular, sizeof(light_specular));

	glClearColor(20/255., 30/255., 50/255., 1.0);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);
	glPointSize(2);

	planet.data = mesh;
	planet.upload_to_gpu = mesh_upload_to_gpu;
	planet.render = mesh_render;
	planet.shader = shader_light;
	renderable_upload_to_gpu(&planet);

	/* Transformation matrices */
	cam_projection_matrix(&cam, glmProjectionMatrix);

	/* Start rendering */
	while(handle_input(ev_queue, &cam))
	{
		void *ctx;
		Entity *renderlist, *prev;

		t += 365*86400;

		/* Physics stuff */
		solsys_update(solsys, t);
		ctx = ralloc_context(NULL);

		prev = NULL;
		for (i = 0; i < solsys->num_bodies; i++)
		{
			Entity *e;

			e = ralloc(ctx, Entity);
			e->orientation = (Quaternion) {1, 0, 0, 0};
			e->renderable = &planet;
			e->position = solsys->body[i].position;
			e->radius = solsys->body[i].radius;
			e->prev = prev;
			e->next = NULL;
			if (prev != NULL)
				e->prev->next = e;
			prev = e;

			if (i == 0)
				renderlist = e;
		}

		/* Rendering stuff */
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		glmLoadIdentity(glmModelMatrix);
		glmLoadIdentity(glmViewMatrix);
		cam_view_matrix(&cam, glmViewMatrix); /* view */

		light_upload_to_gpu(&light, shader_light);

		render_entity_list(renderlist);

		al_flip_display();
		calcfps();

		ralloc_free(ctx);
	}

	ralloc_free(mesh);
	ralloc_free(solsys);

	shader_delete(shader_light);
	shader_delete(shader_simple);
	glmFreeMatrixStack(glmProjectionMatrix);
	glmFreeMatrixStack(glmViewMatrix);
	glmFreeMatrixStack(glmModelMatrix);
	al_destroy_display(dpy);
	return 0;
}
Exemplo n.º 3
0
void shader_free(struct shader *s)
{
	shader_uniforms_free(s);
	shader_delete(s);
}