Textures overlap

When creating a cube with a different texture on each side, the last created side of the cube shows on top of the others. No matter which side is facing towards the user.

The problem only occurs when running in fullscreen.

The code used is shown below by sthorsen.

Thanks in advance tiels.

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import static org.lwjgl.opengl.GL11.*;

public class Texture
{

      /**
       * Texture loading directly from LWJGL examples
       */
      public static final int[] loadTexture(String[] paths) {
            int[] texs = new int[paths.length];
            for (int i=0; i<paths.length; i++)
            {
                  Image image = (new javax.swing.ImageIcon(paths[i])).getImage();
      
                  // Exctract The Image
                  BufferedImage tex = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_3BYTE_BGR);
                  Graphics2D g = (Graphics2D) tex.getGraphics();
                  g.drawImage(image, null, null);
                  g.dispose();
      
                  // Flip Image
                  AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
                  tx.translate(0, -image.getHeight(null));
                  AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
                  tex = op.filter(tex, null);
      
                  // Put Image In Memory
                  ByteBuffer scratch = ByteBuffer.allocateDirect(4 * tex.getWidth() * tex.getHeight());
      
                  byte data[] = (byte[]) tex.getRaster().getDataElements(0, 0, tex.getWidth(), tex.getHeight(), null);
                  scratch.clear();
                  scratch.put(data);
                  scratch.rewind();
      
                  // Create A IntBuffer For Image Address In Memory   
                  IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
                  glGenTextures(buf); // Create Texture In OpenGL   
      
                  glBindTexture(GL_TEXTURE_2D, buf.get(0));
                  // Typical Texture Generation Using Data From The Image
      
                  // Linear Filtering
                  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                  // Linear Filtering
                  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                  // Generate The Texture
                  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex.getWidth(), tex.getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, scratch);
      
                  texs[i]= buf.get(0); // Return Image Address In Memory
            }
            return texs;
      }
}
/* Standard imports.
 */

import org.lwjgl.*;
import org.lwjgl.input.*;
import org.lwjgl.opengl.*;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import org.lwjgl.fmod.FMOD;
import org.lwjgl.fmod.FMODException;
import org.lwjgl.fmod.FSound;
import org.lwjgl.fmod.FSoundStream;


/* Static imports.
 */

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.glu.GLU.*;

import static org.lwjgl.fmod.FMOD.*;
import static org.lwjgl.fmod.FSound.*;

public abstract class LWJGL {


        /** Is game finished ? **/
      protected boolean finished;
      
        /** Parameters **/
      protected boolean fullscreen;
      protected String title;

      /** Lwjgl constructor.
       */

      public LWJGL(String title, boolean fullscreen)
      {
            this.title = title;
            this.fullscreen = fullscreen;
            createWindow();
            this.title = "constructor";
      }

      protected void createWindow()
      {
            try
            {
                  if (fullscreen)
                  {
                        int mode = -1;
                        DisplayMode modes[] = Display.getAvailableDisplayModes();
      
                        for(int i = 0; i < modes.length; i++)
                        {
                              if(modes[i].width == 1024
                                 && modes[i].height == 768
                                 && modes[i].bpp >= 32)
                              {
                                    mode = i;
                                    break;
                              }
                        }
      
                        if(mode != -1)
                        {
                              System.out.println("Setting display mode to " + modes[mode]);
                              Display.setDisplayMode(modes[mode]);
                        }
                        Window.create(title, 32, 0, 0, 0);
                  }
                  else
                        Window.create(title, 0, 0, 800, 600, 32, 0, 8, 0);
            }
            catch(Exception e)
            {
                  System.err.println("Failed to create display/OpenGL mode due to " + e);
                  System.exit(1);
            }
      }


      /** Start OpenGL application.
       */
      
      protected void start()
      {
            title = "start()";
            try
            {
                  init();

                  while(!finished)
                  {
                        if(Window.isCloseRequested())
                              System.exit(0);

                        process_keyboard();
                        render();
                        Window.update();
                  }   
            }
            catch(Throwable t)
            {
                  t.printStackTrace();
            } 
            finally
            {
                  cleanup();
            }
      }



      /** Init OpenGL and FMOD.
       */

      protected void init() throws Exception
      {
            title = "init()";
            loadTextures();
            initGL();
            initFMOD();
      }
 
       protected void loadTextures() {}
 
      protected void initGL() throws Exception
      {
              // Enable keyboard buffer.
            Keyboard.enableBuffer();

        glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
        glShadeModel(GL_SMOOTH); // Enable Smooth Shading
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background  
        glClearDepth(1.0f); // Depth Buffer Setup
        glEnable(GL_DEPTH_TEST); // Enables Depth Testing
        glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do

              // Go into 3D projection mode.
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            gluPerspective(45.0f, (float)Display.getWidth() / (float)Display.getHeight(), 1.0f, 200.0f);
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Background color (r,g,b,alpha)
      }

      protected void initFMOD() {}

      /** Rendering method.
       */

      abstract void render();


      /** Process keyboard events.
       */

      protected void process_keyboard()
      {
            for(int i = 0; i < Keyboard.getNumKeyboardEvents(); i++)
            {
                  Keyboard.next();

                  if(Keyboard.getEventKey() == Keyboard.KEY_ESCAPE && Keyboard.getEventKeyState())
                        finished = true;
                        
                  if(Keyboard.getEventKey() == Keyboard.KEY_F1 && Keyboard.getEventKeyState())
                  {
                        Window.destroy();
                        fullscreen = !fullscreen;
                        createWindow();
                        loadTextures();
                        try
                        {
                              initGL();
                        }
                        catch (Throwable t)
                        {
                              t.printStackTrace();
                              System.exit(0);
                        }
                  }
            }
      }
 


      /** Cleanup.
       */

      protected void cleanup()
      {
            Keyboard.destroy();
            Window.destroy();

            cleanupFMOD();

            try
            {
                  Display.resetDisplayMode();
            }
            catch(Exception e)
            {
                  e.printStackTrace();
            }
      }
      
      protected void cleanupFMOD() {}
}
import static org.lwjgl.opengl.GL11.*;

public class Lesson06 extends LWJGL
{
      //private String[] paths = {"pic1.png","pic2.png"};
      private String[] paths = {       "pic1.png"
                                                ,"pic2.png"
                                                ,"pic3.png"
                                                ,"pic4.png"
                                                ,"pic5.png"
                                                ,"pic6.png"};
      private int[] texs;
      private float xrot; // X Rotation ( NEW )
      private float yrot; // Y Rotation ( NEW )
      private float zrot; // Z Rotation ( NEW )
      
      public Lesson06()
      {
            super("Lesson06",true);
      }
      
      protected void render()
      {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer

            glLoadIdentity(); // Reset The Current Modelview Matrix

            glTranslatef(0.0f, 0.0f, -5.0f); // Move Into The Screen 5 Units
            glRotatef(xrot, 1.0f, 0.0f, 0.0f); // Rotate On The X Axis
            glRotatef(yrot, 0.0f, 1.0f, 0.0f); // Rotate On The Y Axis
            glRotatef(zrot, 0.0f, 0.0f, 1.0f); // Rotate On The Z Axis
            glBindTexture(GL_TEXTURE_2D, texs[0]); // Select Our Texture
            glBegin(GL_QUADS);
                  // Front Face
                  glTexCoord2f(0.0f, 0.0f);
                  glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
                  glTexCoord2f(1.0f, 0.0f);
                  glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
                  glTexCoord2f(1.0f, 1.0f);
                  glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
                  glTexCoord2f(0.0f, 1.0f);
                  glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
            glEnd();
            glBindTexture(GL_TEXTURE_2D, texs[5]); // Select Our Texture
            glBegin(GL_QUADS);
                  // Back Face
                  glTexCoord2f(1.0f, 0.0f);
                  glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
                  glTexCoord2f(1.0f, 1.0f);
                  glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
                  glTexCoord2f(0.0f, 1.0f);
                  glVertex3f(1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
                  glTexCoord2f(0.0f, 0.0f);
                  glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
            glEnd();
            glBindTexture(GL_TEXTURE_2D, texs[2]); // Select Our Texture
            glBegin(GL_QUADS);
                  // Top Face
                  glTexCoord2f(0.0f, 1.0f);
                  glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
                  glTexCoord2f(0.0f, 0.0f);
                  glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
                  glTexCoord2f(1.0f, 0.0f);
                  glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
                  glTexCoord2f(1.0f, 1.0f);
                  glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
            glEnd();
            glBindTexture(GL_TEXTURE_2D, texs[3]);
            glBegin(GL_QUADS);
                  // Bottom Face
                  glTexCoord2f(1.0f, 1.0f);
                  glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
                  glTexCoord2f(0.0f, 1.0f);
                  glVertex3f(1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
                  glTexCoord2f(0.0f, 0.0f);
                  glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
                  glTexCoord2f(1.0f, 0.0f);
                  glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
            glEnd();
            glBindTexture(GL_TEXTURE_2D, texs[1]);
            glBegin(GL_QUADS);
                  // Right face
                  glTexCoord2f(1.0f, 0.0f);
                  glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
                  glTexCoord2f(1.0f, 1.0f);
                  glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
                  glTexCoord2f(0.0f, 1.0f);
                  glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
                  glTexCoord2f(0.0f, 0.0f);
                  glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
            glEnd();
            glBindTexture(GL_TEXTURE_2D, texs[4]);
            glBegin(GL_QUADS);
                  // Left Face
                  glTexCoord2f(0.0f, 0.0f);
                  glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
                  glTexCoord2f(1.0f, 0.0f);
                  glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
                  glTexCoord2f(1.0f, 1.0f);
                  glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
                  glTexCoord2f(0.0f, 1.0f);
                  glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
            glEnd();

            xrot += 0.3f; // X Axis Rotation
            yrot += 0.2f; // Y Axis Rotation
            zrot += 0.4f; // Z Axis Rotation
      }
      
      protected void loadTextures()
      {
            texs=Texture.loadTexture(paths);
      }
      
      public static void main(String[] args)
      {
            (new Lesson06()).start();
      }      
}

You’ve got no depth buffer. Speicifically:
Window.create(title, 32, 0, 0, 0);
should read
Window.create(title, 32, 0, 16, 0);

Cas :slight_smile:

Thank you very much! (from tiels as well)