feat: load obj
This commit is contained in:
		
							
								
								
									
										135
									
								
								src/engine.cpp
									
									
									
									
									
								
							
							
						
						
									
										135
									
								
								src/engine.cpp
									
									
									
									
									
								
							| @@ -111,7 +111,7 @@ void Engine::run() | ||||
| 		const double now	= glfwGetTime(); | ||||
| 		const double delta_time = now - last_update_time_; | ||||
|  | ||||
| 		process_input(); | ||||
| 		process_input(delta_time); | ||||
| 		update(delta_time); | ||||
|  | ||||
| 		if (now - last_frame_time_ >= fps_limit) { | ||||
| @@ -156,78 +156,27 @@ void Engine::setup() | ||||
| 	world_->add_entity(camera_); | ||||
| 	Logger::info("Camera created and added to world"); | ||||
|  | ||||
| 	// Load textures | ||||
| 	auto top_texture   = Texture::load("data/textures/top.png"); | ||||
| 	auto front_texture = Texture::load("data/textures/front.png"); | ||||
| 	// Load the box_stack model | ||||
| 	auto box_stack_mesh = Mesh::load("data/models/box_stack.obj"); | ||||
| 	if (box_stack_mesh) { | ||||
| 		auto box_stack_model = std::make_shared<Model>(box_stack_mesh); | ||||
| 		box_stack_model->set_position(glm::vec3(-2.0f, 0.0f, 0.0f)); | ||||
| 		models_.push_back(box_stack_model); | ||||
| 		world_->add_entity(box_stack_model); | ||||
| 		Logger::info("Box stack model loaded and added to world"); | ||||
| 	} | ||||
|  | ||||
| 	// Create cube mesh with two buffers | ||||
| 	mesh_ = std::make_shared<Mesh>(); | ||||
|  | ||||
| 	// Buffer 1: Top and bottom faces | ||||
| 	std::vector<Vertex> top_bottom_vertices = { | ||||
| 	    // Top face (y = 0.5) | ||||
| 	    {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, | ||||
| 	    {{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}}, | ||||
| 	    {{0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}}, | ||||
| 	    {{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}, | ||||
| 	    // Bottom face (y = -0.5) | ||||
| 	    {{-0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, | ||||
| 	    {{0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}}, | ||||
| 	    {{0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}}, | ||||
| 	    {{-0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}}; | ||||
| 	std::vector<uint16_t> top_bottom_indices = {// Top face | ||||
| 						    0, 1, 2, 0, 2, 3, | ||||
| 						    // Bottom face | ||||
| 						    4, 6, 5, 4, 7, 6}; | ||||
|  | ||||
| 	auto top_bottom_buffer = | ||||
| 	    std::make_shared<Buffer>(top_bottom_vertices, top_bottom_indices); | ||||
| 	Material top_material(top_texture); | ||||
| 	mesh_->add_buffer(top_bottom_buffer, top_material); | ||||
|  | ||||
| 	// Buffer 2: Front, back, left, right faces | ||||
| 	std::vector<Vertex> side_vertices = { | ||||
| 	    // Front face (z = 0.5) | ||||
| 	    {{-0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, | ||||
| 	    {{0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}}, | ||||
| 	    {{0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}}, | ||||
| 	    {{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}, | ||||
| 	    // Back face (z = -0.5) | ||||
| 	    {{-0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}}, | ||||
| 	    {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}}, | ||||
| 	    {{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}, | ||||
| 	    {{0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, | ||||
| 	    // Left face (x = -0.5) | ||||
| 	    {{-0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}, | ||||
| 	    {{-0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}}, | ||||
| 	    {{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}}, | ||||
| 	    {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}, | ||||
| 	    // Right face (x = 0.5) | ||||
| 	    {{0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}}, | ||||
| 	    {{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}}, | ||||
| 	    {{0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}, | ||||
| 	    {{0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}}; | ||||
| 	std::vector<uint16_t> side_indices = {// Front face | ||||
| 					      0, 1, 2, 0, 2, 3, | ||||
| 					      // Back face | ||||
| 					      4, 5, 6, 4, 6, 7, | ||||
| 					      // Left face | ||||
| 					      8, 9, 10, 8, 10, 11, | ||||
| 					      // Right face | ||||
| 					      12, 13, 14, 12, 14, 15}; | ||||
|  | ||||
| 	auto side_buffer = | ||||
| 	    std::make_shared<Buffer>(side_vertices, side_indices); | ||||
| 	Material side_material(front_texture); | ||||
| 	mesh_->add_buffer(side_buffer, side_material); | ||||
|  | ||||
| 	Logger::info("Cube mesh created with two buffers"); | ||||
|  | ||||
| 	// Create model at origin | ||||
| 	auto model = std::make_shared<Model>(mesh_); | ||||
| 	model->set_position(glm::vec3(0.0f, 0.0f, 0.0f)); | ||||
| 	models_.push_back(model); | ||||
| 	world_->add_entity(model); | ||||
| 	// Load the gunslinger model | ||||
| 	auto gunslinger_mesh = Mesh::load("data/models/gunslinger.obj"); | ||||
| 	if (gunslinger_mesh) { | ||||
| 		auto gunslinger_model = | ||||
| 		    std::make_shared<Model>(gunslinger_mesh); | ||||
| 		gunslinger_model->set_position(glm::vec3(2.0f, 0.0f, 0.0f)); | ||||
| 		gunslinger_model->set_scale(glm::vec3(0.5f, 0.5f, 0.5f)); | ||||
| 		models_.push_back(gunslinger_model); | ||||
| 		world_->add_entity(gunslinger_model); | ||||
| 		Logger::info("Gunslinger model loaded and added to world"); | ||||
| 	} | ||||
|  | ||||
| 	Logger::info("Scene setup complete"); | ||||
| } | ||||
| @@ -238,22 +187,44 @@ void Engine::start() | ||||
| 	// Can be used for initialization that needs the scene to be ready | ||||
| } | ||||
|  | ||||
| void Engine::process_input() | ||||
| void Engine::process_input(const double delta_time) | ||||
| { | ||||
| 	glfwPollEvents(); | ||||
|  | ||||
| 	if (!camera_) { | ||||
| 		return; | ||||
| 	} | ||||
|  | ||||
| 	constexpr float camera_speed = 7.5f; | ||||
|  | ||||
| 	glm::vec3 forward = glm::vec3(0.0f, 0.0f, -1.0f); | ||||
| 	glm::vec3 right	  = glm::vec3(1.0f, 0.0f, 0.0f); | ||||
|  | ||||
| 	glm::mat4 rotation = glm::mat4(1.0f); | ||||
| 	rotation	   = glm::rotate(rotation, camera_->rotation().y, | ||||
| 					 glm::vec3(0.0f, 1.0f, 0.0f)); | ||||
| 	rotation	   = glm::rotate(rotation, camera_->rotation().x, | ||||
| 					 glm::vec3(1.0f, 0.0f, 0.0f)); | ||||
| 	forward		   = glm::vec3(rotation * glm::vec4(forward, 0.0f)); | ||||
| 	right		   = glm::vec3(rotation * glm::vec4(right, 0.0f)); | ||||
|  | ||||
| 	const float movement = camera_speed * static_cast<float>(delta_time); | ||||
| 	if (glfwGetKey(window_, GLFW_KEY_UP) == GLFW_PRESS) { | ||||
| 		camera_->set_position(camera_->position() + forward * movement); | ||||
| 	} | ||||
| 	if (glfwGetKey(window_, GLFW_KEY_DOWN) == GLFW_PRESS) { | ||||
| 		camera_->set_position(camera_->position() - forward * movement); | ||||
| 	} | ||||
| 	if (glfwGetKey(window_, GLFW_KEY_LEFT) == GLFW_PRESS) { | ||||
| 		camera_->set_position(camera_->position() - right * movement); | ||||
| 	} | ||||
| 	if (glfwGetKey(window_, GLFW_KEY_RIGHT) == GLFW_PRESS) { | ||||
| 		camera_->set_position(camera_->position() + right * movement); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| void Engine::update(const double delta_time) | ||||
| { | ||||
| 	// Update angle for rotation | ||||
| 	angle_ += 32.0 * delta_time; | ||||
|  | ||||
| 	// Update rotation for all models | ||||
| 	for (auto& model : models_) { | ||||
| 		model->set_rotation(glm::vec3( | ||||
| 		    0.0f, glm::radians(static_cast<float>(angle_)), 0.0f)); | ||||
| 	} | ||||
|  | ||||
| 	// Update world | ||||
| 	world_->update(static_cast<float>(delta_time)); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user