private FloatBuffer vals = Buffers.newDirectFloatBuffer(16); private Matrix4f pMat = new Matrix4f(); private Matrix4f vMat = new Matrix4f(); private Matrix4f mMat = new Matrix4f(); private Matrix4f mvMat = new Matrix4f(); 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); 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; cubeLocZ = 0.0f; pyrLocX = 2.0f; pyrLocY = 2.0f; pyrLocZ = 0.0f; } public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { } public void dispose(GLAutoDraw
private FloatBuffer vals = Buffers.newDirectFloatBuffer(16); private Matrix4f pMat = new Matrix4f(); private Matrix4f vMat = new Matrix4f(); private Matrix4f mMat = new Matrix4f(); private Matrix4f mvMat = new Matrix4f(); 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); 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; cubeLocZ = 0.0f; pyrLocX = 2.0f; pyrLocY = 2.0f; pyrLocZ = 0.0f; } public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { } public void dispose(GLAutoDraw
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
private FloatBuffer vals = Buffers.newDirectFloatBuffer(16);
private Matrix4f pMat = new Matrix4f();
private Matrix4f vMat = new Matrix4f();
private Matrix4f mMat = new Matrix4f();
private Matrix4f mvMat = new Matrix4f();
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);
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;
cubeLocZ = 0.0f;
pyrLocX = 2.0f;
pyrLocY = 2.0f;
pyrLocZ = 0.0f;
}
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);
pLoc = gl.glGetUniformLocation(renderingProgram, "p_matrix");
mvLoc = gl.glGetUniformLocation(renderingProgram, "mv_matrix");
aspect = (float) myCanvas.getWidth() / (float) myCanvas.getHeight();
pMat.setPerspective((float) Math.toRadians(60.0f), aspect, 0.1f, 1000.0f);
vMat.translation(-cameraX, -cameraY, -cameraZ);
mMat.translation(cubeLocX, cubeLocY, cubeLocZ);
mvMat.identity();
mvMat.mul(vMat);
mvMat.mul(mMat);
gl.glUniformMatrix4fv(mvLoc, 1, false, mvMat.get(vals));
gl.glUniformMatrix4fv(pLoc, 1, false, pMat.get(vals));
gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
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, 36);
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));
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();
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,
1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.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
};
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);
}
}
private Matrix4f pMat = new Matrix4f();
private Matrix4f vMat = new Matrix4f();
private Matrix4f mMat = new Matrix4f();
private Matrix4f mvMat = new Matrix4f();
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);
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;
cubeLocZ = 0.0f;
pyrLocX = 2.0f;
pyrLocY = 2.0f;
pyrLocZ = 0.0f;
}
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);
pLoc = gl.glGetUniformLocation(renderingProgram, "p_matrix");
mvLoc = gl.glGetUniformLocation(renderingProgram, "mv_matrix");
aspect = (float) myCanvas.getWidth() / (float) myCanvas.getHeight();
pMat.setPerspective((float) Math.toRadians(60.0f), aspect, 0.1f, 1000.0f);
vMat.translation(-cameraX, -cameraY, -cameraZ);
mMat.translation(cubeLocX, cubeLocY, cubeLocZ);
mvMat.identity();
mvMat.mul(vMat);
mvMat.mul(mMat);
gl.glUniformMatrix4fv(mvLoc, 1, false, mvMat.get(vals));
gl.glUniformMatrix4fv(pLoc, 1, false, pMat.get(vals));
gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
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, 36);
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));
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();
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,
1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.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
};
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 sphere 3D shape. 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 earth’s rotation, but we may choose which axis to rotate on, x, y, or z-axis) ?
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 4 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY