PDA

View Full Version : SkyBox in OpenGL



adam22.
10-28-2011, 01:16 PM
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:



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.

BionicBytes
10-29-2011, 06:05 AM
Firstly you need to bind a skybox face texture for each face of the skybox. Currently you only bind the texture once.
To make the texture appear inside the cube you need to change the polygon winding. You can use glFrontFace if you wish or just reorder the glVertex calls in the reverse order.