problem about skybox -- looking for help

Hi there,

I am writing a game that needs the skybox. I am fresh to JOGL, so I hope someone can help me with this problem.

I create a 400200 square as the floor and want to add a skybox to cover it. I put the draw_skybox method in the initRender() method, but I cannot see the result. When I use 2020 square as the floor, I can see the skybox.

My code structure is below.

//=========================================================================================

import …

class game implements Runnable
{
Texture skybox;

public void run()
{
initRender();
renderLoop();

// discard the rendering context and exit
context.destroy();
System.exit(0);
} // end of run()

private void initRender()
{
makeContentCurrent();

gl = context.getGL();
glu = new GLU();
glut = new GLUT();

gl.glClearColor(0.17f, 0.65f, 0.92f, 1.0f); // sky blue
gl.glClearDepth(1.0f);
gl.glEnable(GL.GL_DEPTH_TEST); // z- (depth) selectBuffer initialization for hidden surface removal
gl.glShadeModel(GL.GL_SMOOTH); // use smooth shading
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

addLight();
loadTextures();
drawSkyBox();
}

The drawSkyBox() method is below. I follow the “Java Prog. Techniques for Games. JOGL 2: Touring” written by Andrew Davison.

private void drawSkyBox()
{
gl.glDisable(GL.GL_LIGHTING);
// enable texturing and choose the ‘stars’ texture
gl.glEnable(GL.GL_TEXTURE_2D);
skyTex.bind();
// replace the quad colours with the texture
gl.glTexEnvf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_ENV_MODE,GL.GL_REPLACE);
gl.glBegin(GL.GL_QUADS);
// back wall
int edge = 200;
gl.glTexCoord2i(0, 0); gl.glVertex3i(-edge, 0, -edge);
gl.glTexCoord2i(1, 0); gl.glVertex3i(edge, 0, -edge);
gl.glTexCoord2i(1, 1); gl.glVertex3i(edge, edge, -edge);
gl.glTexCoord2i(0, 1); gl.glVertex3i(-edge, edge, -edge);
// right wall
gl.glTexCoord2i(0, 0); gl.glVertex3i(edge, 0, -edge);
gl.glTexCoord2i(1, 0); gl.glVertex3i(edge, 0, edge);
gl.glTexCoord2i(1, 1); gl.glVertex3i(edge, edge, edge);
gl.glTexCoord2i(0, 1); gl.glVertex3i(edge, edge, -edge);
// front wall
gl.glTexCoord2i(0, 0); gl.glVertex3i(edge, 0, edge);
gl.glTexCoord2i(1, 0); gl.glVertex3i(-edge, 0, edge);
gl.glTexCoord2i(1, 1); gl.glVertex3i(-edge, edge, edge);
gl.glTexCoord2i(0, 1); gl.glVertex3i(edge, edge, edge);
// left wall
gl.glTexCoord2i(0, 0); gl.glVertex3i(-edge, 0, edge);
gl.glTexCoord2i(1, 0); gl.glVertex3i(-edge, 0, -edge);
gl.glTexCoord2i(1, 1); gl.glVertex3i(-edge, edge, -edge);
gl.glTexCoord2i(0, 1); gl.glVertex3i(-edge, edge, edge);
// ceiling
gl.glTexCoord2i(0, 0); gl.glVertex3i(edge, edge, edge);
gl.glTexCoord2i(2, 0); gl.glVertex3i(-edge, edge, edge);
gl.glTexCoord2i(2, 2); gl.glVertex3i(-edge, edge, -edge);
gl.glTexCoord2i(0, 2); gl.glVertex3i(edge, edge, -edge);
gl.glEnd();
// switch back to modulation of quad colours and texture
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE,
GL.GL_MODULATE);
gl.glDisable(GL.GL_TEXTURE_2D);
gl.glEnable(GL.GL_LIGHTING);
}

I am still learning JOGL, so please help me with this.

Thanks a lot !

Hi anyegongjue,

if the change from a 2020 floor to a 400200 floor changes the behavior of the skybox. I suggest you problem is in the method, which renders the floor.

Are you only new to jogl or also new to OpenGL. Keep in mind that OpenGL is a state engine. So if you change a state in one method it is still changed in all following methods.

Cheers,

Torsten

Hi wiederke,

Thanks for reply. And I am new to JOGL and OPENGL. So I just get confused.

If I make a change to the size of the floor, how can I change the other method? When I draw the skybox, I already change the edge boundary. How can I make the change? I use the following method to draw floor.

private void drawFloor()
{
gl.glDisable(GL.GL_LIGHTING);

if (floorTex != null)
{
floorTex.enable();
gl.glEnable(GL.GL_TEXTURE_2D);
floorTex.bind();
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);

  TextureCoords tc = floorTex.getImageTexCoords();
  float left = tc.left()*40;
  float right = tc.right()*40;
  float bottom = tc.bottom()*40;
  float top = tc.top()*40;
    
  gl.glBegin(GL.GL_QUADS);         // draw floor
  gl.glTexCoord2f(left, bottom);gl.glVertex3f(0.0f, 0.0f, 0.0f);
  gl.glTexCoord2f(right, bottom);gl.glVertex3f(400.0f, 0.0f, 0.0f);
  gl.glTexCoord2f(right, top);gl.glVertex3f(400.0f, 0.0f, -200.0f);
  gl.glTexCoord2f(left, top);gl.glVertex3f(0.0f, 0.0f, -200.0f);
  gl.glEnd();

}
gl.glEnable(GL.GL_LIGHTING);
} // end of CheckerFloor()

Looking at your code, I wonder whether using GLEventListener would be better, since it calls you at the approriate times. For example:


class MyClass extends GLCanvas implements GLEventListener, Runnable {

   MyClass () {
      addGLEventListener(this);
   }

   // ... followed by the implementations of the GLEventListener and Runnable methods

  public void run () {
        while ( true ) {
            this.repaint();
    
            // Used to reduce the FPS
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
  }
} 

The code in the run method is only necessary if you need to implement code that needs to happen, without user interaction.

Some other observations, is that you sky texture is going to be repeated and not stretched:


  gl.glTexCoord2i(0, 2);

0.0 - 1.0, is the extent of the texture and anything beyond that will cause it to repeat. So, 0.0 - 2.0 will cause the texture to be repeated twice.

I also noticed in my tests that anything beyond -50, on the z-axis, resulted in cropping during the rotation (I am tried grafting your code onto a skeleton I use for tests). I am not an expert in OpenGL myself, so I am not sure why this is the case.

The clipping is probably a result from the far z plane clipping. Generally if you followed a tutorial and it might potentially be set by default, but the far z plane is generally set to 50, meaning anything past -50 on the z-axis is cut off.

Hi guys,

Thank you very much for hellping me!

So, I will try to change the position of the floor. And is there any clipping or limited along Z+ direction?

Thanks.

Basically, if you’re using gluPerspective() you specify a near z distance and a far z distance. Both of these are positive values representing distance along the camera’s z axis for clipping purposes. Don’t make near z <= 0 or you will see odd visual results.