Impl 1.2.1

This commit is contained in:
hodasemi 2018-10-17 10:22:24 +02:00
parent 63305f25e6
commit 4a2c752bc0
4 changed files with 77 additions and 37 deletions

2
.vscode/tasks.json vendored
View file

@ -6,7 +6,7 @@
{ {
"label": "Build All Exercises", "label": "Build All Exercises",
"type": "shell", "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": [] "problemMatcher": []
}, },
{ {

View file

@ -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! // chair of the TU Dresden. Do not distribute!
// Copyright (C) CGV TU Dresden - All Rights Reserved // Copyright (C) CGV TU Dresden - All Rights Reserved
#version 130 #version 130
@ -9,7 +9,7 @@ out vec4 color;
void main(void) void main()
{ {
color = fragment_color * 0.5 + vec4(0.5); color = fragment_color * 0.5 + vec4(0.5);

View file

@ -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! // chair of the TU Dresden. Do not distribute!
// Copyright (C) CGV TU Dresden - All Rights Reserved // Copyright (C) CGV TU Dresden - All Rights Reserved
#version 130 #version 130
@ -10,7 +10,7 @@ out vec4 fragment_color;
void main(void) void main()
{ {
gl_Position = in_position; gl_Position = in_position;
fragment_color = in_position; fragment_color = in_position;

View file

@ -11,6 +11,7 @@
#include <gui/SliderHelper.h> #include <gui/SliderHelper.h>
#include <iostream> #include <iostream>
#include <fstream>
#include "glsl.h" #include "glsl.h"
@ -57,12 +58,7 @@ void Viewer::CreateVertexBuffers()
GLfloat positions[] = { GLfloat positions[] = {
0, 1, 0, 1, 0, 1, 0, 1,
-1, -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); glGenVertexArrays(1, &vertex_array_id);
@ -92,39 +88,83 @@ void Viewer::CreateVertexBuffers()
/*** End of task 1.2.2 (a) ***/ /*** End of task 1.2.2 (a) ***/
// Unbind the vertex array to leave OpenGL in a clean state // Unbind the vertex array to leave OpenGL in a clean state
glBindVertexArray(0); glBindVertexArray(0);
} }
//Checks if the given shader has been compiled successfully. Otherwise, prints an inline std::string loadShaderText(const char *path)
//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)
{ {
GLint status; std::ifstream is(path);
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &status); std::string s_save;
if (status != GL_TRUE) if (is.is_open())
{ {
char buffer[512]; s_save.assign(std::istreambuf_iterator<char>(is), std::istreambuf_iterator<char>());
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!");
} }
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 // Read, Compile and link the shader codes to a shader program
void Viewer::CreateShaders() void Viewer::CreateShaders()
{ {
std::string vs((char*)shader_vert, shader_vert_size); const char *vs = "exercise1/glsl/shader.vert";
const char *vertex_content = vs.c_str(); const char *fs = "exercise1/glsl/shader.frag";
std::string fs((char*)shader_frag, shader_frag_size); vertex_shader_id = loadShader(vs, GL_VERTEX_SHADER);
const char *fragment_content = fs.c_str(); 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 *** /*** Begin of task 1.2.1 ***
Use the appropriate OpenGL commands to create a shader object for Use the appropriate OpenGL commands to create a shader object for