Results 1 to 3 of 3
  1. #1
    Mr. Java is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default Backface culling jogl

    Hello I made my first opengl app in jogl but I cant get the back face culling to work. I think maybe I need to have the normals. It draws a 3d rectangle with the faces in counter clock wise direction.

    Here's my code:
    Java Code:
    package javagame2;
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import net.java.games.jogl.Animator;
    import net.java.games.jogl.GL;
    import net.java.games.jogl.GL.*;
    import net.java.games.jogl.GLCanvas;
    import net.java.games.jogl.GLCapabilities;
    import net.java.games.jogl.GLDrawable;
    import net.java.games.jogl.GLDrawableFactory;
    import net.java.games.jogl.GLEventListener;
    import net.java.games.jogl.GLJPanel;
    import net.java.games.jogl.GLU;
    
    public class Main
    {
        static float rotateT = 0;
        public static void main(String[] args)
        {
            GLCapabilities capabilities = new GLCapabilities();
            
            final GLCanvas canvas =
                GLDrawableFactory.getFactory().createGLCanvas(capabilities);
            final Frame frame=new Frame("- - - - -");
            frame.add(canvas);
            frame.setSize(640, 480);
            //frame.setUndecorated(true);
    	//frame.setExtendedState(Frame.MAXIMIZED_BOTH);
            frame.show();
            canvas.requestFocus();
            final Animator animator = new Animator(canvas);
    
            animator.start();
            
            frame.addWindowListener(new WindowListener() {
    
                public void windowOpened(WindowEvent e) {
                }
    
                public void windowClosing(WindowEvent e) {
                    animator.stop();
                    frame.dispose();
                    System.exit(0);
                }
    
                public void windowClosed(WindowEvent e) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
    
                public void windowIconified(WindowEvent e) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
    
                public void windowDeiconified(WindowEvent e) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
    
                public void windowActivated(WindowEvent e) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
    
                public void windowDeactivated(WindowEvent e) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            });
    
            canvas.addGLEventListener(new GLEventListener() {
                public void init(GLDrawable gld) {
                    GL gl = gld.getGL();
                    gl.glShadeModel(GL.GL_SMOOTH);
                    gl.glEnable(GL.GL_CULL_FACE);
                    gl.glCullFace(GL.GL_BACK);
                    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
                    gl.glClearDepth(1.0f);
                    gl.glEnable(GL.GL_DEPTH);
                    gl.glDepthFunc(GL.GL_LEQUAL);
                    gl.glPolygonMode(GL.GL_FRONT, GL.GL_FILL);
                    gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT,
                              GL.GL_NICEST);
    
                }
    
                public void display(GLDrawable gld) {
                    GL gl = gld.getGL();
                    GLU glu = gld.getGLU();
                    gl.glClear(gl.GL_COLOR_BUFFER_BIT);
                    gl.glClear(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_TRIANGLES);
    
                    gl.glColor3f(0.0f, 1.0f, 0.0f);
                    gl.glVertex3f(0.0f, 1.0f, 0.0f);
                    gl.glColor3f(0.0f, -1.0f, 0.0f);
                    gl.glVertex3f(0.0f, -1.0f, 0.0f);
                    gl.glColor3f(0.0f, 0.0f, 0.0f);
                    gl.glVertex3f(0.0f, -1.0f, 1.0f);
    
                    gl.glColor3f(0.0f, 1.0f, 1.0f);
                    gl.glVertex3f(0.0f, 1.0f, 0.0f);
                    gl.glColor3f(0.0f, 0.0f, 1.0f);
                    gl.glVertex3f(0.0f, -1.0f, 1.0f);
                    gl.glColor3f(0.0f, 0.0f, 0.0f);
                    gl.glVertex3f(0.0f, 1.0f, 1.0f);
                    
    
                    gl.glEnd();
    
                    frame.validate();
    
                    rotateT += 0.5f;
    
                    gld.addKeyListener(new KeyListener() {
    
                        public void keyTyped(KeyEvent e) {
                            throw new UnsupportedOperationException("Not supported yet.");
                        }
    
                        public void keyPressed(KeyEvent e) {
                            if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
                            {
                                animator.stop();
                                frame.dispose();
                                System.exit(0);
                            }
                        }
    
                        public void keyReleased(KeyEvent e) {
                            throw new UnsupportedOperationException("Not supported yet.");
                        }
                    });
                }
    
                public void reshape(GLDrawable gld, int x, int y, int width, int height) {
                    GLU glu = gld.getGLU();
                    GL gl = gld.getGL();
                    if(height <= 0) {
                        height = 1;
                    }
                    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 displayChanged(GLDrawable gld, boolean bln, boolean bln1) {
                    
                }
            });
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    It draws a 3d rectangle with the faces in counter clock wise direction.
    Is that good or bad?
    Please explain what it is you want.

  3. #3
    Mr. Java is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Installing JOGL
    By RandomTree in forum New To Java
    Replies: 5
    Last Post: 12-21-2012, 10:26 AM
  2. Jogl
    By Supamagier in forum Advanced Java
    Replies: 2
    Last Post: 12-21-2012, 10:26 AM
  3. JOGL lightmapping
    By deepthought in forum Advanced Java
    Replies: 1
    Last Post: 06-11-2010, 06:45 PM
  4. JOGL and texturing
    By Archange in forum Java 2D
    Replies: 0
    Last Post: 06-06-2008, 03:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •