3d transform

This commit is contained in:
k 2025-01-06 00:40:31 -05:00
parent 987961cfe4
commit 335c79e23e
4 changed files with 40 additions and 1 deletions

View File

@ -1,6 +1,7 @@
#ifndef SHADER_H #ifndef SHADER_H
#define SHADER_H #define SHADER_H
#include <glad/glad.h> #include <glad/glad.h>
#include <glm/glm.hpp>
#include <string> #include <string>
class Shader { class Shader {
@ -10,6 +11,7 @@ public:
void use(); void use();
void setInt(const std::string &name, int value); void setInt(const std::string &name, int value);
void setFloat(const std::string &name, float value); void setFloat(const std::string &name, float value);
void setMat4(const std::string &name, glm::mat4 value);
private: private:
unsigned int ID; unsigned int ID;

18
shaders/MVPTexVert.glsl Normal file
View File

@ -0,0 +1,18 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 TexCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = aTexCoord;
}

View File

@ -1,5 +1,8 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#include <shader.hpp> #include <shader.hpp>
@ -54,7 +57,7 @@ int main(void) {
GL_STATIC_DRAW); GL_STATIC_DRAW);
Shader *shaderProgram = Shader *shaderProgram =
new Shader("./shaders/TexVert.glsl", "./shaders/TexFrag.glsl"); new Shader("./shaders/MVPTexVert.glsl", "./shaders/TexFrag.glsl");
unsigned int texture; unsigned int texture;
glGenTextures(1, &texture); glGenTextures(1, &texture);
@ -80,6 +83,12 @@ int main(void) {
stbi_image_free(data); stbi_image_free(data);
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
glm::mat4 model = glm::mat4(1.0f); /*move model*/
glm::mat4 view = glm::mat4(1.0f); /*move camera*/
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
glm::mat4 projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); /*camera settings*/
processInput(window); processInput(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
@ -87,6 +96,10 @@ int main(void) {
shaderProgram->use(); shaderProgram->use();
shaderProgram->setInt("Tex0",0); shaderProgram->setInt("Tex0",0);
shaderProgram->setMat4("model",model);
shaderProgram->setMat4("view",view);
shaderProgram->setMat4("projection",projection);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(VAO); glBindVertexArray(VAO);

View File

@ -1,5 +1,6 @@
#include <fstream> #include <fstream>
#include <glad/glad.h> #include <glad/glad.h>
#include <glm/gtc/type_ptr.hpp>
#include <iostream> #include <iostream>
#include <shader.hpp> #include <shader.hpp>
#include <sstream> #include <sstream>
@ -44,6 +45,11 @@ void Shader::setFloat(const std::string &name, float value) {
glUniform1f(glGetUniformLocation(ID, name.c_str()), value); glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
} }
void Shader::setMat4(const std::string &name, glm::mat4 value) {
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
}
/*priv*/ /*priv*/
unsigned int Shader::shaderCMPL(GLenum type, const char *src) { unsigned int Shader::shaderCMPL(GLenum type, const char *src) {
unsigned int shader; unsigned int shader;