From 237272c17b79428ba6a9a07a84e7a86a40372a0b Mon Sep 17 00:00:00 2001
From: onTheZero <114521696+onTheZero@users.noreply.github.com>
Date: Sat, 12 Oct 2024 19:46:28 -0400
Subject: [PATCH] lighting and imgui
---
.gitmodules | 4 +
LearningOpenGL.vcxproj | 9 +-
LearningOpenGL.vcxproj.filters | 13 +++
camera.h | 115 +++++++++++++++++++
cube.frag | 23 ++++
cube.h | 100 +++++++++++++++++
cube.vert | 19 ++++
cubeFactory.h | 162 +++++++++++++++++++++++++++
fragment.frag | 6 +-
imgui | 1 +
light.frag | 7 ++
light.vert | 12 ++
main.cpp | 199 +++++++++++++++++++--------------
vertex.vert | 10 +-
14 files changed, 587 insertions(+), 93 deletions(-)
create mode 100644 .gitmodules
create mode 100644 camera.h
create mode 100644 cube.frag
create mode 100644 cube.h
create mode 100644 cube.vert
create mode 100644 cubeFactory.h
create mode 160000 imgui
create mode 100644 light.frag
create mode 100644 light.vert
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..a6ee341
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,4 @@
+[submodule "imgui"]
+path = imgui
+url = https://github.com/ocornut/imgui.git
+branch = docking
diff --git a/LearningOpenGL.vcxproj b/LearningOpenGL.vcxproj
index b97f0e0..ac4c1ad 100644
--- a/LearningOpenGL.vcxproj
+++ b/LearningOpenGL.vcxproj
@@ -71,7 +71,7 @@
- C:\Development Projects\LearningOpenGL\Include\glm;C:\Development Projects\LearningOpenGL\Include\glfw-3.4.bin.WIN64\include;C:\Development Projects\LearningOpenGL\Include\glad\include;$(IncludePath)
+ C:\Development Projects\LearningOpenGL\imgui\backends;C:\Development Projects\LearningOpenGL\imgui;C:\Development Projects\LearningOpenGL\Include\glm;C:\Development Projects\LearningOpenGL\Include\glfw-3.4.bin.WIN64\include;C:\Development Projects\LearningOpenGL\Include\glad\include;$(IncludePath)
C:\Development Projects\LearningOpenGL\Include\glfw-3.4.bin.WIN64\lib-vc2022;$(LibraryPath)
@@ -137,10 +137,17 @@
+
+
+
+
+
+
+
diff --git a/LearningOpenGL.vcxproj.filters b/LearningOpenGL.vcxproj.filters
index fbb8a73..8612da1 100644
--- a/LearningOpenGL.vcxproj.filters
+++ b/LearningOpenGL.vcxproj.filters
@@ -25,10 +25,23 @@
+
+
+
+
Header Files
+
+ Header Files
+
+
+ Header Files
+
+
+ Header Files
+
\ No newline at end of file
diff --git a/camera.h b/camera.h
new file mode 100644
index 0000000..a1437ba
--- /dev/null
+++ b/camera.h
@@ -0,0 +1,115 @@
+#pragma once
+#ifndef CAMERA_H
+#define CAMERA_H
+
+#include
+#include
+#include
+
+enum Camera_Movement
+{
+ FORWARD,
+ BACKWARD,
+ LEFT,
+ RIGHT
+};
+
+const float YAW = -90.0f;
+const float PITCH = 0.0f;
+const float SPEED = 2.5f;
+const float SENSITIVITY = 0.1f;
+const float ZOOM = 45.0f;
+
+class Camera
+{
+public:
+ glm::vec3 Position;
+ glm::vec3 Front;
+ glm::vec3 Up;
+ glm::vec3 Right;
+ glm::vec3 WorldUp;
+
+ float Yaw;
+ float Pitch;
+
+ float MovementSpeed;
+ float MouseSensitivity;
+ float Zoom;
+
+ Camera(
+ glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
+ glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
+ float yaw = YAW,
+ float pitch = PITCH)
+ :
+ Front(glm::vec3(0.0f, 0.0f, -1.0f)),
+ MovementSpeed(SPEED),
+ MouseSensitivity(SENSITIVITY),
+ Zoom(ZOOM)
+ {
+ Position = position;
+ WorldUp = up;
+ Yaw = yaw;
+ Pitch = pitch;
+ updateCameraVectors();
+ }
+
+ glm::mat4 GetViewMatrix()
+ {
+ return glm::lookAt(Position, Position + Front, Up);
+ }
+
+ void ProcessKeyboard(Camera_Movement direction, float deltaTime)
+ {
+ float velocity = MovementSpeed * deltaTime;
+ if (direction == FORWARD)
+ Position += Front * velocity;
+ if (direction == BACKWARD)
+ Position -= Front * velocity;
+ if (direction == LEFT)
+ Position -= Right * velocity;
+ if (direction == RIGHT)
+ Position += Right * velocity;
+ }
+
+ void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
+ {
+ xoffset *= MouseSensitivity;
+ yoffset *= MouseSensitivity;
+
+ Yaw += xoffset;
+ Pitch += yoffset;
+
+ if (constrainPitch)
+ {
+ if (Pitch > 89.0f)
+ Pitch = 89.0f;
+ if (Pitch < -89.0f)
+ Pitch = -89.0f;
+ }
+
+ updateCameraVectors();
+ }
+
+ void ProcessMouseScroll(float yoffset)
+ {
+ Zoom -= (float)yoffset;
+ if (Zoom < 1.0f)
+ Zoom = 1.0f;
+ if (Zoom > 45.0f)
+ Zoom = 45.0f;
+ }
+private:
+ void updateCameraVectors()
+ {
+ glm::vec3 front;
+ front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
+ front.y = sin(glm::radians(Pitch));
+ front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
+ Front = glm::normalize(front);
+ Right = glm::normalize(glm::cross(Front, WorldUp));
+ Up = glm::normalize(glm::cross(Right, Front));
+ }
+};
+
+#endif
\ No newline at end of file
diff --git a/cube.frag b/cube.frag
new file mode 100644
index 0000000..7bdc90e
--- /dev/null
+++ b/cube.frag
@@ -0,0 +1,23 @@
+#version 330 core
+out vec4 FragColor;
+
+uniform vec3 objectColor;
+uniform vec3 lightColor;
+uniform vec3 lightPos;
+
+in vec3 FragPos;
+in vec3 Normal;
+
+void main()
+{
+ float ambientStrength = 0.1f;
+ vec3 ambient = ambientStrength * lightColor;
+
+ vec3 norm = normalize(Normal);
+ vec3 lightDir = normalize(lightPos - FragPos);
+ float diff = max(dot(norm, lightDir), 0.0);
+ vec3 diffuse = diff * lightColor;
+
+ vec3 result = (ambient + diffuse) * objectColor;
+ FragColor = vec4(result, 1.0f);
+}
\ No newline at end of file
diff --git a/cube.h b/cube.h
new file mode 100644
index 0000000..07973dd
--- /dev/null
+++ b/cube.h
@@ -0,0 +1,100 @@
+#pragma once
+
+#ifndef CUBE_H
+#define CUBE_H
+
+#include
+#include
+#include
+
+class Cube
+{
+private:
+ glm::vec3 position;
+ glm::vec3 scale;
+ glm::vec3 rotation;
+ glm::vec3 color;
+
+ //const float CUBE_COLORS[24]{
+ // 1.0f, 0.0f, 0.0f,
+ // 0.0f, 1.0f, 0.0f,
+ // 0.0f, 0.0f, 1.0f,
+ // 1.0f, 1.0f, 0.0f,
+ // 0.0f, 1.0f, 1.0f,
+ // 1.0f, 0.0f, 1.0f,
+ // 1.0f, 1.0f, 1.0f,
+ // 0.0f, 0.0f, 0.0f
+ //};
+
+public:
+
+ Cube(glm::vec3 position, glm::vec3 scale, glm::vec3 rotation, glm::vec3 color)
+ {
+ this->position = position;
+ this->scale = scale;
+ this->rotation = rotation;
+ this->color = color;
+ }
+
+ //void draw(unsigned int& modelLoc, unsigned int& VAO)
+ //{
+ // glBindVertexArray(VAO);
+ // glm::mat4 model = getModelMatrix();
+ // glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
+ // glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); // 36 indices for the cube
+ // glBindVertexArray(0);
+ //}
+
+ glm::mat4 getModelMatrix() const
+ {
+ glm::mat4 model = glm::mat4(1.0f);
+ model = glm::translate(model, position);
+ model = glm::rotate(model, glm::radians(rotation.x), glm::vec3(1, 0, 0));
+ model = glm::rotate(model, glm::radians(rotation.y), glm::vec3(0, 1, 0));
+ model = glm::rotate(model, glm::radians(rotation.z), glm::vec3(0, 0, 1));
+ model = glm::scale(model, scale);
+
+ return model;
+ }
+
+ void setPosition(glm::vec3 position)
+ {
+ this->position = position;
+ }
+
+ void setScale(glm::vec3 scale)
+ {
+ this->scale = scale;
+ }
+
+ void setRotation(glm::vec3 rotation)
+ {
+ this->rotation = rotation;
+ }
+
+ void setColor(glm::vec3 color)
+ {
+ this->color = color;
+ }
+
+ glm::vec3 getPosition() const
+ {
+ return position;
+ }
+
+ glm::vec3 getScale() const
+ {
+ return scale;
+ }
+
+ glm::vec3 getRotation() const
+ {
+ return rotation;
+ }
+
+ glm::vec3 getColor() const
+ {
+ return color;
+ }
+};
+#endif
\ No newline at end of file
diff --git a/cube.vert b/cube.vert
new file mode 100644
index 0000000..7932c9d
--- /dev/null
+++ b/cube.vert
@@ -0,0 +1,19 @@
+
+#version 330 core
+layout (location = 0) in vec3 aPos;
+layout (location = 1) in vec3 aNormal;
+
+out vec3 FragPos;
+out vec3 Normal;
+
+uniform mat4 model;
+uniform mat4 view;
+uniform mat4 projection;
+
+void main()
+{
+ FragPos = vec3(model * vec4(aPos, 1.0));
+ Normal = mat3(transpose(inverse(model))) * aNormal;
+
+ gl_Position = projection * view * vec4(FragPos, 1.0);
+}
diff --git a/cubeFactory.h b/cubeFactory.h
new file mode 100644
index 0000000..19401af
--- /dev/null
+++ b/cubeFactory.h
@@ -0,0 +1,162 @@
+#pragma once
+
+#ifndef CUBE_FACTORY_H
+#define CUBE_FACTORY_H
+
+#include
+
+#include
+#include
+#include
+#include
+#include
+
+#include
+#include
+
+class CubeFactory
+{
+private:
+ Shader shader;
+
+ unsigned int VAO, VBO, EBO;
+
+ std::list cubes;
+
+ const unsigned int CUBE_INDICES[36]{
+ 0, 1, 2, 0, 2, 3, // Front face
+ 4, 5, 6, 4, 6, 7, // Back face
+ 0, 1, 5, 0, 5, 4, // Bottom face
+ 3, 2, 6, 3, 6, 7, // Top face
+ 0, 3, 7, 0, 7, 4, // Left face
+ 1, 2, 6, 1, 6, 5 // Right face
+ };
+
+ void setupBuffers()
+ {
+ float CUBE_VERTICES[] = {
+ -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
+ 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
+ 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
+ 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
+ -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
+ -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
+
+ -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
+ 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
+ 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
+ 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
+ -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
+ -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
+
+ -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
+ -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
+ -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
+ -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
+ -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
+ -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
+
+ 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
+ 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
+ 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
+ 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
+ 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
+ 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
+
+ -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
+ 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
+ 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
+ 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
+ -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
+ -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
+
+ -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
+ 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
+ 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
+ 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
+ -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
+ -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
+ };
+
+ glGenVertexArrays(1, &VAO);
+ glGenBuffers(1, &VBO);
+ glGenBuffers(1, &EBO);
+
+ glBindVertexArray(VAO);
+
+ glBindBuffer(GL_ARRAY_BUFFER, VBO);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(CUBE_VERTICES), CUBE_VERTICES, GL_STATIC_DRAW);
+
+ //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
+ //glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CUBE_INDICES), CUBE_INDICES, GL_STATIC_DRAW);
+
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
+ glEnableVertexAttribArray(0);
+
+ glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
+ glEnableVertexAttribArray(1);
+
+ //glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
+ //glEnableVertexAttribArray(1);
+
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ glBindVertexArray(0);
+ }
+public:
+ CubeFactory() : shader (Shader("cube.vert", "cube.frag"))
+ {
+ std::cout << "Setting up cube factory" << std::endl;
+ setupBuffers();
+ //std::cout << VAO << " " << VBO << " " << EBO << std::endl;
+ }
+
+ void clear()
+ {
+ glDeleteVertexArrays(1, &VAO);
+ glDeleteBuffers(1, &VBO);
+ glDeleteBuffers(1, &EBO);
+ }
+
+ void addCube(glm::vec3 position, glm::vec3 scale, glm::vec3 rotation, glm::vec3 color)
+ {
+ cubes.push_back(Cube(position, scale, rotation, color));
+ }
+
+ void render(glm::mat4 view, glm::mat4 projection, glm::vec3 lightPos, float deltaTime)
+ {
+ //std::cout << "Rendering cubes" << std::endl;
+ shader.use();
+ shader.setMat4("projection", projection);
+ shader.setMat4("view", view);
+ //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+ for (Cube& cube : cubes)
+ {
+ //std::cout << "Cube position: (" << cube.getColor().x << "," << cube.getColor().y << "," << cube.getColor().z << ")" << std::endl;
+ cube.setRotation(cube.getRotation() + (glm::vec3(5.0f, 10.0f, 20.0f))*deltaTime);
+ glm::mat4 model = cube.getModelMatrix();
+ glm::vec3 color = cube.getColor();
+ shader.setMat4("model", model);
+ shader.setVec3("objectColor", color);
+ shader.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
+ shader.setVec3("lightPos", lightPos);
+
+ glBindVertexArray(VAO);
+ glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+ glDrawArrays(GL_TRIANGLES, 0, 36);
+ //glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
+ //shader.setVec3("objectColor", 0.0f, 0.0f, 0.0f);
+ //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+ //glDrawArrays(GL_TRIANGLES, 0, 36);
+ //glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
+
+ glBindVertexArray(0);
+ }
+ }
+
+ std::list getCubes()
+ {
+ return cubes;
+ }
+};
+
+#endif
\ No newline at end of file
diff --git a/fragment.frag b/fragment.frag
index f0f2177..1b23324 100644
--- a/fragment.frag
+++ b/fragment.frag
@@ -1,8 +1,10 @@
#version 330 core
out vec4 FragColor;
-in vec3 ourColor;
+
+uniform vec3 objectColor;
+uniform vec3 lightColor;
void main()
{
- FragColor = vec4(ourColor, 1.0f);
+ FragColor = vec4(objectColor * lightColor, 1.0f);
}
\ No newline at end of file
diff --git a/imgui b/imgui
new file mode 160000
index 0000000..22503bf
--- /dev/null
+++ b/imgui
@@ -0,0 +1 @@
+Subproject commit 22503bfe75359d5d7c24e03e9fe03ffca70585cf
diff --git a/light.frag b/light.frag
new file mode 100644
index 0000000..1c649f4
--- /dev/null
+++ b/light.frag
@@ -0,0 +1,7 @@
+#version 330 core
+out vec4 FragColor;
+
+void main()
+{
+ FragColor = vec4(1.0); // set all 4 vector values to 1.0
+}
\ No newline at end of file
diff --git a/light.vert b/light.vert
new file mode 100644
index 0000000..1d74826
--- /dev/null
+++ b/light.vert
@@ -0,0 +1,12 @@
+#version 330 core
+layout (location = 0) in vec3 aPos;
+
+uniform mat4 model;
+uniform mat4 view;
+uniform mat4 projection;
+
+void main()
+{
+ gl_Position = projection * view * model * vec4(aPos, 1.0);
+}
+
diff --git a/main.cpp b/main.cpp
index 4c96477..f9fba4a 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,40 +1,49 @@
+
+#include
+#include
+
#include
#include
#include
#include
#include
-#include
-
-#include
-#include
+#include
+#include
+#include
+#include
+#include
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
+void mouse_callback(GLFWwindow* window, double xpos, double ypos);
+void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
int SCR_WIDTH = 800;
int SCR_HEIGHT = 800;
-float triangleVertices[] = {
- -0.5f,-0.5f,0.0f, 1.0f,0.0f,0.0f,
- 0.5f,-0.5f,0.0f, 0.0f,1.0f,0.0f,
- 0.0f, 0.5f,0.0f, 0.0f,0.0f,1.0f
-};
+Camera camera(glm::vec3(0.0f, 0.0f, 20.0f));
+float lastX = SCR_WIDTH / 2.0f;
+float lastY = SCR_HEIGHT / 2.0f;
+bool firstMouse = true;
-float squareVertices[] = {
- -0.8f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
- -0.8f, 0.8f, 0.0f, 1.0f, 0.0f, 0.0f,
- -1.0f, 0.8f, 0.0f, 0.0f, 1.0f, 0.0f,
- -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f
-};
+float deltaTime = 0.0f;
+float lastFrame = 0.0f;
+float elapsedTime = 0.0f;
-unsigned int indices[] = {
- 0,1,3,
- 1,2,3
-};
+//glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
-int main() {
+int main()
+{
+ IMGUI_CHECKVERSION();
+ ImGui::CreateContext();
+ ImGuiIO& io = ImGui::GetIO();
+ io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
+ io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
+ io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
+
+ std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
@@ -46,106 +55,128 @@ int main() {
glfwTerminate();
return -1;
}
+
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
+ glfwSetCursorPosCallback(window, mouse_callback);
+ glfwSetScrollCallback(window, scroll_callback);
+
+ glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
- Shader ourShader("vertex.vert", "fragment.frag");
+ glEnable(GL_DEPTH_TEST);
- glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
+ std::cout << "Loaded GLFW context, OpenGL 3.3" << std::endl;
- unsigned int VAO_1, VAO_2, VBO_1, VBO_2, EBO;
- glGenVertexArrays(1, &VAO_1);
- glGenVertexArrays(1, &VAO_2);
- glGenBuffers(1, &VBO_1);
- glGenBuffers(1, &VBO_2);
- glGenBuffers(1, &EBO);
+ CubeFactory cubeFactory;
- glBindVertexArray(VAO_1);
+ float offset = 2.0f;
+ int xAmount = 10;
+ int yAmount = 10;
+ int zAmount = 10;
- glBindBuffer(GL_ARRAY_BUFFER, VBO_1);
- glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
- glEnableVertexAttribArray(0);
+ for (int x = 0; x < xAmount; x++)
+ {
+ for (int y = 0; y < yAmount; y++)
+ {
+ for (int z = 0; z < zAmount; z++)
+ {
+ glm::vec3 position = glm::vec3( (x * offset) - (xAmount * offset / 2.0f),
+ (y * offset) - (yAmount * offset / 2.0f),
+ (z * offset) - (zAmount * offset / 2.0f));
+ glm::vec3 scale = glm::vec3(1.0f, 1.0f, 1.0f);
+ glm::vec3 rotation = glm::vec3(0.0f, 0.0f, 0.0f);
+ glm::vec3 color = glm::vec3(x/10.0f, y/10.0f, z/10.0f);
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
- glEnableVertexAttribArray(1);
+ cubeFactory.addCube(position, scale, rotation, color);
+ }
+ }
+ }
- glBindVertexArray(VAO_2);
-
- glBindBuffer(GL_ARRAY_BUFFER, VBO_2);
- glBufferData(GL_ARRAY_BUFFER, sizeof(squareVertices), squareVertices, GL_STATIC_DRAW);
-
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
-
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
- glEnableVertexAttribArray(0);
-
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
- glEnableVertexAttribArray(1);
-
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
+ //std::cout << "Amount of cubes: " << cubeFactory.getCubes().size() << std::endl;
- float r = 0.0f, g = 0.0f, b = 0.0f;
+ float r = 0.05f, g = 0.05f, b = 0.05f;
+
while (!glfwWindowShouldClose(window)) {
+ //timing
+ float currentFrame = glfwGetTime();
+ deltaTime = currentFrame - lastFrame;
+ lastFrame = currentFrame;
+ elapsedTime += deltaTime;
+
//input
processInput(window);
- //rendering
- if (r < 1) {
- r = r + 0.01f;
- }
- else if (g < 1) {
- g = g + 0.01f;
- }
- else if (b < 1) {
- b = b + 0.01f;
- }
- else {
- r = 0;
- g = 0;
- b = 0;
- }
-
glClearColor(r, g, b, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- //transforms
- glm::mat4 defaultTransform = glm::mat4(1.0f);
- glm::mat4 rotatedTransform = glm::rotate(defaultTransform, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
+ // view/projection transformations
+ glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
+ glm::mat4 view = camera.GetViewMatrix();
- //draw
- ourShader.use();
- unsigned int transformLoc = glGetUniformLocation(ourShader.ID, "transform");
- glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(rotatedTransform));
- glBindVertexArray(VAO_1);
- glDrawArrays(GL_TRIANGLES, 0, 3);
+ glm::vec3 lightPos = glm::vec3(100.2f, 100.0f, 200.0f);
- glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(defaultTransform));
- glBindVertexArray(VAO_2);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
+ cubeFactory.render(view, projection, lightPos, deltaTime);
//check and call
glfwSwapBuffers(window);
glfwPollEvents();
}
+ //cubeFactory.clear();
glfwTerminate();
return 0;
}
-void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
+void framebuffer_size_callback(GLFWwindow* window, int width, int height)
+{
glViewport(0, 0, width, height);
}
-void processInput(GLFWwindow* window) {
+void processInput(GLFWwindow* window)
+{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
-}
\ No newline at end of file
+
+ const float cameraSpeed = 5.0f * deltaTime;
+ if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
+ camera.ProcessKeyboard(FORWARD, deltaTime);
+ if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
+ camera.ProcessKeyboard(BACKWARD, deltaTime);
+ if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
+ camera.ProcessKeyboard(LEFT, deltaTime);
+ if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
+ camera.ProcessKeyboard(RIGHT, deltaTime);
+}
+
+void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
+{
+ //std::cout << "Mouse callback" << std::endl;
+ float xpos = static_cast(xposIn);
+ float ypos = static_cast(yposIn);
+
+ if (firstMouse)
+ {
+ lastX = xpos;
+ lastY = ypos;
+ firstMouse = false;
+ }
+
+ float xoffset = xpos - lastX;
+ float yoffset = lastY - ypos;
+
+ lastX = xpos;
+ lastY = ypos;
+
+ camera.ProcessMouseMovement(xoffset, yoffset);
+}
+
+void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
+{
+ camera.ProcessMouseScroll(static_cast(yoffset));
+}
diff --git a/vertex.vert b/vertex.vert
index c6e1603..9dd5e89 100644
--- a/vertex.vert
+++ b/vertex.vert
@@ -1,13 +1,11 @@
#version 330 core
layout (location = 0) in vec3 aPos;
-layout (location = 1) in vec3 aColor;
-out vec3 ourColor;
-
-uniform mat4 transform;
+uniform mat4 model;
+uniform mat4 view;
+uniform mat4 projection;
void main()
{
- gl_Position = transform * vec4(aPos, 1.0);
- ourColor = aColor;
+ gl_Position = projection * view * model * vec4(aPos, 1.0);
}