VBO with shaders - nothing shows up at all.

Started by beefmongerer, September 05, 2011, 22:21:12

Previous topic - Next topic

beefmongerer

package com.exoterragame.client;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

import com.exoterragame.client.util.FileUtility;

public class Main {

	public void start() {
		try {
			Display.setDisplayMode(new DisplayMode(800, 600));
			Display.create();
		} catch (LWJGLException e) {
			e.printStackTrace();
			System.exit(0);
		}
		
		Display.setTitle("Codename Fantasy Client");
		
		GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		
		FloatBuffer vert_b = BufferUtils.createFloatBuffer(8);
		IntBuffer idx_b = BufferUtils.createIntBuffer(4);
		float[] verts = {
				-1.0f, -1.0f,
				 1.0f, -1.0f,
				-1.0f,  1.0f,
				 1.0f,  1.0f
		};
		int[] idx = {
				0, 1, 2, 3
		};
		
		vert_b.put(verts).rewind();
		idx_b.put(idx).rewind();
		
		int buffer_id = GL15.glGenBuffers();
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer_id);
		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vert_b, GL15.GL_STATIC_DRAW);
		
		int idx_buffer_id = GL15.glGenBuffers();
		GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, idx_buffer_id);
		GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, idx_b, GL15.GL_STATIC_DRAW);
		
		int vert_shader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
		String vert_source = "";
		try {
			vert_source = FileUtility.loadTextFile("res/shaders/default.v.glsl");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.exit(0);
		}
		GL20.glShaderSource(vert_shader, vert_source);
		GL20.glCompileShader(vert_shader);
		if (GL20.glGetShader(vert_shader, GL20.GL_COMPILE_STATUS) == 0) {
			System.exit(0);
		}
		
		int frag_shader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
		String frag_source = "";
		try {
			frag_source = FileUtility.loadTextFile("res/shaders/default.f.glsl");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.exit(0);
		}
		GL20.glShaderSource(frag_shader, frag_source);
		GL20.glCompileShader(frag_shader);
		if (GL20.glGetShader(frag_shader, GL20.GL_COMPILE_STATUS) == 0) {
			System.exit(0);
		}
		
		int program = GL20.glCreateProgram();
		GL20.glAttachShader(program, vert_shader);
		GL20.glAttachShader(program, frag_shader);
		GL20.glLinkProgram(program);
		if (GL20.glGetProgram(program, GL20.GL_LINK_STATUS) == 0) {
			System.exit(0);
		}
		
		int tex_uniform = GL20.glGetUniformLocation(program, "texture");
		int pos_attribute = GL20.glGetAttribLocation(program, "position");
		
		Texture texture = null;
		try {
			texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/tex0.png"));
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(0);
		}
		
		while (!Display.isCloseRequested()) {
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
			
			GL20.glUseProgram(program);
			
			GL13.glActiveTexture(GL13.GL_TEXTURE0);
			GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
			GL20.glUniform1i(tex_uniform, 0);
			
			GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer_id);
			GL20.glVertexAttribPointer(pos_attribute, 2, GL11.GL_FLOAT, false, Float.SIZE * 2, 0);
			GL20.glEnableVertexAttribArray(pos_attribute);
			
			GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, idx_buffer_id);
			GL11.glDrawElements(GL11.GL_TRIANGLE_STRIP, 4, GL11.GL_INT, 0L);
		
			GL20.glDisableVertexAttribArray(pos_attribute);
			
			Display.update();
		}
		GL15.glDeleteBuffers(buffer_id);
		GL15.glDeleteBuffers(idx_buffer_id);
		texture.release();
		Display.destroy();
	}
	
	public static void main(String[] args) {
		Main main = new Main();
		main.start();
	}
}


Okay, it renders nothing. I've looked over it line by line, what could possibly be wrong? I know it's not actually rendering anything because my fragment shader is set to output pure white and this is... black.

I might also add that there are no errors at all. I've looked at every single variable and it's all in order - glDrawElements just isn't doing a thing. My code is essentially a direct port of this, tailored to work with LWJGL in Java. I just don't see the problem.



EDIT: AS it turns out, it's the stride in glVertexAttribPointer. Setting it to 0 rather than Float.SIZE * 2 makes it work.