Triangle with colors
This commit is contained in:
@@ -10,9 +10,6 @@
|
||||
#endif
|
||||
|
||||
#include "Controller.h"
|
||||
#include "ShaderProgram.h"
|
||||
#include "VertexBuffer.h"
|
||||
#include "defines.h"
|
||||
|
||||
Controller::Controller() {
|
||||
if(!glfwInit()) exit(-1);
|
||||
@@ -34,39 +31,34 @@ Controller::~Controller() {
|
||||
}
|
||||
|
||||
void Controller::run() {
|
||||
glClearColor(0.241f, 0.578f, 0.308f, 1.0f);
|
||||
glClearColor(0.15f, 0.15f, 0.15f, 1.0f);
|
||||
|
||||
ShaderProgram shaderProgram("res/shaders/old.vs", "res/shaders/old.fs");
|
||||
shaderProgram.bind();
|
||||
shaderProgram = new 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, 0.0f},
|
||||
Vertex{0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
|
||||
Vertex{0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}
|
||||
};
|
||||
uint32_t numVertices = sizeof(vertices) / sizeof(Vertex);
|
||||
|
||||
VertexBuffer vertexBuffer(vertices, numVertices);
|
||||
vertexBuffer.unbind();
|
||||
vertexBuffer = new VertexBuffer(vertices, numVertices);
|
||||
|
||||
// This is the game loop
|
||||
while(!glfwWindowShouldClose(gameWindow->getGLFWwindow())) {
|
||||
// Timing
|
||||
limit_framerate();
|
||||
static int slowdown = 0;
|
||||
if(slowdown++ == 60) {
|
||||
static int frameCount = 55;
|
||||
if(frameCount++ == 60) {
|
||||
std::cout << "FPS: " << 1/deltaTime << std::endl;
|
||||
slowdown = 0;
|
||||
frameCount = 0;
|
||||
}
|
||||
|
||||
// Update game
|
||||
// ...
|
||||
|
||||
// Render and buffer swap
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
vertexBuffer.bind();
|
||||
vertexBuffer->bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
vertexBuffer.unbind();
|
||||
vertexBuffer->unbind();
|
||||
|
||||
glfwSwapBuffers(gameWindow->getGLFWwindow());
|
||||
|
||||
@@ -76,11 +68,6 @@ void Controller::run() {
|
||||
|
||||
}
|
||||
|
||||
void Controller::error_callback(int error, const char* description) {
|
||||
(void)error;
|
||||
fprintf(stderr, "Error: %s\n", description);
|
||||
}
|
||||
|
||||
void Controller::limit_framerate() {
|
||||
static double startingTime = 0.0;
|
||||
static double lastTime = 0.0;
|
||||
@@ -101,3 +88,9 @@ void Controller::limit_framerate() {
|
||||
|
||||
startingTime = glfwGetTime();
|
||||
}
|
||||
|
||||
// GLFW error function
|
||||
void Controller::error_callback(int error, const char* description) {
|
||||
(void)error;
|
||||
fprintf(stderr, "Error: %s\n", description);
|
||||
}
|
||||
|
||||
13
Controller.h
13
Controller.h
@@ -4,6 +4,9 @@
|
||||
|
||||
#include "Window.h"
|
||||
#include "EventHandler.h"
|
||||
#include "ShaderProgram.h"
|
||||
#include "VertexBuffer.h"
|
||||
#include "defines.h"
|
||||
|
||||
class Controller {
|
||||
|
||||
@@ -23,7 +26,15 @@ private:
|
||||
EventHandler *gameEventHandler;
|
||||
|
||||
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}
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
@@ -4,18 +4,20 @@
|
||||
#include "defines.h"
|
||||
|
||||
VertexBuffer::VertexBuffer(void* data, uint32_t numVertices) {
|
||||
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
|
||||
|
||||
glGenBuffers(1, &bufferId);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
|
||||
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(Vertex), data, GL_STATIC_DRAW);
|
||||
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, x));
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, r));
|
||||
|
||||
// This will also unbind the vertex buffer
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
void main() {
|
||||
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec3 aPos;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||
}
|
||||
Reference in New Issue
Block a user