LWJGL Forum

Programming => OpenGL => Topic started by: andretti on March 26, 2005, 14:08:39

Title: Problem of visualization during intersection of surfaces
Post by: andretti on March 26, 2005, 14:08:39
Hi, i've coded an algorithm to generate a terrain surface. I store all the height values in a matrix and then, using GL_QUADS, i draw the surface. And everything is ok. The problem comes when i draw a simple and stupid box to simulate water level: when the two surfaces (water and terrain) intersect, i come into problem of visualization. Sometimes i can see regions that should be underwater! And only by changing the viewing angle i can see or not see the water! Sorry, but my english is poor and i don't know exactly how to explain the effect! Then i made an experiment: i draw two rectangles ortogonal and make them intersect and i see the same problem of visualization: the intersection of the two object always produce a strange effect similar to a flickering or a distorsion, not a perfect intersection. I post the initialization code (base on NeHe's tut) and the render method. I think this is a problem of Depth testing but i don't know how to solve it. Can somebody help me? Thank you a lot.

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

      GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
      GL11.glLoadIdentity(); // Reset The Projection Matrix

      // Calculate The Aspect Ratio Of The Window
      GLU.gluPerspective(45.0f, (float) displayMode.getWidth() / (float) displayMode.getWidth(),0.1f,400.0f);
      GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix

      // Really Nice Perspective Calculations
      GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
      ByteBuffer temp = ByteBuffer.allocateDirect(16);
      temp.order(ByteOrder.nativeOrder());
      GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip());              // Setup The Ambient Light
      GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip());              // Setup The Diffuse Light
      GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip());         // Position The Light
      GL11.glEnable(GL11.GL_LIGHT1);                          // Enable Light One
  }


private boolean render() {
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);          // Clear The Screen And The Depth Buffer

      GL11.glLoadIdentity();                          // Reset The Current Modelview Matrix

      GL11.glTranslatef(-10.0f,-10.0f,z);                     // Translate Into/Out Of The Screen By z

      GL11.glRotatef(xrot,1.0f,0.0f,0.0f);                        // Rotate On The X Axis By xrot
      GL11.glRotatef(yrot,0.0f,1.0f,0.0f);                        // Rotate On The Y Axis By yrot
      GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture[filter]);                // Select A Texture Based On filter

      if (sea) {
      GL11.glBegin(GL11.GL_QUADS);                            // Start Drawing the sea rectangle in beautiful blue!
             GL11.glColor3f(0.313f,0.313f,0.902f);
             //Disegno livello del mare
             GL11.glNormal3f( 0.0f, -1.0f, 0.0f);
             GL11.glVertex3f(0f,0f,0f);
             GL11.glVertex3f(0f,0f,(float)(latoMondo));
             GL11.glVertex3f((float)(latoMondo),0f,(float)(latoMondo));
             GL11.glVertex3f((float)(latoMondo),0f,0f);
           
             GL11.glNormal3f( 0.0f, 1.0f, 0.0f);


             GL11.glVertex3f(0f,(livelloMare*normalFactor),0f);
            GL11.glVertex3f(0f,(livelloMare*normalFactor),(float)(latoMondo));
            GL11.glVertex3f((float)(latoMondo),(livelloMare*normalFactor),(float)(latoMondo));
             GL11.glVertex3f((float)(latoMondo),(livelloMare*normalFactor),0f);

             GL11.glNormal3f( -1.0f, 0.0f, 0.0f);
             GL11.glVertex3f(0f,0f,0f);
            GL11.glVertex3f(0f,0f,(float)(latoMondo));
            GL11.glVertex3f(0f,(livelloMare*normalFactor),(float)(latoMondo));
             GL11.glVertex3f(0f,(livelloMare*normalFactor),0f);

             GL11.glNormal3f( 1.0f, 0.0f, 0.0f);
             GL11.glVertex3f((float)(latoMondo),0f,0f);
            GL11.glVertex3f((float)(latoMondo),0f,(float)(latoMondo));
            GL11.glVertex3f((float)(latoMondo),(livelloMare*normalFactor),(float)(latoMondo));
             GL11.glVertex3f((float)(latoMondo),(livelloMare*normalFactor),0f);

             GL11.glNormal3f( 0.0f, 0.0f, -1.0f);
             GL11.glVertex3f(0f,0f,0f);
            GL11.glVertex3f((float)(latoMondo),0f,0f);
            GL11.glVertex3f((float)(latoMondo),(livelloMare*normalFactor),0f);
             GL11.glVertex3f(0f,(livelloMare*normalFactor),0f);

             GL11.glNormal3f( 0.0f, 0.0f, 1.0f);
             GL11.glVertex3f(0f,0f,(float)(latoMondo));
            GL11.glVertex3f((float)(latoMondo),0f,(float)(latoMondo));
            GL11.glVertex3f((float)(latoMondo),(livelloMare*normalFactor),(float)(latoMondo));
             GL11.glVertex3f(0f,(livelloMare*normalFactor),(float)(latoMondo));

      GL11.glEnd();                               // Done Drawing Sea
     }


      GL11.glBegin(GL11.GL_QUADS);                            // Start Drawing Terrain

      for (int ax=0;ax<map-1;ax++) {
         for (int az=0;az<map-1;az++) {
            choose(altezze[ax][az]);
            GL11.glVertex3f(ax,altezze[ax][az],az);
            choose(altezze[ax+1][az]);
            GL11.glVertex3f(ax+1,altezze[ax+1][az],az);
            choose(altezze[ax+1][az+1]);
            GL11.glVertex3f(ax+1,altezze[ax+1][az+1],az+1);
            choose(altezze[ax][az+1]);
            GL11.glVertex3f(ax,altezze[ax][az+1],az+1);
            }
      }
      GL11.glEnd();                               // Done Drawing terrain



      xrot+=xspeed;                               // Add xspeed To xrot
      yrot+=yspeed;                               // Add yspeed To yrot
      return true;
  }
Title: Problem of visualization during intersection of surfaces
Post by: napier on March 26, 2005, 16:04:21
I haven't looked closely at your code (have to run out in a minute), so this is a quick thought:  how many bits are you using for depth testing?

I create Display like so:


int depthBufferBits = 24;
Display.create(new PixelFormat(0, depthBufferBits, 0));  // set bits per buffer: alpha, depth, stencil


When I used 8 bits for depth buffer, or left this at the default value (I think it's 8 ).  my depth testing got odd results, mostly visible when two objects intersected in a large scene.
Title: Problem of visualization during intersection of surfaces
Post by: andretti on March 26, 2005, 17:52:47
I tried your solution changing Dysplay.create() with the code you wrote. But i got an exception like this "Could not apply pixel format to window". I post the code of a really silly change made to NeHe's Lesson07 that show you very well the effect i want to remove! This code makes two quads (almost perpendicular) intersect themselves. Try rotating a little and then stop rotation with S key when you can see well the two quads (You starts perpendicular to one of them). Then, looking at the two quads intersecting themselves (there's a sort of movement!) go far from them using "Page Up Key" (next or prior key, i don't know how you call them!). Look at the intersection...when you reach a particular distance you won't see them intersect anymore!! This is a very ugly effect!! How can i fix? Remeber to put a file called map.bmp (or change the code appropriately!) in a dir called Data. Thank you very much!

/*
*      This Code Was Created By Jeff Molofee 2000
*      A HUGE Thanks To Fredric Echols For Cleaning Up
*      And Optimizing The Base Code, Making It More Flexible!
*      If You've Found This Code Useful, Please Let Me Know.
*      Visit My Site At nehe.gamedev.net
*/

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.FloatBuffer;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.glu.GLU;
import org.lwjgl.devil.IL;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.PixelFormat;

/**
* @author Mark Bernard
* date:    16-Nov-2003
*
* Port of NeHe's Lesson 7 to LWJGL
* Title: Texture Filters, Lighting & Keyboard Control
* Uses version 0.94alpha of LWJGL http://www.lwjgl.org/
*
* Be sure that the LWJGL libraries are in your classpath
*
* Ported directly from the C++ version
*
* 2004-05-08: Updated to version 0.9alpha of LWJGL.
*             Changed from all static to all instance objects.
* 2004-09-22: Updated to version 0.92alpha of LWJGL.
* 2004-12-19: Updated to version 0.94alpha of LWJGL and to use
*             DevIL for image loading.
*/
public class Miniter2 {
  private boolean done = false;
  private boolean fullscreen = false;
  private final String windowTitle = "NeHe's OpenGL Lesson 7 for LWJGL (Texture Filters, Lighting & Keyboard Control)";
  private boolean f1 = false;
  private DisplayMode displayMode;

  private boolean light;                                      // Lighting ON / OFF
  private boolean lp;                                         // L Pressed?
  private boolean fp;                                         // F Pressed?
  private float xrot;                                         // X Rotation
  private float yrot;                                         // Y Rotation
  private float xspeed;                                       // X Rotation Speed
  private float yspeed;                                       // Y Rotation Speed
  private float z = -5.0f;                                        // Depth Into The Screen
  private float lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f };  // Ambient Light Values ( NEW )
  private float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };      // Diffuse Light Values ( NEW )
  private float lightPosition[] = { 0.0f, 0.0f, 2.0f, 1.0f }; // Light Position ( NEW )
  private int filter;                                         // Which Filter To Use
  private int texture[] = new int[3];                         // Storage for 3 textures
  private boolean keys[] = new boolean[9];
  public float startx=20f;

  public static void main(String args[]) {
      boolean fullscreen = false;
      if(args.length>0) {
          if(args[0].equalsIgnoreCase("fullscreen")) {
              fullscreen = true;
          }
      }

      Miniter2 l7 = new Miniter2();
      l7.run(fullscreen);
  }
  public void run(boolean fullscreen) {
      this.fullscreen = fullscreen;
      try {
          init();
          while (!done) {
              mainloop();
              render();
              Display.update();
          }
          cleanup();
      }
      catch (Exception e) {
          e.printStackTrace();
          System.exit(0);
      }
  }
  private void mainloop() {
      if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {       // Exit if Escape is pressed
          done = true;
      }
      if(Display.isCloseRequested()) {                     // Exit if window is closed
          done = true;
      }
      if(Keyboard.isKeyDown(Keyboard.KEY_F1) && !f1) {    // Is F1 Being Pressed?
          f1 = true;                                      // Tell Program F1 Is Being Held
          switchMode();                                   // Toggle Fullscreen / Windowed Mode
      }
      if(!Keyboard.isKeyDown(Keyboard.KEY_F1)) {          // Is F1 Being Pressed?
          f1 = false;
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_L) && !lp) {
          lp=true;
          light=!light;
          if (!light)                             // If Not Light
          {
              GL11.glDisable(GL11.GL_LIGHTING);       // Disable Lighting
          }
          else                                    // Otherwise
          {
              GL11.glEnable(GL11.GL_LIGHTING);        // Enable Lighting
          }
      }
      else if (!Keyboard.isKeyDown(Keyboard.KEY_L)) {
          lp=false;
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_F) && !fp) {    // Is F Key Being Pressed?
          fp=true;                                // fp Becomes TRUE
          filter+=1;                              // filter Value Increases By One
          if (filter>2            )               // Is Value Greater Than 2?
          {
              filter=0;                           // If So, Set filter To 0
          }
      }
      else if (!Keyboard.isKeyDown(Keyboard.KEY_F)) {     // Has F Key Been Released?
          fp=false;                               // If So, fp Becomes FALSE
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_PRIOR)) {       // Is Page Up Being Pressed?
          z-=1f;                               // If So, Move Into The Screen
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_NEXT)) {        // Is Page Down Being Pressed?
          z+=1f;                               // If So, Move Towards The Viewer
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {          // Is Up Arrow Being Pressed?
          xspeed-=0.01f;                          // If So, Decrease xspeed
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {        // Is Down Arrow Being Pressed?
          xspeed+=0.01f;                          // If So, Increase xspeed
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {       // Is Right Arrow Being Pressed?
          yspeed+=0.01f;                          // If So, Increase yspeed
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {        // Is Left Arrow Being Pressed?
          yspeed-=0.01f;                          // If So, Decrease yspeed
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_S)) {        // Is Left Arrow Being Pressed?
          yspeed=0.0f;                          // If So, Decrease yspeed
          xspeed=0.0f;                          // If So, Decrease yspeed
      }
  }
  private void switchMode() {
      fullscreen = !fullscreen;
      try {
          Display.setFullscreen(fullscreen);
      }
      catch(Exception e) {
          e.printStackTrace();
      }
  }

  private boolean render() {
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);          // Clear The Screen And The Depth Buffer

      GL11.glLoadIdentity();                          // Reset The Current Modelview Matrix

      GL11.glTranslatef(0.0f,0.0f,z);                     // Translate Into/Out Of The Screen By z

      GL11.glRotatef(xrot,1.0f,0.0f,0.0f);                        // Rotate On The X Axis By xrot
      GL11.glRotatef(yrot,0.0f,1.0f,0.0f);                        // Rotate On The Y Axis By yrot
      GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture[filter]);                // Select A Texture Based On filter

      startx=startx-0.5f;
      if (startx<-20f)
         startx=20f;

      GL11.glBegin(GL11.GL_QUADS);                            // Start Drawing Quads
                   GL11.glColor3f(0.313f,0.313f,0.902f);
                   //Disegno livello del mare

                 GL11.glNormal3f(0.0f,-1.0f,0.0f);
                 GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(startx,0f,-5.0f);
                 GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(startx+10f,0f,-5.0f);
                 GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(startx+10f,0f,5f);
                 GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(startx,0f,5f);

                 GL11.glColor3f(1f,0f,0f);
                 GL11.glNormal3f(0.0f,0.0f,-1.0f);
                 GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-5.0f,5f,2f);
                 GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(5f,5f,2f);
                 GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(5f,-5f,0f);
                 GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-5f,-5f,0f);


      GL11.glEnd();


      /*
      GL11.glBegin(GL11.GL_QUADS);                            // Start Drawing Quads
      // Front Face
      GL11.glNormal3f( 0.0f, 0.0f, 1.0f);                 // Normal Pointing Towards Viewer
      GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);    // Point 1 (Front)
      GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);    // Point 2 (Front)
      GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);    // Point 3 (Front)
      GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);    // Point 4 (Front)
      // Back Face
      GL11.glNormal3f( 0.0f, 0.0f,-1.0f);                 // Normal Pointing Away From Viewer
      GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);    // Point 1 (Back)
      GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);    // Point 2 (Back)
      GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);    // Point 3 (Back)
      GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);    // Point 4 (Back)
      // Top Face
      GL11.glNormal3f( 0.0f, 1.0f, 0.0f);                 // Normal Pointing Up
      GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);    // Point 1 (Top)
      GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);    // Point 2 (Top)
      GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);    // Point 3 (Top)
      GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);    // Point 4 (Top)
      // Bottom Face
      GL11.glNormal3f( 0.0f,-1.0f, 0.0f);                 // Normal Pointing Down
      GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);    // Point 1 (Bottom)
      GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);    // Point 2 (Bottom)
      GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);    // Point 3 (Bottom)
      GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);    // Point 4 (Bottom)
      // Right face
      GL11.glNormal3f( 1.0f, 0.0f, 0.0f);                 // Normal Pointing Right
      GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f, -1.0f);    // Point 1 (Right)
      GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f, -1.0f);    // Point 2 (Right)
      GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f( 1.0f,  1.0f,  1.0f);    // Point 3 (Right)
      GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f( 1.0f, -1.0f,  1.0f);    // Point 4 (Right)
      // Left Face
      GL11.glNormal3f(-1.0f, 0.0f, 0.0f);                 // Normal Pointing Left
      GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f, -1.0f);    // Point 1 (Left)
      GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(-1.0f, -1.0f,  1.0f);    // Point 2 (Left)
      GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f,  1.0f);    // Point 3 (Left)
      GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(-1.0f,  1.0f, -1.0f);    // Point 4 (Left)
      GL11.glEnd();                               // Done Drawing Quads
      */

      xrot+=xspeed;                               // Add xspeed To xrot
      yrot+=yspeed;                               // Add yspeed To yrot
      return true;
  }
  private void createWindow() throws Exception {
      Display.setFullscreen(fullscreen);
      DisplayMode d[] = Display.getAvailableDisplayModes();
      for (int i = 0; i < d.length; i++) {
          if (d.getWidth() == 640
              && d.getHeight() == 480
              && d.getBitsPerPixel() == 32) {
              displayMode = d;
              break;
          }
      }
      Display.setDisplayMode(displayMode);
      Display.setTitle(windowTitle);
      Display.create();
  }
  private void init() throws Exception {
      createWindow();
      IL.create();

      loadTextures();
      initGL();
  }
  private void loadTextures() {
      texture = loadTexture("Data/map.bmp");
  }
  private void initGL() {
      GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
      GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
      GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
      GL11.glClearDepth(1.0f); // Depth Buffer Setup
      GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
      GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do

      GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
      GL11.glLoadIdentity(); // Reset The Projection Matrix

      // Calculate The Aspect Ratio Of The Window
      GLU.gluPerspective(45.0f, (float) displayMode.getWidth() / (float) displayMode.getWidth(),0.1f,800.0f);
      GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix

      // Really Nice Perspective Calculations
      GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
      //GL11.glPolygonMode(GL11.GL_FRONT, GL11.GL_FILL);
      //GL11.glPolygonMode(GL11.GL_BACK, GL11.GL_LINE);
      ByteBuffer temp = ByteBuffer.allocateDirect(16);
      temp.order(ByteOrder.nativeOrder());
      GL11.glLight(GL11.GL_LIGHT1, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip());              // Setup The Ambient Light
      GL11.glLight(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip());              // Setup The Diffuse Light
      GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip());         // Position The Light
      GL11.glEnable(GL11.GL_LIGHT1);                          // Enable Light One
  }
  private void cleanup() {
      Display.destroy();
  }
  /**
   * Texture loading directly from LWJGL examples
   */
  private int[] loadTexture(String path) {
      IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
      IL.ilGenImages(1, image);
      IL.ilBindImage(image.get(0));
      IL.ilLoadImage(path);
      IL.ilConvertImage(IL.IL_RGB, IL.IL_BYTE);
      ByteBuffer scratch = ByteBuffer.allocateDirect(IL.ilGetInteger(IL.IL_IMAGE_WIDTH) * IL.ilGetInteger(IL.IL_IMAGE_HEIGHT) * 3);
      IL.ilCopyPixels(0, 0, 0, IL.ilGetInteger(IL.IL_IMAGE_WIDTH), IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), 1, IL.IL_RGB, IL.IL_BYTE, scratch);

      // Create A IntBuffer For Image Address In Memory
      IntBuffer buf = ByteBuffer.allocateDirect(12).order(ByteOrder.nativeOrder()).asIntBuffer();
      GL11.glGenTextures(buf); // Create Texture In OpenGL

      GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
      // Typical Texture Generation Using Data From The Image

      // Create Nearest Filtered Texture
      GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
      GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, IL.ilGetInteger(IL.IL_IMAGE_WIDTH),
              IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, scratch);

      // Create Linear Filtered Texture
      GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(1));
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
      GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, IL.ilGetInteger(IL.IL_IMAGE_WIDTH),
              IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, scratch);

      // Create MipMapped Texture
      GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(2));
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
      GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_NEAREST);
      GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, 3, IL.ilGetInteger(IL.IL_IMAGE_WIDTH),
              IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, scratch);

    return new int[]{ buf.get(0), buf.get(1), buf.get(2) };     // Return Image Addresses In Memory
  }
}
Title: Problem of visualization during intersection of surfaces
Post by: napier on March 27, 2005, 00:00:22
This sounds very familiar to the problem I was having, and the fix for me was to increase the number of bits per pixel in the depth buffer (from 8 to 24).  

