From 4a2c752bc02e5152e1131a6bda4441c6d7e950f6 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Wed, 17 Oct 2018 10:22:24 +0200 Subject: [PATCH] Impl 1.2.1 --- .vscode/tasks.json | 2 +- exercise1/glsl/shader.frag | 4 +- exercise1/glsl/shader.vert | 4 +- exercise1/src/Viewer.cpp | 104 +++++++++++++++++++++++++------------ 4 files changed, 77 insertions(+), 37 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index a9d78c3..c0fe4d4 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -6,7 +6,7 @@ { "label": "Build All Exercises", "type": "shell", - "command": "mkdir -p build && cd build && cmake .. && make -j 8", + "command": "rm -r build/bin/ && mkdir -p build && cd build && cmake .. && make -j 16", "problemMatcher": [] }, { diff --git a/exercise1/glsl/shader.frag b/exercise1/glsl/shader.frag index 21e616c..5089dc0 100644 --- a/exercise1/glsl/shader.frag +++ b/exercise1/glsl/shader.frag @@ -1,4 +1,4 @@ -// This source code is property of the Computer Graphics and Visualization +// This source code is property of the Computer Graphics and Visualization // chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #version 130 @@ -9,7 +9,7 @@ out vec4 color; -void main(void) +void main() { color = fragment_color * 0.5 + vec4(0.5); diff --git a/exercise1/glsl/shader.vert b/exercise1/glsl/shader.vert index d52639a..23a733b 100644 --- a/exercise1/glsl/shader.vert +++ b/exercise1/glsl/shader.vert @@ -1,4 +1,4 @@ -// This source code is property of the Computer Graphics and Visualization +// This source code is property of the Computer Graphics and Visualization // chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #version 130 @@ -10,7 +10,7 @@ out vec4 fragment_color; -void main(void) +void main() { gl_Position = in_position; fragment_color = in_position; diff --git a/exercise1/src/Viewer.cpp b/exercise1/src/Viewer.cpp index 64488b1..0cd0d1d 100644 --- a/exercise1/src/Viewer.cpp +++ b/exercise1/src/Viewer.cpp @@ -1,5 +1,5 @@ -// This source code is property of the Computer Graphics and Visualization -// chair of the TU Dresden. Do not distribute! +// This source code is property of the Computer Graphics and Visualization +// chair of the TU Dresden. Do not distribute! // Copyright (C) CGV TU Dresden - All Rights Reserved #include "Viewer.h" @@ -11,12 +11,13 @@ #include #include +#include #include "glsl.h" Viewer::Viewer() : AbstractViewer("CG1 Exercise 1") -{ +{ SetupGUI(); CreateShaders(); @@ -57,14 +58,9 @@ void Viewer::CreateVertexBuffers() GLfloat positions[] = { 0, 1, 0, 1, -1, -1, 0, 1, - 1, -1, 0, 1 - }; + 1, -1, 0, 1}; - - - - - // Generate the vertex array + // Generate the vertex array glGenVertexArrays(1, &vertex_array_id); glBindVertexArray(vertex_array_id); @@ -75,7 +71,7 @@ void Viewer::CreateVertexBuffers() // Supply the position data glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW); // The buffer shall now be linked to the shader attribute - // "in_position". First, get the location of this attribute in + // "in_position". First, get the location of this attribute in // the shader program GLuint vid = glGetAttribLocation(program_id, "in_position"); // Enable this vertex attribute array @@ -91,40 +87,84 @@ void Viewer::CreateVertexBuffers() /*** End of task 1.2.2 (a) ***/ - - // Unbind the vertex array to leave OpenGL in a clean state glBindVertexArray(0); } -//Checks if the given shader has been compiled successfully. Otherwise, prints an -//error message and throws an exception. -// shaderId - the id of the shader object -// name - a human readable name for the shader that is printed together with the error -void CheckShaderCompileStatus(GLuint shaderId, std::string name) +inline std::string loadShaderText(const char *path) { - GLint status; - glGetShaderiv(shaderId, GL_COMPILE_STATUS, &status); + std::ifstream is(path); + std::string s_save; - if (status != GL_TRUE) + if (is.is_open()) { - char buffer[512]; - std::cerr << "Error while compiling shader \"" << name << "\":" << std::endl; - glGetShaderInfoLog(shaderId, 512, nullptr, buffer); - std::cerr << "Error: " << std::endl << buffer << std::endl; - throw std::runtime_error("Shader compilation failed!"); + s_save.assign(std::istreambuf_iterator(is), std::istreambuf_iterator()); } + else + { + std::cout << "could not open " << path << std::endl; + } + + is.close(); + return s_save; +} + +inline GLuint loadShader(const char *path, GLenum shadertype) +{ + std::string s_source = loadShaderText(path); + + if (s_source.empty()) + return 0; + + const char *c_source = s_source.c_str(); + + GLuint shader = glCreateShader(shadertype); + glShaderSource(shader, 1, (const char **)&c_source, NULL); + glCompileShader(shader); + + GLint shaderCompiled = GL_FALSE; + glGetShaderiv(shader, GL_COMPILE_STATUS, &shaderCompiled); + if (shaderCompiled != GL_TRUE) + { + GLint log_length = 0; + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length); + + char *log = new char[log_length]; + + glGetShaderInfoLog(shader, (GLsizei)log_length, NULL, log); + + std::cout << "error compiling shader( " << path << " ): " << std::endl + << log << std::endl; + + delete[] log; + + glDeleteShader(shader); + shader = 0; + } + + return shader; } // Read, Compile and link the shader codes to a shader program void Viewer::CreateShaders() { - std::string vs((char*)shader_vert, shader_vert_size); - const char *vertex_content = vs.c_str(); + const char *vs = "exercise1/glsl/shader.vert"; + const char *fs = "exercise1/glsl/shader.frag"; - std::string fs((char*)shader_frag, shader_frag_size); - const char *fragment_content = fs.c_str(); + vertex_shader_id = loadShader(vs, GL_VERTEX_SHADER); + fragment_shader_id = loadShader(fs, GL_FRAGMENT_SHADER); + + if ((vertex_shader_id == 0) || (fragment_shader_id == 0)) + { + std::cout << "error creating shader" << std::endl; + abort(); + } + + program_id = glCreateProgram(); + glAttachShader(program_id, vertex_shader_id); + glAttachShader(program_id, fragment_shader_id); + glLinkProgram(program_id); /*** Begin of task 1.2.1 *** Use the appropriate OpenGL commands to create a shader object for @@ -169,13 +209,13 @@ void Viewer::drawContents() then set them with the command glUniformMatrix4fv. */ - // Bind the vertex array + // Bind the vertex array glBindVertexArray(vertex_array_id); // Draw the bound vertex array. Start at element 0 and draw 3 vertices glDrawArrays(GL_TRIANGLES, 0, 3); /*** End of task 1.2.4 (b) ***/ - + // Unbind the vertex array glBindVertexArray(0); // Deactivate the shader program