PDA

View Full Version : OpenGL Backgrounds



Brainer
20-11-2010, 12:42 PM
Hello. :)

For my little project, I decided to move back to OpenGL 2.0. The project's a very simple 2D game I'm going to work on with my classmates.

I've got a question regarding rendering backgrounds. I have a 128x128 texture which I want to use as a background. Here's how I do it:


procedure TMainForm.RenderBackground;
var
Test: TTexture;
I, J, CellsX, CellsY: Integer;
Viewport: array[0..3] of GLint;
begin
Test := TextureLibrary[0];
I := 0;
J := 0;

Test.Bind();
glPushMatrix();
glGetIntegerv(GL_VIEWPORT, @Viewport[0]);
CellsX := (Viewport[2] div Test.TextureWidth) + 1;
CellsY := (Viewport[3] div Test.TextureHeight) + 1;

while (True) do
begin
glLoadIdentity();
glTranslatef(I * Test.TextureWidth, J * Test.TextureHeight, 0.0);

glBegin(GL_QUADS);
glTexCoord2i(0, Test.TextureHeight);
glVertex2i(0, 0);
glTexCoord2i(Test.TextureWidth, Test.TextureHeight);
glVertex2i(Test.TextureWidth, 0);
glTexCoord2i(Test.TextureWidth, 0);
glVertex2i(Test.TextureWidth, Test.TextureHeight);
glTexCoord2i(0, 0);
glVertex2i(0, Test.TextureHeight);
glEnd();

if (I < CellsX) then
Inc(I)
else
begin
I := 0;
Inc(J);
end;
if (J >= CellsY) then
break;
end;
glPopMatrix();
Test.UnBind();
end;

I feel it's not the best way to do the background, so if you could possibly suggest me a better way of doing it, I'd really appreciate it. Note that in the example I use GL_TEXTURE_RECTANGLE_EXT.

Andru
20-11-2010, 12:58 PM
First of all you must remove all operations with matrices(glTranslatef and glLoadIdentity) and replace them by shifting X/Y coordinates in glVertx2i. After this you can conclude your "while ( True ) do" like this:


glBegin( GL_QUADS );
while ( True ) do
begin
// render code without glBegin/glEnd, only glVertex/glTexCoord and some other operations
end;
glEnd;

But only if you use only one 128x128 texture. And one more optimization - do not use glGetIntegerv, try to use constant Width/Height.

Brainer
20-11-2010, 01:13 PM
Thanks. :)
But how do I shift these coordinates?

Andru
20-11-2010, 01:17 PM
But how do I shift these coordinates?
Emm...


var
shiftX, shiftY : Integer;
...
shiftX := I * Test.TextureWidth;
shiftY := J * Test.TextureHeight;
...
glVertex2i( shiftX, shiftY );
glTexCoord2i(Test.TextureWidth, Test.TextureHeight);
glVertex2i(shiftX + Test.TextureWidth, shiftY);
// and so on :)

Brainer
20-11-2010, 01:33 PM
Thanks a lot, Andru! :)

User137
20-11-2010, 02:50 PM
I see that you only use 1 texture which fills the entire texture image. Therefore you can take use of texture clamping with GL_REPEAT. All you need to do is render 1 quad that fills the entire screen and have texture coordinates that multiply themselves as many times as you can fit cells.

from
glTexCoord2f(0,0);
to

(if texture max is 1,1) glTexCoord2f(Form.ClientWidth/Test.TextureWidth, Form.ClientHeight/Test.TextureHeight);
(if texture max is width,height) glTexCoord2f(Form.ClientWidth, Form.ClientHeight);

You can get rid of the whole while loop then... now if i understood the idea correctly.

Andru
20-11-2010, 02:57 PM
Therefore you can take use of texture clamping with GL_REPEAT.
How can I forgot about this... Yes, this will be the fastest solution in this case :)