i followed those steps in order to setup and create my first project:
setup
tutorial
Here is the problematic code:
main class:
/**
*
*/
package name.wadewalker.tutorial;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
/**
* #author PTOSH
*
*/
public class openGL{
/**
* #param args
*/
GLU glu;
public static void main(String[] args) {
// TODO Auto-generated method stub
GLProfile glp = GLProfile.getDefault();
GLProfile.initSingleton();
GLCapabilities CAPS = new GLCapabilities(glp);
GLCanvas CANVAS = new GLCanvas(CAPS);
CANVAS.addGLEventListener(new newLstn());
Frame FRAME = new Frame("AWT WINDOW TEST");
FRAME.setSize(1300, 700);
FRAME.add(CANVAS);
FRAME.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent E){
System.exit(0);
}
});
FRAME.setVisible(true);
}
}
GLEventListener :
package name.wadewalker.tutorial;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.media.opengl.glu.GLU;
public class newLstn implements GLEventListener {
private GL2 gl;
private GLU glu;
#Override
public void display(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(0f,0f,0f,0f);
gl.glBegin(GL.GL_TRIANGLES);
// Font-face triangle
gl.glColor3f(1.0f, 0.0f, 0.0f); // Red
gl.glVertex3f(0.0f, 0.5f, 0.0f);
// gl.glColor3f(0.0f, 1.0f, 0.0f); // Green
gl.glVertex3f(-0.5f, -0.5f, 0.5f);
// gl.glColor3f(0.0f, 0.0f, 1.0f); // Blue
gl.glVertex3f(0.5f, -0.5f, 0.5f);
// Right-face triangle
// gl.glColor3f(1.0f, 0.0f, 0.0f); // Red
gl.glVertex3f(0.0f, 0.5f, 0.0f);
// gl.glColor3f(0.0f, 0.0f, 1.0f); // Blue
gl.glVertex3f(0.5f, -0.5f, 0.5f);
// gl.glColor3f(0.0f, 1.0f, 0.0f); // Green
gl.glVertex3f(0.5f, -0.5f, -0.5f);
// Back-face triangle
// gl.glColor3f(1.0f, 0.0f, 0.0f); // Red
// gl.glVertex3f(0.0f, 1.0f, 0.0f);
// gl.glColor3f(0.0f, 1.0f, 0.0f); // Green
// gl.glVertex3f(1.0f, -1.0f, -1.0f);
// gl.glColor3f(0.0f, 0.0f, 1.0f); // Blue
// gl.glVertex3f(-1.0f, -1.0f, -1.0f);
//
// // Left-face triangle
// gl.glColor3f(1.0f, 0.0f, 0.0f); // Red
// gl.glVertex3f(0.0f, 1.0f, 0.0f);
// gl.glColor3f(0.0f, 0.0f, 1.0f); // Blue
// gl.glVertex3f(-1.0f, -1.0f, -1.0f);
// gl.glColor3f(0.0f, 1.0f, 0.0f); // Green
// gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glEnd();
gl.glFlush();
// ----- Render the Pyramid -----
gl.glLoadIdentity(); // reset the model-view matrix
gl.glTranslatef(-0.6f, 0.0f, 0.0f); // translate left and into the screen
gl.glRotatef(15, -0.2f, 1.0f, 0.0f); // rotate about the y-axis
}
#Override
public void dispose(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
}
#Override
public void init(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
gl = arg0.getGL().getGL2(); // get the OpenGL graphics context
glu = new GLU(); // get GL Utilities
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // set background (clear) color
gl.glClearDepth(1.0f); // set clear depth value to farthest
gl.glEnable(GL.GL_DEPTH_TEST); // enables depth testing
gl.glEnable(GL.GL_CULL_FACE_MODE);
gl.glDepthFunc(GL.GL_LEQUAL); // the type of depth test to do
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // best perspective correction
// gl.glShadeModel(0); // blends colors nicely, and smoothes out lighting
}
#Override
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
int arg4) {
// float red = 0.0f; float green = 0.0f; float blue = 0.0f;
////
//// gl = arg0.getGL().getGL2();
////
//// gl.glClear(GL.GL_COLOR_BUFFER_BIT);
////
// gl.glPointSize(5.0f);
//
// for (int i=0; i<50; i++) {
//
// red -= .09f; green -= .12f; blue -= .15f;
//
// if (red < 0.15) red = 1.0f; if (green < 0.15) green = 1.0f; if (blue < 0.15) blue = 1.0f;
//
// gl.glColor3f(red, green, blue);
// gl.glBegin(GL.GL_POINTS); gl.glVertex2i((i*10), 150); gl.glEnd(); }
// TODO Auto-generated method stub
glu.gluPerspective(45.0, 15, 0.1, 100.0); // fovy, aspect, zNear, zFar
}
}
I took the source code from the tutorial and modified it, my purpose is to start drawing simple 3d shapes. Problem when I execute the code, it is not rendering depth apparently. I also noticed that the tutorial was for JOGL 2.0 and some parts of the code is not working any more like : "gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);"
so I have to delete or comment this part of the code to see the shape in 2D.
Please if someone can show me a path to a good update tutorial, or give me some help understanding the issues I'm facing.
Thank you for your help and the effort.
Your display function has the lines:
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
gl.glLoadIdentity();
This will reset both the modelview and the projection matrix to identity, so in the following, you draw directly in clip space. This means that there will be no perspective effect at all.
In your reshape callback, you have
glu.gluPerspective(45.0, 15, 0.1, 100.0); // fovy, aspect, zNear, zFar
which is meant to set up some perspective transform. But it has no effect as you overwrite that matrix in the display function. However, you also use it wrong. gluPerspective() will multiply the top matrix fo the currently selected matrix stack by some perspective transform.
What you should do is something like
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0, 15, 0.1, 100.0); // fovy, aspect, zNear, zFar
gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
in the reshape function and completely remove the
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
from the display function.
However, this will not give the result that you might expect. Since you draw your triangles partly out of the frustum, then.
Your display function ends with
gl.glLoadIdentity(); // reset the model-view matrix
gl.glTranslatef(-0.6f, 0.0f, 0.0f); // translate left and into the screen
gl.glRotatef(15, -0.2f, 1.0f, 0.0f); // rotate about the y-axis
This code does not do anything at all. You completely have misunderstood how openGL works. It does not manage 3D objects for you, which you can later shift around in the world. It is a rendering API. And OpenGL is designed as a state machine. You set some rendering state, which affects the restulst of the draw calls. Here, you set some rendering state (the modelview matrix), but you draw nothing while this state is in effect. You must make those transforms before you issue the draw cals of the objects you want to appear transformed that way.
Note that all the things I meantioned so far are correctly used in the tutorial you linked.
Another big issue with this is that you should be aware that this code totally relies on deprecated GL. In modern GL, almost all of the GL functions you use here are gone. If you are learning OpenGL nowadays, you really should consider learning the modern way (which is not so modern anymore, we have the programmable pipeline now for a decade).
Related
While trying to set up a minimal reproducable example using LWJGL because my original question was closed, I ran into a problem I didn't have originally (Originally it was just not displaying), which is that the JVM now crashes with an EXCEPTION_ACCESS_VIOLATION error during the glDrawArrays() call.
I don't know what could be causing this, but I've tried for example initializing the vertex attribute pointers each frame. There is also no debug information or errors logged, while I did set up an error callback and I think a debug message callback.
All of the code:
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.GLUtil;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
public class MinimalExample {
private static String readResource(String res) {
try {
InputStream is = MinimalExample.class.getResourceAsStream(res);
String s = new String(is.readAllBytes(), StandardCharsets.UTF_8);
is.close();
return s;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
// vertex data buffer
private static final ByteBuffer buf = ByteBuffer.allocateDirect(4096);
// shader program
static int program;
// render objects
static int vao;
static int vbo;
public static void main(String[] args) {
// set buffer limit
buf.limit(4096);
// init glfw and create window
GLFW.glfwInit();
long window = GLFW.glfwCreateWindow(500, 500, "Hello", 0, 0);
// create GL
GLFW.glfwMakeContextCurrent(window);
GL.createCapabilities();
GLUtil.setupDebugMessageCallback(System.out);
GLFW.glfwSetErrorCallback(GLFWErrorCallback.createPrint(System.out));
// create vertex objects
vao = GL30.glGenVertexArrays();
vbo = GL30.glGenBuffers();
GL30.glBindVertexArray(vao);
GL30.glVertexAttribPointer(0, 3, GL30.GL_FLOAT, false, 7 * 4, 0);
GL30.glVertexAttribPointer(1, 4, GL30.GL_FLOAT, false, 7 * 4, 0);
GL30.glEnableVertexAttribArray(0);
GL30.glEnableVertexAttribArray(1);
GL30.glBindVertexArray(0);
// compile and link shaders
int vertexShader = GL30.glCreateShader(GL30.GL_VERTEX_SHADER);
int fragmentShader = GL30.glCreateShader(GL30.GL_FRAGMENT_SHADER);
GL30.glShaderSource(vertexShader, readResource("/test.vsh"));
GL30.glShaderSource(fragmentShader, readResource("/test.fsh"));
GL30.glCompileShader(vertexShader);
GL30.glCompileShader(fragmentShader);
program = GL30.glCreateProgram();
GL30.glAttachShader(program, vertexShader);
GL30.glAttachShader(program, fragmentShader);
GL30.glLinkProgram(program);
// render loop
while (!GLFW.glfwWindowShouldClose(window)) {
// poll events
GLFW.glfwPollEvents();
// clear screen
GL30.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
GL30.glClear(GL30.GL_COLOR_BUFFER_BIT);
// render
render();
// swap buffers
GLFW.glfwSwapBuffers(window);
}
}
static void render() {
// put vertex data
// manual to simulate graphics library
putVec3(0.25f, 0.25f, 1f); putVec4(1.0f, 0.0f, 0.0f, 1.0f);
putVec3(0.75f, 0.25f, 1f); putVec4(0.0f, 1.0f, 0.0f, 1.0f);
putVec3(0.50f, 0.75f, 1f); putVec4(0.0f, 0.0f, 1.0f, 1.0f);
// bind program
GL30.glUseProgram(program);
// bind vertex array
GL30.glBindVertexArray(vao);
GL30.glEnableVertexAttribArray(0);
GL30.glEnableVertexAttribArray(1);
// upload graphics data and draw
GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER, vbo);
GL30.glBufferData(GL30.GL_ARRAY_BUFFER, buf, GL30.GL_STATIC_DRAW);
GL30.glDrawArrays(GL30.GL_TRIANGLES, 0, 3);
GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER, 0);
GL30.glBindVertexArray(0);
// reset vertex data buffer
buf.position(0);
}
//////////////////////////////////////////
static void putVec3(float x, float y, float z) {
buf.putFloat(x).putFloat(y).putFloat(z);
}
static void putVec4(float x, float y, float z, float w) {
buf.putFloat(x).putFloat(y).putFloat(z).putFloat(z);
}
}
The full JVM error log (hs_err_pidX.log): pastes.dev
For context, I'm running this with JDK 17 from IntelliJ directly. Here is the build.gradle file if you want to check the dependencies: build.gradle (GitHub)
I am trying to rotate the ball(sfb) asset file in augmented Reality using android studio. i tried some code to rotate the ball by referring the solar system app by google. But i don't know why the ball rotate? i need some suggestion where i did mistake on my code.
private void addModelToScene(Anchor anchor, ModelRenderable modelRenderable) {
AnchorNode anchorNode = new AnchorNode(anchor);
TransformableNode transformableNode = new
TransformableNode(arFragment.getTransformationSystem());
RotatingNode rotatingNode = new RotatingNode();
arFragment.getArSceneView().getScene().addChild(anchorNode);
rotatingNode.setParent(anchorNode);
rotatingNode.setRenderable(modelRenderable);
transformableNode.select();
}
//this is my RotatingNode
private ObjectAnimator createAnimation(){
Quaternion orientation1 = Quaternion.axisAngle(new Vector3(0.0f, 1.0f, 0.0f), 0);
Quaternion orientation2 = Quaternion.axisAngle(new Vector3(0.0f, 1.0f, 0.0f), 120);
Quaternion orientation3 = Quaternion.axisAngle(new Vector3(0.0f, 1.0f, 0.0f), 240);
Quaternion orientation4 = Quaternion.axisAngle(new Vector3(0.0f, 1.0f, 0.0f), 360);
ObjectAnimator orbitAnimation = new ObjectAnimator();
orbitAnimation.setObjectValues(orientation1, orientation2, orientation3, orientation4);
orbitAnimation.setPropertyName("localRotation");
orbitAnimation.setEvaluator(new QuaternionEvaluator());
orbitAnimation.setRepeatCount(ObjectAnimator.INFINITE);
orbitAnimation.setRepeatMode(ObjectAnimator.RESTART);
orbitAnimation.setInterpolator(new LinearInterpolator());
orbitAnimation.setAutoCancel(true);
return orbitAnimation;
}
i expect the output to rotate the ball but only able to place the ball.
I am following this tutorial about using OpenGL for my Android application.
I am trying to make a conditional statement that will choose what to put in the triangleCoords variable based on the activity that it is being called from.
Will String gt=this.getClass().getSimpleName(); give me the current running activity when coded in a non-activity class? What format does the code display as?
How can I make the gt.equals("PlayGameType1Activity") not have a "Cannot resolve symbol 'equals'" error message.
String gt=this.getClass().getSimpleName();
// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float triangleCoords[];
if(gt.equals("PlayGameType1Activity"))
{
float triangleCoords[] = { // in counterclockwise order:
1.0f, -1.0f, 0.0f, // top
-1.0f, -1.0f, 0.0f, // bottom left
-1.0f, 1.0f, 0.0f // bottom right
};
}
if(gt.equals("PlayGameType2Activity"))
{
float triangleCoords[] = { // in counterclockwise order:
1.0f, 1.0f, 0.0f, // top
-1.0f, -1.0f, 0.0f, // bottom left
-1.0f, 1.0f, 0.0f // bottom right
};
}
This is what my code looks like with the error colors
Pass a reference to the current activity into this class and use currentActivity.getClass().getName()
Put them in a method
private void myMethod() {
if(gt.equals("PlayGameType1Activity"))
{
float triangleCoords[] = { // in counterclockwise order:
1.0f, -1.0f, 0.0f, // top
-1.0f, -1.0f, 0.0f, // bottom left
-1.0f, 1.0f, 0.0f // bottom right
};
if(gt.equals("PlayGameType2Activity"))
{
float triangleCoords[] = { // in counterclockwise order:
1.0f, 1.0f, 0.0f, // top
-1.0f, -1.0f, 0.0f, // bottom left
-1.0f, 1.0f, 0.0f // bottom right
};
}
I usually pass the context of the activity to static methods in another class.
Usually in utility.
This is an example of my utility class. Hope it helps give a vivid idea
public class Utility {
public static void picassoLoader(Context context, ImageView imageView, String url){
Log.d("PICASSO", "loading image");
Picasso.with(context)
.load(url)
//.resize(30,30)
.placeholder(R.drawable.one)
.error(R.drawable.one)
.into(imageView);
}
}
Then in any activity i call
Utility.picassoLoader(this, imageview, url);
passing "this" as the context or pass getContext() method
Utility.picassoLoader(getContext(), imageview, url);
Sorry for using the picasso example, Ive not used OpenGL before
I have a cube with single texture -
public class TextureDemo implements GLEventListener, KeyListener {
private Texture texture;
}
public void init(GLAutoDrawable gLDrawable) {
String filename="Picture1.jpg"; // the FileName to open
texture=TextureIO.newTexture(new File( filename ),true);
}
public void display(GLAutoDrawable gLDrawable) {
texture.bind();
gl.glBegin(GL.GL_QUADS);
// Front Face
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(2f, 0.0f);
gl.glVertex3f(1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(2f, 1.0f);
gl.glVertex3f(1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
...
}
I trying to set multiple texture on the above cube , means after the "Front Face" , replace to another texture which will take effect on the other cube face's .
How could I accomplish this ?
You bind a new texture. That tells GL that whatever we're about to draw will use that texture.
So, create new texture for a different JPG file, then bind it, then draw some vertices.
I have been using JOGL for a few days now and this is now becoming a major road block. I can not get shapes to draw in the correct z-order, instead, they are drawn in the order they are given to OpenGL.
I have spent the last few hours researching this and the general resolutions (and my reactions) seem to be the following:
Make sure your frustum is correct
I have double checked the frustum, it seems correct
I have switched to gluLookAt instead of a custom built frustum
I have switched to glOrthof just to make sure it is not perspective that is the problem.
I have not even set ANY view at all, instead working in the -1, 1 range that seems to be default
Make sure the following calls are in the init:
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
Make sure you are clearing the depth buffer on each redraw
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
I have provided the very basic sample of a program below where this problem is occurring. If you have JOGL and run it, you will see the red triangle at Z-position -0.5f ALWAYS on top while the triangles rotate around each other. If you swap the two triangle vertex calls, the green triangle will then always be on top.
This has been an immense headache for me so any insight will be helpful, either from JOGL or OpenGL in general, but I can't seem to see what is wrong.
Also note that for brevity I removed the proper code to destroy the window.
import java.awt.Frame;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import com.jogamp.opengl.util.Animator;
public class JOGLTest implements GLEventListener
{
static GLU glu = new GLU();
static GLCanvas canvas = new GLCanvas();
static Frame frame = new Frame("JOGL test");
static Animator animator = new Animator(canvas);
float rot = 0.0f;
public void display(GLAutoDrawable glDrawable)
{
final GL2 gl = glDrawable.getGL().getGL2();
rot++;
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glRotatef(rot, 0.0f, 1.0f, 0.0f);
gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(0.0f, 1.0f, 0.0f);
gl.glVertex3f(0.0f, 1.0f, 0.0f);
gl.glVertex3f(-1.0f, -1.0f, 0.0f);
gl.glVertex3f(1.0f, -1.0f, 0.0f);
gl.glColor3f(1.0f, 0.0f, 0.0f);
gl.glVertex3f(-1.0f, 1.0f, -0.5f);
gl.glVertex3f(1.0f, 1.0f, -0.5f);
gl.glVertex3f(0.0f, 0.0f, -0.5f);
gl.glEnd();
}
public void dispose(GLAutoDrawable arg0)
{
}
public void init(GLAutoDrawable glDrawable)
{
GL2 gl = glDrawable.getGL().getGL2();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glDepthFunc(GL.GL_LEQUAL);
}
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
int arg4)
{
}
public static void main(String[] args)
{
canvas.addGLEventListener(new JOGLTest());
frame.add(canvas);
frame.setSize(640, 480);
frame.setVisible(true);
animator.start();
canvas.requestFocus();
}
}
I have solved the problem over much deliberation.
I created a GLCapabilities object and manually set the number of bits for a depth buffer while creating a GLCanvas. The code is as follows:
GLProfile glp = GLProfile.getDefault();
GLCapabilities caps = new GLCapabilities(glp);
caps.setDepthBits(16);
canvas = new GLCanvas(caps);
Just stumbled across your question... are you using an Intel graphics card and an 'older' (pre-24th November) version of JOGL, by any chance? If so, the following may be helpful (ignore the mention of win7 64-bit; the problem described is more general and was solved in that instance in subsequent JOGL releases):
http://jogamp.762907.n3.nabble.com/Depth-buffer-not-working-on-Win7-64b-td1737435.html
Got the same problem, but unlike #david I just set the depthSize in GLData:
GLData data = new GLData ();
data.depthSize = 24;
#jogl #linux