Messed about with shaders to get coloured triangles

This commit is contained in:
Warwick 2021-03-15 16:58:12 +00:00
parent 0e7bb764a6
commit 66226f6c62
3 changed files with 33 additions and 12 deletions

View file

@ -1,6 +1,9 @@
#version 330 core #version 330 core
out vec4 FragColor; out vec4 FragColor;
in vec3 ourColor;
void main() void main()
{ {
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); FragColor = vec4(ourColor, 1.0);
} }

View file

@ -8,6 +8,7 @@
// File reader // File reader
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <string>
// Include error class // Include error class
#include "Error.h" #include "Error.h"
@ -65,6 +66,13 @@ int main(int argc, char **argv) {
SDL_GLContext glContext = SDL_GL_CreateContext(window); SDL_GLContext glContext = SDL_GL_CreateContext(window);
// TODO: Test that glContext was created successfully // TODO: Test that glContext was created successfully
// Tell us the number of vertex attributes allowed in bug reports
//
int nrAttributes;
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes);
error.log("Maximum num of vertex attributes supported: " +
std::to_string(nrAttributes));
// Initialise Glew // Initialise Glew
if (glewInit() != GLEW_OK) { if (glewInit() != GLEW_OK) {
error.crash("Unable to initialise GLEW (OpenGL Extention Wrangler)"); error.crash("Unable to initialise GLEW (OpenGL Extention Wrangler)");
@ -75,10 +83,10 @@ int main(int argc, char **argv) {
// test square! // test square!
float vertices[] = { float vertices[] = {
0.5f, 0.5f, 0.0f, // top right // positions // colors
0.5f, -0.5f, 0.0f, // bottom right 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top
}; };
unsigned int indices[] = { unsigned int indices[] = {
// note that we start from 0! // note that we start from 0!
@ -119,9 +127,9 @@ int main(int argc, char **argv) {
// Get Vertex shader src // Get Vertex shader src
// https://badvertex.com/2012/11/20/how-to-load-a-glsl-shader-in-opengl-using-c.html // https://badvertex.com/2012/11/20/how-to-load-a-glsl-shader-in-opengl-using-c.html
std::string vertShaderStr = readFile("vertex.glsl"); std::string vertShaderStr = readFile("src/vertex.glsl");
const char *vertShaderSrc = vertShaderStr.c_str(); const char *vertShaderSrc = vertShaderStr.c_str();
std::string fragShaderStr = readFile("fragment.glsl"); std::string fragShaderStr = readFile("src/fragment.glsl");
const char *fragShaderSrc = fragShaderStr.c_str(); const char *fragShaderSrc = fragShaderStr.c_str();
// create vertex shader // create vertex shader
@ -154,9 +162,14 @@ int main(int argc, char **argv) {
error.crash("fragment shader compilation failed", infoLog); error.crash("fragment shader compilation failed", infoLog);
} }
// Tell openGL how to interpret vertex data // Tell openGL how to interpret shader data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); // position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
(void *)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// Create the shader program // Create the shader program
unsigned int shaderProgram; unsigned int shaderProgram;
@ -200,8 +213,8 @@ int main(int argc, char **argv) {
// TODO: Run game here lol // TODO: Run game here lol
// Draw triangle // Draw triangle
// glDrawArrays(GL_TRIANGLES, 0, 3); glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0); glBindVertexArray(0);
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);

View file

@ -1,6 +1,11 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
out vec3 ourColor;
void main() void main()
{ {
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
}; };