24 bits provides a finer range of Z values, which improves the resolution of the depth test, especially valuable when zooming out of a scene as you describe.

The error you got suggests that your grafix card or video drivers do not support higher resolution in the depth buffer.  Take a look at this thread for another case of the "Could not apply pixel format to window" error:

http://lwjgl.org/forum/viewtopic.php?t=999
Title: Problem of visualization during intersection of surfaces
Post by: tomb on March 27, 2005, 00:12:07
The fix is to push forward the near clipping plane as much as possible. It is the 3rd parameter in the following function:
QuoteGLU.gluPerspective(45.0f, (float) displayMode.getWidth() / (float) displayMode.getWidth(),0.1f,800.0f);

Instead of 0.1f try as high value as possible while still seeing the objects. Begin with 10.0f. That would give you enough precission. This can be a problem with landscape engines where you need to render large areas and also detailed object up close. There are other workarounds if you can't get a 24 bit depth buffer and you need the close clipping plane. You would have to render the scene several time each with a different set a near/far plane. Draw it twice with 0.1f, 10.0f and 10.0f,800.0f, etc.
Title: Problem of visualization during intersection of surfaces
Post by: andretti on March 30, 2005, 13:43:23
That was the problem! Pushing away the view plane has solved the problem...at the high cost of losing control when i'm too near to the objects! It'll be a great trouble to code a changing view plane! I think that the problem about the depth buffer is due to my graphic card...an old matroxG200!! Maybe they don't even know what was a depth buffer at that time and xp drivers were updated too much time ago! Thank you all!