wip: practica1
This commit is contained in:
		
							
								
								
									
										70
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										70
									
								
								src/main.cpp
									
									
									
									
									
								
							| @@ -3,52 +3,96 @@ | ||||
| #endif | ||||
|  | ||||
|  | ||||
| #include "../lib/glfw/glfw3.h" | ||||
| #include <iostream> | ||||
| #include <vector> | ||||
|  | ||||
| #include "../lib/glew/GL/glew.h" | ||||
| #include "../lib/glfw/glfw3.h" | ||||
| #include "../lib/glm/glm.hpp" | ||||
| #include "../lib/glm/gtc/matrix_transform.hpp" | ||||
|  | ||||
| #include "vertex.h" | ||||
| #include "shader.h" | ||||
| #include "buffer.h" | ||||
|  | ||||
| #define SCREEN_WIDTH 800 | ||||
| #define SCREEN_HEIGHT 600 | ||||
|  | ||||
| int main() { | ||||
| 	// init glfw | ||||
| 	if ( !glfwInit() ) { | ||||
| 		std::cout << "could not initialize glfw" << std::endl; | ||||
| 		return -1; | ||||
| 	if (!glfwInit()) { | ||||
| 		std::cerr << "Failed to initialize glfw\n"; | ||||
| 		return 1; | ||||
| 	} | ||||
|  | ||||
| 	// create window | ||||
| 	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); | ||||
| 	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); | ||||
| 	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | ||||
| 	//glfwWindowHint(GLFW_RESIZABLE, false); | ||||
| 	glfwWindowHint(GLFW_SAMPLES, 8); | ||||
| 	GLFWwindow* win = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "", nullptr, nullptr); | ||||
|  | ||||
| 	GLFWwindow* win = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Daniel Poveda", nullptr, nullptr); | ||||
| 	if (!win) { | ||||
| 		std::cout << "could not create opengl window" << std::endl; | ||||
| 		std::cerr << "Failed to create opengl window\n"; | ||||
| 		glfwTerminate(); | ||||
| 		return -1; | ||||
| 		return 1; | ||||
| 	} | ||||
| 	glfwMakeContextCurrent(win); | ||||
|  | ||||
| 	 | ||||
| 	glewExperimental = GL_TRUE; | ||||
| 	if (glewInit() != GLEW_OK) { | ||||
| 		std::cerr << "Failed to initialize GLEW\n"; | ||||
| 		glfwTerminate(); | ||||
| 		return 1; | ||||
| 	} | ||||
|  | ||||
| 	glEnable(GL_DEPTH_TEST); | ||||
| 	Shader shader("data/vertex.glsl", "data/fragment.glsl"); | ||||
|  | ||||
| 	std::vector<Vertex> vertices = { | ||||
| 		{{-0.5f, -0.5f, 0.0f}}, | ||||
| 		{{0.5f, -0.5f, 0.0f}},  | ||||
| 		{{0.0f, 0.5f, 0.0f}} | ||||
| 	}; | ||||
| 	std::vector<uint16_t> indices = { 0, 1, 2 }; | ||||
| 	Buffer buffer(vertices, indices); | ||||
|  | ||||
| 	glm::mat4 projection = glm::perspective(glm::radians(45.0f), static_cast<float>(SCREEN_WIDTH) / SCREEN_HEIGHT, 0.1f, 100.0f); | ||||
| 	glm::mat4 view = glm::translate(glm::mat4(1.0f), glm::vec3(0, 0, 6)); | ||||
|  | ||||
| 	// main loop | ||||
| 	float angle = 0; | ||||
| 	double lastTime = glfwGetTime(); | ||||
| 	while ( !glfwWindowShouldClose(win) && !glfwGetKey(win, GLFW_KEY_ESCAPE) ) { | ||||
| 		// get delta time | ||||
| 		float deltaTime = static_cast<float>(glfwGetTime() - lastTime); | ||||
| 		lastTime = glfwGetTime(); | ||||
| 		float delta = static_cast<float>(glfwGetTime() - lastTime); | ||||
|  | ||||
| 		// get window size | ||||
| 		int screenWidth, screenHeight; | ||||
| 		glfwGetWindowSize(win, &screenWidth, &screenHeight); | ||||
| 		//int screenWidth, screenHeight; | ||||
| 		//glfwGetWindowSize(win, &screenWidth, &screenHeight); | ||||
|  | ||||
| 		 | ||||
| 		// Logic | ||||
| 		glm::mat4 model = glm::rotate(glm::mat4(1.0f), glm::radians(delta * 32.0f), glm::vec3(0, 1, 0)); | ||||
| 		glm::mat4 mvp = projection * view * model; | ||||
|  | ||||
| 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | ||||
| 		//glClearColor(0, 255, 0, 255); | ||||
| 		shader.use(); | ||||
| 		uint32_t mvpLoc = glGetUniformLocation(shader.getId(), "MVP"); | ||||
| 		glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, &mvp[0][0]); | ||||
| 		buffer.Draw(shader); | ||||
|  | ||||
| 		// refresh screen | ||||
| 		glfwSwapBuffers(win); | ||||
| 		glfwPollEvents(); | ||||
|  | ||||
| 		// Set last time | ||||
| 		lastTime = glfwGetTime(); | ||||
| 	} | ||||
|  | ||||
| 	// shutdown | ||||
| 	glfwTerminate(); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 izenynn
					izenynn