Initial commit
This commit is contained in:
parent
0889e4de0d
commit
10bc916711
456 changed files with 119668 additions and 0 deletions
33
src/CMakeLists.txt
Normal file
33
src/CMakeLists.txt
Normal file
|
@ -0,0 +1,33 @@
|
|||
|
||||
set(VENDOR ${CMAKE_SOURCE_DIR}/vendor)
|
||||
|
||||
add_executable(example
|
||||
"main.cpp"
|
||||
"objects/Texture.hpp" "objects/Texture.cpp" "objects/primitives/Cube.hpp")
|
||||
|
||||
file(GLOB SOURCE_FILES
|
||||
"${VENDOR}/src/*.c"
|
||||
"${VENDOR}/src/*.cpp"
|
||||
"objects/*.cpp"
|
||||
"objects/primitives/*.cpp"
|
||||
)
|
||||
|
||||
target_sources(example PRIVATE
|
||||
${SOURCE_FILES}
|
||||
)
|
||||
|
||||
target_include_directories(example PRIVATE
|
||||
glfw
|
||||
${VENDOR}/include
|
||||
${VENDOR}/include/imgui
|
||||
objects
|
||||
objects/primitives
|
||||
)
|
||||
|
||||
target_link_libraries(example
|
||||
glfw
|
||||
)
|
||||
|
||||
add_custom_command(TARGET example POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/assets $<TARGET_FILE_DIR:example>/assets
|
||||
)
|
264
src/main.cpp
Normal file
264
src/main.cpp
Normal file
|
@ -0,0 +1,264 @@
|
|||
#include <iostream>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <imgui/imgui.h>
|
||||
#include <imgui/imgui_impl_glfw.h>
|
||||
#include <imgui/imgui_impl_opengl3.h>
|
||||
|
||||
#include "Shader.hpp"
|
||||
#include "Texture.hpp"
|
||||
#include "Cube.hpp"
|
||||
|
||||
void LogGlfwError(const char* message)
|
||||
{
|
||||
const char* error;
|
||||
int code = glfwGetError(&error);
|
||||
std::cerr << message << std::endl
|
||||
<< "(" << code << ") " << error << std::endl;
|
||||
}
|
||||
|
||||
void PrintVersionInfo()
|
||||
{
|
||||
int glfwMajor, glfwMinor, glfwRev;
|
||||
glfwGetVersion(&glfwMajor, &glfwMinor, &glfwRev);
|
||||
|
||||
const char* vendor = (const char*)glGetString(GL_VENDOR);
|
||||
const char* renderer = (const char*)glGetString(GL_RENDERER);
|
||||
const char* version = (const char*)glGetString(GL_VERSION);
|
||||
const char* glslVersion = (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
|
||||
const char* extensions = (const char*)glGetString(GL_EXTENSIONS);
|
||||
|
||||
std::cout << "Using GLFW " << glfwMajor << "." << glfwMinor << "." << glfwRev << std::endl;
|
||||
std::cout << "GPU Vendor: " << vendor << std::endl;
|
||||
std::cout << "GPU Model: " << renderer << std::endl;
|
||||
std::cout << "OpenGL Version: " << version << std::endl;
|
||||
std::cout << "GLSL Version: " << glslVersion << std::endl;
|
||||
if (extensions != nullptr)
|
||||
std::cout << "Loaded extensions: " << extensions << std::endl;
|
||||
std::cout << "----------------------------------------" << std::endl;
|
||||
}
|
||||
|
||||
void FramebufferSizeCallback(GLFWwindow* window, int w, int h)
|
||||
{
|
||||
glViewport(0, 0, w, h);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
int success;
|
||||
success = glfwInit();
|
||||
if (!success)
|
||||
{
|
||||
LogGlfwError("Failed to initialize GLFW");
|
||||
return -1;
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
GLFWwindow* window;
|
||||
window = glfwCreateWindow(800, 800, "OpenGL Test", NULL, NULL);
|
||||
if (window == nullptr)
|
||||
{
|
||||
LogGlfwError("Failed to create GLFWwindow");
|
||||
return -1;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||
{
|
||||
std::cerr << "Failed to initialize GLAD" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ImGui_ImplOpenGL3_Init((char*)glGetString(GL_NUM_SHADING_LANGUAGE_VERSIONS));
|
||||
|
||||
ImGui::StyleColorsDark();
|
||||
|
||||
PrintVersionInfo();
|
||||
|
||||
glViewport(0, 0, 800, 800);
|
||||
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");
|
||||
Texture cubeTexture("crate.jpg");
|
||||
cubeShader.Use();
|
||||
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
|
||||
glm::mat4 view = glm::mat4(1.0f);
|
||||
|
||||
glm::mat4 perspective = glm::mat4(1.0f);
|
||||
|
||||
cubeShader.SetUniformMat4("model", glm::value_ptr(model));
|
||||
cubeShader.SetUniformMat4("view", glm::value_ptr(view));
|
||||
|
||||
cubeShader.SetUniformInt("lauch", 0);
|
||||
|
||||
Cube cube(0.0f, 0.0f, 0.0f, 0.5f);
|
||||
|
||||
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)
|
||||
};
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
glfwPollEvents();
|
||||
|
||||
float t = glfwGetTime();
|
||||
static float fov = glm::radians(45.0f);
|
||||
static float zNear = 0.1f;
|
||||
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));
|
||||
|
||||
glClearColor(0.1f, 0.4f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
// Render ImGui
|
||||
ImGui::Begin("Rendering Controls");
|
||||
|
||||
if (ImGui::CollapsingHeader("Projection"))
|
||||
{
|
||||
ImGui::SliderAngle("FOV", &fov, 10.0f, 120.0f, "%.0f°");
|
||||
ImGui::SliderFloat("zNear", &zNear, 0.1f, 10.0f);
|
||||
ImGui::SliderFloat("zFar", &zFar, 10.0f, 100.0f);
|
||||
}
|
||||
|
||||
if (ImGui::CollapsingHeader("View"))
|
||||
{
|
||||
ImGui::SliderFloat("CamX", &camX, -2.0f, 2.0f);
|
||||
ImGui::SliderFloat("CamY", &camY, -2.0f, 2.0f);
|
||||
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();
|
||||
|
||||
perspective = glm::perspective(fov, 1.0f, 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();
|
||||
cubeShader.Use();
|
||||
cubeShader.SetUniformMat4("projection", &perspective[0][0]);
|
||||
cubeShader.SetUniformMat4("view", &view[0][0]);
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
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]);
|
||||
cube.Draw(cubeShader);
|
||||
}
|
||||
|
||||
ImGui::Render();
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
|
||||
return 0;
|
||||
}
|
112
src/objects/Shader.cpp
Normal file
112
src/objects/Shader.cpp
Normal file
|
@ -0,0 +1,112 @@
|
|||
#include "Shader.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
void Shader::LoadShaderSource(const char* filepath, char** buffer)
|
||||
{
|
||||
std::string t = std::string(SHADER_DIR) + std::string(filepath);
|
||||
std::ifstream file(std::string(SHADER_DIR) + std::string(filepath));
|
||||
if (!file.good())
|
||||
{
|
||||
std::cerr << "Could not open file: " << filepath << std::endl;
|
||||
*buffer = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
std::string source(
|
||||
(std::istreambuf_iterator<char>(file)),
|
||||
std::istreambuf_iterator<char>()
|
||||
);
|
||||
size_t size = source.size();
|
||||
*buffer = (char*)malloc(size + sizeof(char));
|
||||
if (*buffer == nullptr)
|
||||
{
|
||||
std::cerr << "Failed to allocate memory for shader source code" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy_s(*buffer, size, source.c_str(), size);
|
||||
(*buffer)[size] = '\0';
|
||||
}
|
||||
|
||||
Shader::Shader(const char* vertShader, const char* fragShader) :
|
||||
program(0)
|
||||
{
|
||||
int success;
|
||||
char error[512];
|
||||
|
||||
// Load Vertex Shader
|
||||
unsigned int vert = glCreateShader(GL_VERTEX_SHADER);
|
||||
char* buffer;
|
||||
LoadShaderSource(vertShader, &buffer);
|
||||
if (buffer == nullptr)
|
||||
buffer = "";
|
||||
glShaderSource(vert, 1, &buffer, NULL);
|
||||
glCompileShader(vert);
|
||||
glGetShaderiv(vert, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(vert, 512, NULL, error);
|
||||
std::cerr << error << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Load Fragment Shader
|
||||
unsigned int frag = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
LoadShaderSource(fragShader, &buffer);
|
||||
if (buffer == nullptr)
|
||||
buffer = "";
|
||||
glShaderSource(frag, 1, &buffer, NULL);
|
||||
glCompileShader(frag);
|
||||
glGetShaderiv(frag, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(frag, 512, NULL, error);
|
||||
std::cerr << error << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Link Shaders
|
||||
program = glCreateProgram();
|
||||
glAttachShader(program, vert);
|
||||
glAttachShader(program, frag);
|
||||
glLinkProgram(program);
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetProgramInfoLog(program, 512, NULL, error);
|
||||
std::cerr << error << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
glDeleteShader(frag);
|
||||
glDeleteShader(vert);
|
||||
}
|
||||
|
||||
Shader::~Shader()
|
||||
{
|
||||
glDeleteProgram(program);
|
||||
}
|
||||
|
||||
void Shader::Use()
|
||||
{
|
||||
glUseProgram(program);
|
||||
}
|
||||
|
||||
void Shader::SetUniformInt(const char* uniform, int value)
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(program, uniform), value);
|
||||
}
|
||||
|
||||
void Shader::SetUniformFloat(const char* uniform, float value)
|
||||
{
|
||||
glUniform1f(glGetUniformLocation(program, uniform), value);
|
||||
}
|
||||
|
||||
void Shader::SetUniformMat4(const char* uniform, const float* ptrToMatrix)
|
||||
{
|
||||
glUniformMatrix4fv(glGetUniformLocation(program, uniform), 1, GL_FALSE, ptrToMatrix);
|
||||
}
|
22
src/objects/Shader.hpp
Normal file
22
src/objects/Shader.hpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#define SHADER_DIR "assets/shaders/"
|
||||
|
||||
class Shader
|
||||
{
|
||||
public:
|
||||
static void LoadShaderSource(const char* filepath, char** buffer);
|
||||
|
||||
public:
|
||||
Shader(const char* vertShader, const char* fragShader);
|
||||
~Shader();
|
||||
|
||||
void Use();
|
||||
|
||||
void SetUniformInt(const char* uniform, int value);
|
||||
void SetUniformFloat(const char* uniform, float value);
|
||||
void SetUniformMat4(const char* uniform, const float* ptrToMatrix);
|
||||
|
||||
private:
|
||||
unsigned int program;
|
||||
};
|
44
src/objects/Texture.cpp
Normal file
44
src/objects/Texture.cpp
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include "Texture.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <stb_image.h>
|
||||
|
||||
Texture::Texture(const char* name) :
|
||||
width(0), height(0), nrChannels(0), ID(0)
|
||||
{
|
||||
glGenTextures(1, &ID);
|
||||
glBindTexture(GL_TEXTURE_2D, ID);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
unsigned char* data = stbi_load((TEXTURE_DIR + std::string(name)).c_str(), &width, &height, &nrChannels, 0);
|
||||
if (data)
|
||||
{
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Failed to load texture: " << name << std::endl;
|
||||
}
|
||||
|
||||
|
||||
stbi_image_free(data);
|
||||
}
|
||||
|
||||
Texture::~Texture()
|
||||
{
|
||||
glDeleteTextures(1, &ID);
|
||||
}
|
||||
|
||||
void Texture::Bind()
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, ID);
|
||||
}
|
18
src/objects/Texture.hpp
Normal file
18
src/objects/Texture.hpp
Normal file
|
@ -0,0 +1,18 @@
|
|||
#pragma once
|
||||
|
||||
#define TEXTURE_DIR "assets/textures/"
|
||||
|
||||
class Texture
|
||||
{
|
||||
public:
|
||||
Texture(const char* name);
|
||||
~Texture();
|
||||
|
||||
void Bind();
|
||||
|
||||
private:
|
||||
int width, height, nrChannels;
|
||||
|
||||
public:
|
||||
unsigned int ID;
|
||||
};
|
80
src/objects/primitives/Cube.cpp
Normal file
80
src/objects/primitives/Cube.cpp
Normal file
|
@ -0,0 +1,80 @@
|
|||
#include "Cube.hpp"
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
Cube::Cube(float x, float y, float z, float a) :
|
||||
VAO(0), VBO(0), EBO(0)
|
||||
{
|
||||
float vertices[36 * (3 + 2)] = {
|
||||
-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
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
Cube::~Cube()
|
||||
{
|
||||
glDeleteBuffers(1, &EBO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
}
|
||||
|
||||
void Cube::Draw(const Shader& program)
|
||||
{
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
15
src/objects/primitives/Cube.hpp
Normal file
15
src/objects/primitives/Cube.hpp
Normal file
|
@ -0,0 +1,15 @@
|
|||
#pragma once
|
||||
|
||||
#include "Shader.hpp"
|
||||
|
||||
class Cube
|
||||
{
|
||||
public:
|
||||
Cube(float x, float y, float z, float a);
|
||||
~Cube();
|
||||
|
||||
void Draw(const Shader& program);
|
||||
|
||||
private:
|
||||
unsigned int VAO, VBO, EBO;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue