Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java OpenGL Coding: import com.jogamp.common.nio.Buffers; import com.jogamp.opengl.GL4; import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLContext; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.awt.GLCanvas; import org.joml.Matrix4f; import javax.swing.*; import java.nio.FloatBuffer; import static com.jogamp.opengl.GL4.*; public

Java OpenGL Coding:

import com.jogamp.common.nio.Buffers; import com.jogamp.opengl.GL4; import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLContext; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.awt.GLCanvas; import org.joml.Matrix4f;

import javax.swing.*; import java.nio.FloatBuffer;

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

public class Program4_3 extends JFrame implements GLEventListener { private GLCanvas myCanvas; private int renderingProgram; private int vao[] = new int[1]; private int vbo[] = new int[2]; private float cameraX, cameraY, cameraZ; private float cubeLocX, cubeLocY, cubeLocZ; private float pyrLocX, pyrLocY, pyrLocZ;

// allocate variables used in display() function, so that they wont need to be allocated during rendering private FloatBuffer vals = Buffers.newDirectFloatBuffer(16); // utility buffer for transferring matrices private Matrix4f pMat = new Matrix4f(); // perspective matrix private Matrix4f vMat = new Matrix4f(); // view matrix private Matrix4f mMat = new Matrix4f(); // model matrix private Matrix4f mvMat = new Matrix4f(); // model-view matrix private int pLoc, mvLoc; private float aspect; private double elapsedTime, startTime, tf;

public Program4_3() { setTitle("Chapter4 - program3"); setSize(600, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

/*Animator anmtr = new Animator(myCanvas); anmtr.start();*/

this.setVisible(true); }

public void init(GLAutoDrawable drawable) { GL4 gl = (GL4) GLContext.getCurrentGL(); renderingProgram = UtilsV7.createShaderProgram("./shaders/vertShader4_1_2.glsl", "./shaders/fragShader4_2.glsl"); setupVertices(); cameraX = 0.0f; cameraY = 0.0f; cameraZ = 8.0f; cubeLocX = 0.0f; cubeLocY = -2.0f; // shifted down the Y-axis to reveal perspective cubeLocZ = 0.0f; pyrLocX = 2.0f; // shifted right pyrLocY = 2.0f; // shifted down the Y-axis to reveal perspective pyrLocZ = 0.0f;

//startTime = System.currentTimeMillis(); }

// reshape() and dispose() are unchanged public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }

public void dispose(GLAutoDrawable drawable) { }

public void display(GLAutoDrawable drawable) { GL4 gl = (GL4) GLContext.getCurrentGL(); gl.glClear(GL_DEPTH_BUFFER_BIT); gl.glClear(GL_COLOR_BUFFER_BIT); gl.glUseProgram(renderingProgram);

// get references to the uniform variables for the MV and projection matrices pLoc = gl.glGetUniformLocation(renderingProgram, "p_matrix"); mvLoc = gl.glGetUniformLocation(renderingProgram, "mv_matrix");

// build perspective matrix. This one has fovy=60, aspect ratio matches the screen window. aspect = (float) myCanvas.getWidth() / (float) myCanvas.getHeight(); pMat.setPerspective((float) Math.toRadians(60.0f), aspect, 0.1f, 1000.0f);

// build view matrix vMat.translation(-cameraX, -cameraY, -cameraZ);

/*// use system time to generate slowly-increasing sequence of floating-point values elapsedTime = System.currentTimeMillis() - startTime; // elapsedTime, startTime, and tf tf = elapsedTime / 1000.0; // would all be declared of type double. */

// draw the cube (use buffer #0) mMat.translation(cubeLocX, cubeLocY, cubeLocZ); mvMat.identity(); mvMat.mul(vMat); mvMat.mul(mMat); // copy matrices and variables to corresponding uniform variables // by giving the uniform variable ID, one matrix to be sent, not transposed, and a pointer to the value sent gl.glUniformMatrix4fv(mvLoc, 1, false, mvMat.get(vals)); gl.glUniformMatrix4fv(pLoc, 1, false, pMat.get(vals)); // associate VBO with the corresponding vertex attribute in the vertex shader gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); // Sending to the location ID, 3 vertices of type float, not normalized, and with 0 offsets gl.glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); gl.glEnableVertexAttribArray(0); // adjust OpenGL settings and draw model gl.glEnable(GL_DEPTH_TEST); gl.glDepthFunc(GL_LEQUAL); gl.glDrawArrays(GL_TRIANGLES, 0, 36);

// draw the pyramid (use buffer #1 and different model matrix) mMat.translation(pyrLocX, pyrLocY, pyrLocZ); mvMat.identity(); mvMat.mul(vMat); mvMat.mul(mMat); gl.glUniformMatrix4fv(mvLoc, 1, false, mvMat.get(vals)); gl.glUniformMatrix4fv(pLoc, 1, false, pMat.get(vals));

// (repeated for clarity) gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); gl.glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); gl.glEnableVertexAttribArray(0); gl.glEnable(GL_DEPTH_TEST); gl.glDepthFunc(GL_LEQUAL); gl.glDrawArrays(GL_TRIANGLES, 0, 18); }

private void setupVertices() { GL4 gl = (GL4) GLContext.getCurrentGL();

// 36 vertices of the 12 triangles making up a 2 x 2 x 2 cube centered at the origin float[] cubePositions = {-1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f};

float[ ] pyramidPositions = { -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.0f, // front face 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f, // right face 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f, // back face -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.0f, // left face -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, // base left front 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f // base right back }; gl.glGenVertexArrays(vao.length, vao, 0); gl.glBindVertexArray(vao[0]); gl.glGenBuffers(vbo.length, vbo, 0);

gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); FloatBuffer cubeBuf = Buffers.newDirectFloatBuffer(cubePositions); gl.glBufferData(GL_ARRAY_BUFFER, cubeBuf.limit() * 4, cubeBuf, GL_STATIC_DRAW);

gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); FloatBuffer pyrBuf = Buffers.newDirectFloatBuffer(pyramidPositions); gl.glBufferData(GL_ARRAY_BUFFER, pyrBuf.limit() * 4, pyrBuf, GL_STATIC_DRAW); } }

Shader Coding:

fragShader.glsl

#version 430 in vec4 varyingColor; out vec4 color; uniform mat4 mv_matrix; uniform mat4 p_matrix; void main(void) { color = varyingColor; }

vertShader.glsl

#version 430 layout (location=0) in vec3 position; uniform mat4 mv_matrix; uniform mat4 p_matrix; out vec4 varyingColor;

void main(void) { gl_Position = p_matrix * mv_matrix * vec4(position,1.0); varyingColor = vec4(position,1.0) * 0.5 + vec4(0.5, 0.5, 0.5, 0.5); }

Questions :

1. How to modify with explanation above Java openGL coding to add another simple 3D shape (not sphere). Be sure to properly specify the number of vertices in the glDrawArrays() command ?

2. How to modify with explanation above Java openGL coding from No.1, so that the additional 3D shape rotates on its axis (just like the earths rotation, but we may choose which axis to rotate on, x, y, or z-axis) ?

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

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions