From 6cbf05fce9821efdbbb141c6a0dbff6e6745fbc0 Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Tue, 18 Sep 2018 11:09:23 +0100 Subject: [PATCH] Add yellow shader --- assignment1.cpp | 78 ++++++++++++++++++++++++++++--------------------- yellow.glsl | 5 ++++ 2 files changed, 50 insertions(+), 33 deletions(-) create mode 100644 yellow.glsl diff --git a/assignment1.cpp b/assignment1.cpp index fd55f4e..b3049f0 100644 --- a/assignment1.cpp +++ b/assignment1.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #ifdef __APPLE__ #include #else @@ -14,10 +15,12 @@ using namespace std; GLuint* vaos; +GLuint* progIds; void display() { glClear(GL_COLOR_BUFFER_BIT); for (int i = 0; i < 2; i++) { + glUseProgram(progIds[i]); glBindVertexArray(vaos[i]); glDrawArrays(GL_TRIANGLES, 0, 3); } @@ -50,11 +53,11 @@ void attachShader(GLuint progId, const char* filePath, GLenum type) { glAttachShader(progId, shader); } -GLuint compileShaders() { +GLuint compileShaders(char* vertexShader, char* fragmentShader) { GLuint progId = glCreateProgram(); - attachShader(progId, "vertex.glsl", GL_VERTEX_SHADER); - attachShader(progId, "fragment.glsl", GL_FRAGMENT_SHADER); + attachShader(progId, vertexShader, GL_VERTEX_SHADER); + attachShader(progId, fragmentShader, GL_FRAGMENT_SHADER); glLinkProgram(progId); GLint success = 0; @@ -66,26 +69,12 @@ GLuint compileShaders() { exit(1); } - glUseProgram(progId); return progId; } #define BUFFER_OFFSET(i) ((char *)NULL + (i)) -GLuint* setupBuffers(GLuint progId) { - GLfloat vertices[2][9] = { - { - 0.0f, -1.0f, 0.0f, - 1.0f, -1.0f, 0.0f, - 0.5f, 1.0f, 0.0f - }, - - { - -1.0f, -1.0f, 0.0f, - 0.0f, -1.0f, 0.0f, - -0.5f, 1.0f, 0.0f - } - }; +GLuint setupBuffers(GLfloat* vertices, GLuint progId) { GLfloat colors[] = { 0, 1, 0, 1, @@ -95,11 +84,11 @@ GLuint* setupBuffers(GLuint progId) { GLuint numVerts = 3; - GLuint vbos[2]; - glGenBuffers(2, vbos); + GLuint vbo; + glGenBuffers(1, &vbo); - GLuint* vaos = new GLuint[2]; - glGenVertexArrays(2, vaos); + GLuint vao; + glGenVertexArrays(1, &vao); GLuint posId = glGetAttribLocation(progId, "vPosition"); GLuint colorId = glGetAttribLocation(progId, "vColor"); @@ -107,30 +96,24 @@ GLuint* setupBuffers(GLuint progId) { GLuint vertsLen = numVerts * 3 * sizeof(GLfloat); GLuint colorsLen = numVerts * 4 * sizeof(GLfloat); - - for(int i = 0; i < 2; i++) { - glBindBuffer(GL_ARRAY_BUFFER, vbos[i]); + glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, vertsLen + colorsLen, NULL, GL_STATIC_DRAW); - glBufferSubData(GL_ARRAY_BUFFER, 0, vertsLen, vertices[i]); + glBufferSubData(GL_ARRAY_BUFFER, 0, vertsLen, vertices); glBufferSubData(GL_ARRAY_BUFFER, vertsLen, colorsLen, colors); - glBindVertexArray(vaos[i]); + glBindVertexArray(vao); glEnableVertexAttribArray(posId); glEnableVertexAttribArray(colorId); glVertexAttribPointer(posId, 3, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(colorId, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVerts * 3 * sizeof(GLfloat))); - } - return vaos; + return vao; } -void init() { - GLuint progId = compileShaders(); - vaos = setupBuffers(progId); - +void validateProgram(GLuint progId) { glValidateProgram(progId); GLint success; @@ -143,6 +126,35 @@ void init() { } } +void init() { + GLfloat vertices[2][9] = { + { + 0.0f, -1.0f, 0.0f, + 1.0f, -1.0f, 0.0f, + 0.5f, 1.0f, 0.0f + }, + + { + -1.0f, -1.0f, 0.0f, + 0.0f, -1.0f, 0.0f, + -0.5f, 1.0f, 0.0f + } + }; + + vaos = new GLuint[2]; + progIds = new GLuint[2]; + + GLuint progId1 = compileShaders((char*)"vertex.glsl", (char*)"fragment.glsl"); + vaos[0] = setupBuffers(vertices[0], progId1); + progIds[0] = progId1; + validateProgram(progId1); + + GLuint progId2 = compileShaders((char*)"vertex.glsl", (char*)"yellow.glsl"); + vaos[1] = setupBuffers(vertices[1], progId2); + progIds[1] = progId2; + validateProgram(progId2); +} + int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE); diff --git a/yellow.glsl b/yellow.glsl new file mode 100644 index 0000000..d1de908 --- /dev/null +++ b/yellow.glsl @@ -0,0 +1,5 @@ +#version 330 +out vec4 FragColor; +void main() { + FragColor = vec4(1.0, 1.0, 0.0, 1.0); +} -- 2.30.2