Results 1 to 2 of 2
  1. #1
    adam21. is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default SkyBox in OpenGL

    Hy All!

    I have a problem in my opengl project. I want to do a skybox with square. I read the texture and use it with starTexture.enable(); starTexture.bind(); method. But the texture is outside the square. I would like to using the texture inside the square. How can i do it?
    This is the java code:

    Java Code:
    public class Ujskybox implements GLEventListener {
    
        public static void main(String[] args) {
            Frame frame = new Frame("UjSkyBox");
            GLCanvas canvas = new GLCanvas();
    
            canvas.addGLEventListener(new Ujskybox());
            frame.add(canvas);
            frame.setSize(640, 480);
            final Animator animator = new Animator(canvas);
            frame.addWindowListener(new WindowAdapter() {
    
                @Override
                public void windowClosing(WindowEvent e) {
                    // Run this on another thread than the AWT event queue to
                    // make sure the call to Animator.stop() completes before
                    // exiting
                    new Thread(new Runnable() {
    
                        public void run() {
                            animator.stop();
                            System.exit(0);
                        }
                    }).start();
                }
            });
            // Center frame
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            animator.start();
        }
        private Texture starTexture;
        private int _skybox[] = new int[6];
        private IntBuffer starTextures;
        public void init(GLAutoDrawable drawable) {
            // Use debug pipeline
            // drawable.setGL(new DebugGL(drawable.getGL()));
    
            GL gl = drawable.getGL();
            System.err.println("INIT GL IS: " + gl.getClass().getName());
     gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
            try {
                InputStream stream = getClass().getResourceAsStream("stars.png");
                TextureData data = TextureIO.newTextureData(stream, false, "png");
                starTexture = TextureIO.newTexture(data);
            }
            catch (IOException exc) {
                exc.printStackTrace();
                System.exit(1);
            }       
          gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
          gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
            // Enable VSync
            gl.setSwapInterval(1);
    
            // Setup the drawing area and shading mode
            gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
           
        }
    
        public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
            GL gl = drawable.getGL();
            GLU glu = new GLU();
    
            if (height <= 0) { // avoid a divide by zero error!
            
                height = 1;
            }
            final float h = (float) width / (float) height;
            gl.glViewport(0, 0, width, height);
            gl.glMatrixMode(GL.GL_PROJECTION);
            gl.glLoadIdentity();
            glu.gluPerspective(45.0f, h, 1.0, 20.0);
            gl.glMatrixMode(GL.GL_MODELVIEW);
            gl.glLoadIdentity();
        }
    
        public void display(GLAutoDrawable drawable) {
            GLU glu = new GLU();
            GL gl = drawable.getGL();
            gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
            gl.glLoadIdentity();
        gl.glPushMatrix();
        // Reset and transform the matrix.
        gl.glLoadIdentity();
        glu.gluLookAt(1,2,0, 0,0,0,0,1,0);
        // Enable/Disable features
        gl.glPushAttrib(gl.GL_ENABLE_BIT);
        gl.glEnable(gl.GL_TEXTURE_2D);
        gl.glDisable(gl.GL_DEPTH_TEST);
        gl.glDisable(gl.GL_LIGHTING);
        gl.glDisable(gl.GL_BLEND);
        gl.glColor4f(1,1,1,1);
        starTexture.enable();
        starTexture.bind();
        gl.glBegin(GL.GL_QUADS);
            gl.glTexCoord2f(0, 0); gl.glVertex3f(  0.5f, -0.5f, -0.5f );
            gl.glTexCoord2f(1, 0); gl.glVertex3f( -0.5f, -0.5f, -0.5f );
            gl.glTexCoord2f(1, 1); gl.glVertex3f( -0.5f,  0.5f, -0.5f );
            gl.glTexCoord2f(0, 1); gl.glVertex3f(  0.5f,  0.5f, -0.5f );
        gl.glEnd();
        gl.glBegin(GL.GL_QUADS);
            gl.glTexCoord2f(0, 0); gl.glVertex3f(0.5f, -0.5f,  0.5f );
            gl.glTexCoord2f(1, 0); gl.glVertex3f(  0.5f, -0.5f, -0.5f );
            gl.glTexCoord2f(1, 1); gl.glVertex3f(  0.5f,  0.5f, -0.5f );
            gl.glTexCoord2f(0, 1); gl.glVertex3f(  0.5f,  0.5f, 0.5f );
        gl.glEnd();
        gl.glBegin(GL.GL_QUADS);
            gl.glTexCoord2f(0, 0); gl.glVertex3f( -0.5f, -0.5f,  0.5f );
            gl.glTexCoord2f(1, 0); gl.glVertex3f(  0.5f, -0.5f,  0.5f );
            gl.glTexCoord2f(1, 1); gl.glVertex3f(  0.5f,  0.5f,  0.5f );
            gl.glTexCoord2f(0, 1); gl.glVertex3f( -0.5f,  0.5f,  0.5f );
        gl.glEnd();
        gl.glBegin(GL.GL_QUADS);   
            gl.glTexCoord2f(0, 0); gl.glVertex3f( -0.5f, -0.5f, -0.5f );
            gl.glTexCoord2f(1, 0); gl.glVertex3f( -0.5f, -0.5f,  0.5f );
            gl.glTexCoord2f(1, 1); gl.glVertex3f( -0.5f,  0.5f,  0.5f );
            gl.glTexCoord2f(0, 1); gl.glVertex3f( -0.5f,  0.5f, -0.5f );
        gl.glEnd();
        gl.glBegin(GL.GL_QUADS);  
            gl.glTexCoord2f(0, 1); gl.glVertex3f( -0.5f,  0.5f, -0.5f );
            gl.glTexCoord2f(0, 0); gl.glVertex3f( -0.5f,  0.5f,  0.5f );
            gl.glTexCoord2f(1, 0); gl.glVertex3f( 0.5f, 0.5f,  0.5f );
            gl.glTexCoord2f(1, 1); gl.glVertex3f( 0.5f,  0.5f, -0.5f );
        gl.glEnd();
        gl.glBegin(GL.GL_QUADS);
            gl.glTexCoord2f(0, 0); gl.glVertex3f( -0.5f, -0.5f, -0.5f );
            gl.glTexCoord2f(0, 1); gl.glVertex3f( -0.5f, -0.5f,  0.5f );
            gl.glTexCoord2f(1, 1); gl.glVertex3f(  0.5f, -0.5f,  0.5f );
            gl.glTexCoord2f(1, 0); gl.glVertex3f(  0.5f, -0.5f, -0.5f );
        gl.glEnd();
        gl.glPopAttrib();
        gl.glPopMatrix();
        }
    
        public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
        }
    }
    This is Java code with JOGL. Thanks for your reply.

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: SkyBox in OpenGL

    This is likely to be an OpenGL problem, you should try reading up on your opengl and find what you need to do.

Similar Threads

  1. Got problem with Java OpenGL
    By Igbear in forum Advanced Java
    Replies: 1
    Last Post: 05-07-2011, 07:51 PM
  2. OpenGl transformation question newbie
    By ankitmcgill in forum New To Java
    Replies: 1
    Last Post: 11-16-2008, 03:46 AM
  3. SWT OpenGL snippet: draw a square
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 09:29 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
  •