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