Results 1 to 7 of 7

Thread: OpenGL Backgrounds

  1. #1

    OpenGL Backgrounds

    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:
    Code:
    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.

  2. #2
    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:
    Code:
    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.

  3. #3
    Thanks.
    But how do I shift these coordinates?

  4. #4
    Quote Originally Posted by Brainer
    But how do I shift these coordinates?
    Emm...
    Code:
    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 :)

  5. #5

  6. #6
    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.
    Last edited by User137; 20-11-2010 at 02:54 PM.

  7. #7
    Quote Originally Posted by User137
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •