Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

BELOW IS Program2_4.Java: import javax.swing.*; import static com.jogamp.opengl.GL4.*; import com.jogamp.opengl.*; import com.jogamp.opengl.awt.GLCanvas; public class Program2_4 extends JFrame implements GLEventListener { private GLCanvas myCanvas; private int

BELOW IS Program2_4.Java:

import javax.swing.*; import static com.jogamp.opengl.GL4.*; import com.jogamp.opengl.*; import com.jogamp.opengl.awt.GLCanvas;

public class Program2_4 extends JFrame implements GLEventListener { private GLCanvas myCanvas; private int renderingProgram; private int vao[] = new int[1];

public Program2_4() { setTitle("Chapter2 - Program4"); setSize(600, 400); setLocation(200, 200);

myCanvas = new GLCanvas(); myCanvas.addGLEventListener(this); this.add(myCanvas);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); }

public void display(GLAutoDrawable drawable) { GL4 gl = (GL4) GLContext.getCurrentGL(); gl.glUseProgram(renderingProgram); gl.glPointSize(30.0f); gl.glDrawArrays(GL_POINTS, 0, 1); }

public void init(GLAutoDrawable drawable) { GL4 gl = (GL4) GLContext.getCurrentGL(); renderingProgram = createShaderProgram(); gl.glBindVertexArray(vao[0]); }

private int createShaderProgram() { GL4 gl = (GL4) GLContext.getCurrentGL(); String[] vshaderSource = UtilsV7.readShaderSource("./shaders/vertShader2.glsl"); String[] fshaderSource = UtilsV7.readShaderSource("./shaders/fragShader2.glsl");

int vShader = gl.glCreateShader(GL_VERTEX_SHADER); int fShader = gl.glCreateShader(GL_FRAGMENT_SHADER); gl.glShaderSource(vShader, vshaderSource.length, vshaderSource, null, 0); gl.glShaderSource(fShader, fshaderSource.length, fshaderSource, null, 0); gl.glCompileShader(vShader); 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; }

public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }

public void dispose(GLAutoDrawable drawable) { } }

BELOW IS Program2_5.Java:

import javax.swing.*; import static com.jogamp.opengl.GL4.*; import com.jogamp.opengl.*; import com.jogamp.opengl.awt.GLCanvas;

public class Program2_5 extends JFrame implements GLEventListener { private GLCanvas myCanvas; private int renderingProgram; private int vao[] = new int[1];

public Program2_5() { setTitle("Chapter2 - Program5"); setSize(600, 400); setLocation(200, 200);

myCanvas = new GLCanvas(); myCanvas.addGLEventListener(this); this.add(myCanvas);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); }

public void display(GLAutoDrawable drawable) { GL4 gl = (GL4) GLContext.getCurrentGL(); gl.glUseProgram(renderingProgram); //gl.glPointSize(30.0f); gl.glDrawArrays(GL_TRIANGLES, 0, 3); }

public void init(GLAutoDrawable drawable) { GL4 gl = (GL4) GLContext.getCurrentGL(); renderingProgram = createShaderProgram(); gl.glBindVertexArray(vao[0]); }

private int createShaderProgram() { GL4 gl = (GL4) GLContext.getCurrentGL(); String[] vshaderSource = UtilsV7.readShaderSource("./shaders/vertShader2.glsl"); String[] fshaderSource = UtilsV7.readShaderSource("./shaders/fragShader2.glsl");

int vShader = gl.glCreateShader(GL_VERTEX_SHADER); int fShader = gl.glCreateShader(GL_FRAGMENT_SHADER); gl.glShaderSource(vShader, vshaderSource.length, vshaderSource, null, 0); gl.glShaderSource(fShader, fshaderSource.length, fshaderSource, null, 0); gl.glCompileShader(vShader); 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; }

public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }

public void dispose(GLAutoDrawable drawable) { } }

BELOW IS Program UtilsV7.Java

import com.jogamp.opengl.glu.GLU;

import static com.jogamp.opengl.GL4.*;

import com.jogamp.opengl.*; import com.jogamp.opengl.util.texture.Texture; import com.jogamp.opengl.util.texture.TextureIO;

import java.io.File; import java.io.IOException; import java.util.Scanner; import java.util.Vector;

public class UtilsV7 {

public static void printShaderLog(int shader) { GL4 gl = (GL4) GLContext.getCurrentGL(); int[] len = new int[1]; int[] chWrittn = new int[1]; byte[] log = null;

// determine the length of the shader compilation log gl.glGetShaderiv(shader, GL_INFO_LOG_LENGTH, len, 0); if (len[0] > 0) { log = new byte[len[0]]; gl.glGetShaderInfoLog(shader, len[0], chWrittn, 0, log, 0); System.out.println("Shader Info Log: "); for (int i = 0; i < log.length; i++) { System.out.print((char) log[i]); } } }

public static void printProgramLog(int prog) { GL4 gl = (GL4) GLContext.getCurrentGL(); int[] len = new int[1]; int[] chWrittn = new int[1]; byte[] log = null;

// determine the length of the program linking log gl.glGetProgramiv(prog, GL_INFO_LOG_LENGTH, len, 0); if (len[0] > 0) { log = new byte[len[0]]; gl.glGetProgramInfoLog(prog, len[0], chWrittn, 0, log, 0); System.out.println("Program Info Log: "); for (int i = 0; i < log.length; i++) { System.out.print((char) log[i]); } } }

public static boolean checkOpenGLError() { GL4 gl = (GL4) GLContext.getCurrentGL(); boolean foundError = false; GLU glu = new GLU(); int glErr = gl.glGetError(); while (glErr != GL_NO_ERROR) { System.err.println("glError: " + glu.gluErrorString(glErr)); foundError = true; glErr = gl.glGetError(); } return foundError; }

public static String[] readShaderSource(String filename) { Vector lines = new Vector(); Scanner sc; String[] program; try { sc = new Scanner(new File(filename)); while (sc.hasNext()) { lines.addElement(sc.nextLine()); } program = new String[lines.size()]; for (int i = 0; i < lines.size(); i++) { program[i] = (String) lines.elementAt(i) + " "; } } catch (IOException e) { System.err.println("IOException reading file: " + e); return null; } return program; }

public static int createShaderProgram(String vS, String fS){ GL4 gl = (GL4) GLContext.getCurrentGL(); String[] vshaderSource = UtilsV7.readShaderSource(vS); String[] fshaderSource = UtilsV7.readShaderSource(fS);

int vShader = gl.glCreateShader(GL_VERTEX_SHADER); int fShader = gl.glCreateShader(GL_FRAGMENT_SHADER); gl.glShaderSource(vShader, vshaderSource.length, vshaderSource, null, 0); gl.glShaderSource(fShader, fshaderSource.length, fshaderSource, null, 0); gl.glCompileShader(vShader); 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; }

public static int loadTexture(String textureFileName) { Texture tex = null; try { tex = TextureIO.newTexture(new File(textureFileName), false); } catch (Exception e) { e.printStackTrace(); } int textureID = tex.getTextureObject(); return textureID; }

// GOLD material - ambient, diffuse, specular, and shininess public static float[ ] goldAmbient() { return (new float [ ] {0.2473f, 0.1995f, 0.0745f, 1} ); } public static float[ ] goldDiffuse() { return (new float [ ] {0.7516f, 0.6065f, 0.2265f, 1} ); } public static float[ ] goldSpecular() { return (new float [ ] {0.6283f, 0.5558f, 0.3661f, 1} ); } public static float goldShininess() { return 51.2f; }

// SILVER material - ambient, diffuse, specular, and shininess public static float[ ] silverAmbient() { return (new float [ ] {0.1923f, 0.1923f, 0.1923f, 1} ); } public static float[ ] silverDiffuse() { return (new float [ ] {0.5075f, 0.5075f, 0.5075f, 1} ); } public static float[ ] silverSpecular() { return (new float [ ] {0.5083f, 0.5083f, 0.5083f, 1} ); } public static float silverShininess() { return 51.2f; }

// BRONZE material - ambient, diffuse, specular, and shininess public static float[ ] bronzeAmbient() { return (new float [ ] {0.2125f, 0.1275f, 0.0540f, 1} ); } public static float[ ] bronzeDiffuse() { return (new float [ ] {0.7140f, 0.4284f, 0.1814f, 1} ); } public static float[ ] bronzeSpecular() { return (new float [ ] {0.3935f, 0.2719f, 0.1667f, 1} ); } public static float bronzeShininess() { return 25.6f; } }

Below is program vertShader: #version 430 void main(void) { if (gl_VertexID == 0) gl_Position = vec4( 0.25, -0.25, 0.0, 1.0); else if (gl_VertexID == 1) gl_Position = vec4(-0.25, -0.25, 0.0, 1.0); else gl_Position = vec4( 0.25, 0.25, 0.0, 1.0); }

Below is program fragShader: #version 430 out vec4 color; void main(void) { if (gl_FragCoord.x < 295) color = vec4(1.0, 0.0, 0.0, 1.0); else color = vec4(0.0, 0.0, 1.0, 1.0); }

Questions:

Based on above information from Computer Graphics Programming in OpenGL with Java:

1. How to modify above program so that it draws a rectangle (rather than the right triangle) with explanations?

2. How to modify program from no.1, so, that it draws a rectangle with 4 different colors depending on the x and y coordinates of the pixel with explanations?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

Can negative outcomes associated with redundancy be avoided?

Answered: 1 week ago

Question

Understand the key features of recruitment and selection policies

Answered: 1 week ago