Screwing up while trying to draw a triangle in OpenGL 3.3

Started by Cubic, April 07, 2012, 23:38:15

Previous topic - Next topic

Cubic

Edited because the title may have been misleading

It has been a while since I last used OpenGL, so it's possible that I did something OpenGL related wrong... but as I haven't used LWJGL so much I think it's probably buffer related.

First the code:

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;

public class Triangle implements Drawable {
	int vertexBuffer;
	FloatBuffer colorBuffer;
	final static private String vertexShaderSource = "#version 330\n"
			+ "layout(location=0) in vec2 pos;" + "void main() {"
			+ "	gl_Position = vec4(pos,0,1);" + "}";
	final static private String fragmentShaderSource = "#version 330\n"
			+ "layout(location=0) out vec4 fragColor;"
			+ "layout(location=0) uniform vec3 color;" + "void main() {"
			+ "	fragColor = vec4(color,1.0);" + "}";
	private static int vertexShader = 0;
	private static int fragmentShader = 0;
	private static int shaderProgram = 0;

	public Triangle(float red, float blue, float green, float x1,
			float y1, float x2, float y2, float x3, float y3) {
		vertexBuffer = GL15.glGenBuffers();
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBuffer);
		FloatBuffer db = BufferUtils.createFloatBuffer(6).put(
				new float[] { x1, y1, x2, y2, x3, y3});
		db.flip();
		GL15.glBufferData(
				GL15.GL_ARRAY_BUFFER,
				db,
				GL15.GL_STATIC_DRAW);
		colorBuffer = BufferUtils.createFloatBuffer(3).put(new float[]{green,blue,red});
		colorBuffer.flip();
		if (shaderProgram == 0) {
			vertexShader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
			GL20.glShaderSource(vertexShader, vertexShaderSource);
			GL20.glCompileShader(vertexShader);

			fragmentShader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
			GL20.glShaderSource(fragmentShader, fragmentShaderSource);
			GL20.glCompileShader(fragmentShader);

			shaderProgram = GL20.glCreateProgram();
			GL20.glAttachShader(shaderProgram, vertexShader);
			GL20.glAttachShader(shaderProgram, fragmentShader);
			GL20.glLinkProgram(shaderProgram);
		}
	}

	@Override
	public void draw() {
		GL20.glUseProgram(shaderProgram);
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBuffer);
		GL20.glEnableVertexAttribArray(0);
		GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
		GL20.glUniform3(0, colorBuffer);
		GL11.glDrawArrays(GL11.GL_TRIANGLES, 0,3);
	}

	@Override
	public void setPosition(double x, double y) {
		// TODO Auto-generated method stub

	}

	@Override
	public void rotate(double angle) {
		// TODO Auto-generated method stub

	}

}


I am creating it with

Triangle triangle = new Triangle(0.0f,1.0f,0.0f, 
					                         -1.0f,-1.0f,1.0f,-1.0f,0.0f,1.0f);


And drawing it in my main loop. The display is created like this:

Display.setDisplayMode(new DisplayMode(800, 600));
			Display.sync(60);
			Display.create(new PixelFormat(), new ContextAttribs(3, 3)
					.withProfileCore(true).withForwardCompatible(true));


Basically, I get 'a' triangle to be drawn, but not the one I want to (no matter what I pass for color, I always get a white triangle - and the triangle itself ends up at pretty weird positions, in the example given at the bottom left corner. I am thinking in coordinates from -1.0 to 1.0 - apparently thats wrong, but I haven't really discovered a pattern yet).

If anyone could tell me what's wrong, that would be great. Oh, and while its not important for the question, if you could inform me of the crimes against humanity I undoubtedly have committed with this code, that would be useful as well.

Cubic

Well, I actually managed to fix it on my own. Here's the fixed code.


import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;

public class Triangle implements Drawable {
   int vertexBuffer;
   FloatBuffer colorBuffer;
   int rgbUniformLocation;
   final static private String vertexShaderSource = "#version 330\n"
         + "layout(location=0) in vec2 pos;" + "void main() {"
         + "   gl_Position = vec4(pos,0,1);" + "}";
   final static private String fragmentShaderSource = "#version 330\n"
         + "layout(location=0) out vec4 fragColor;"
         + "uniform vec3 rgbColor = vec3(0.0,1.0,0.0);"
         + "void main() {"
         + "   fragColor = vec4(rgbColor,1.0);"
         + "}";
   private static int vertexShader = 0;
   private static int fragmentShader = 0;
   private static int shaderProgram = 0;

   public Triangle(float red, float green, float blue, float x1, float y1,
         float x2, float y2, float x3, float y3) {
      vertexBuffer = GL15.glGenBuffers();
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBuffer);
      FloatBuffer floatBuffer = BufferUtils.createFloatBuffer(6).put(
            new float[] { x1, y1, x2, y2, x3, y3 });
      floatBuffer.flip();
      GL15.glBufferData(GL15.GL_ARRAY_BUFFER, floatBuffer,
            GL15.GL_STATIC_DRAW);
      colorBuffer = BufferUtils.createFloatBuffer(3).put(
            new float[] { red, blue, green });
      colorBuffer.flip();
      if (shaderProgram == 0) {
         vertexShader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
         GL20.glShaderSource(vertexShader, vertexShaderSource);
         GL20.glCompileShader(vertexShader);
         if (GL20.glGetShader(vertexShader, GL20.GL_COMPILE_STATUS) == 0) {
            throw new RuntimeException("Error in vertex shader!");
         }
         fragmentShader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
         GL20.glShaderSource(fragmentShader, fragmentShaderSource);
         GL20.glCompileShader(fragmentShader);
         if (GL20.glGetShader(fragmentShader, GL20.GL_COMPILE_STATUS) == 0) {
            throw new RuntimeException("Error in fragment shader!");
         }
         shaderProgram = GL20.glCreateProgram();
         GL20.glAttachShader(shaderProgram, vertexShader);
         GL20.glAttachShader(shaderProgram, fragmentShader);
         GL20.glLinkProgram(shaderProgram);
         if (GL20.glGetProgram(shaderProgram, GL20.GL_LINK_STATUS) == 0) {
            throw new RuntimeException("Error linking shader program!");
         }
         rgbUniformLocation = GL20.glGetUniformLocation(shaderProgram, "rgbColor");
         if(rgbUniformLocation == -1) {
            throw new RuntimeException("Couldn't bind uniform!");
         }
      }
   }

   @Override
   public void draw() {
      GL20.glUseProgram(shaderProgram);
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBuffer);
      GL20.glEnableVertexAttribArray(0);
      GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0);
      GL20.glUniform3(rgbUniformLocation,
            colorBuffer);
      GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);
   }

   @Override
   public void setPosition(double x, double y) {
      // TODO Auto-generated method stub

   }

   @Override
   public void rotate(double angle) {
      // TODO Auto-generated method stub

   }

}