wip: practica1
This commit is contained in:
		| @@ -1 +1,31 @@ | ||||
| #include "buffer.h" | ||||
| #include "buffer.h" | ||||
|  | ||||
| #include <vector> | ||||
|  | ||||
| #include "../lib/glew/GL/glew.h" | ||||
| #include "../lib/glfw/glfw3.h" | ||||
|  | ||||
| #include "vertex.h" | ||||
|  | ||||
| Buffer::Buffer(const std::vector<Vertex>& vertices, const std::vector<uint16_t>& indices) | ||||
| { | ||||
| 	glGenVertexArrays(1, &VAO); | ||||
| 	glGenBuffers(1, &VBO); | ||||
| 	glGenBuffers(1, &EBO); | ||||
|  | ||||
| 	glBindVertexArray(VAO); | ||||
| 	glBindBuffer(GL_ARRAY_BUFFER, VBO); | ||||
| 	glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW); | ||||
|  | ||||
| 	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); | ||||
| 	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint16_t), indices.data(), GL_STATIC_DRAW); | ||||
|  | ||||
| 	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0); | ||||
| 	glEnableVertexAttribArray(0); | ||||
| } | ||||
|  | ||||
| void Buffer::Draw(const Shader& shader) const | ||||
| { | ||||
| 	glBindVertexArray(VAO); | ||||
| 	glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0); | ||||
| } | ||||
							
								
								
									
										15
									
								
								src/buffer.h
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								src/buffer.h
									
									
									
									
									
								
							| @@ -1,10 +1,21 @@ | ||||
| #ifndef BUFFER_H_ | ||||
| #define BUFFER_H_ | ||||
|  | ||||
| #include <vector> | ||||
|  | ||||
| #include "../lib/glm/glm.hpp" | ||||
|  | ||||
| struct Buffer { | ||||
| 	glm::vec3 position{ 0.0f, 0.0f, 0.0f }; | ||||
| #include "vertex.h" | ||||
| #include "shader.h" | ||||
|  | ||||
| class Buffer { | ||||
| public: | ||||
| 	Buffer(const std::vector<Vertex>& vertices, const std::vector<uint16_t>& indices); | ||||
|  | ||||
| 	void Draw(const Shader& shader) const; | ||||
|  | ||||
| private: | ||||
| 	uint32_t VAO, VBO, EBO; | ||||
| }; | ||||
|  | ||||
| #endif // BUFFER_H_ | ||||
							
								
								
									
										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; | ||||
| } | ||||
| @@ -1 +1,43 @@ | ||||
| #include "shader.h" | ||||
| #include "shader.h" | ||||
|  | ||||
| #include <vector> | ||||
| #include <fstream> | ||||
| #include <sstream> | ||||
|  | ||||
| #include "../lib/glew/GL/glew.h" | ||||
| #include "../lib/glfw/glfw3.h" | ||||
|  | ||||
| namespace { | ||||
| 	std::string readShaderFile(const std::string& filename) { | ||||
| 		std::ifstream file(filename); | ||||
| 		if (!file) | ||||
| 			throw std::runtime_error("Failed to open shader file: " + filename); | ||||
| 		std::stringstream buffer; | ||||
| 		buffer << file.rdbuf(); | ||||
| 		return buffer.str(); | ||||
| 	} | ||||
| } // namespace | ||||
|  | ||||
| Shader::Shader(const std::string& vertexPath, const std::string& fragmentPath) | ||||
| { | ||||
| 	std::string vertexCode = readShaderFile(vertexPath); | ||||
| 	std::string fragmentCode = readShaderFile(fragmentPath); | ||||
| 	const char* vShaderCode = vertexCode.c_str(); | ||||
| 	const char* fShaderCode = fragmentCode.c_str(); | ||||
|  | ||||
| 	const uint32_t vertex = glCreateShader(GL_VERTEX_SHADER); | ||||
| 	glShaderSource(vertex, 1, &vShaderCode, NULL); | ||||
| 	glCompileShader(vertex); | ||||
|  | ||||
| 	const uint32_t fragment = glCreateShader(GL_FRAGMENT_SHADER); | ||||
| 	glShaderSource(fragment, 1, &fShaderCode, NULL); | ||||
| 	glCompileShader(fragment); | ||||
|  | ||||
| 	program = glCreateProgram(); | ||||
| 	glAttachShader(program, vertex); | ||||
| 	glAttachShader(program, fragment); | ||||
| 	glLinkProgram(program); | ||||
|  | ||||
| 	glDeleteShader(vertex); | ||||
| 	glDeleteShader(fragment); | ||||
| } | ||||
							
								
								
									
										16
									
								
								src/shader.h
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								src/shader.h
									
									
									
									
									
								
							| @@ -1,10 +1,22 @@ | ||||
| #ifndef SHADER_H_ | ||||
| #define SHADER_H_ | ||||
|  | ||||
| #include <string> | ||||
|  | ||||
| #include "../lib/glew/GL/glew.h" | ||||
| #include "../lib/glfw/glfw3.h" | ||||
| #include "../lib/glm/glm.hpp" | ||||
|  | ||||
| struct Shader { | ||||
| 	glm::vec3 position{ 0.0f, 0.0f, 0.0f }; | ||||
| class Shader { | ||||
| public: | ||||
| 	Shader(const std::string& vertexPath, const std::string& fragmentPath); | ||||
|  | ||||
| 	void use() const { glUseProgram(program); } | ||||
| 	uint32_t getId() const { return program; } | ||||
|  | ||||
| private: | ||||
| 	uint32_t program; | ||||
| }; | ||||
| 	 | ||||
|  | ||||
| #endif // SHADER_H_ | ||||
		Reference in New Issue
	
	Block a user
	 izenynn
					izenynn