VBOs, Customised Shaders and no GLU -> Black screen

Started by theGINItheM, February 25, 2011, 16:06:32

Previous topic - Next topic

theGINItheM

Hello there!

For my application I need a highly customised shader. Started off by writing my shaders and they compiled fine. All the rest however confuses me, and I ended up with code that executes but does not draw anything. I am cluesless as to why and would be very happy if a pro could go through my code.
What I have attached is a very short (but executable) version of my code - but it has the same problem...

Thanks in advance!
My experience with forums is usually that I don't get any answer - leaving me wondering, whether others don't know the answer either or whether it's apparent in their eyes. So any kind of answer would be of some help...

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;

public class LWJGLGraphics
{
	private static final String BumpMapVertexShader =
									"uniform mat4 normal_matrix;\n"
											+ "uniform mat4 proj_matrix;\n"
											+ "attribute vec4 aPosition;\n"
											+ "void main() {\n"
											+ "  gl_Position = proj_matrix * normal_matrix * aPosition;\n"
											+ "}\n";
	private static final String BumpMapFragmentShader =
								"void main() {\n"
								+ "  gl_FragColor = vec4(1,0.5,0.0,1);\n"
								+ "}\n";

	private static final int FLOAT_SIZE_BYTES = 4;
	private static final int INT_SIZE_BYTES = 4;
	private static final int SHORT_SIZE_BYTES = 2;
	
	private static int BumpmapProgram;
	private static int mProjMatrixHandle;
	private static int muNOMatrixHandle;
	private static int maPositionHandle;

	public static void main(String[] argv)
	{
		try
		{
			init(640, 480, "Test");
		}
		catch(LWJGLException e)
		{
			e.printStackTrace();
			System.exit(0);
		}
		while(!Display.isCloseRequested())
		{
			drawColorfulRectangle(25,25);
			Display.update();
		}
		Display.destroy();
	}
	
	public static void drawColorfulRectangle(int cx, int cy)
	{
		// USE MY SHADERS
		glUseProgram(BumpmapProgram);
		// Define vertices (of a quad)
		IntBuffer vert = makeIntBuffer(new int[] { 0,10,-10,
												   10, 0,-10,
												   	0, 0,-10});
		vert.position(0);
		glEnableVertexAttribArray(maPositionHandle);
		glVertexAttribPointer(maPositionHandle, 3, false, false, 3 * INT_SIZE_BYTES, vert);
		// index buffer 
		ShortBuffer Primitives = makeShortBuffer(new short[] { 0, 1, 2 });
		glDrawElements(GL_TRIANGLES, Primitives);
	}
	
	private static void init(int width, int height, String title) throws LWJGLException
	{
		// WINDOW STUFF
		int hw = width / 2;
		int hh = height / 2;
		Display.setLocation(Display.getDisplayMode().getWidth() / 2 - hw, Display.getDisplayMode().getHeight() / 2 - hh);
		Display.setDisplayMode(new DisplayMode(width, height));
		Display.setTitle(title);
		Display.create();
				
		// CREATE, COMPILE SHADERS, PROGRAM
		BumpmapProgram = createProgram(BumpMapVertexShader, BumpMapFragmentShader);
		glValidateProgram(BumpmapProgram);
		
		// GET POINTERS TO ATTRIBUTES, UNIFORMS
		maPositionHandle = glGetAttribLocation(BumpmapProgram, "aPosition");
		mProjMatrixHandle = glGetUniformLocation(BumpmapProgram, "proj_matrix");
		muNOMatrixHandle = glGetUniformLocation(BumpmapProgram, "normal_matrix");

		// PROJECTION STUFF (leave everything standard)
		float[] normal_matrix = new float[16];
		float[] proj_matrix = new float[16];
		FloatBuffer Normal = makeFloatBuffer(normal_matrix);
		FloatBuffer Projection = makeFloatBuffer(proj_matrix);
		glGetFloat(GL_PROJECTION_MATRIX, Projection);
		glGetFloat(GL_MODELVIEW_MATRIX, Normal);
		glUniformMatrix4(mProjMatrixHandle, false, Projection);
		glUniformMatrix4(muNOMatrixHandle, false, Normal);
	}

	private static int loadShader(int shaderType, String source)
	{
		int shader = glCreateShader(shaderType);
		String errors;
		if (shader != 0)
		{
			glShaderSource(shader, source);
			glCompileShader(shader);
		}
		errors = glGetShaderInfoLog(shader, 1000).trim();
		if (errors.length() > 0)
			System.err.println("Shader errors: " + errors);
		return shader;
	}

	private static int createProgram(String vertexSource, String fragmentSource)
	{
		String errors;
		int vertexShader = loadShader(GL_VERTEX_SHADER, vertexSource);
		int fragmentShader = loadShader(GL_FRAGMENT_SHADER, fragmentSource);
		int program = glCreateProgram();
		if (program != 0)
		{
			glAttachShader(program, vertexShader);
			glAttachShader(program, fragmentShader);
			glLinkProgram(program);
		}
		errors=glGetProgramInfoLog(BumpmapProgram, 1000).trim();
		if(errors.length()>0)
			System.err.println("Program errors:" + errors);
		return program;
	}

	private static IntBuffer makeIntBuffer(int[] data)
	{
		int l = data.length;
		IntBuffer I = ByteBuffer.allocateDirect(l * INT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asIntBuffer();
		I.put(data);
		I.flip();
		return I;
	}

	private static ShortBuffer makeShortBuffer(short[] data)
	{
		int l = data.length;
		ShortBuffer I = ByteBuffer.allocateDirect(l * SHORT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asShortBuffer();
		I.put(data);
		I.flip();
		return I;
	}

	private static FloatBuffer makeFloatBuffer(float[] data)
	{
		int l = data.length;
		FloatBuffer I = ByteBuffer.allocateDirect(l * FLOAT_SIZE_BYTES).order(ByteOrder.nativeOrder()).asFloatBuffer();
		I.put(data);
		I.flip();
		return I;
	}
}