uniforms
This commit is contained in:
Binary file not shown.
@@ -1,7 +1,10 @@
|
|||||||
#version 150 core
|
#version 130
|
||||||
|
|
||||||
out vec4 outColor;
|
in vec3 Colour;
|
||||||
|
out vec4 outColour;
|
||||||
|
|
||||||
|
uniform vec3 triangleColour;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
outColor = vec4(1.0, 1.0, 1.0, 1.0);
|
outColour = vec4(Colour, 1.0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
#version 150 core
|
#version 130
|
||||||
|
|
||||||
in vec2 position;
|
in vec2 position;
|
||||||
|
in vec3 colour;
|
||||||
|
|
||||||
|
out vec3 Colour;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
Colour = colour;
|
||||||
gl_Position = vec4(position, 0.0, 1.0);
|
gl_Position = vec4(position, 0.0, 1.0);
|
||||||
// Equivilent to vec4(position.x, position.y, 0.0, 1.0)
|
// Equivilent to vec4(position.x, position.y, 0.0, 1.0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,23 @@
|
|||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <chrono>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
|
void GLAPIENTRY MessageCallback(GLenum source,
|
||||||
|
GLenum type,
|
||||||
|
GLuint id,
|
||||||
|
GLenum severity,
|
||||||
|
GLsizei length,
|
||||||
|
const GLchar* message,
|
||||||
|
const void* userParam) {
|
||||||
|
fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
|
||||||
|
(type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ),
|
||||||
|
type, severity, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class Game {
|
class Game {
|
||||||
public:
|
public:
|
||||||
SDL_Window* window = nullptr;
|
SDL_Window* window = nullptr;
|
||||||
@@ -108,13 +122,19 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
game->isWindowClosed = false;
|
game->isWindowClosed = false;
|
||||||
|
|
||||||
|
// Set GL error handling callback
|
||||||
|
// glEnable(GL_DEBUG_OUTPUT);
|
||||||
|
glDebugMessageCallback(MessageCallback, 0);
|
||||||
|
|
||||||
|
// Set GL clear colour (light blue)
|
||||||
glClearColor(0.1f, 0.45f, 0.9f, 1.0f);
|
glClearColor(0.1f, 0.45f, 0.9f, 1.0f);
|
||||||
|
|
||||||
// GL Screen coordinates of a 2D triangle
|
// GL Screen coordinates of a 2D triangle followed by the colour
|
||||||
|
GLint numOfVerticies = 15;
|
||||||
float vertices[] = {
|
float vertices[] = {
|
||||||
0.0f, 0.5f, // Vertex 1 (X, Y)
|
0.0f, 0.5f, 1.0f, 0.0f, 0.0f, // Vertex 1: (X, Y, R, G, B)
|
||||||
0.5f, -0.5f, // Vertex 2 (X, Y)
|
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // Vertex 2: (X, Y, R, G, B)
|
||||||
-0.5f, -0.5f // Vertex 3 (X, Y)
|
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f // Vertex 3: (X, Y, R, G, B)
|
||||||
};
|
};
|
||||||
|
|
||||||
// Generate a vertex array object
|
// Generate a vertex array object
|
||||||
@@ -129,7 +149,7 @@ int main(int argc, char** argv) {
|
|||||||
// Binding buffer to GPU
|
// Binding buffer to GPU
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||||
// Copy vertex data to the vertex buffer already on the GPU
|
// Copy vertex data to the vertex buffer already on the GPU
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * 6, vertices, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * numOfVerticies, vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
// Load, compile, apply and link shader programs
|
// Load, compile, apply and link shader programs
|
||||||
if (!loadShader(readShader("shaders/simple.vert"), GL_VERTEX_SHADER , "simpleVert") ||
|
if (!loadShader(readShader("shaders/simple.vert"), GL_VERTEX_SHADER , "simpleVert") ||
|
||||||
@@ -137,22 +157,28 @@ int main(int argc, char** argv) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
makeShaderProgram("simpleVert", "simpleFrag", "simpleProc");
|
makeShaderProgram("simpleVert", "simpleFrag", "simpleProc");
|
||||||
glBindFragDataLocation(ShaderPrograms["simpleProc"], 0, "outColor");
|
|
||||||
applyShader("simpleProc");
|
applyShader("simpleProc");
|
||||||
|
|
||||||
GLint posAttrib = glGetAttribLocation(ShaderPrograms["simpleProc"], "position");
|
GLint posAttrib = glGetAttribLocation(ShaderPrograms["simpleProc"], "position");
|
||||||
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
|
||||||
glEnableVertexAttribArray(posAttrib);
|
glEnableVertexAttribArray(posAttrib);
|
||||||
|
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE,
|
||||||
|
5*sizeof(float), 0);
|
||||||
|
|
||||||
|
GLint colAttrib = glGetAttribLocation(ShaderPrograms["simpleProc"], "colour");
|
||||||
|
glEnableVertexAttribArray(colAttrib);
|
||||||
|
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE,
|
||||||
|
5*sizeof(float), (void*)(2*sizeof(float)));
|
||||||
|
|
||||||
|
GLint triangleUniColour = glGetUniformLocation(ShaderPrograms["simpleProc"], "triangleColour");
|
||||||
|
glUniform3f(triangleUniColour, 0.2f, 0.8f, 0.3f);
|
||||||
|
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (!game->isWindowClosed) {
|
while (!game->isWindowClosed) {
|
||||||
// Input handling
|
// Input handling
|
||||||
while (SDL_PollEvent(&event) != 0)
|
while (SDL_PollEvent(&event) != 0)
|
||||||
if (event.key.keysym.sym == SDLK_ESCAPE)
|
if (event.key.keysym.sym == SDLK_ESCAPE || event.type == SDL_QUIT)
|
||||||
game->isWindowClosed = true;
|
game->isWindowClosed = true;
|
||||||
|
|
||||||
// Render
|
|
||||||
|
|
||||||
// Clear the screen
|
// Clear the screen
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
// Draw a triangle from the 3 vertices
|
// Draw a triangle from the 3 vertices
|
||||||
|
|||||||
Reference in New Issue
Block a user