[Solved] gldepthtest not working

Started by Wicpar, September 20, 2014, 19:17:50

Previous topic - Next topic

Wicpar

hello, I followed thinMatrix's tutorials and i got a problem when I render the cube: glDepthTest is not working look in the attached file.
I enabled depth Test and the clear buffer bit. It's easier to show the relevant code:
public class Renderer {
	public static final float fov = 45;
	public static final float ZNear = 0.1f;
	public static final float ZFar = 10000;
	
	private Matrix4f projectionMatrix;
	
	public Renderer(StaticShader shader){
		createProjectionMatrix();
		shader.start();
		shader.loadProjectionMatrix(projectionMatrix);
		shader.stop();
	}

	public void prepare() {
		GL11.glEnable(GL11.GL_DEPTH_TEST);
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
		
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
		GL11.glClearColor(0.25f, 0, 0, 1);
	}

	public void render(Entity entity,StaticShader shader) {
		RawModel model = entity.getModel().getModel();
		GL30.glBindVertexArray(model.getVaoID());
		GL20.glEnableVertexAttribArray(0);
		GL20.glEnableVertexAttribArray(1);
		Matrix4f transformation = Maths.createTransformationMatrix(entity.getTransform());
		shader.loadTransformationMatrix(transformation);
		
		GL13.glActiveTexture(GL13.GL_TEXTURE0);
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, entity.getModel().getTexture().getID());
		GL11.glDrawElements(GL11.GL_TRIANGLES, model.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
		GL20.glDisableVertexAttribArray(0);
		GL20.glDisableVertexAttribArray(1);
		GL30.glBindVertexArray(0);
	}
	
	private void createProjectionMatrix(){
		float aspectRatio = (float) Display.getWidth()/ (float) Display.getHeight();
		float yscale = (float)((1f/Math.tan(Math.toRadians(fov /2f ))) * aspectRatio);
		float xscale = yscale/aspectRatio;
		float frustrumlength = ZFar - ZNear;
		
		projectionMatrix = new Matrix4f();
		projectionMatrix.m00 = xscale;
		projectionMatrix.m11 = yscale;
		projectionMatrix.m22 = -((ZFar + ZNear)/ frustrumlength);
		projectionMatrix.m23 = -1;
		projectionMatrix.m32 = -((2*ZNear*ZFar)/frustrumlength);
		projectionMatrix.m33 = 0;
	}

}

public class MainTest {

	public MainTest() {
		DisplayManager.createDisplay();
		Loader loader = new Loader();

		StaticShader shader = new StaticShader();
		Renderer renderer = new Renderer(shader);
		float[] vertices = { -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f,

		-0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f,

		0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f,

		-0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f,

		-0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f,

		-0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f

		};

		float[] textureCoords = {

		0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0

		};

		int[] indices = { 0, 1, 3, 3, 1, 2, 4, 5, 7, 7, 5, 6, 8, 9, 11, 11, 9, 10, 12, 13, 15, 15, 13, 14, 16, 17, 19, 19, 17, 18, 20, 21, 23, 23, 21, 22

		};

		RawModel model = loader.loadToVAO(vertices, textureCoords, indices);
		ModelTexture texture = new ModelTexture(loader.loadTexture("castle"));
		TexturedModel texmodel = new TexturedModel(model, texture);
		Entity entity = new Entity(texmodel, new Transform(new Vector3f(0.1f, 0.1f, -10), new Vector3f(0, 0, 0), new Vector3f(1, 1, 1)));
		Camera camera = new Camera();
		while (!Display.isCloseRequested()) {
			// game logic
			entity.rotate(1, 1, 0);
			camera.move();
			renderer.prepare();
			shader.start();
			shader.loadViewMatrix(camera);
			renderer.render(entity, shader);
			shader.stop();
			DisplayManager.updateDisplay();
		}

		shader.cleanUp();
		loader.cleanUp();
		DisplayManager.closeDisplay();
	}
}

public class Loader {

	private List<Integer>	vaos	= new ArrayList<Integer>();
	private List<Integer>	vbos	= new ArrayList<Integer>();
	private List<Integer> textures = new ArrayList<Integer>();

