Compare commits

...

4 Commits

Author SHA1 Message Date
k
8f7ca3ae38 More cubes 2025-01-07 15:51:26 -05:00
k
b06cf47480 3d cube 2025-01-06 01:04:24 -05:00
k
335c79e23e 3d transform 2025-01-06 00:40:31 -05:00
k
987961cfe4 fix typo 2025-01-02 15:34:26 -05:00
6 changed files with 124 additions and 14 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;

View File

@ -26,6 +26,51 @@ float sqr_tex_vertices[] = {
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f /*top left*/ -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f /*top left*/
}; };
float cube_vertices[] = {
/*positions*texture cords*/
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
const char *basicVertexShaderSource = const char *basicVertexShaderSource =
"#version 330 core\n" "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n" "layout (location = 0) in vec3 aPos;\n"

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

@ -8,5 +8,5 @@ uniform sampler2D tex0;
void main() void main()
{ {
FragColor = texture(Tex0, TexCoord) * vec4(ourColor, 1.0); FragColor = texture(tex0, TexCoord) * vec4(ourColor, 1.0);
} }

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>
@ -11,6 +14,19 @@
void processInput(GLFWwindow *window); void processInput(GLFWwindow *window);
void framebuffer_size_callback(GLFWwindow *window, int width, int height); void framebuffer_size_callback(GLFWwindow *window, int width, int height);
glm::vec3 cubePositions[] = {
glm::vec3( 0.0f, 0.0f, 0.0f),
glm::vec3( 2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3( 2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3( 1.3f, -2.0f, -2.5f),
glm::vec3( 1.5f, 2.0f, -2.5f),
glm::vec3( 1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
int main(void) { int main(void) {
int tmp = 0; int tmp = 0;
@ -36,25 +52,26 @@ int main(void) {
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(sqr_tex_vertices), sqr_tex_vertices, glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices),cube_vertices,
GL_STATIC_DRAW); GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
(void *)(3 * sizeof(float))); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
(void *)0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
(void *)(6 * sizeof(float))); (void *)(3 * sizeof(float)));
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
glGenBuffers(1, &EBO); // glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sqr_indices), sqr_indices, // glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sqr_indices), sqr_indices,
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,18 +97,40 @@ int main(void) {
stbi_image_free(data); stbi_image_free(data);
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
glm::mat4 view = glm::mat4(1.0f); /*move camera*/
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.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);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shaderProgram->use(); shaderProgram->use();
shaderProgram->setInt("Tex0",0); shaderProgram->setInt("Tex0",0);
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);
glEnable(GL_DEPTH_TEST);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); for(unsigned int i = 0; i < 10; i++)
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * i;
model = glm::rotate(model, glm::radians(angle),
glm::vec3(1.0f, 0.3f, 0.5f));
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(55.0f),
glm::vec3(1.0f, 0.5f, 0.0f));
shaderProgram->setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glDrawArrays(GL_TRIANGLES, 0, 36);
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();

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;