Exemplo n.º 1
0
void generateObjectBufferTeapot () {
	const char * file_name = "../Shaders/grass.png";
	unsigned int tex = 0;
	unsigned int vp_vbo = 0, vt_vbo = 0;
	unsigned int tex_id;
	GLuint vn_vbo = 0;

	unsigned int vp_vbo2 = 0, vt_vbo2 = 0;
	GLuint vn_vbo2 = 0;
	unsigned int vp_vbo3 = 0, vt_vbo3 = 0;
	GLuint vn_vbo3 = 0;
	float * points = NULL, * normals = NULL, * textures = NULL;
	//	load_obj_file(MESH_NAME, points, textures, normals, g_point_count1);


	//	loading_meshes(g_vp, g_vn, vaos[0], g_point_count1, vp_vbo, vn_vbo, vt_vbo);
	g_vp.clear(); g_vn.clear(); g_vt.clear();
	// plane
	load_mesh(WORLD, g_point_count2);
	//	load_image_to_texture(file_name, tex, true);
	float texcoords[] = { 0.0f,1.0f,0.0f,0.0f,1.0f,0.0f,1.0,0.0,1.0,1.0,0.0,1.0 };
	loading_meshes(g_vp, g_vn, vaos[1], g_point_count2, vp_vbo2, vn_vbo2, vt_vbo2, true, tex = 1, "../Shaders/grass.bmp");
	g_vp.clear(); g_vn.clear();
	// spiral
	load_mesh(OBJECT, g_point_count3);
	loading_meshes(g_vp, g_vn, vaos[2], g_point_count3, vp_vbo3, vn_vbo3, vt_vbo3, true, tex = 1, "../Shaders/grass.bmp");
	e_init(&zombie, vec3(0.0,0.0, 10.0), collisionsphere(vec3(0.0,0.0, 10.0), 2), vec3(0,0,0), getPosition());
	g_vp.clear(); g_vn.clear();
//	drawSkybox(g_vp, g_point_count1, vaos[0]);
}
Exemplo n.º 2
0
static int load_object(scene_t *scene,FILE *file,char *path,int texture_mode) {
    char buffer[128],name[256];
    fscanf(file,"%s",buffer);
    while(fscanf(file,"%s",buffer) != EOF) {
        if(buffer[0] == '#') skeep_comment(file);
        else if(!strcmp(buffer,"mesh")) {
            load_name(file,path,name);
            scene->mesh = load_mesh(name,path,&scene->num_mesh,texture_mode);
            if(!scene->mesh) return 0;
        } else if(!strcmp(buffer,"thing")) {
            load_name(file,path,name);
            scene->thing = load_thing(name,scene->mesh,scene->num_mesh,&scene->num_thing);
            if(!scene->thing) return 0;
        } else if(!strcmp(buffer,"animation")) {
            load_name(file,path,name);
            scene->animation = load_animation(name,path,scene->mesh,scene->num_mesh,&scene->num_animation);
            if(!scene->animation) return 0;
        } else if(!strcmp(buffer,"particle")) {
            load_name(file,path,name);
            scene->particle = load_particle(name,path,&scene->num_particle,texture_mode);
            if(!scene->particle) return 0;
        } else if(!strcmp(buffer,"dynamiclight")) {
            load_name(file,path,name);
            scene->dynamiclight = load_dynamiclight(name,path,&scene->num_dynamiclight,texture_mode);
            if(!scene->dynamiclight) return 0;
        } else if(!strcmp(buffer,"}")) break;
        else return 0;
    }
    return 1;
}
Exemplo n.º 3
0
Arquivo: bind.c Projeto: ccxvii/mio
static int ffi_new_mesh(lua_State *L)
{
	const char *name = luaL_checkstring(L, 1);
	struct mesh *mesh = load_mesh(name);
	if (!mesh)
		return luaL_error(L, "cannot load mesh: %s", name);
	lua_pushlightuserdata(L, mesh);
	return 1;
}
Exemplo n.º 4
0
void CDomain::signal_load_mesh ( Common::SignalArgs& node )
{
  SignalOptions options( node );

  URI fileuri = options.value<URI>("file");

  std::string name ("mesh");
  if( options.check("name") )
    name = options.value<std::string>("name");

  load_mesh( fileuri, name);
}
Exemplo n.º 5
0
	Mesh(std::string filename) :
		pos(0.1f, -1.0f, -1.5f),
		scale(10.0f),
		ambient(1.0f),
		diffuse(1.0f),
		specular(0.0f),
		specPower(0.0f),
		Immediate(true),
		render(&Mesh::RenderImmediate)
	{
		load_mesh(filename);
	}
Exemplo n.º 6
0
void Domain::signal_load_mesh ( common::SignalArgs& node )
{
  SignalOptions options( node );

  URI fileuri = options.value<URI>("file");

  std::string name ("mesh");
  if( options.check("name") )
    name = options.value<std::string>("name");

  Mesh& created_component = load_mesh( fileuri, name);

  SignalFrame reply = node.create_reply(uri());
  SignalOptions reply_options(reply);
  reply_options.add("created_component", created_component.uri());
}
Exemplo n.º 7
0
rdr_t* setup_render_data(void)
{
//    vertex* vertices;
//    obj_t* render_object;
    rdr_t* render_data;
    obj_t* square;

    render_data = (rdr_t*)calloc(1, sizeof(rdr_t));
    render_data->objects = (obj_t**)calloc(1, sizeof(obj_t*));
    render_data->asociation = (short*)calloc(1, sizeof(short));
    square = load_mesh("square.txt");
    render_data->objects[0] = setup_object(square);
    render_data->asociation[0] = 0;
    render_data->draw_calls = 1;
    return render_data;
}
Exemplo n.º 8
0
/**
 * FbxNode からメッシュ情報を再帰的に読み込む
 *
 * @param node FbxNode
 */