	public RawModel loadToVAO(float[] positions,float[] textureCoords, int[] indices) {
		int vaoID = createVAO();
		bindIndicesBuffer(indices);
		storeDataInAttributeList(0,3, positions);
		storeDataInAttributeList(1,2, textureCoords);
		unbindVAO();
		Logger.log("Successfully loaded index to vao", Logger.INFO);
		return new RawModel(vaoID, indices.length);

	}

	public int loadTexture(String name) {

		Texture texture = null;
		try {
			texture = TextureLoader.getTexture("PNG", new FileInputStream("res/" + name + ".png"));
		} catch (IOException e) {
			Logger.log("Failed loading texture (" + name + ")", Logger.SEVERE);
			e.printStackTrace();
		}
		Logger.log("Sucessfully loaded texture "+name , Logger.INFO);
		return texture.getTextureID();

	}

	public void cleanUp() {
		for (int vao : vaos) {
			GL30.glDeleteVertexArrays(vao);
		}
		for (int vbo : vbos) {
			GL15.glDeleteBuffers(vbo);
		}
		for (int tex : textures){
			GL11.glDeleteTextures(tex);
		}
		Logger.log("VBO,VAO,Texture cleanup successfully done",Logger.INFO);
	}

	private int createVAO() {
		int vaoID = GL30.glGenVertexArrays();
		vaos.add(vaoID);
		GL30.glBindVertexArray(vaoID);
		Logger.log("Successfully created VAO "+vaoID, Logger.INFO);
		return vaoID;
	}

	private void storeDataInAttributeList(int attributeNumber,int AttribSize, float[] data) {
		int vboID = GL15.glGenBuffers();
		vbos.add(vboID);
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
		FloatBuffer buffer = storeDataInFloatBuffer(data);
		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
		GL20.glVertexAttribPointer(attributeNumber, AttribSize, GL11.GL_FLOAT, false, 0, 0);
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
		Logger.log("Successfully stored data in atribute list", Logger.INFO);
	}

	private void unbindVAO() {
		GL30.glBindVertexArray(0);
		Logger.log("Successfully unbound vao", Logger.INFO);
	}

	private void bindIndicesBuffer(int[] indices) {
		int vboID = GL15.glGenBuffers();
		vbos.add(vboID);
		GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboID);
		IntBuffer buffer = storeDataInIntBuffer(indices);
		GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
		Logger.log("Sucessfully bound indices buffer", Logger.INFO);
	}

	private IntBuffer storeDataInIntBuffer(int[] data) {
		IntBuffer buffer = BufferUtils.createIntBuffer(data.length);
		buffer.put(data);
		buffer.flip();
		Logger.log("Sucessfully stored data in an int buffer", Logger.INFO);
		return buffer;
	}

	private FloatBuffer storeDataInFloatBuffer(float[] data) {
		FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length);
		buffer.put(data);
		buffer.flip();
		Logger.log("Sucessfully stored data in a float buffer", Logger.INFO);
		return buffer;
	}

}

public abstract class ShaderProgram {
	
	private int programID;
	private int vertexShaderID;
	private int fragmentShaderID;
	
	private static FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
	
	public ShaderProgram(String vertexFile,String fragmentFile){
		vertexShaderID = loadShader(vertexFile,GL20.GL_VERTEX_SHADER);
		fragmentShaderID = loadShader(fragmentFile,GL20.GL_FRAGMENT_SHADER);
		programID = GL20.glCreateProgram();
		GL20.glAttachShader(programID, vertexShaderID);
		GL20.glAttachShader(programID, fragmentShaderID);
		bindAttributes();
		GL20.glLinkProgram(programID);
		GL20.glValidateProgram(programID);
		getAllUniformLocations();
	}
	protected abstract void getAllUniformLocations();
	
	protected int getUniformLocation(String uniformName){
		return GL20.glGetUniformLocation(programID, uniformName);
	}
	
	public void start(){
		GL20.glUseProgram(programID);
	}
	
	public void stop(){
		GL20.glUseProgram(0);
	}
	
	public void cleanUp(){
		stop();
		GL20.glDetachShader(programID, vertexShaderID);
		GL20.glDetachShader(programID, fragmentShaderID);
		GL20.glDeleteShader(vertexShaderID);
		GL20.glDeleteShader(fragmentShaderID);
		GL20.glDeleteProgram(programID);
	}
	
