Jump to content
  • Advertisement
Sign in to follow this  
Java_newbie

OpenGL Drawing a Cube

This topic is 3759 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'm new to Java; I've only been programming for a six months or so. I was trying to draw a cube, but I always get "<identifier> expected errors" for my GLAutoDrawable and gLDrawable objects. If it helps, I ran a sample successfully using very similar code. Even suggestions on the efficiency of the code would be appreciated. Thanks a lot for your help! Anyway, this is my code: import javax.media.opengl.GL; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.glu.GLU; import java.awt.event.*; public class CubeRenderer implements GLEventListener, KeyListener { protected float rotateT = 0.0f; protected static final GLU glu = new GLU( ); public void display(GLAutoDrawable gLDrawable) { final GL gl = gLDrawable.getGL( ); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity( ); gl.glTranslatef(0.0f, 0.0f, -5.0f); gl.glRotatef(rotateT, 1.0f, 0.0f, 0.0f); gl.glRotatef(rotateT, 0.0f, 1.0f, 0.0f); gl.glRotatef(rotateT, 0.0f, 0.0f, 1.0f); gl.glRotatef(rotateT, 0.0f, 1.0f, 0.0f); gl.glBegin(GL.GL_QUADS); // Front Face gl.glColor3f(0.0f, 1.0f, 1.0f); gl.glVertex3f(-0.5f, 0.5f, 0.0f); // Top-left gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(0.5f, 0.5f, 0.0f); // Top-right gl.glColor3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(0.5f, -0.5f, 0.0f); // Bottom-right gl.glVertex3f(-0.5f, -0.5f, 0.0f); // Bottom-left // Right Face gl.glColor3f(0.0f, 1.0f, 1.0f); gl.glVertex3f(-0.5f, 0.5f, 1.0f); // Top-left gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(-0.5f, 0.5f, 0.0f); // Top-right gl.glColor3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(-0.5f, -0.5f, 0.0f); // Bottom-right gl.glVertex3f(-0.5f, -0.5f, 1.0f); // Bottom-left // Left Face gl.glColor3f(0.0f, 1.0f, 1.0f); gl.glVertex3f(0.5f, 0.5f, 0.0f); // Top-left gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(0.5f, 0.5f, 1.0f); // Top-right gl.glColor3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(-0.5f, -0.5f, 1.0f); // Bottom-right gl.glVertex3f(-0.5f, -0.5f, 0.0f); // Bottom-left // Top Face gl.glColor3f(0.0f, 1.0f, 1.0f); gl.glVertex3f(-0.5f, 0.5f, 0.0f); // Top-left gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(-0.5f, 0.5f, 1.0f); // Top-right gl.glColor3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(0.5f, 0.5f, 1.0f); // Bottom-right gl.glVertex3f(0.5f, 0.5f, 0.0f); // Bottom-left // Bottom Face gl.glColor3f(0.0f, 1.0f, 1.0f); gl.glVertex3f(-0.5f, -0.5f, 0.0f); // Top-left gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(-0.5f, -0.5f, 1.0f); // Top-right gl.glColor3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(0.5f, -0.5f, 1.0f); // Bottom-right gl.glVertex3f(0.5f, -0.5f, 0.0f); // Bottom-left // Back Face gl.glColor3f(0.0f, 1.0f, 1.0f); gl.glVertex3f(-0.5f, 0.5f, 1.0f); // Top-left gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(0.5f, 0.5f, 1.0f); // Top-right gl.glColor3f(0.0f, 0.0f, 0.0f); gl.glVertex3f(0.5f, 0.5f, 1.0f); // Bottom-right gl.glVertex3f(-0.5f, -0.5f, 1.0f); // Bottom-left gl.glEnd( ); rotateT += 0.2f; } public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) { } public void init(GLAutoDrawable gLDrawable) { final GL gl = new gLDrawable.getGL( ); gl.glShadeModel(GL.GL_SMOOTH); 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); gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); gLDrawable.addKeyListener(this); } public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) { final GL gl = gLDrawable.getGL( ); if (height <= 0) height = 1; final float h = (float)width / (float)(height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity( ); glu.gluPerspective(50.0f, h, 1.0, 1000.0); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity( ); } public void keyPressed(KeyEvent e) { if (e.getKeyCode( ) == KeyEvent.VK_ESCAPE) { CubeFrame.bQuit = true; // Constructor class CubeFrame.displayT = null; // Constructor class System.exit(0); } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } }

Share this post


Link to post
Share on other sites
Advertisement
public void display(GLAutoDrawable, gLDrawable) {

These are names without types, or types without names... either way, it's wrong. A method signature that contains parameters must define both type and name for the parameter.

On closer inspection, this seems to be JOGL code. Just remove all those commas between GLAutoDrawable and gLDrawable in all method signatures.

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!