Ejemplo n.º 1
0
void				load_obj(t_object *obj, char *filename)
{
	int				fd;
	char			*line;

	ft_exit((fd = open(filename, O_RDONLY)) < 0, "Can't read file");
	obj->vertex_buffer_size = obj->index_buffer_size = obj->normal_buffer_size
	= obj->tex_buffer_size = 0;
	while (get_next_line(fd, &line) > 0)
	{
		if (ft_strlen(line) < 3 || line[0] == '#')
			;
		else if (line[0] == 'o')
			ft_strcpy((char *)obj->name, line + 2);
		else if (line[0] == 'v' && line[1] == ' ')
			load_vertex(obj, line + 1);
		else if (line[0] == 'v' && line[1] == 'n')
			load_normal(obj, line + 2);
		else if (line[0] == 'v' && line[1] == 't')
			load_tex(obj, line + 2);
		else if (line[0] == 'f' && line[1] == ' ')
			load_index(obj, line + 1);
		ft_memdel((void **)&line);
	}
	close(fd);
	print_obj(obj);
}
Ejemplo n.º 2
0
GLFWwindow			*setup_program(t_properties *properties)
{
	static const float	verts[] = {-1, 1, -1, -1, 1, -1, 1, 1};
	static const GLuint	inds[] = {0, 1, 3, 1, 2, 3};
	GLFWwindow			*window;

	properties->width = 1000;
	properties->height = 1000;
	window = make_glfw(properties->width, properties->height);
	glClearColor(0, 0, 0, 1);
	properties->model = vao();
	properties->verts = data_buffer((GLvoid *)verts, sizeof(verts));
	properties->indices = index_buffer((GLvoid *)inds, sizeof(inds));
	vao_add_indices(properties->model, properties->indices);
	vao_add_vdata(properties->model, properties->verts, 2, GL_FALSE);
	ft_putnbr(glGetError());
	ft_putendl(" -0");
	properties->shaders[0] = load_vertex();
	properties->shaders[1] = load_fragment(properties->map);
	properties->program = shader_program(properties->shaders, 2);
	ft_putnbr(glGetError());
	ft_putendl(" -1");
	glUseProgram(properties->program);
	glBindVertexArray(properties->model);
	glEnableVertexAttribArray(0);
	init_uniforms(properties->program);
	return (window);
}
Ejemplo n.º 3
0
/** 
 * import umdraw mesh list
 */