void FbxFileLoader::load_mesh_recursive( FbxNode* node )
{
	load_mesh( node->GetMesh() );

	// std::cout << "material count : " << node->GetMaterialCount() << std::endl;

	for ( int n = 0; n < node->GetMaterialCount(); n++ )
	{
		fbx_material_index_ = n;
		load_material( node->GetMaterial( n ) );
	}

	for ( int n = 0; n < node->GetChildCount(); n++ )
	{
		load_mesh_recursive( node->GetChild( n ) );
	}
}
Exemplo n.º 9
0
int main()
{
    // decomp
    plugin_ldpath(".");

    void *pl = plugin_load("libdecomp.so");

    if (!pl)
    {
        printf("missing plugin\n");
        return 1;
    }

    decomp3f_init_f pl_init = (decomp3f_init_f)plugin_fn(pl, DECOMP3F_INIT_F_SYM);
    decomp3f_make_f pl_make = (decomp3f_make_f)plugin_fn(pl, DECOMP3F_MAKE_F_SYM);
    decomp3f_clear_f pl_clear = (decomp3f_clear_f)plugin_fn(pl, DECOMP3F_CLEAR_F_SYM);

    struct decomp3f_s d;

    struct decompmesh3f_s dm;
    load_mesh(&dm, "../data/mushroom.off");

    pl_init(&d);

    uint64_t start = timer_usec();

    pl_make(&d, &dm);

    uint64_t end = timer_usec();

    printf("decomposition took %lu usecs; sub-meshes: %d\n", static_cast<unsigned long>(end - start), static_cast<int>(d.ms));

    pl_clear(&d);

    plugin_unload(pl);

    free(dm.f);
    free(dm.v);

    return 0;
}
Exemplo n.º 10
0
	void MeshCache::get_mesh(const String &name,
		AbstractMeshSharedPtr &mesh, StringVector &materials)
	{
		MeshCache::MeshCacheItem tmp;
		MeshCacheMap::const_iterator found;

		found = m_mesh_cache.find(name);

		if (found == m_mesh_cache.end())
		{
			load_mesh(name, tmp.m_mesh, tmp.m_materials);

			m_mesh_cache[name] = tmp;

			mesh = tmp.m_mesh;
			materials = tmp.m_materials;
		}
		else
		{
			mesh = found->second.m_mesh;
			materials = found->second.m_materials;
		}
	}
