Jump to content
  • Advertisement
Sign in to follow this  
Razz

OpenGL Weird Error with JOGL

This topic is 2704 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I apologize in advance if this is the wrong section of the forum to post this in.

So I'm trying to get a grip with the various methods in JOGL and have been reading a couple of texts on how to use it effectively. This error popped up when I copied word for word one of the first examples in a textbook I was using. The code is as follows:



import java.awt.*;
import javax.swing.*;
import javax.media.opengl.*; // If thise does not work, try net.java.games.jogl.*, or get the newest version of JOGL.

import com.sun.opengl.util.Animator;


public class JOGLApp extends JFrame {

// Causes our JOGL app to be animated- that is, it keeps calling display()
static Animator anim = null;

public static void main(String[] args) {
final JOGLApp app = new JOGLApp();

// Creates the window in another thread
SwingUtilities.invokeLater (
new Runnable() {
public void run() {
app.setVisible(true);
}
}
);
}

public JOGLApp() {
// Set the JFrame title
super("JOGL Application");

// Kill the process when the JFrame is closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GLCanvas glCanvas = new GLCanvas(new GLCapabilities());
glCanvas.addGLEventListener(new GameGLEventListener());

// Add the GLCanvas just like we would any Component
getContentPane().add(glCanvas, BorderLayout.CENTER);
setSize(500, 300);

centerWindow(this);
}

public void centerWindow(Component frame){
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();

if(frameSize.width > screenSize.width){
frameSize.width = screenSize.width;
}
if(frameSize.height > screenSize.height){
frameSize.height = screenSize.height;
}

frame.setLocation(
(screenSize.width - frameSize.width) >> 1,
(screenSize.height - frameSize.height) >> 1
);

}

}




import javax.media.opengl.*; // If thise does not work, try net.java.games.jogl.*, or get the newest version of JOGL.
import javax.media.opengl.glu.GLU;

public class GameGLEventListener implements GLEventListener {
float rotate = 0;

// Only put stuff here that should happen once, at the beginning of the program
public void init(GLAutoDrawable gld) {
GL gl = gld.getGL();
GLU glu = new GLU();

gl.glClearColor(0, 0, 0, 1);

gl.glViewport(0, 0, 500, 300);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(0, 500, 0, 300);
}

public void display(GLAutoDrawable gld) {
float red = 0;
float green = 0;
float blue = 0;

GL gl = gld.getGL();

gl.glClear(GL.GL_COLOR_BUFFER_BIT);

gl.glPointSize(5);

for(int i=0; i<50; i++){

red -= 0.09;
green -= 0.12;
blue -= 0.15;

if(red < 0.15) red = 1;
if(green < 0.15) green = 1;
if(blue < 0.15) blue = 1;

gl.glColor3f(red, green, blue);

gl.glBegin(GL.GL_POINTS);
gl.glVertex2i(i*10, 150);
gl.glEnd();
}
}

// This function is only called when the window is reshaped. You probably won't need this.
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {}

// This function is only called when the display settings are changed. Again, not often used.
public void displayChanged(GLAutoDrawable drawable, boolean a, boolean B) {}
}



What happens is that the window displays on me screen, flashes the image quickly then becomes a black window. However, if I resize the window, the intended image appears as it should. I can't for the life of me figure out where the error is... can any one of you help?

Thanks a lot!

Share this post


Link to post
Share on other sites
Advertisement
So I've tried adding both gl.glDisable(GL.GL_DEPTH_TEST) and gl.glClear(GL.GL_DEPTH_BUFFER_BIT) to my code (independently and together) and now the problem's got even weirder... Sometimes when the program runs, the scene flickers and disappears, whereas other times it stays on screen without disappearing.

Share this post


Link to post
Share on other sites
some quick thoughts:

1.to center your frame on the screen just use this single line

setLocationRelativeTo(null);

2. you should move your viewport and projection setup calls to the reshape method and replace 500/300 with width/height


3. don't have each point you render in a single begin end "bracket". Do a glBegin infront the loop and a glEnd after it.

4. [color="#1C2837"][color="#000000"]gl[color="#666600"].[color="#000000"]glClear[color="#666600"]([color="#000000"]GL[color="#666600"].[color="#000000"]GL_COLOR_BUFFER_BIT | [color="#1C2837"]GL.GL_DEPTH_BUFFER_BIT[color="#666600"]);


Finally, your Problem is that u don't use an Animator. An Animator is an Thread in JOGL who calls your display function over and over again.

just use these two lines

Animator a = new Animator(canvas);
a.start();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!