Cleaned up code
This commit is contained in:
parent
10bc916711
commit
dfce02cd1c
|
@ -3,7 +3,7 @@ set(VENDOR ${CMAKE_SOURCE_DIR}/vendor)
|
||||||
|
|
||||||
add_executable(example
|
add_executable(example
|
||||||
"main.cpp"
|
"main.cpp"
|
||||||
"objects/Texture.hpp" "objects/Texture.cpp" "objects/primitives/Cube.hpp")
|
)
|
||||||
|
|
||||||
file(GLOB SOURCE_FILES
|
file(GLOB SOURCE_FILES
|
||||||
"${VENDOR}/src/*.c"
|
"${VENDOR}/src/*.c"
|
||||||
|
|
155
src/main.cpp
155
src/main.cpp
|
@ -1,4 +1,5 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
@ -11,10 +12,14 @@
|
||||||
#include <imgui/imgui_impl_glfw.h>
|
#include <imgui/imgui_impl_glfw.h>
|
||||||
#include <imgui/imgui_impl_opengl3.h>
|
#include <imgui/imgui_impl_opengl3.h>
|
||||||
|
|
||||||
|
#include "Camera.hpp"
|
||||||
#include "Shader.hpp"
|
#include "Shader.hpp"
|
||||||
#include "Texture.hpp"
|
#include "Texture.hpp"
|
||||||
#include "Cube.hpp"
|
#include "Cube.hpp"
|
||||||
|
|
||||||
|
int width = 800;
|
||||||
|
int height = 800;
|
||||||
|
|
||||||
void LogGlfwError(const char* message)
|
void LogGlfwError(const char* message)
|
||||||
{
|
{
|
||||||
const char* error;
|
const char* error;
|
||||||
|
@ -46,15 +51,19 @@ void PrintVersionInfo()
|
||||||
|
|
||||||
void FramebufferSizeCallback(GLFWwindow* window, int w, int h)
|
void FramebufferSizeCallback(GLFWwindow* window, int w, int h)
|
||||||
{
|
{
|
||||||
|
width = w;
|
||||||
|
height = h;
|
||||||
glViewport(0, 0, w, h);
|
glViewport(0, 0, w, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
// Initialize ImGui
|
||||||
IMGUI_CHECKVERSION();
|
IMGUI_CHECKVERSION();
|
||||||
ImGui::CreateContext();
|
ImGui::CreateContext();
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
|
|
||||||
|
// Initialize GLFW
|
||||||
int success;
|
int success;
|
||||||
success = glfwInit();
|
success = glfwInit();
|
||||||
if (!success)
|
if (!success)
|
||||||
|
@ -77,129 +86,78 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
|
|
||||||
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
// Load OpenGL Bindings
|
||||||
|
|
||||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||||
{
|
{
|
||||||
std::cerr << "Failed to initialize GLAD" << std::endl;
|
std::cerr << "Failed to initialize GLAD" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Finally initialize ImGui
|
||||||
|
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
||||||
ImGui_ImplOpenGL3_Init((char*)glGetString(GL_NUM_SHADING_LANGUAGE_VERSIONS));
|
ImGui_ImplOpenGL3_Init((char*)glGetString(GL_NUM_SHADING_LANGUAGE_VERSIONS));
|
||||||
|
|
||||||
ImGui::StyleColorsDark();
|
ImGui::StyleColorsDark();
|
||||||
|
|
||||||
PrintVersionInfo();
|
PrintVersionInfo();
|
||||||
|
|
||||||
|
// Set Viewport and callbacks
|
||||||
glViewport(0, 0, 800, 800);
|
glViewport(0, 0, 800, 800);
|
||||||
glfwSetFramebufferSizeCallback(window, FramebufferSizeCallback);
|
glfwSetFramebufferSizeCallback(window, FramebufferSizeCallback);
|
||||||
|
|
||||||
/*
|
|
||||||
Shader shader("basic.vert", "basic.frag");
|
|
||||||
Texture texture1("lauch.jpg");
|
|
||||||
Texture texture2("tomato.jpg");
|
|
||||||
|
|
||||||
shader.Use();
|
|
||||||
shader.SetUniformInt("texture1", 0);
|
|
||||||
shader.SetUniformInt("texture2", 1);
|
|
||||||
|
|
||||||
glm::mat4 model = glm::mat4(1.0f);
|
|
||||||
model = glm::translate(model, glm::vec3(0.f, -0.7f, 0.f));
|
|
||||||
model = glm::rotate(model, glm::radians(-55.f), glm::vec3(1.0f, 0.0f, 0.0f));
|
|
||||||
|
|
||||||
glm::mat4 view = glm::mat4(1.0f);
|
|
||||||
view = glm::translate(view, glm::vec3(0.f, 0.f, -3.f));
|
|
||||||
|
|
||||||
glm::mat4 projection = glm::mat4(1.0f);
|
|
||||||
projection = glm::perspective(glm::radians(45.f), 1.f, 0.1f, 100.0f);
|
|
||||||
|
|
||||||
shader.SetUniformMat4("model", glm::value_ptr(model));
|
|
||||||
shader.SetUniformMat4("view", glm::value_ptr(view));
|
|
||||||
shader.SetUniformMat4("projection", glm::value_ptr(projection));
|
|
||||||
|
|
||||||
float vertices[4 * (3 + 3 + 2)] = {
|
|
||||||
// Position //Color // Texture
|
|
||||||
-0.5f, 0.5f, 1.0f, 0.3f, 1.0f, 0.6f, 0.0f, 1.0f,
|
|
||||||
0.5f, 0.5f, 1.0f, 0.8f, 0.1f, 0.5f, 1.0f, 1.0f,
|
|
||||||
0.5f, -0.5f, 1.0f, 0.5f, 0.6f, 0.2f, 1.0f, 0.0f,
|
|
||||||
-0.5f, -0.5f, 1.0f, 0.2f, 0.7f, 0.8f, 0.0f, 0.0f,
|
|
||||||
};
|
|
||||||
|
|
||||||
unsigned int indices[2 * 3] = {
|
|
||||||
0, 1, 3,
|
|
||||||
1, 2, 3
|
|
||||||
};
|
|
||||||
|
|
||||||
unsigned int VAO, VBO, EBO;
|
|
||||||
glGenVertexArrays(1, &VAO);
|
|
||||||
glGenBuffers(1, &VBO);
|
|
||||||
glGenBuffers(1, &EBO);
|
|
||||||
|
|
||||||
glBindVertexArray(VAO);
|
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, 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, (3 + 3 + 2) * sizeof(float), (void*)0);
|
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, (3 + 3 + 2) * sizeof(float), (void*)(3 * sizeof(float)));
|
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, (3 + 3 + 2) * sizeof(float), (void*)(6 * sizeof(float)));
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
glEnableVertexAttribArray(2);
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
Shader cubeShader("cube.vert", "cube.frag");
|
Shader cubeShader("cube.vert", "cube.frag");
|
||||||
Texture cubeTexture("crate.jpg");
|
|
||||||
cubeShader.Use();
|
cubeShader.Use();
|
||||||
|
|
||||||
glm::mat4 model = glm::mat4(1.0f);
|
|
||||||
|
|
||||||
glm::mat4 view = glm::mat4(1.0f);
|
|
||||||
|
|
||||||
glm::mat4 perspective = glm::mat4(1.0f);
|
glm::mat4 perspective = glm::mat4(1.0f);
|
||||||
|
|
||||||
cubeShader.SetUniformMat4("model", glm::value_ptr(model));
|
Texture cubeTexture("crate.jpg");
|
||||||
cubeShader.SetUniformMat4("view", glm::value_ptr(view));
|
|
||||||
|
|
||||||
cubeShader.SetUniformInt("lauch", 0);
|
cubeShader.SetUniformInt("lauch", 0);
|
||||||
|
|
||||||
Cube cube(0.0f, 0.0f, 0.0f, 0.5f);
|
|
||||||
|
|
||||||
glm::vec3 cubePositions[] = {
|
glm::vec3 cubePositions[] = {
|
||||||
glm::vec3(0.0f, 0.0f, 0.0f),
|
glm::vec3(0.0f, 0.0f, 0.0f),
|
||||||
glm::vec3(2.0f, 5.0f, -15.0f),
|
glm::vec3(2.0f, 5.0f, -7.0f),
|
||||||
glm::vec3(-1.5f, -2.2f, -2.5f),
|
glm::vec3(-1.5f, -2.2f, -2.5f),
|
||||||
glm::vec3(-3.8f, -2.0f, -12.3f),
|
glm::vec3(-3.8f, -2.0f, 6.3f),
|
||||||
glm::vec3(2.4f, -0.4f, -3.5f),
|
glm::vec3(2.4f, -0.4f, 3.5f),
|
||||||
glm::vec3(-1.7f, 3.0f, -7.5f),
|
glm::vec3(-1.7f, 3.0f, -7.5f),
|
||||||
glm::vec3(1.3f, -2.0f, -2.5f),
|
glm::vec3(1.3f, -2.0f, 2.5f),
|
||||||
glm::vec3(1.5f, 2.0f, -2.5f),
|
glm::vec3(1.5f, 2.0f, -2.5f),
|
||||||
glm::vec3(1.5f, 0.2f, -1.5f),
|
glm::vec3(1.5f, 0.2f, -1.5f),
|
||||||
glm::vec3(-1.3f, 1.0f, -1.5f)
|
glm::vec3(-1.3f, 1.0f, 1.5f)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::vector<Cube*> cubes;
|
||||||
|
for (int i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
Cube* newCube = new Cube(cubePositions[i], glm::vec3(0.5f));
|
||||||
|
newCube->Rotate(glm::radians(20.0f * i), glm::vec3(1.0f, 0.3f, 0.5f));
|
||||||
|
|
||||||
|
cubes.push_back(newCube);
|
||||||
|
}
|
||||||
|
|
||||||
|
// A camera
|
||||||
|
Camera cam(glm::vec3(0.0f, 0.0f, 10.0f), glm::vec3(0.0f, 0.0f, 0.0f));
|
||||||
|
|
||||||
|
// So that stuff isnt clipping through each other
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(window))
|
while (!glfwWindowShouldClose(window))
|
||||||
{
|
{
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
float t = glfwGetTime();
|
float t = glfwGetTime();
|
||||||
|
|
||||||
|
static float PI = glm::pi<float>();
|
||||||
|
|
||||||
static float fov = glm::radians(45.0f);
|
static float fov = glm::radians(45.0f);
|
||||||
static float zNear = 0.1f;
|
static float zNear = 0.1f;
|
||||||
static float zFar = 100.0f;
|
static float zFar = 100.0f;
|
||||||
static float camX = 0.0f;
|
|
||||||
static float camY = 0.0f;
|
|
||||||
static float camZ = -3.0f;
|
|
||||||
static float yaw = 0.0f;
|
|
||||||
static float pitch = 0.0f;
|
|
||||||
static float roll = 0.0f;
|
|
||||||
//model = glm::rotate(model, glm::radians(1.0f), glm::vec3(1.0f, 1.0f, 1.0f));
|
|
||||||
|
|
||||||
|
static float orbitSpeed = 1.0f;
|
||||||
|
static float orbitRadius = 10.0f;
|
||||||
|
|
||||||
glClearColor(0.1f, 0.4f, 0.1f, 1.0f);
|
glClearColor(0.1f, 0.4f, 0.1f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
@ -217,40 +175,25 @@ int main(int argc, char** argv)
|
||||||
ImGui::SliderFloat("zFar", &zFar, 10.0f, 100.0f);
|
ImGui::SliderFloat("zFar", &zFar, 10.0f, 100.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ImGui::CollapsingHeader("View"))
|
if (ImGui::CollapsingHeader("Orbit"))
|
||||||
{
|
{
|
||||||
ImGui::SliderFloat("CamX", &camX, -2.0f, 2.0f);
|
ImGui::SliderFloat("Speed", &orbitSpeed, 0.0f, 4 * PI, "%.2f rad/s");
|
||||||
ImGui::SliderFloat("CamY", &camY, -2.0f, 2.0f);
|
ImGui::SliderFloat("Radius", &orbitRadius, 1.0f, 50.0f, "%.1f u");
|
||||||
ImGui::SliderFloat("CamZ", &camZ, -10.0f, 0.0f);
|
|
||||||
ImGui::Separator();
|
|
||||||
ImGui::SliderAngle("Yaw", &yaw, -45.0f, 45.0f, "%.0f°");
|
|
||||||
ImGui::SliderAngle("Pitch", &pitch, -45.0f, 45.0f, "%.0f°");
|
|
||||||
ImGui::SliderAngle("Roll", &roll, -45.0f, 45.0f, "%.0f°");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
perspective = glm::perspective(fov, 1.0f, zNear, zFar);
|
perspective = glm::perspective(fov, (float)width/(float)height, zNear, zFar);
|
||||||
view = glm::mat4(1.0f);
|
|
||||||
view = glm::rotate(view, yaw, glm::vec3(0.f, 1.f, 0.f));
|
|
||||||
view = glm::rotate(view, pitch, glm::vec3(1.f, 0.f, 0.f));
|
|
||||||
view = glm::rotate(view, roll, glm::vec3(0.f, 0.f, 1.f));
|
|
||||||
view = glm::translate(view, glm::vec3(camX, camY, camZ));
|
|
||||||
|
|
||||||
cubeTexture.Bind();
|
cubeTexture.Bind();
|
||||||
cubeShader.Use();
|
cubeShader.Use();
|
||||||
cubeShader.SetUniformMat4("projection", &perspective[0][0]);
|
cubeShader.SetUniformMat4("projection", &perspective[0][0]);
|
||||||
cubeShader.SetUniformMat4("view", &view[0][0]);
|
|
||||||
|
|
||||||
for (int i = 0; i < 10; i++)
|
cam.SetPosition(orbitRadius * glm::vec3(cos(orbitSpeed * t), 0.0f, sin(orbitSpeed * t)));
|
||||||
{
|
cam.Use(cubeShader);
|
||||||
model = glm::mat4(1.0f);
|
|
||||||
model = glm::translate(model, cubePositions[i]);
|
|
||||||
model = glm::rotate(model, glm::radians(20.0f * i), glm::vec3(1.0f, 0.3f, 0.5f));
|
|
||||||
|
|
||||||
cubeShader.SetUniformMat4("model", &model[0][0]);
|
for (Cube* cube : cubes)
|
||||||
cube.Draw(cubeShader);
|
cube->Draw(cubeShader);
|
||||||
}
|
|
||||||
|
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||||
|
|
41
src/objects/Camera.cpp
Normal file
41
src/objects/Camera.cpp
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
#include "Camera.hpp"
|
||||||
|
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
|
Camera::Camera(glm::vec3 position, glm::vec3 target) :
|
||||||
|
position(position), target(target)
|
||||||
|
{
|
||||||
|
CalculateCamera();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::SetPosition(glm::vec3 newPos)
|
||||||
|
{
|
||||||
|
position = newPos;
|
||||||
|
CalculateCamera();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::SetTarget(glm::vec3 newTarget)
|
||||||
|
{
|
||||||
|
target = newTarget;
|
||||||
|
CalculateCamera();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::Move(glm::vec3 movement)
|
||||||
|
{
|
||||||
|
position += movement;
|
||||||
|
CalculateCamera();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::Use(const Shader& program)
|
||||||
|
{
|
||||||
|
program.SetUniformMat4("view", &viewMatrix[0][0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::CalculateCamera()
|
||||||
|
{
|
||||||
|
direction = glm::normalize(target - position);
|
||||||
|
right = glm::normalize(glm::cross(glm::vec3(0.0f, 1.0f, 0.0f), direction));
|
||||||
|
up = glm::cross(direction, right);
|
||||||
|
|
||||||
|
viewMatrix = glm::lookAt(position, target, up);
|
||||||
|
}
|
27
src/objects/Camera.hpp
Normal file
27
src/objects/Camera.hpp
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
#include "Shader.hpp"
|
||||||
|
|
||||||
|
class Camera
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Camera() = default;
|
||||||
|
Camera(glm::vec3 position, glm::vec3 target);
|
||||||
|
|
||||||
|
void SetPosition(glm::vec3 newPos);
|
||||||
|
void SetTarget(glm::vec3 newTarget);
|
||||||
|
|
||||||
|
void Move(glm::vec3 movement);
|
||||||
|
|
||||||
|
void Use(const Shader& program);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void CalculateCamera();
|
||||||
|
|
||||||
|
glm::vec3 position, target;
|
||||||
|
glm::vec3 direction, up, right;
|
||||||
|
|
||||||
|
glm::mat4 viewMatrix;
|
||||||
|
};
|
|
@ -96,17 +96,17 @@ void Shader::Use()
|
||||||
glUseProgram(program);
|
glUseProgram(program);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::SetUniformInt(const char* uniform, int value)
|
void Shader::SetUniformInt(const char* uniform, int value) const
|
||||||
{
|
{
|
||||||
glUniform1i(glGetUniformLocation(program, uniform), value);
|
glUniform1i(glGetUniformLocation(program, uniform), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::SetUniformFloat(const char* uniform, float value)
|
void Shader::SetUniformFloat(const char* uniform, float value) const
|
||||||
{
|
{
|
||||||
glUniform1f(glGetUniformLocation(program, uniform), value);
|
glUniform1f(glGetUniformLocation(program, uniform), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::SetUniformMat4(const char* uniform, const float* ptrToMatrix)
|
void Shader::SetUniformMat4(const char* uniform, const float* ptrToMatrix) const
|
||||||
{
|
{
|
||||||
glUniformMatrix4fv(glGetUniformLocation(program, uniform), 1, GL_FALSE, ptrToMatrix);
|
glUniformMatrix4fv(glGetUniformLocation(program, uniform), 1, GL_FALSE, ptrToMatrix);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,9 +13,9 @@ public:
|
||||||
|
|
||||||
void Use();
|
void Use();
|
||||||
|
|
||||||
void SetUniformInt(const char* uniform, int value);
|
void SetUniformInt(const char* uniform, int value) const;
|
||||||
void SetUniformFloat(const char* uniform, float value);
|
void SetUniformFloat(const char* uniform, float value) const;
|
||||||
void SetUniformMat4(const char* uniform, const float* ptrToMatrix);
|
void SetUniformMat4(const char* uniform, const float* ptrToMatrix) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int program;
|
unsigned int program;
|
||||||
|
|
|
@ -2,79 +2,115 @@
|
||||||
|
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
|
|
||||||
Cube::Cube(float x, float y, float z, float a) :
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
VAO(0), VBO(0), EBO(0)
|
|
||||||
|
const float Cube::VERTICES[36 * (3 + 2)] = {
|
||||||
|
-1.0f, -1.0f, -1.0f, 0.0f, 0.0f,
|
||||||
|
1.0f, -1.0f, -1.0f, 1.0f, 0.0f,
|
||||||
|
1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
|
||||||
|
1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
|
||||||
|
-1.0f, 1.0f, -1.0f, 0.0f, 1.0f,
|
||||||
|
-1.0f, -1.0f, -1.0f, 0.0f, 0.0f,
|
||||||
|
|
||||||
|
-1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
|
||||||
|
1.0f, -1.0f, 1.0f, 1.0f, 0.0f,
|
||||||
|
1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
|
||||||
|
1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
|
||||||
|
-1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
|
||||||
|
-1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
|
||||||
|
|
||||||
|
-1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
|
||||||
|
-1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
|
||||||
|
-1.0f, -1.0f, -1.0f, 0.0f, 1.0f,
|
||||||
|
-1.0f, -1.0f, -1.0f, 0.0f, 1.0f,
|
||||||
|
-1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
|
||||||
|
-1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
|
||||||
|
|
||||||
|
1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
|
||||||
|
1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
|
||||||
|
1.0f, -1.0f, -1.0f, 0.0f, 1.0f,
|
||||||
|
1.0f, -1.0f, -1.0f, 0.0f, 1.0f,
|
||||||
|
1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
|
||||||
|
1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
|
||||||
|
|
||||||
|
-1.0f, -1.0f, -1.0f, 0.0f, 1.0f,
|
||||||
|
1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
|
||||||
|
1.0f, -1.0f, 1.0f, 1.0f, 0.0f,
|
||||||
|
1.0f, -1.0f, 1.0f, 1.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, 1.0f,
|
||||||
|
1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
|
||||||
|
1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
|
||||||
|
1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
|
||||||
|
-1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
|
||||||
|
-1.0f, 1.0f, -1.0f, 0.0f, 1.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
unsigned int Cube::VAO = 0;
|
||||||
|
unsigned int Cube::VBO = 0;
|
||||||
|
|
||||||
|
Cube::Cube(glm::vec3 position, glm::vec3 sidelengths)
|
||||||
{
|
{
|
||||||
float vertices[36 * (3 + 2)] = {
|
if (VAO == 0)
|
||||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
{
|
||||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
|
glGenVertexArrays(1, &VAO);
|
||||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
glGenBuffers(1, &VBO);
|
||||||
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,
|
glBindVertexArray(VAO);
|
||||||
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,
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), VERTICES, GL_STATIC_DRAW);
|
||||||
-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,
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, (3 + 2) * sizeof(float), (void*)0);
|
||||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, (3 + 2) * sizeof(float), (void*)(3 * sizeof(float)));
|
||||||
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
glEnableVertexAttribArray(0);
|
||||||
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
glEnableVertexAttribArray(1);
|
||||||
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
|
|
||||||
};
|
|
||||||
|
|
||||||
glGenVertexArrays(1, &VAO);
|
|
||||||
glGenBuffers(1, &VBO);
|
|
||||||
|
|
||||||
glBindVertexArray(VAO);
|
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, (3 + 2) * sizeof(float), (void*)0);
|
|
||||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, (3 + 2) * sizeof(float), (void*)(3 * sizeof(float)));
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
|
|
||||||
|
ResetTransformation();
|
||||||
|
Move(position);
|
||||||
|
Scale(sidelengths);
|
||||||
}
|
}
|
||||||
|
|
||||||
Cube::~Cube()
|
Cube::~Cube()
|
||||||
{
|
{
|
||||||
glDeleteBuffers(1, &EBO);
|
|
||||||
glDeleteBuffers(1, &VBO);
|
glDeleteBuffers(1, &VBO);
|
||||||
glDeleteVertexArrays(1, &VAO);
|
glDeleteVertexArrays(1, &VAO);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Cube::Draw(const Shader& program)
|
void Cube::Draw(const Shader& program)
|
||||||
{
|
{
|
||||||
|
program.SetUniformMat4("model", &transformation[0][0]);
|
||||||
|
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Cube::ResetTransformation()
|
||||||
|
{
|
||||||
|
transformation = glm::mat4(1.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cube::Move(glm::vec3 movement)
|
||||||
|
{
|
||||||
|
transformation = glm::translate(transformation, movement);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cube::Rotate(float angleInRad, glm::vec3 axis)
|
||||||
|
{
|
||||||
|
transformation = glm::rotate(transformation, angleInRad, axis);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cube::Scale(glm::vec3 scaling)
|
||||||
|
{
|
||||||
|
transformation = glm::scale(transformation, scaling);
|
||||||
|
}
|
||||||
|
|
||||||
|
const float* Cube::GetMatrix() const
|
||||||
|
{
|
||||||
|
return &transformation[0][0];
|
||||||
|
}
|
||||||
|
|
|
@ -2,14 +2,28 @@
|
||||||
|
|
||||||
#include "Shader.hpp"
|
#include "Shader.hpp"
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
class Cube
|
class Cube
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Cube(float x, float y, float z, float a);
|
static const float VERTICES[36 * (3 + 2)];
|
||||||
|
public:
|
||||||
|
Cube(glm::vec3 position, glm::vec3 sidelengths);
|
||||||
~Cube();
|
~Cube();
|
||||||
|
|
||||||
void Draw(const Shader& program);
|
void Draw(const Shader& program);
|
||||||
|
|
||||||
|
// These should probably all go in a base class
|
||||||
|
void ResetTransformation();
|
||||||
|
void Move(glm::vec3 movement);
|
||||||
|
void Rotate(float angleInRad, glm::vec3 axis);
|
||||||
|
void Scale(glm::vec3 scaling);
|
||||||
|
|
||||||
|
const float* GetMatrix() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int VAO, VBO, EBO;
|
static unsigned int VAO, VBO;
|
||||||
|
|
||||||
|
glm::mat4 transformation;
|
||||||
};
|
};
|
Loading…
Reference in a new issue