Exemplo n.º 11
0
Arquivo: main.cpp Projeto: HPeX/hermes
int main(int argc, char* argv[])
{
  // Load mesh.
  load_mesh(mesh, "domain.xml", INIT_REF_NUM);

  // Create an H1 space with default shapeset.
  SpaceSharedPtr<double> space(new H1Space<double>(mesh, &essential_bcs, P_INIT));
  solver.set_weak_formulation(&weak_formulation);
  solver.set_space(space);

#pragma region Time stepping loop.
  /*
    solver.set_time_step(time_step_length);
    do
    {
    std::cout << "Time step: " << time_step_number << std::endl;

    #pragma region Spatial adaptivity loop.
    adaptivity.set_space(space);
    int adaptivity_step = 1;
    do
    {
    std::cout << "Adaptivity step: " << adaptivity_step << std::endl;
    #pragma region Construct globally refined reference mesh and setup reference space.
    MeshSharedPtr ref_mesh = ref_mesh_creator.create_ref_mesh();
    SpaceSharedPtr<double> ref_space = ref_space_creator.create_ref_space(space, ref_mesh);
    solver.set_space(ref_space);
    #pragma endregion
    try
    {
    // Solving.
    solver.solve(get_initial_Newton_guess(adaptivity_step, &weak_formulation, space, ref_space, sln_time_prev));
    Solution<double>::vector_to_solution(solver.get_sln_vector(), ref_space, sln_time_new);
    }
    catch(Exceptions::Exception& e)
    {
    std::cout << e.info();
    }
    catch(std::exception& e)
    {
    std::cout << e.what();
    }

    // Project the fine mesh solution onto the coarse mesh.
    OGProjection<double>::project_global(space, sln_time_new, sln_time_new_coarse);

    // Calculate element errors and error estimate.
    errorCalculator.calculate_errors(sln_time_new_coarse, sln_time_new);
    double error_estimate = errorCalculator.get_total_error_squared() * 100;
    std::cout << "Error estimate: " << error_estimate << "%" << std::endl;

    // Visualize the solution and mesh.
    display(sln_time_new, ref_space);

    // If err_est too large, adapt the mesh.
    if (error_estimate < ERR_STOP)
    break;
    else
    adaptivity.adapt(&refinement_selector);

    adaptivity_step++;
    }
    while(true);
    #pragma endregion

    #pragma region No adaptivity in space.
    try
    {
    // Solving.
    solver.solve(sln_time_prev);

    // Get the solution for visualization etc. from the coefficient vector.
    Solution<double>::vector_to_solution(solver.get_sln_vector(), space, sln_time_new);

    // Visualize the solution and mesh.
    display(sln_time_new, space);
    }
    catch(Exceptions::Exception& e)
    {
    std::cout << e.info();
    }
    catch(std::exception& e)
    {
    std::cout << e.what();
    }
    #pragma endregion

    sln_time_prev->copy(sln_time_new);

    // Increase current time and counter of time steps.
    current_time += time_step_length;
    time_step_number++;
    }
    while (current_time < T_FINAL);
    */
#pragma endregion

#pragma region No time stepping (= stationary problem).
  try
  {
    // Solving.
    solver.solve();

    // Get the solution for visualization etc. from the coefficient vector.
    Solution<double>::vector_to_solution(solver.get_sln_vector(), space, sln_time_new);

    // Visualize the solution and mesh.
    display(sln_time_new, space);
  }
  catch (Exceptions::Exception& e)
  {
    std::cout << e.info();
  }
  catch (std::exception& e)
  {
    std::cout << e.what();
  }
#pragma endregion

  View::wait();
  return 0;
}
Exemplo n.º 12
0
int main () {
	restart_gl_log ();
	start_gl ();
	glEnable (GL_DEPTH_TEST); // enable depth-testing
	glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"
	glEnable (GL_CULL_FACE); // cull face
	glCullFace (GL_BACK); // cull back face
	glFrontFace (GL_CCW); // set counter-clock-wise vertex order to mean the front
	glClearColor (0.2, 0.2, 0.2, 1.0); // grey background to help spot mistakes
	glViewport (0, 0, g_gl_width, g_gl_height);

	/* load the mesh using assimp */
	GLuint monkey_vao;
	int monkey_point_count = 0;
	assert (load_mesh (MESH_FILE, &monkey_vao, &monkey_point_count));
	
/*-------------------------------CREATE SHADERS-------------------------------*/
	GLuint shader_programme = create_programme_from_files (
		VERTEX_SHADER_FILE, FRAGMENT_SHADER_FILE
	);
	
	#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
	// input variables
	float near = 0.1f; // clipping plane
	float far = 100.0f; // clipping plane
	float fov = 67.0f * ONE_DEG_IN_RAD; // convert 67 degrees to radians
	float aspect = (float)g_gl_width / (float)g_gl_height; // aspect ratio
	// matrix components
	float range = tan (fov * 0.5f) * near;
	float Sx = (2.0f * near) / (range * aspect + range * aspect);
	float Sy = near / range;
	float Sz = -(far + near) / (far - near);
	float Pz = -(2.0f * far * near) / (far - near);
	GLfloat proj_mat[] = {
		Sx, 0.0f, 0.0f, 0.0f,
		0.0f, Sy, 0.0f, 0.0f,
		0.0f, 0.0f, Sz, -1.0f,
		0.0f, 0.0f, Pz, 0.0f
	};
	
		
	float cam_speed = 1.0f; // 1 unit per second
	float cam_yaw_speed = 10.0f; // 10 degrees per second
	float cam_pos[] = {0.0f, 0.0f, 5.0f}; // don't start at zero, or we will be too close
	float cam_yaw = 0.0f; // y-rotation in degrees
	mat4 T = translate (identity_mat4 (), vec3 (-cam_pos[0], -cam_pos[1], -cam_pos[2]));
	mat4 R = rotate_y_deg (identity_mat4 (), -cam_yaw);
	mat4 view_mat = R * T;
	
	int view_mat_location = glGetUniformLocation (shader_programme, "view");
	glUseProgram (shader_programme);
	glUniformMatrix4fv (view_mat_location, 1, GL_FALSE, view_mat.m);
	int proj_mat_location = glGetUniformLocation (shader_programme, "proj");
	glUseProgram (shader_programme);
	glUniformMatrix4fv (proj_mat_location, 1, GL_FALSE, proj_mat);
	
	while (!glfwWindowShouldClose (g_window)) {
		static double previous_seconds = glfwGetTime ();
		double current_seconds = glfwGetTime ();
		double elapsed_seconds = current_seconds - previous_seconds;
		previous_seconds = current_seconds;
	
		_update_fps_counter (g_window);
		// wipe the drawing surface clear
		glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glViewport (0, 0, g_gl_width, g_gl_height);
		
		glUseProgram (shader_programme);
		glBindVertexArray (monkey_vao);
		glDrawArrays (GL_TRIANGLES, 0, monkey_point_count);
		// update other events like input handling 
		glfwPollEvents ();
		
		// control keys
		bool cam_moved = false;
		if (glfwGetKey (g_window, GLFW_KEY_A)) {
			cam_pos[0] -= cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_D)) {
			cam_pos[0] += cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_PAGE_UP)) {
			cam_pos[1] += cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_PAGE_DOWN)) {
			cam_pos[1] -= cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_W)) {
			cam_pos[2] -= cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_S)) {
			cam_pos[2] += cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_LEFT)) {
			cam_yaw += cam_yaw_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_RIGHT)) {
			cam_yaw -= cam_yaw_speed * elapsed_seconds;
			cam_moved = true;
		}
		// update view matrix
		if (cam_moved) {
			mat4 T = translate (identity_mat4 (), vec3 (-cam_pos[0], -cam_pos[1], -cam_pos[2])); // cam translation
			mat4 R = rotate_y_deg (identity_mat4 (), -cam_yaw); // 
			mat4 view_mat = R * T;
			glUniformMatrix4fv (view_mat_location, 1, GL_FALSE, view_mat.m);
		}
		
		
		if (GLFW_PRESS == glfwGetKey (g_window, GLFW_KEY_ESCAPE)) {
			glfwSetWindowShouldClose (g_window, 1);
		}
		// put the stuff we've been drawing onto the display
		glfwSwapBuffers (g_window);
	}
	
	// close GL context and any other GLFW resources
	glfwTerminate();
	return 0;
}
Exemplo n.º 13
0
int main () {
	restart_gl_log ();
	start_gl ();
	// tell GL to only draw onto a pixel if the shape is closer to the viewer
	glEnable (GL_DEPTH_TEST); // enable depth-testing
	// depth-testing interprets a smaller value as "closer"

	glDepthFunc (GL_LESS);
	assert (load_mesh ("monkey.obj"));

	GLuint vao;
	glGenVertexArrays (1, &vao);
	glBindVertexArray (vao);

	GLuint points_vbo;
	if (NULL != g_vp) {
		glGenBuffers (1, &points_vbo);
		glBindBuffer (GL_ARRAY_BUFFER, points_vbo);
		glBufferData (GL_ARRAY_BUFFER, 3 * g_point_count * sizeof (GLfloat), g_vp,
									GL_STATIC_DRAW);
		glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
		glEnableVertexAttribArray (0);
		printf ("enabled points\n");
	}
	
	GLuint normals_vbo;
	if (NULL != g_vn) {
		glGenBuffers (1, &normals_vbo);
		glBindBuffer (GL_ARRAY_BUFFER, normals_vbo);
		glBufferData (GL_ARRAY_BUFFER, 3 * g_point_count * sizeof (GLfloat), g_vn,
									GL_STATIC_DRAW);
		glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
		glEnableVertexAttribArray (1);
		printf ("enabled normals\n");
	}
	
	GLuint texcoords_vbo;
	if (NULL != g_vt) {
		glGenBuffers (1, &texcoords_vbo);
		glBindBuffer (GL_ARRAY_BUFFER, texcoords_vbo);
		glBufferData (GL_ARRAY_BUFFER, 2 * g_point_count * sizeof (GLfloat), g_vt,
									GL_STATIC_DRAW);
		glVertexAttribPointer (2, 2, GL_FLOAT, GL_FALSE, 0, NULL);
		glEnableVertexAttribArray (2);
		printf ("enabled texcoords\n");
	}
	
	GLuint shader_programme = create_programme_from_files ("test_vs.glsl",
																												 "test_fs.glsl");
	
	/* if converting to GLSL 410 do this to replace GLSL texture bindings:
	 GLint diffuse_map_loc, specular_map_loc, ambient_map_loc, emission_map_loc;
	 diffuse_map_loc = glGetUniformLocation (shader_programme, "diffuse_map");
	 specular_map_loc = glGetUniformLocation (shader_programme, "specular_map");
	 ambient_map_loc = glGetUniformLocation (shader_programme, "ambient_map");
	 emission_map_loc = glGetUniformLocation (shader_programme, "emission_map");
	 assert (diffuse_map_loc > -1);
	 assert (specular_map_loc > -1);
	 assert (ambient_map_loc > -1);
	 assert (emission_map_loc > -1);
	 glUseProgram (shader_programme);
	 glUniform1i (diffuse_map_loc, 0);
	 glUniform1i (specular_map_loc, 1);
	 glUniform1i (ambient_map_loc, 2);
	 glUniform1i (emission_map_loc, 3);*/
	
	// load texture
	GLuint tex_diff, tex_spec, tex_amb, tex_emiss;
	glActiveTexture (GL_TEXTURE0);
	assert (load_texture ("boulder_diff.png", &tex_diff));
	glActiveTexture (GL_TEXTURE1);
	assert (load_texture ("boulder_spec.png", &tex_spec));
	glActiveTexture (GL_TEXTURE2);
	assert (load_texture ("ao.png", &tex_amb));
	glActiveTexture (GL_TEXTURE3);
	assert (load_texture ("tileable9b_emiss.png", &tex_emiss));
	
	#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
	// input variables
	float near = 0.1f; // clipping plane
	float far = 100.0f; // clipping plane
	float fov = 67.0f * ONE_DEG_IN_RAD; // convert 67 degrees to radians
	float aspect = (float)g_gl_width / (float)g_gl_height; // aspect ratio
	// matrix components
	float range = tan (fov * 0.5f) * near;
	float Sx = (2.0f * near) / (range * aspect + range * aspect);
	float Sy = near / range;
	float Sz = -(far + near) / (far - near);
	float Pz = -(2.0f * far * near) / (far - near);
	GLfloat proj_mat[] = {
		Sx, 0.0f, 0.0f, 0.0f,
		0.0f, Sy, 0.0f, 0.0f,
		0.0f, 0.0f, Sz, -1.0f,
		0.0f, 0.0f, Pz, 0.0f
	};
	
		
	float cam_speed = 1.0f; // 1 unit per second
	float cam_yaw_speed = 90.0f; // 10 degrees per second
	// don't start at zero, or we will be too close
	float cam_pos[] = {0.0f, 0.0f, 5.0f};
	float cam_yaw = 0.0f; // y-rotation in degrees
	mat4 T = translate (identity_mat4 (), vec3 (-cam_pos[0], -cam_pos[1],
																							-cam_pos[2]));
	mat4 R = rotate_y_deg (identity_mat4 (), -cam_yaw);
	mat4 view_mat = R * T;
	
	int view_mat_location = glGetUniformLocation (shader_programme, "view");
	glUseProgram (shader_programme);
	glUniformMatrix4fv (view_mat_location, 1, GL_FALSE, view_mat.m);
	int proj_mat_location = glGetUniformLocation (shader_programme, "proj");
	glUseProgram (shader_programme);
	glUniformMatrix4fv (proj_mat_location, 1, GL_FALSE, proj_mat);
	
	glEnable (GL_CULL_FACE); // cull face
	glCullFace (GL_BACK); // cull back face
	glFrontFace (GL_CCW); // GL_CCW for counter clock-wise
	
	while (!glfwWindowShouldClose (g_window)) {
		static double previous_seconds = glfwGetTime ();
		double current_seconds = glfwGetTime ();
		double elapsed_seconds = current_seconds - previous_seconds;
		previous_seconds = current_seconds;
	
		_update_fps_counter (g_window);
		// wipe the drawing surface clear
		glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glViewport (0, 0, g_gl_width, g_gl_height);
		
		glUseProgram (shader_programme);
		glBindVertexArray (vao);
		// draw points 0-3 from the currently bound VAO with current in-use shader
		glDrawArrays (GL_TRIANGLES, 0, g_point_count);
		// update other events like input handling 
		glfwPollEvents ();
		
		// control keys
		bool cam_moved = false;
		if (glfwGetKey (g_window, GLFW_KEY_A)) {
			cam_pos[0] -= cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_D)) {
			cam_pos[0] += cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_PAGE_UP)) {
			cam_pos[1] += cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_PAGE_DOWN)) {
			cam_pos[1] -= cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_W)) {
			cam_pos[2] -= cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_S)) {
			cam_pos[2] += cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_LEFT)) {
			cam_yaw += cam_yaw_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_RIGHT)) {
			cam_yaw -= cam_yaw_speed * elapsed_seconds;
			cam_moved = true;
		}
		// update view matrix
		if (cam_moved) {
			mat4 T = translate (identity_mat4 (), vec3 (-cam_pos[0], -cam_pos[1],
				-cam_pos[2])); // cam translation
			mat4 R = rotate_y_deg (identity_mat4 (), -cam_yaw); // 
			mat4 view_mat = R * T;
			glUniformMatrix4fv (view_mat_location, 1, GL_FALSE, view_mat.m);
		}
		
		
		if (GLFW_PRESS == glfwGetKey (g_window, GLFW_KEY_ESCAPE)) {
			glfwSetWindowShouldClose (g_window, 1);
		}
		// put the stuff we've been drawing onto the display
		glfwSwapBuffers (g_window);
	}
	
	// close GL context and any other GLFW resources
	glfwTerminate();
	return 0;
}
Exemplo n.º 14
0
bool app_Scene::load_resources() {
	std::vector<std::pair<const char*, const char*>> meshesToLoad = {
		{"room",			"data/meshes/room.obj"},
		{"table", 			"data/meshes/table.obj"},

		{"whisky", 			"data/meshes/whisky.obj"},
		{"whisky-glass",	"data/meshes/whisky-glass.obj"},

		{"ashtray", 		"data/meshes/ashtray.obj"},
		{"armchair", 		"data/meshes/armchair.obj"},
		{"projector", 		"data/meshes/projector.obj"},

		{"buddha",			"data/meshes/buddha.obj"}
	};

	std::vector<std::pair<const char*, const char*>> texturesToLoad = {
		{"room-diffuse", 		"data/textures/room-diffuse.png"}, 
		{"room-ao", 			"data/textures/room-ao.png"}, 
		{"room-specular", 		"data/textures/room-specular.png"}, 

		{"table-diffuse", 		"data/textures/table-diffuse.png"}, 
		{"table-ao", 			"data/textures/table-ao.png"}, 
		{"table-specular", 		"data/textures/table-specular.png"}, 

		{"armchair-diffuse", 	"data/textures/armchair-diffuse.png"}, 
		{"armchair-ao", 		"data/textures/armchair-ao.png"}, 
		{"armchair-specular", 	"data/textures/armchair-specular.png"}, 

		{"projector-diffuse", 	"data/textures/projector-diffuse.png"}, 
		{"projector-ao", 		"data/textures/projector-ao.png"}, 
		{"projector-specular", 	"data/textures/projector-specular.png"},

		{"white", 				"data/textures/white.png"},
		{"buddha-ao", 			"data/textures/buddha-ao.png"},

		{"strawberry",			"data/textures/strawberry.png"}
	};

	std::vector<std::tuple<const char*, const char*, const char*>> shadersToLoad = {
		std::make_tuple("textured", "data/shaders/textured.vs", "data/shaders/textured.fs"), 
		std::make_tuple("opaque",	"data/shaders/opaque.vs", 	"data/shaders/opaque.fs"), 
		std::make_tuple("project", "data/shaders/projtex.vs", "data/shaders/projtex.fs"),  
		std::make_tuple("depth", "data/shaders/depth.vs", "data/shaders/depth.fs")
	};

	std::cout << "Loading shaders... \n";
	for(std::tuple<const char*, const char*, const char*> t : shadersToLoad) {
		std::cout << "\t" << std::get<0>(t) << " <- " << std::get<1>(t) << ", " << std::get<2>(t) << "... ";

		if(!load_shader(std::get<0>(t),std::get<1>(t),std::get<2>(t))) {
			std::cout << "fail!\n";
			return false;
		}

		std::cout << "success\n";
	}
	std::cout << std::endl;

	std::cout << "Loading meshes... \n";
	for(std::pair<const char*, const char*> p : meshesToLoad) {
		std::cout << "\t" << p.first << " <- " << p.second << "... ";

		if(!load_mesh(p.first, p.second)) {
			std::cout << "fail!\n";
			return false;
		}

		std::cout << "success\n";
	}
	std::cout << std::endl;

	std::cout << "Loading textures... \n";
	for(std::pair<const char*, const char*> p : texturesToLoad) {
		std::cout << "\t" << p.first << " <- " << p.second << "... ";

		if(!load_texture(p.first, p.second)) {
			std::cout << "fail!\n";
			return false;
		}

		std::cout << "success\n";
	}
	m_Textures["strawberry"]->parameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
	m_Textures["strawberry"]->parameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	std::cout << std::endl;

	std::cout << "Creating FBO... ";
	{
		std::cout << "\tCreating texture... ";
			GLuint textureHandle;
			glGenTextures(1, &textureHandle);
			glBindTexture(GL_TEXTURE_2D, textureHandle);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);

		    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
		    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

		    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
		    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS);

			//glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, m_DepthMapSize.x, m_DepthMapSize.y, 0, GL_RED, GL_FLOAT, NULL);
			glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT24, m_DepthMapSize.x, m_DepthMapSize.y);
		std::cout << "done\n";
		
		m_ProjectorDepthMap.attach_texture(GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textureHandle, 0, true);
		m_ProjectorDepthMap.unbind();

		m_DepthTexture = textureHandle;
	}
	std::cout << "Done";

	return true;
}
Exemplo n.º 15
0
void parse_world(state_t* state, FILE* f) {
	char line[4096];

	char** tokens;
	int n_tokens;

	int bsdf_index = 0;

	transform_data_t t;
	t.has_users = false;
	t.index = 0;
	matrix_t m_identity = {{1.f,0.f,0.f,0.f},
			       {0.f,1.f,0.f,0.f},
			       {0.f,0.f,1.f,0.f},
			       {0.f,0.f,0.f,1.f}};
	memcpy(t.t.m, m_identity, sizeof(matrix_t));
	memcpy(t.t.m_inv, m_identity, sizeof(matrix_t));

	state->transforms[t.index++] = t.t;
	state->n_transforms++;
	realloc_transforms();

	transform_data_t tstack[1024];
	int tstack_ptr = 0;
	
	while(fgets(line, 4096, f) != NULL) {
		if(line[0] != '#') {
			line[strlen(line)-1] = '\0';

			tokens = tokenize(line, &n_tokens);
			if(n_tokens != 0) {
				if(strcmp(tokens[0],"EndWorld") == 0) {
					free(tokens);
					return;
				}
				else if(strcmp(tokens[0],"PushTransform") == 0) {
					qr_assert(n_tokens==1, "parser", "PushTransform takes only no arguments, found %d",n_tokens-1);
					tstack[++tstack_ptr] = t;
				}
				else if(strcmp(tokens[0],"PopTransform") == 0) {
					qr_assert(n_tokens==1, "parser", "PopTransform takes no arguments, found %d",n_tokens-1);
					transform_data_t tmp = t;
					t = tstack[tstack_ptr--];
					t.has_users = t.has_users || tmp.has_users;
					t.index = state->n_transforms;
				}
				else if(strcmp(tokens[0],"LoadIdentity") == 0) {
					qr_assert(n_tokens==1, "parser", "LoadIdentity takes no arguments, found %d",n_tokens-1);
					if(t.has_users == true) {
						t.index = state->n_transforms;
						t.has_users = false;
					}
				}
				else if(strcmp(tokens[0],"Translate") == 0) {
					qr_assert(n_tokens==4, "parser", "Translate takes only 3 arguments, found %d",n_tokens-1);
					if(t.has_users == true) {
						t.index = state->n_transforms;
						t.has_users = false;
					}
					transform_t tmp;
					translate(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), &tmp);
					mul_matrix(&tmp.m, &t.t.m, &t.t.m);
					mul_matrix(&tmp.m_inv, &t.t.m_inv, &t.t.m_inv);
				}
				else if(strcmp(tokens[0],"Scale") == 0) {
					qr_assert(n_tokens==4, "parser", "Scale takes only 3 arguments, found %d",n_tokens-1);
					if(t.has_users == true) {
						t.index = state->n_transforms;
						t.has_users = false;
					}
					transform_t tmp;
					scale(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), &tmp);
					mul_matrix(&tmp.m, &t.t.m, &t.t.m);
				}
				else if(strcmp(tokens[0],"LookAt") == 0) {
					qr_assert(n_tokens==7, "parser", "LookAt takes only 6 arguments, found %d",n_tokens-1);
					if(t.has_users == true) {
						t.index = state->n_transforms;
						t.has_users = false;
					}
					lookat(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), make_vec3(to_float(tokens[4]),to_float(tokens[5]),to_float(tokens[6])), &t.t);
				}
				else if(strcmp(tokens[0],"BSDFDiffuse") == 0) {
					qr_assert(n_tokens==4, "parser", "BSDFDiffuse takes only 3 arguments, found %d",n_tokens-1);
					state->n_bsdfs++;
					realloc_bsdfs();
					state->bsdfs[state->n_bsdfs-1] = make_bsdf_diffuse(make_vec3(to_float(tokens[1]), to_float(tokens[2]), to_float(tokens[3])));
					bsdf_index = state->n_bsdfs-1;
				}
				else if(strcmp(tokens[0],"Sphere") == 0) {
					qr_assert(n_tokens==2, "parser", "Sphere takes only one argument, found %d",n_tokens-1);
					state->primitives = (primitive_t*)realloc(state->primitives, sizeof(primitive_t)*(++state->n_primitives));
					primitive_t p;
					p.type = PRIMITIVE_SPHERE;
					p.data = make_primitive_sphere(to_float(tokens[1]));
					p.t = t.index;
					p.bsdf = bsdf_index;
					t.has_users = true;
					state->primitives[state->n_primitives-1] = p;

					state->transforms[t.index] = t.t;
					state->n_transforms++;
					realloc_transforms();
				}
				else if(strcmp(tokens[0],"Mesh") == 0) {
					qr_assert(n_tokens==2, "parser", "Mesh takes only one argument, found %d",n_tokens-1);
					state->transforms[t.index] = t.t;
					t.has_users = true;
					state->n_transforms++;
					realloc_transforms();
					load_mesh(tokens[1], state, t, bsdf_index, NULL);
				}
				else if(strcmp(tokens[0],"MeshMat") == 0) {
					qr_assert(n_tokens==3, "parser", "MeshMat takes only 2 arguments, found %d",n_tokens-1);
					t.has_users = true;
					state->transforms[t.index] = t.t;
					state->n_transforms++;
					realloc_transforms();
					load_mesh(tokens[1], state, t, bsdf_index, tokens[2]);
				}
				else if(strcmp(tokens[0],"PointLight") == 0) {
					qr_assert(n_tokens==7, "parser", "PointLight takes only 6 arguments, found %d",n_tokens-1);
					realloc_lights();
					state->lights[state->n_lights++] = make_light_point(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), make_vec3(to_float(tokens[4]),to_float(tokens[5]),to_float(tokens[6])));
				}
				else if(strcmp(tokens[0],"SphereLight") == 0) {
					qr_assert(n_tokens==8, "parser", "SphereLight takes only 7 arguments, found %d",n_tokens-1);
					realloc_lights();
					state->lights[state->n_lights++] = make_light_sphere(make_vec3(to_float(tokens[1]),to_float(tokens[2]),to_float(tokens[3])), to_float(tokens[4]), make_vec3(to_float(tokens[5]),to_float(tokens[6]),to_float(tokens[7])));
				}
				else if(strcmp(tokens[0],"Camera") == 0) {
					qr_assert(n_tokens==2, "parser", "Camera takes only one argument, found %d",n_tokens-1);
					state->camera_fplane = to_float(tokens[1]);
					state->camera_transform = t.index;
					t.has_users = true;
					state->camera_origin = transform_point(make_vec3(0.f,0.f,0.f), t.t, true);

					state->transforms[t.index] = t.t;
					state->n_transforms++;
					realloc_transforms();
				}
				else {
					ERROR("parser", "Unknown directive: %s",tokens[0]);
				}
			}
		}
	}
	ERROR("parser", "File ended in the middle of a World section");
}
Exemplo n.º 16
0
TrulyViewDependentPM32::TrulyViewDependentPM32(const std::string& filename)
    : TrulyViewDependentPM()
{
    load_mesh(filename);
}
Exemplo n.º 17
0
int main () {
	assert (restart_gl_log ());
	assert (start_gl ());
	glEnable (GL_DEPTH_TEST); // enable depth-testing
	glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"
	glEnable (GL_CULL_FACE); // cull face
	glCullFace (GL_BACK); // cull back face
	glFrontFace (GL_CCW); // set counter-clock-wise vertex order to mean the front
	glClearColor (0.2, 0.2, 0.2, 1.0); // grey background to help spot mistakes
	glViewport (0, 0, g_gl_width, g_gl_height);

	/* load the mesh using assimp */
	GLuint monkey_vao;
	mat4 monkey_bone_offset_matrices[MAX_BONES];
	mat4 monkey_bone_animation_mats[MAX_BONES];
	for (int i = 0; i < MAX_BONES; i++) {
		monkey_bone_animation_mats[i] = identity_mat4 ();
		monkey_bone_offset_matrices[i] = identity_mat4 ();
		monkey_bone_animation_mats[i] = identity_mat4 ();
	}
	int monkey_point_count = 0;
	int monkey_bone_count = 0;
	Skeleton_Node* monkey_root_node = NULL;
	double monkey_anim_duration = 0.0;
	assert (load_mesh (
		MESH_FILE,
		&monkey_vao,
		&monkey_point_count,
		monkey_bone_offset_matrices,
		&monkey_bone_count,
		&monkey_root_node,
		&monkey_anim_duration
	));
	printf ("monkey bone count %i\n", monkey_bone_count);
	
	/* create a buffer of bone positions for visualising the bones */
	float bone_positions[3 * 256];
	int c = 0;
	for (int i = 0; i < monkey_bone_count; i++) {
		//print (monkey_bone_offset_matrices[i]);
	
		// get the x y z translation elements from the last column in the array
		bone_positions[c++] = -monkey_bone_offset_matrices[i].m[12];
		bone_positions[c++] = -monkey_bone_offset_matrices[i].m[13];
		bone_positions[c++] = -monkey_bone_offset_matrices[i].m[14];
	}
	GLuint bones_vao;
	glGenVertexArrays (1, &bones_vao);
	glBindVertexArray (bones_vao);
	GLuint bones_vbo;
	glGenBuffers (1, &bones_vbo);
	glBindBuffer (GL_ARRAY_BUFFER, bones_vbo);
	glBufferData (
		GL_ARRAY_BUFFER,
		3 * monkey_bone_count * sizeof (float),
		bone_positions,
		GL_STATIC_DRAW
	);
	glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
	glEnableVertexAttribArray (0);
	
	
/*-------------------------------CREATE SHADERS-------------------------------*/
	GLuint shader_programme = create_programme_from_files (
		VERTEX_SHADER_FILE, FRAGMENT_SHADER_FILE
	);
	GLuint bones_shader_programme = create_programme_from_files (
		"bones.vert", "bones.frag"
	);
	
	#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
	// input variables
	float near = 0.1f; // clipping plane
	float far = 100.0f; // clipping plane
	float fov = 67.0f * ONE_DEG_IN_RAD; // convert 67 degrees to radians
	float aspect = (float)g_gl_width / (float)g_gl_height; // aspect ratio
	// matrix components
	float range = tan (fov * 0.5f) * near;
	float Sx = (2.0f * near) / (range * aspect + range * aspect);
	float Sy = near / range;
	float Sz = -(far + near) / (far - near);
	float Pz = -(2.0f * far * near) / (far - near);
	GLfloat proj_mat[] = {
		Sx, 0.0f, 0.0f, 0.0f,
		0.0f, Sy, 0.0f, 0.0f,
		0.0f, 0.0f, Sz, -1.0f,
		0.0f, 0.0f, Pz, 0.0f
	};
		
	float cam_speed = 5.0f; // 1 unit per second
	float cam_yaw_speed = 40.0f; // 10 degrees per second
	float cam_pos[] = {0.0f, 0.0f, 5.0f}; // don't start at zero, or we will be too close
	float cam_yaw = 0.0f; // y-rotation in degrees
	mat4 T = translate (identity_mat4 (), vec3 (-cam_pos[0], -cam_pos[1], -cam_pos[2]));
	mat4 R = rotate_y_deg (identity_mat4 (), -cam_yaw);
	mat4 view_mat = R * T;
	
	/* apply a model matrix that rotates our mesh up the correct way */
	mat4 model_mat = identity_mat4 ();
	
	glUseProgram (shader_programme);
	int model_mat_location = glGetUniformLocation (shader_programme, "model");
	glUniformMatrix4fv (model_mat_location, 1, GL_FALSE, model_mat.m);
	int view_mat_location = glGetUniformLocation (shader_programme, "view");
	glUniformMatrix4fv (view_mat_location, 1, GL_FALSE, view_mat.m);
	int proj_mat_location = glGetUniformLocation (shader_programme, "proj");
	glUniformMatrix4fv (proj_mat_location, 1, GL_FALSE, proj_mat);
	int bone_matrices_locations[MAX_BONES];
	// reset all the bone matrices
	char name[64];
	for (int i = 0; i < MAX_BONES; i++) {
		sprintf (name, "bone_matrices[%i]", i);
		bone_matrices_locations[i] = glGetUniformLocation (shader_programme, name);
		glUniformMatrix4fv (bone_matrices_locations[i], 1, GL_FALSE, identity_mat4 ().m);
	}
	
	glUseProgram (bones_shader_programme);
	int bones_view_mat_location = glGetUniformLocation (bones_shader_programme, "view");
	glUniformMatrix4fv (bones_view_mat_location, 1, GL_FALSE, view_mat.m);
	int bones_proj_mat_location = glGetUniformLocation (bones_shader_programme, "proj");
	glUniformMatrix4fv (bones_proj_mat_location, 1, GL_FALSE, proj_mat);
	
	double anim_time = 0.0;
	
	while (!glfwWindowShouldClose (g_window)) {
		static double previous_seconds = glfwGetTime ();
		double current_seconds = glfwGetTime ();
		double elapsed_seconds = current_seconds - previous_seconds;
		previous_seconds = current_seconds;
		
		/* update animation timer and loop */
		anim_time += elapsed_seconds * 0.5;
		if (anim_time >= monkey_anim_duration) {
			anim_time = monkey_anim_duration - anim_time;
		}
	
		_update_fps_counter (g_window);
		// wipe the drawing surface clear
		glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glViewport (0, 0, g_gl_width, g_gl_height);
		
		glEnable (GL_DEPTH_TEST);
		glUseProgram (shader_programme);
		glBindVertexArray (monkey_vao);
		glDrawArrays (GL_TRIANGLES, 0, monkey_point_count);
		
		glDisable (GL_DEPTH_TEST);
		glEnable (GL_PROGRAM_POINT_SIZE);
		glUseProgram (bones_shader_programme);
		glBindVertexArray (bones_vao);
		glDrawArrays (GL_POINTS, 0, monkey_bone_count);
		glDisable (GL_PROGRAM_POINT_SIZE);
		
		// update other events like input handling 
		glfwPollEvents ();
		
		// control keys
		bool cam_moved = false;
		if (glfwGetKey (g_window, GLFW_KEY_A)) {
			cam_pos[0] -= cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_D)) {
			cam_pos[0] += cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_PAGE_UP)) {
			cam_pos[1] += cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_PAGE_DOWN)) {
			cam_pos[1] -= cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_W)) {
			cam_pos[2] -= cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_S)) {
			cam_pos[2] += cam_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_LEFT)) {
			cam_yaw += cam_yaw_speed * elapsed_seconds;
			cam_moved = true;
		}
		if (glfwGetKey (g_window, GLFW_KEY_RIGHT)) {
			cam_yaw -= cam_yaw_speed * elapsed_seconds;
			cam_moved = true;
		}
		// update view matrix
		if (cam_moved) {
			mat4 T = translate (identity_mat4 (), vec3 (-cam_pos[0], -cam_pos[1], -cam_pos[2])); // cam translation
			mat4 R = rotate_y_deg (identity_mat4 (), -cam_yaw); // 
			mat4 view_mat = R * T;
			glUseProgram (shader_programme);
			glUniformMatrix4fv (view_mat_location, 1, GL_FALSE, view_mat.m);
			glUseProgram (bones_shader_programme);
			glUniformMatrix4fv (bones_view_mat_location, 1, GL_FALSE, view_mat.m);
		}
		skeleton_animate (
			monkey_root_node,
			anim_time,
			identity_mat4 (),
			monkey_bone_offset_matrices,
			monkey_bone_animation_mats
		);
		glUseProgram (shader_programme);
		glUniformMatrix4fv (
			bone_matrices_locations[0],
			monkey_bone_count,
			GL_FALSE,
			monkey_bone_animation_mats[0].m
		);
		
		if (GLFW_PRESS == glfwGetKey (g_window, GLFW_KEY_ESCAPE)) {
			glfwSetWindowShouldClose (g_window, 1);
		}
		// put the stuff we've been drawing onto the display
		glfwSwapBuffers (g_window);
	}
	
	// close GL context and any other GLFW resources
	glfwTerminate();
	return 0;
}