bool UMSoftwareIO::import_mesh_list(UMMeshList& dst, const umio::UMObjectPtr src, const umstring& absolute_file_path)
{
	if (!src) return false;

	bool result = false;
	umio::UMMesh::IDToMeshMap::iterator it = src->mutable_mesh_map().begin();
	for (; it != src->mutable_mesh_map().end(); ++it)
	{
		umio::UMMesh& ummesh = it->second;

		UMMeshPtr mesh(std::make_shared<UMMesh>());
		dst.push_back(mesh);
		result = true;
		
		sort_by_material(ummesh);
		load_material(absolute_file_path, mesh, ummesh);
		load_vertex_index(mesh, ummesh);
		load_vertex(mesh, ummesh);
		load_normal(mesh, ummesh);
		load_uv(mesh, ummesh);
		load_skin(mesh, ummesh);
		mesh->update_box();
	}
	return result;
}
Ejemplo n.º 4
0
int main(int argc, char *argv[])
{

	if (argc > 1){
		if  (strcmp(argv[1], "-debug") == 0 )
			mode = MODE_DEBUG;
		else if ( strcmp(argv[1], "-oculus") == 0 )
			mode = MODE_OCULUS;
		else if ( strcmp(argv[1], "-oculus-debug") == 0 )
			mode = MODE_OCULUS_DEBUG;
		else return 100;
	}else
		mode = MODE_DEBUG;

	int err;

	// Init OVR library, hardware and sensors.
	err = init_ovr();
	if ( err != 0 )
		exit( 10 + err );

	//Init windows and OpenGL context
	err = init_SDL_GL();
	if ( err != 0 )
		exit( 0 + err );
	
	// Load and Init shader and shaders Program
	err = load_init_shaders();
	if ( err != 0 )
		exit( 20 + err );

	// Load the Vertices, vertex arrays, etc... And bind them, along with the shaders.
	err = load_vertex();
	if ( err != 0 )
		exit( 30 + err );

	// Loads the texture from files and bien them as uniform in the frag shader
	err = load_textures();
	if ( err != 0 )
		exit( 40 + err );
	
	if (mode != MODE_DEBUG){
		// Inits the frame buffer, usefull for rendering the scene in a texture to send it to Oculus
		err = init_framebuffers();
		if ( err != 0 )
			exit( 50 + err );

		err = init_render_ovr();
		if ( err != 0 )
			exit( 60 + err );
	}

	std::cout << "Recommended w " << recommendedTex0Size.w << std::endl << "Recommended h " << recommendedTex0Size.h << std::endl;

	// Tansformations
	//---------------------------------------------
	// ---- Transfo
	glm::mat4 trans;
	GLuint uniTrans = glGetUniformLocation(shaderProgram, "trans");

	// ---- View
	glm::mat4 view;

	// ---- Projection
	glm::mat4 proj;
	
	
	


	// Render in Texture, and display
	//-------------------------------------------------
	if (mode == MODE_OCULUS_DEBUG ){

		load_init_passthrough_shaders();
		GLuint passthroughOB;
		glGenBuffers(1, &passthroughOB);
		glBindBuffer(GL_ARRAY_BUFFER, passthroughOB);
		glBufferData(GL_ARRAY_BUFFER, sizeof(passthroughScreen), passthroughScreen, GL_STATIC_DRAW);
		
		// Binding the fragment Shader output to the current buffer
		glBindFragDataLocation(passthroughShadersProgram, 0, "passthroughColor");
		errorCode = glGetError();

		// Link and Use Program
		glLinkProgram(passthroughShadersProgram);
		glUseProgram(passthroughShadersProgram);
		
		// Store the attributes for the shaders
		
		glGenVertexArrays(1, &passthroughVAO);
		glBindVertexArray(passthroughVAO);

		// Attributes Locations for Shaders and Enable
		GLint posAttrib = glGetAttribLocation(passthroughShadersProgram, "position");
		glVertexAttribPointer(posAttrib, 0, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void*) 2 );
		glEnableVertexAttribArray(posAttrib);

		GLint colorAttrib = glGetAttribLocation(passthroughShadersProgram, "texCoords");
		glVertexAttribPointer(colorAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void*)(sizeof(float) * 2) );
		glEnableVertexAttribArray(colorAttrib);

		glUseProgram(passthroughShadersProgram);
		glUniform1i(glGetUniformLocation(passthroughShadersProgram, "renderedTex"), 0);
	}

	
	
	



	// Event Loop
	//--------------------------------------------------
	SDL_Event windowEvent;
	while (true)
	{
		if (SDL_PollEvent(&windowEvent))
		{

			// Quit events
			if (windowEvent.type == SDL_QUIT) break;
			else if (windowEvent.type == SDL_KEYUP && windowEvent.key.keysym.sym == SDLK_ESCAPE) break;

		}



		// Enabling ressources to draw the cube
		// Before entering the rendering loop
		glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, textures[0]);
		glActiveTexture(GL_TEXTURE1);
		glBindTexture(GL_TEXTURE_2D, textures[1]);


		
		// ---- View
		view = glm::lookAt(
			glm::vec3(5.0f, 5.0f, 5.0f),
			glm::vec3(0.0f, 0.0f, 0.0f),
			glm::vec3(0.0f, 0.0f, 1.0f)
		);

		GLint uniView = glGetUniformLocation(shaderProgram, "view");
		glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
		
		// ---- Projection
		if ( mode == MODE_DEBUG ){
			proj = glm::perspective(45.0f, 1280.0f / 720.0f, 1.0f, 10.0f);
		}else{
			proj = glm::perspective(45.0f, 640.0f / 720.0f, 1.0f, 10.0f);
		}
		GLint uniProj = glGetUniformLocation(shaderProgram, "proj");
		glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
		
		//Turn around Z
		trans = glm::rotate(
			trans,
			0.7f,
			glm::vec3(0.0f, 0.0f, 1.0f)
		);
		glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(trans));

		
		if ( mode == MODE_OCULUS || mode == MODE_OCULUS_DEBUG ){

			hdmFrameTiming = ovrHmd_BeginFrame(hmd, 0);
			

			

			glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
			
			for (int eyeIndex = 0; eyeIndex < ovrEye_Count; eyeIndex++){

				
	
				ovrEyeType eye = hmd->EyeRenderOrder[eyeIndex];
				headPose[eye] = ovrHmd_GetEyePose(hmd, eye);


				if (eye == ovrEye_Right){
					glScissor(renderTargetSize.w / 2, 0, renderTargetSize.w / 2, renderTargetSize.h);
					glViewport(renderTargetSize.w / 2, 0, renderTargetSize.w / 2, renderTargetSize.h);
				}else{
					glScissor(0, 0, renderTargetSize.w / 2, renderTargetSize.h);
					glViewport(0, 0, renderTargetSize.w / 2, renderTargetSize.h);
				}
				
				if (eye == ovrEye_Right)
					glClearColor(0.0f, 0.3f, 0.0f, 1.0f);
				else
					glClearColor(0.3f, 0.0f, 0.0f, 1.0f);

				glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
				 
				// Drawing
				glDrawArrays(GL_TRIANGLES, 0, 36);

			}

			if (mode == MODE_OCULUS ){
				
				glScissor(0, 0, renderTargetSize.w, renderTargetSize.h);
				glViewport(0, 0, renderTargetSize.w, renderTargetSize.h);

				ovrHmd_EndFrame(hmd, headPose, eyeTex);
				Sleep(1);
			}else if ( mode == MODE_OCULUS_DEBUG ){

				glBindBuffer(GL_FRAMEBUFFER, 0);
				glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
				glBindBuffer(GL_ARRAY_BUFFER, 0);
				glUseProgram(0);

				glBindFramebuffer(GL_FRAMEBUFFER, 0);
				glBindVertexArray(passthroughVAO);
				glDisable(GL_DEPTH_TEST);
				glUseProgram(passthroughShadersProgram);

				glActiveTexture(GL_TEXTURE0);

				glDrawArrays(GL_TRIANGLES, 0, 6);

			}
		
		}else if (mode == MODE_DEBUG){


			// Clear the screen and the depth buffer (as it is filled with 0 initially, 
			// nothing will be draw (0 = on top);
			glClearColor(0.0f, 0.3f, 0.0f, 1.0f);
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


			// Drawing
			glDrawArrays(GL_TRIANGLES, 0, 36);

		}

		
		if ( mode != MODE_OCULUS )
			SDL_GL_SwapWindow(window);


	}





	// Destoy the HMD and shutdown the library
	ovrHmd_Destroy(hmd);
	ovr_Shutdown();

	// Quite SDL and OpenGL
	glDeleteFramebuffers(1, &frameBuffer);
	SDL_GL_DeleteContext(context);
	SDL_Quit();

	return 0;
}