wip: practica1
This commit is contained in:
@@ -79,6 +79,7 @@
|
|||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
|
||||||
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<AdditionalLibraryDirectories>../lib/glfw</AdditionalLibraryDirectories>
|
<AdditionalLibraryDirectories>../lib/glfw</AdditionalLibraryDirectories>
|
||||||
@@ -93,6 +94,7 @@
|
|||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
|
||||||
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<AdditionalLibraryDirectories>../lib/glfw</AdditionalLibraryDirectories>
|
<AdditionalLibraryDirectories>../lib/glfw</AdditionalLibraryDirectories>
|
||||||
@@ -109,6 +111,7 @@
|
|||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
|
||||||
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
@@ -127,6 +130,7 @@
|
|||||||
<ConformanceMode>true</ConformanceMode>
|
<ConformanceMode>true</ConformanceMode>
|
||||||
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
|
||||||
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<Link>
|
<Link>
|
||||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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\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\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
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
0
project/x64/Debug/Practica3D.tlog/unsuccessfulbuild
Normal file
0
project/x64/Debug/Practica3D.tlog/unsuccessfulbuild
Normal file
@@ -1,4 +1,9 @@
|
|||||||
glew.c
|
buffer.cpp
|
||||||
main.cpp
|
main.cpp
|
||||||
LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
|
Generating Code...
|
||||||
ugine3d.vcxproj -> C:\Users\rabil\git\utad-programacion3d\project\x64\Debug\Practica3D.exe
|
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 '()'
|
||||||
|
|
||||||
|
|||||||
@@ -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_
|
#ifndef BUFFER_H_
|
||||||
#define BUFFER_H_
|
#define BUFFER_H_
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "../lib/glm/glm.hpp"
|
#include "../lib/glm/glm.hpp"
|
||||||
|
|
||||||
struct Buffer {
|
#include "vertex.h"
|
||||||
glm::vec3 position{ 0.0f, 0.0f, 0.0f };
|
#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_
|
#endif // BUFFER_H_
|
||||||
64
src/main.cpp
64
src/main.cpp
@@ -3,52 +3,96 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#include "../lib/glfw/glfw3.h"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#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_WIDTH 800
|
||||||
#define SCREEN_HEIGHT 600
|
#define SCREEN_HEIGHT 600
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// init glfw
|
// init glfw
|
||||||
if (!glfwInit()) {
|
if (!glfwInit()) {
|
||||||
std::cout << "could not initialize glfw" << std::endl;
|
std::cerr << "Failed to initialize glfw\n";
|
||||||
return -1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create window
|
// 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_RESIZABLE, false);
|
||||||
glfwWindowHint(GLFW_SAMPLES, 8);
|
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) {
|
if (!win) {
|
||||||
std::cout << "could not create opengl window" << std::endl;
|
std::cerr << "Failed to create opengl window\n";
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return -1;
|
return 1;
|
||||||
}
|
}
|
||||||
glfwMakeContextCurrent(win);
|
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
|
// main loop
|
||||||
float angle = 0;
|
float angle = 0;
|
||||||
double lastTime = glfwGetTime();
|
double lastTime = glfwGetTime();
|
||||||
while ( !glfwWindowShouldClose(win) && !glfwGetKey(win, GLFW_KEY_ESCAPE) ) {
|
while ( !glfwWindowShouldClose(win) && !glfwGetKey(win, GLFW_KEY_ESCAPE) ) {
|
||||||
// get delta time
|
// get delta time
|
||||||
float deltaTime = static_cast<float>(glfwGetTime() - lastTime);
|
float delta = static_cast<float>(glfwGetTime() - lastTime);
|
||||||
lastTime = glfwGetTime();
|
|
||||||
|
|
||||||
// get window size
|
// get window size
|
||||||
int screenWidth, screenHeight;
|
//int screenWidth, screenHeight;
|
||||||
glfwGetWindowSize(win, &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
|
// refresh screen
|
||||||
glfwSwapBuffers(win);
|
glfwSwapBuffers(win);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
|
// Set last time
|
||||||
|
lastTime = glfwGetTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
// shutdown
|
// shutdown
|
||||||
glfwTerminate();
|
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_
|
#ifndef SHADER_H_
|
||||||
#define SHADER_H_
|
#define SHADER_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "../lib/glew/GL/glew.h"
|
||||||
|
#include "../lib/glfw/glfw3.h"
|
||||||
#include "../lib/glm/glm.hpp"
|
#include "../lib/glm/glm.hpp"
|
||||||
|
|
||||||
struct Shader {
|
class Shader {
|
||||||
glm::vec3 position{ 0.0f, 0.0f, 0.0f };
|
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_
|
#endif // SHADER_H_
|
||||||
Reference in New Issue
Block a user