From 907100c0a40be626f285684b2166cd40ceab4b1d Mon Sep 17 00:00:00 2001 From: hodasemi Date: Wed, 17 Oct 2018 16:39:10 +0200 Subject: [PATCH] Fix Julia and let gui configure it --- exercise1/glsl/shader.frag | 24 +++++------------- exercise1/glsl/shader.vert | 14 ---------- exercise1/include/Viewer.h | 2 ++ exercise1/src/Viewer.cpp | 52 +++++++++++++++++++++----------------- 4 files changed, 38 insertions(+), 54 deletions(-) diff --git a/exercise1/glsl/shader.frag b/exercise1/glsl/shader.frag index 5f7d2b9..eb0c3af 100644 --- a/exercise1/glsl/shader.frag +++ b/exercise1/glsl/shader.frag @@ -8,22 +8,21 @@ in vec2 vertex_pos; out vec4 color; +uniform float m; +uniform vec2 c; + const int i_max = 200; void main() { - float m = 0.3; - vec2 c = vec2(0.5, 1.5); - vec2 z = vertex_pos * m; + int i; - int i = 0; - - for(; i < i_max; i++) { + for(i = 0; i < i_max; i++) { float x = (z.x * z.x - z.y * z.y) + c.x; - float y = (z.y * z.x - z.x * z.y) + c.y; + float y = (z.y * z.x + z.x * z.y) + c.y; - if (x*x + y*y > 4.0) { + if ((x*x + y*y) > 4.0) { break; } @@ -38,13 +37,4 @@ void main() } color = vec4(alpha, alpha, alpha, alpha) * 10.0 * fragment_color; - - /**** Begin of tasks *** - - 1.2.5 - Implement the pseudo-code for calculating the julia fractal at a point. - To get the point you can declare a new "in" variable which contains the - position and just use the X- and Y- value. - - *** End of tasks ***/ - } \ No newline at end of file diff --git a/exercise1/glsl/shader.vert b/exercise1/glsl/shader.vert index e07b317..9e11959 100644 --- a/exercise1/glsl/shader.vert +++ b/exercise1/glsl/shader.vert @@ -18,18 +18,4 @@ void main() gl_Position = proj * view * in_position; fragment_color = in_color; - - /* - 1.2.2 (b) - * Declare a new "in" variable with the name "in_color". Instead of setting - * "fragment_color" to the position, set it to "in_color. */ - - /* - 1.2.4 (a) - * Declare two new "uniform" variables with the type "mat4" (above the main function) - * that store the modelview and projection matrix. To apply the transformations - * multiply the value of "in_position" before setting "gl_Position". */ - - /* - 1.2.5 - * The algorithm to calculate the julia fractal needs a position as input. - * Declare another "out" variable and set it to the untransformed input - * position. */ } \ No newline at end of file diff --git a/exercise1/include/Viewer.h b/exercise1/include/Viewer.h index 261284c..650663d 100644 --- a/exercise1/include/Viewer.h +++ b/exercise1/include/Viewer.h @@ -37,6 +37,8 @@ private: GLint view_uniform_id; GLint proj_uniform_id; + GLint julia_m_id; + GLint julia_c_id; // Read, Compile and link the shader codes to a shader program void CreateShaders(); diff --git a/exercise1/src/Viewer.cpp b/exercise1/src/Viewer.cpp index f1ed00b..5830d82 100644 --- a/exercise1/src/Viewer.cpp +++ b/exercise1/src/Viewer.cpp @@ -26,6 +26,9 @@ Viewer::Viewer() view_uniform_id = glGetUniformLocation(program_id, "view"); proj_uniform_id = glGetUniformLocation(program_id, "proj"); + julia_m_id = glGetUniformLocation(program_id, "m"); + julia_c_id = glGetUniformLocation(program_id, "c"); + modelViewMatrix.setIdentity(); projectionMatrix.setIdentity(); @@ -59,40 +62,40 @@ void Viewer::CreateVertexBuffers() // Define 3 vertices for one face GLfloat positions[] = { - -1, 1, -1, 1, - 1, -1, -1, 1, - 1, 1, 1, 1, + -1, -1, -1, 1, // 1 + 1, -1, 1, 1, // 6 + -1, 1, 1, 1, // 8 - -1, 1, -1, 1, - -1, -1, 1, 1, - 1, -1, -1, 1, + -1, -1, -1, 1, // 1 + 1, 1, -1, 1, // 3 + 1, -1, 1, 1, // 6 - -1, 1, -1, 1, - -1, -1, 1, 1, - 1, 1, 1, 1, + -1, -1, -1, 1, // 1 + -1, 1, 1, 1, // 8 + 1, 1, -1, 1, // 3 - 1, -1, -1, 1, - 1, 1, 1, 1, - -1, -1, 1, 1, + 1, 1, -1, 1, // 3 + -1, 1, 1, 1, // 8 + 1, -1, 1, 1, // 6 }; GLfloat colors[] = { 1, 0, 0, 1, + 1, 0, 0, 1, + 1, 0, 0, 1, + 0, 1, 0, 1, + 0, 1, 0, 1, + 0, 1, 0, 1, + + 0, 0, 1, 1, + 0, 0, 1, 1, 0, 0, 1, 1, - 1, 0, 0, 1, - 0, 1, 0, 1, - 0, 0, 1, 1, - - 1, 0, 0, 1, - 0, 1, 0, 1, - 0, 0, 1, 1, - - 1, 0, 0, 1, - 0, 1, 0, 1, - 0, 0, 1, 1, + 1, 1, 0, 1, + 1, 1, 0, 1, + 1, 1, 0, 1, }; // Generate the vertex array @@ -255,6 +258,9 @@ void Viewer::drawContents() glUniformMatrix4fv(view_uniform_id, 1, false, modelViewMatrix.data()); glUniformMatrix4fv(proj_uniform_id, 1, false, projectionMatrix.data()); + glUniform1f(julia_m_id, juliaZoom); + glUniform2fv(julia_c_id, 1, juliaC.data()); + // Bind the vertex array glBindVertexArray(vertex_array_id); // Draw the bound vertex array. Start at element 0 and draw 3 vertices