wip: practica1

This commit is contained in:
izenynn
2025-02-20 13:02:33 +01:00
parent d2be70c2ba
commit e377b87438
15 changed files with 173 additions and 22 deletions

View File

@@ -79,6 +79,7 @@
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>../lib/glfw</AdditionalLibraryDirectories>
@@ -93,6 +94,7 @@
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>../lib/glfw</AdditionalLibraryDirectories>
@@ -109,6 +111,7 @@
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
@@ -127,6 +130,7 @@
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>

View File

@@ -1,2 +1,5 @@
C:\Users\rabil\git\utad-programacion3d\lib\glew\glew.c;C:\Users\rabil\git\utad-programacion3d\project\x64\Debug\glew.obj
C:\Users\rabil\git\utad-programacion3d\src\buffer.cpp;C:\Users\rabil\git\utad-programacion3d\project\x64\Debug\buffer.obj
C:\Users\rabil\git\utad-programacion3d\src\main.cpp;C:\Users\rabil\git\utad-programacion3d\project\x64\Debug\main.obj
C:\Users\rabil\git\utad-programacion3d\src\shader.cpp;C:\Users\rabil\git\utad-programacion3d\project\x64\Debug\shader.obj
C:\Users\rabil\git\utad-programacion3d\src\vertex.cpp;C:\Users\rabil\git\utad-programacion3d\project\x64\Debug\vertex.obj

View File

@@ -1,4 +1,9 @@
 glew.c
 buffer.cpp
main.cpp
LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
ugine3d.vcxproj -> C:\Users\rabil\git\utad-programacion3d\project\x64\Debug\Practica3D.exe
Generating Code...
C:\Users\rabil\git\utad-programacion3d\src\main.cpp(84,9): error C2660: 'Buffer::Draw': function does not take 0 arguments
C:\Users\rabil\git\utad-programacion3d\src\buffer.h(15,7):
see declaration of 'Buffer::Draw'
C:\Users\rabil\git\utad-programacion3d\src\main.cpp(84,9):
while trying to match the argument list '()'

View File

@@ -1 +1,31 @@
#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);
}

View File

@@ -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_

View File

@@ -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;
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;
}

View File

@@ -1 +1,43 @@
#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);
}

View File

@@ -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_