[LWJGL] Depth-Test?

  • Themenstarter Themenstarter Turakar
  • Beginndatum Beginndatum
T

Turakar

Gast
Hallo,

ich hab mal angefangen in lwjgl zu arbeiten, bin aber (wie so oft) auf einen fehler gestoßen. Der Würfel wird nicht weit weg gerendert, sondern sehr nah dran. Hat wer ne Idee was ich falsch mache?

Java:
package bin.main;

import static org.lwjgl.opengl.GL11.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.util.glu.GLU;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class Main 
{
	final static int WINDOW_WIDTH = 500;
	final static int WINDOW_HEIGHT = 500;
	
	public Main()
	{
		try 
		{
			Display.setDisplayMode(new DisplayMode(WINDOW_WIDTH, WINDOW_HEIGHT));
			Display.create();
		} 
		catch (LWJGLException e) 
		{
			e.printStackTrace();
			Main.exit(1);
		}
		
		Display.setTitle("LWJGL TEST");
		
		glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
	    //glEnable(GL_BLEND);
		glEnable(GL_CULL_FACE);
		glCullFace(GL_FRONT);
	    glEnable(GL_DEPTH_TEST);
	    glClearDepth(1.0f);
	    glEnable(GL_CULL_FACE);
	    //glDepthMask(true);
		glEnable(GL_TEXTURE_2D);
		
		glTranslatef(0.0f, 0.0f, -500.0f);
		//glRotatef(45, 1, 0, 0);
		//glRotatef(45, 0, 1, 0);
		
		GLU.gluPerspective(45.0f,WINDOW_WIDTH/WINDOW_HEIGHT,1.0f,500.0f);
		
		
		Texture texture;
		try 
		{
			texture = TextureLoader.getTexture("PNG", new FileInputStream(new File("Crate.png")));
		} 
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
			Display.destroy();
			Main.exit(1);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
			Main.exit(1);
		}
		
		while (!Display.isCloseRequested()) 
		{
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
			glLoadIdentity();
			
			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
			 
			// 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
			 
			// 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
			 
			// 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
			 
			// 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
			 
			// 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();
			
		    glDepthMask(true);
		    
		    glFlush();
		    
		    
			
			pollInputs();
			
		    Display.update();
		}
	 
		Main.exit(0);
	}
	
	public static void exit(int code)
	{
		Display.destroy();
		System.exit(code);
	}

	private void pollInputs() 
	{
		while(Keyboard.next())
		{
			if(Keyboard.getEventKeyState())
			{
				System.out.println("Key " + Keyboard.getEventCharacter() + " is pressed!");
			}
			else
			{
				System.out.println("Key " + Keyboard.getEventCharacter() + " is released!");
			}
		}
	}


	/**Where the story begins... :D **/
	public static void main(String[] args) 
	{
		new Main();
	}

}

Wahrscheinlich hab ich irgendwo nene "++" vergessen oder so????:L???:L???:L
 

Anhänge

  • code.jpg
    code.jpg
    43,7 KB · Aufrufe: 29
"Depth Test" bezeichnet das, was durchgeführt wird, damit Pixel, die "weiter hinten im Bild" liegen nicht VOR den Pixeln gezeichnet werden, die weiter vorne liegen ( Z-Buffer ? Wikipedia ).

Das Problem hier ist, dass du keine richtige Kameratransformation angibst. Nach
glMatrixMode(GL_PROJECTION);
sollte nicht nur
glLoadIdentity();
stehen, sondern auch z.B. sowas wie
GLU.gluPerspective( 45.0f, (float)viewportWidth/(float)viewportHeight, 1.0f, 200.0f );
 

Neue Themen


Zurück
Oben