Question: From the book 'Computer Graphics Programming in OpenGL with Java (Third Edition)' Program 2.2 Shaders, Drawing a POINT (..imports as before) public class Code extends

From the book 'Computer Graphics Programming in OpenGL with Java (Third Edition)'

Program 2.2 Shaders, Drawing a POINT

(..imports as before) public class Code extends JFrame implements GLEventListener { private int renderingProgram; private int vao[ ] = new int[1]; public Code() { (..constructor as before) } public void display(GLAutoDrawable drawable) { GL4 gl = (GL4) GLContext.getCurrentGL(); gl.glUseProgram(renderingProgram); gl.glDrawArrays(GL_POINTS, 0, 1); } public void init(GLAutoDrawable drawable) { GL4 gl = (GL4) GLContext.getCurrentGL(); renderingProgram = createShaderProgram(); gl.glGenVertexArrays(vao.length, vao, 0); gl.glBindVertexArray(vao[0]); } private int createShaderProgram() { GL4 gl = (GL4) GLContext.getCurrentGL(); String vshaderSource[ ] = { "#version 430 ", "void main(void) ", "{ gl_Position = vec4(0.0, 0.0, 0.0, 1.0); } ", }; String fshaderSource[ ] = { "#version 430 ", "out vec4 color; ", "void main(void) ", "{ color = vec4(0.0, 0.0, 1.0, 1.0); } ", }; int vShader = gl.glCreateShader(GL_VERTEX_SHADER); gl.glShaderSource(vShader, 3, vshaderSource, null, 0); // 3 is the count of lines of source code gl.glCompileShader(vShader); int fShader=gl.glCreateShader(GL_FRAGMENT_SHADER); gl.glShaderSource(fShader, 4, fshaderSource, null, 0); // 4 is the count of lines of source code gl.glCompileShader(fShader); int vfProgram = gl.glCreateProgram(); gl.glAttachShader(vfProgram, vShader); gl.glAttachShader(vfProgram, fShader); gl.glLinkProgram(vfProgram); gl.glDeleteShader(vShader); gl.glDeleteShader(fShader); return vfProgram; } . . . main(), reshape(), and dispose() as before

/* fragshader coding */ #version 430 out vec4 color; void main(void) { if (gl_FragCoord.x < 295) color = vec4(1.0, 0.0, 0.0, 1.0); else if(gl_FragCoord.x > 295 && gl_FragCoord.x < 310) color = vec4(1.0, 0.0, 1.0, 1.0); else color = vec4(0.0, 0.0, 1.0, 1.0); }

/* vertshader coding */ #version 430 uniform float offset; void main(void) { if (gl_VertexID == 0) gl_Position = vec4( 0.25 + offset, -0.25, 0.0, 1.0); else if (gl_VertexID == 1) gl_Position = vec4(-0.25 + offset, -0.25, 0.0, 1.0); else gl_Position = vec4( 0.25 + offset, 0.25, 0.0, 1.0); }

How answer below question with explanations & results picture:

1. How to modify above Java OpenGL program to add animation that causes the drawn point to grow and shrink, in a cycle. Hint: use the glPointSize() function, with a variable as the parameter?

2. How to modify above Java OpenGL program so that the drawn rectangle (not point) will grow and shrink in a cycle (we can also try to move the rectangle in x-axis)?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!