Getting Started With Rendering Using Shaders

Started by ghillieLEAD, November 05, 2011, 20:18:05

Previous topic - Next topic

ghillieLEAD

I'm having trouble transitioning from fixed function programming to using shaders.  I am attempting to learn the basics from this tutorial:  http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Table-of-Contents.html  At the moment I am just trying to draw a rectangle on the screen.  Everything is compiling fine, but nothing is being drawn.  At the bottom of this post is the code for my shaders and the main class.  There are a few other classes, but they are just classes in my project to manage the shaders and shader programs.  But I don't think that is where the issue is.  The places to check would be the initialize method (line 57) and the render method (line 198).  I'm just not sure what is actually wrong though!  Thanks in advance for any help you can give me!

PS:  Oh, and if you have any recommendations on what tutorials/articles I should read, feel free to let me know here.

Vertex Shader:
uniform mat4 uMvp;
attribute vec2 aPosition;

void main()
{
	vec4 position = vec4(aPosition.xy,0,1);
	gl_Position = uMvp * position;
}


Fragment Shader:
void main()
{
	gl_FragColor = vec4(0.4,0.4,0.8,1.0);
}


Main Class:
package net.midnightindie.glint;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Frame;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import net.midnightindie.glint.render.RenderManager;
import net.midnightindie.glint.util.WindowListener;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Controllers;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;

public class Glint implements Runnable
{
	protected Frame frame;
	protected Canvas canvas;
	protected int displayWidth;
	protected int displayHeight;
	
	protected boolean areControllersAvailable;
	
	protected Thread gameThread;
	protected boolean isRunning;
	protected boolean isPaused;
	
	protected RenderManager rm;
	
	protected float[] verticies;
	protected FloatBuffer vertexBuffer;
	protected int vertexBufferHandle;
	
	protected int[] indicies;
	protected IntBuffer indexBuffer;
	protected int indexBufferHandle;
	
	protected float[] mvpData;
	protected FloatBuffer mvpBuffer;
	
	protected int uniformMvpMatrix;
	protected int attributePositionVector;
	
	public Glint()
	{
		
	}
	
	public void Initialize()
	{
		displayWidth = 900;
		displayHeight = 600;
		CreateDisplay();
		
		areControllersAvailable = false;
		CreateInputDevices();
		
		isRunning = true;
		isPaused = false;
		
		rm = new RenderManager();
		rm.Initialize();
		
		mvpData = new float[] {
				(float)(2 / displayWidth), 0, 0, -1,
				0, (float)(2 / displayHeight), 0, 1,
				0, 0, -2, -1,
				0, 0, 0, 1
		};
		
		mvpBuffer = BufferUtils.createFloatBuffer(mvpData.length);
		mvpBuffer.put(mvpData);
		
		verticies = new float[] {
				100, 300,
				300, 300,
				100, 100,
				300, 100
		};
		
		indicies = new int[] {
				0, 1, 2, 3
		};
		
		vertexBuffer = BufferUtils.createFloatBuffer(verticies.length);
		vertexBuffer.put(verticies);
		
		indexBuffer = BufferUtils.createIntBuffer(indicies.length);
		indexBuffer.put(indicies);
		
		vertexBufferHandle = glGenBuffers();
		glBindBuffer(GL_ARRAY_BUFFER, vertexBufferHandle);
		glBufferData(vertexBufferHandle, vertexBuffer, GL_STATIC_DRAW);
		
		indexBufferHandle = glGenBuffers();
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferHandle);
		glBufferData(indexBufferHandle, indexBuffer, GL_STATIC_DRAW);
		
		uniformMvpMatrix = glGetUniformLocation(rm.GetProgramManager().GetProgram("hello").GetHandle(), "uMvp");
		attributePositionVector = glGetAttribLocation(rm.GetProgramManager().GetProgram("hello").GetHandle(), "aPosition");
	}
	
	protected void CreateDisplay()
	{
		frame = new Frame();
		frame.setTitle("glint");
		frame.setSize(displayWidth, displayHeight);
		frame.setLocationRelativeTo(null);
		frame.setLayout(new BorderLayout());
		frame.addWindowListener(new WindowListener(this, gameThread));
		
		canvas = new Canvas();
		frame.add(canvas, "Center");
		
		frame.setVisible(true);
		
		try
		{
			Display.setParent(canvas);
			Display.create();
		}
		catch (LWJGLException ex)
		{
			HandleError(ex.getMessage());
		}
	}
	
	public void CreateInputDevices()
	{
		try
		{
			Keyboard.create();
			Mouse.create();
		}
		catch (LWJGLException ex)
		{
			HandleError("Unable to create input devices");
		}
		
		try
		{
			Controllers.create();
			areControllersAvailable = true;
		}
		catch (LWJGLException ex)
		{
			areControllersAvailable = false;
		}
	}
		
	public void Start()
	{
		gameThread = new Thread(this, "glint");
		gameThread.start();
	}
	
	public void Pause()
	{
		isPaused = true;
	}
	
	public void Resume()
	{
		isPaused = false;
	}
	
	public void Stop()
	{
		isRunning = false;
	}
	
	public void Destroy()
	{
		Keyboard.destroy();
		Mouse.destroy();
		Controllers.destroy();
		Display.destroy();
	}
	
	public void PollInputDevices()
	{
		
	}
	
	public void Update()
	{
		
	}
	
	public void Render()
	{
		glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		
		rm.GetProgramManager().GetProgram("hello").UseProgram();
		
		glUniformMatrix4(uniformMvpMatrix, true, mvpBuffer);
		
		glBindBuffer(GL_VERTEX_ARRAY, vertexBufferHandle);
		glVertexAttribPointer(attributePositionVector, 2, GL_FLOAT, false, 0, 0);
		glEnableVertexAttribArray(attributePositionVector);
		
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferHandle);
		glDrawElements(GL_TRIANGLE_STRIP, 4, GL_INT, 0);
		
		glDisableVertexAttribArray(attributePositionVector);
	}
	
	public void run()
	{
		Initialize();
		
		while (isRunning)
		{
			if (isPaused)
			{
				try
				{
					Thread.currentThread();
					Thread.sleep(100);
				}
				catch (InterruptedException ex)
				{
					
				}
				
				continue;
			}
			
			PollInputDevices();
			Update();
			Render();
			
			Display.update();
		}
		
		Destroy();
		System.exit(0);
	}
	
	public void HandleError(String errorMessage)
	{
		System.out.println("[ERROR]  " + errorMessage);
		Stop();
	}
	
	public static void main(String[] args)
	{
		System.out.println("Working Directory:  " + System.getProperty("user.dir"));
		
		Glint glint = new Glint();
		
		try
		{
			glint.Start();
		}
		catch (Exception ex)
		{
			try
			{
				glint.HandleError(ex.getMessage());
			}
			catch(Exception ex2)
			{
				System.out.println("[ERROR]  " + ex.getMessage());
				System.exit(-1);
			}
		}
	}
}

Fool Running

My guess is that you are drawing stuff, but it is off the screen. Make sure that your matrix (mvpData, I think) is correct. Might be better to replace that matrix by calling GLU.gluLookAt() which will simplify it a lot.

P.S. Try this tutorial: http://www.arcsynthesis.org/gltut/ which I think has been recommended by other people on this forum.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D