Add EBO
This commit is contained in:
@@ -33,12 +33,25 @@ Controller::~Controller() {
|
||||
void Controller::run() {
|
||||
glClearColor(0.15f, 0.15f, 0.15f, 1.0f);
|
||||
|
||||
shaderProgram = new ShaderProgram("res/shaders/basic.vs", "res/shaders/basic.fs");
|
||||
shaderProgram->bind();
|
||||
ShaderProgram shaderProgram("res/shaders/basic.vs", "res/shaders/basic.fs");
|
||||
shaderProgram.bind();
|
||||
|
||||
Vertex vertices[] = {
|
||||
Vertex{-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f},
|
||||
Vertex{0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
|
||||
Vertex{-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f},
|
||||
Vertex{0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f}
|
||||
};
|
||||
|
||||
uint32_t indices[] = {
|
||||
0, 1, 2,
|
||||
1, 2, 3
|
||||
};
|
||||
|
||||
uint32_t numVertices = sizeof(vertices) / sizeof(Vertex);
|
||||
uint32_t numIndices = sizeof(indices) / sizeof(indices[0]);
|
||||
|
||||
vertexBuffer = new VertexBuffer(vertices, numVertices);
|
||||
VertexBuffer vertexBuffer(vertices, indices, numVertices, numIndices);
|
||||
|
||||
// This is the game loop
|
||||
while(!glfwWindowShouldClose(gameWindow->getGLFWwindow())) {
|
||||
@@ -56,9 +69,9 @@ void Controller::run() {
|
||||
// Render and buffer swap
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
vertexBuffer->bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
vertexBuffer->unbind();
|
||||
vertexBuffer.bind();
|
||||
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0);
|
||||
vertexBuffer.unbind();
|
||||
|
||||
glfwSwapBuffers(gameWindow->getGLFWwindow());
|
||||
|
||||
|
||||
@@ -28,13 +28,4 @@ private:
|
||||
const uint16_t MAX_FPS = 60;
|
||||
double deltaTime;
|
||||
|
||||
ShaderProgram *shaderProgram;
|
||||
VertexBuffer *vertexBuffer;
|
||||
|
||||
Vertex vertices[3] = {
|
||||
Vertex{-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f},
|
||||
Vertex{0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
|
||||
Vertex{0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
@@ -3,14 +3,18 @@
|
||||
#include "VertexBuffer.h"
|
||||
#include "defines.h"
|
||||
|
||||
VertexBuffer::VertexBuffer(void* data, uint32_t numVertices) {
|
||||
VertexBuffer::VertexBuffer(void *vertexData, void *indexData, uint32_t numVertices, uint32_t numIndices) {
|
||||
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
glGenBuffers(1, &bufferId);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
|
||||
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(Vertex), data, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(Vertex), vertexData, GL_STATIC_DRAW);
|
||||
|
||||
glGenBuffers(1, &EBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(uint32_t), indexData, GL_STATIC_DRAW);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, x));
|
||||
|
||||
@@ -6,7 +6,7 @@ class VertexBuffer {
|
||||
|
||||
public:
|
||||
|
||||
VertexBuffer(void* data, uint32_t numVertices);
|
||||
VertexBuffer(void *vertexData, void *indexData, uint32_t numVertices, uint32_t numIndices);
|
||||
~VertexBuffer();
|
||||
|
||||
void bind();
|
||||
@@ -16,5 +16,6 @@ private:
|
||||
|
||||
GLuint bufferId;
|
||||
GLuint VAO;
|
||||
GLuint EBO;
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user