1

This is my attempt at making a cube map.

glDisable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_CUBE_MAP);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
PNGDecoder pngDecoder = new PNGDecoder(ResourceLoader.getResourceAsStream("C:/desert_skymap_sample.png"));
ByteBuffer temp = ByteBuffer.allocateDirect(4*pngDecoder.getWidth() * pngDecoder.getHeight());
pngDecoder.decode(temp, pngDecoder.getWidth()*4, PNGDecoder.Format.RGBA);
temp.flip();
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X,0,GL_RGBA,pngDecoder.getWidth(), pngDecoder.getHeight(),0,GL_RGBA,GL_UNSIGNED_BYTE,temp);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X,0,GL_RGBA,pngDecoder.getWidth(), pngDecoder.getHeight(),0,GL_RGBA,GL_UNSIGNED_BYTE,temp);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y,0,GL_RGBA,pngDecoder.getWidth(), pngDecoder.getHeight(),0,GL_RGBA,GL_UNSIGNED_BYTE,temp);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,0,GL_RGBA,pngDecoder.getWidth(), pngDecoder.getHeight(),0,GL_RGBA,GL_UNSIGNED_BYTE,temp);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z,0,GL_RGBA,pngDecoder.getWidth(), pngDecoder.getHeight(),0,GL_RGBA,GL_UNSIGNED_BYTE,temp);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,0,GL_RGBA,pngDecoder.getWidth(), pngDecoder.getHeight(),0,GL_RGBA,GL_UNSIGNED_BYTE,temp);


    gluLookAt(0,0,0, xtrans+xpos,ytrans+walkbias,ztrans+zpos, 0,1,0);

    glPushAttrib(GL_ENABLE_BIT);
    glDisable(GL_DEPTH_TEST);

    glColor4f(1,1,1,1);

    glBindTexture(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_CUBE_MAP_POSITIVE_X);
    glBegin(GL_QUADS);
      glTexCoord2f(0, 0);
      glVertex3f(1f, -1f, -1f);
      glTexCoord2f(1, 0);
      glVertex3f(-1f, -1f, -1f);
      glTexCoord2f(1, 1);
      glVertex3f(-1f, 1f, -1f);
      glTexCoord2f(0, 1);
      glVertex3f(1f, 1f, -1f);
    glEnd();

    glBindTexture(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_CUBE_MAP_NEGETIVE_X);
    glBegin(GL_QUADS);
        glTexCoord2f(0, 0);
        glVertex3f(1f, -1f, 1f);
        glTexCoord2f(1, 0);
        glVertex3f(1f, -1f, -1f);
        glTexCoord2f(1, 1);
        glVertex3f(1f, 1f, -1f);
        glTexCoord2f(0, 1);
        glVertex3f(1f, 1f, 1f);
    glEnd();

I am using TWL's PngDecoder. The probelm is I do not know how to get the texture id/retreve the TexImage2D or if I am doing it correctly.

How do I make a cubemap in LWJGL?

Coupon22
  • 395
  • 8
  • 24

2 Answers2

1

I think you will want a gluLookAt call in there somewhere at the beginning to make set up the camera transform properly.

Have a look at:

The camera->x, camera->y, camera->z elements in the example above should be replaced with a vector that describes the direction that your camera is looking.

mikera
  • 105,238
  • 25
  • 256
  • 415
1

There are a few things that you will need to add to the code to get it to work:

  1. As mikera mentioned, you'll need to setup the 'camera' with glLookAt.
  2. You also need to define the geometry to render the texture onto. Easiest for this would be to define a box around the camera in 2d space. Don't forget to setup the face normals aswell.

There is quite an extensive write-up about hardware cube mapping with opengl here (it's a multi-vendor extension): http://www.nvidia.com/object/cube_map_ogl_tutorial.html

LintfordPickle
  • 420
  • 4
  • 13
  • I looked at that tutorial, but when I tried to run the example it ran an error for not having GL_EXT_TEXTURE_CUBE_MAP. I don't understand the error, I have OpenGL 3.0. – Coupon22 Oct 08 '12 at 13:46
  • Also, what are you saying with number 2? – Coupon22 Oct 08 '12 at 18:57
  • I see that you have now added the drawing code in the original question. With point 2 I meant that you need to setup some geometry onto which to map the cube texture (as I say, originally I couldn't see your draw code). Also, according to the article I linked, some of the generated texture coordinates depend on you properly setting up the vertex normals on your geometry. – LintfordPickle Oct 10 '12 at 06:27
  • Regarding the error you received, it looks like you need to include the multi-vendor cube map extension. You can check to see if it is already included in your opengl version with glGetString(GL_EXTENSIONS) to make sure the GL_EXT_texture_cube_map string is listed. – LintfordPickle Oct 10 '12 at 06:33
  • I don't know what I did, but the tutorial works now. Thank you. – Coupon22 Oct 10 '12 at 12:36