LWJGL Drawing a rectangle at a position

Alright, I’m a bit new to Game Development, but I had been messing around for awhile with it and was able to draw rectangles of a particular size at a position relative to pixels.

Like this:


public static void drawRect(float x, float y, float width, float height, float rot)
	{
		glPushMatrix();
		{
			glTranslatef(x, y, 0); // Shifts the position
			glRotatef(rot, 0, 0, 1);

			glBegin(GL_QUADS);
			{
				glVertex2f(0, 0);
				glVertex2f(0, height);
				glVertex2f(width, height);
				glVertex2f(width, 0);
			}
			glEnd();
		}
		glPopMatrix();
	}

There’s nothing really wrong with that, but I’ve been trying to get the hang of VBO’s which are a more modern technique for rendering. The problem I have is that it draws the rectangle relative to GL co-ordinates (-1 top left, 1 top right for x) etc. But I want it so that I can draw a rectangle at a particular position on the screen.

This is my rendering code:


public void addVertices(Vector2f[] vertices)
	{
		size = vertices.length;
		FloatBuffer bufferedVertices = Render.createFlippedBuffer(vertices);
		
		glBindBuffer(GL_ARRAY_BUFFER, vbo);
		glBufferData(GL_ARRAY_BUFFER, bufferedVertices, GL_STATIC_DRAW);
	}
	
	public void render()
	{
		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 3, GL_FLOAT, false, Vector2f.SIZE * 4, 0);
		glDrawArrays(GL_QUADS, 0, size);
		glDisableVertexAttribArray(0);
	}

I just use a standard floatbuffer adding the x and y coordinates into it, and then call buffer.flip() so that OGL can read it.

And this is how I create my square object


public Square(int x, int y, int height, int width)
	{
		super(x, y);
		mesh = new Mesh();

		Vector2f[] vertices = new Vector2f[] { new Vector2f (0, 0),
												new Vector2f(0, height),
												new Vector2f(width, height),
												new Vector2f(width, 0) };
		mesh.addVertices(vertices);
	}

and then I just call mesh.render() when I’m rendering it…

It just draws on the top left quadrant…
Help please, I want to be able to draw it at a given size and position on the screen, not relative to the quadrant positions of ogl :frowning:

Also, I don’t really understand how to texture using vbo’s. It seems a bit strange to me.
How would I go about drawing multiple rectangles of textures on the screen using vbo… kinda like tile maps could be a nice format to help me understand it.

  1. Positioning. What you want is a projection matrix. It describes a volume which is what you “see” on the screen. See the image below from the red book.

The simplest projection is an orthographic projection, which is just a regular cuboid shaped volume. You give it a “left” value which is analogous to the left side of the screen, a right value which is the right side, a top value and a bottom value which are the top and bottom sides of the screen respectively. There is also a near and far, which are the clip panes but for 2D just put -1 and 1. The function would be glOrtho if you were using the fixed function pipeline (which you aren’t) but you can look up its documentation to give you an idea. To help you any more I would have to see your shader.

  1. Texturing. Texturing with vbos is pretty much the same as positioning. Fill a VBO with the texture coordinates, point a different vertex attribute towards it and render. Your confusion I think is with your shader so again as above, I need to see it. But I would recommend you read these three tutorials: http://www.lwjgl.org/wiki/index.php?title=GLSL_Tutorial:_Syntax , http://www.lwjgl.org/wiki/index.php?title=GLSL_Tutorial:_Communicating_with_Shaders and http://www.lwjgl.org/wiki/index.php?title=GLSL_Tutorial:_Texturing. The first two teach you the basics of GLSL and the last teaches you how to use textures in it.