	protected abstract void bindAttributes();
	
	protected void bindAttribute(int attribute, String variableName){
		GL20.glBindAttribLocation(programID, attribute, variableName);
	}
	
	protected void loadFloat(int location, float value){
		GL20.glUniform1f(location, value);
	}
	
	protected void loadVector(int location, Vector3f vector){
		GL20.glUniform3f(location, vector.x, vector.y, vector.z);
	}
	
	protected void loadBool(int location,boolean value){
		float toLoad = 0;
		if(value){
			toLoad = 1;
		}
		GL20.glUniform1f(location, toLoad);
	}
	
	protected void loadMatrix(int location, Matrix4f matrix){
		matrix.store(matrixBuffer);
		matrixBuffer.flip();
		GL20.glUniformMatrix4(location, false, matrixBuffer);
	}
	
	private static int loadShader(String file, int type){
		StringBuilder shaderSource = new StringBuilder();
		try{
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String line;
			while((line = reader.readLine())!=null){
				shaderSource.append(line).append("\n");
			}
			reader.close();
		}catch(IOException e){
			e.printStackTrace();
			System.exit(-1);
		}
		int shaderID = GL20.glCreateShader(type);
		GL20.glShaderSource(shaderID, shaderSource);
		GL20.glCompileShader(shaderID);
		if(GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS )== GL11.GL_FALSE){
			System.out.println(GL20.glGetShaderInfoLog(shaderID, 500));
			System.err.println("Could not compile shader!");
			System.exit(-1);
		}
		return shaderID;
	}

}

public class Maths {
	
	public static Matrix4f createTransformationMatrix(Transform transform){
		Matrix4f matrix = new Matrix4f();
		
		matrix.setIdentity();
		Matrix4f.translate(transform.getPos(), matrix, matrix);
		Matrix4f.rotate((float) Math.toRadians(transform.getRot().getX()), new Vector3f(1,0,0), matrix, matrix);
		Matrix4f.rotate((float) Math.toRadians(transform.getRot().getY()), new Vector3f(0,1,0), matrix, matrix);
		Matrix4f.rotate((float) Math.toRadians(transform.getRot().getZ()), new Vector3f(0,0,1), matrix, matrix);
		Matrix4f.scale(transform.getScale(), matrix, matrix);
		
		return matrix;
		
	}
	
	public static Matrix4f createViewMatrix(Transform transform){
		Matrix4f matrix = new Matrix4f();
		
		matrix.setIdentity();
		Matrix4f.translate(new Vector3f(-transform.getPos().x,-transform.getPos().y,-transform.getPos().z), matrix, matrix);
		Matrix4f.rotate((float) Math.toRadians(transform.getRot().getX()), new Vector3f(1,0,0), matrix, matrix);
		Matrix4f.rotate((float) Math.toRadians(transform.getRot().getY()), new Vector3f(0,1,0), matrix, matrix);
		Matrix4f.rotate((float) Math.toRadians(transform.getRot().getZ()), new Vector3f(0,0,1), matrix, matrix);
		
		return matrix;
		
	}

}

public class DisplayManager {

	private static final int	WIDTH		= 1280;
	private static final int	HEIGHT	= 720;
	private static final int	FPS_CAP	= 120;

	public static void createDisplay() {
		ContextAttribs attribs = new ContextAttribs(3, 2).withForwardCompatible(true).withProfileCore(true);

		try {
			Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
			Display.create(new PixelFormat(8,0,0,16), attribs);
			Display.setTitle("Germinis");
		} catch (LWJGLException e) {
			e.printStackTrace();
		}

		GL11.glViewport(0, 0, WIDTH, HEIGHT);
	}

	public static void updateDisplay() {

		Display.sync(FPS_CAP);
		Display.update();

	}

	public static void closeDisplay() {

		Display.destroy();

	}

}


I hope someone can find the error soon  :)

thanks for your help.

Suggestions to look somewhere is a good help too.

Wicpar

nevermind, I just had to fix the display manager:
try {
			Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
			Display.create(new PixelFormat(8,8,0,8), attribs);
			Display.setTitle("Germinis");
		} catch (LWJGLException e) {
			e.printStackTrace();
		}

I forgot the 8 in the second position of PixelFormat