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

@ -1,5 +1,5 @@
// 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
#include "Viewer.h" #include "Viewer.h"
@ -11,12 +11,13 @@
#include <gui/SliderHelper.h> #include <gui/SliderHelper.h>
#include <iostream> #include <iostream>
#include <fstream>
#include "glsl.h" #include "glsl.h"
Viewer::Viewer() Viewer::Viewer()
: AbstractViewer("CG1 Exercise 1") : AbstractViewer("CG1 Exercise 1")
{ {
SetupGUI(); SetupGUI();
CreateShaders(); CreateShaders();
@ -57,14 +58,9 @@ 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);
glBindVertexArray(vertex_array_id); glBindVertexArray(vertex_array_id);
@ -75,7 +71,7 @@ void Viewer::CreateVertexBuffers()
// Supply the position data // Supply the position data
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
// The buffer shall now be linked to the shader attribute // 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 // the shader program
GLuint vid = glGetAttribLocation(program_id, "in_position"); GLuint vid = glGetAttribLocation(program_id, "in_position");
// Enable this vertex attribute array // Enable this vertex attribute array
@ -91,40 +87,84 @@ 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
@ -169,13 +209,13 @@ void Viewer::drawContents()
then set them with the command glUniformMatrix4fv. then set them with the command glUniformMatrix4fv.
*/ */
// Bind the vertex array // Bind the vertex array
glBindVertexArray(vertex_array_id); glBindVertexArray(vertex_array_id);
// Draw the bound vertex array. Start at element 0 and draw 3 vertices // Draw the bound vertex array. Start at element 0 and draw 3 vertices
glDrawArrays(GL_TRIANGLES, 0, 3); glDrawArrays(GL_TRIANGLES, 0, 3);
/*** End of task 1.2.4 (b) ***/ /*** End of task 1.2.4 (b) ***/
// Unbind the vertex array // Unbind the vertex array
glBindVertexArray(0); glBindVertexArray(0);
// Deactivate the shader program // Deactivate the shader program