Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Problem to tile a texture in a single quad
- - By Zohran Date 2022-03-01 00:02
Hello, I'm learning actually how to use OpenGL with Ruby. I done this simple scene class actually to test all OpenGL capability. And I have a problem with my part titled: #DRAW THE GROUND
I would like to draw this quad with the texture as tiled. I try to change every thing, but nothing. Every time this quad show only one time my texture. What is the problem ? I recognize I'm a little bit lost with the U/V coordinates and 3D coordinates of my vertex when I'm using a texture. For example, with textured quad, how can I set the angle of my quad ?:

class Test3D < GameScene

  def initialize
    @texture = Gosu::Image.new('Test.png')
    @texture1 = Gosu::Image.new('Test1.png', {tileable: true})
    @texture2 = Gosu::Image.new('Tree.png')
    @texture3 = Gosu::Image.new('Character.png')

    @backgroundMusic = Gosu::Song.new("Test.wav")
    @backgroundMusic.play(true)
  end

  def draw
    Gosu.gl do

      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
      glEnable(GL_TEXTURE_2D)
      glEnable(GL_DEPTH_TEST)
      glMatrixMode(GL_PROJECTION)
      glLoadIdentity
      gluPerspective(45.0, $gameWindow.width.to_f / $gameWindow.height.to_f, 1, 1000)
      glMatrixMode(GL_MODELVIEW)
      glLoadIdentity
      gluLookAt(0,-640,250,0,0,0,0,0,1)

      #DRAW THE BUILDING
      glBindTexture(GL_TEXTURE_2D, @texture.gl_tex_info.tex_name)

      glPushMatrix
        glScalef(@texture.width, 0, @texture.height)

        glBegin(GL_QUADS)
          glTexCoord2d(@texture.gl_tex_info.left,@texture.gl_tex_info.bottom)
          glVertex3f(-0.5, 0.0, 0.0)

          glTexCoord2d(@texture.gl_tex_info.right,@texture.gl_tex_info.bottom)
          glVertex3f(0.5, 0.0, 0.0)

          glTexCoord2d(@texture.gl_tex_info.right,@texture.gl_tex_info.top)
          glVertex3f(0.5, 0.0, 1.0)

          glTexCoord2d(@texture.gl_tex_info.left,@texture.gl_tex_info.top)
          glVertex3f(-0.5, 0.0, 1.0)
        glEnd
      glPopMatrix

      #DRAW THE GROUND
      glBindTexture(GL_TEXTURE_2D, @texture1.gl_tex_info.tex_name)

      glPushMatrix
        glScalef(@texture1.width, @texture1.height, 0)

        glBegin(GL_QUADS)
          glTexCoord2d(@texture1.gl_tex_info.left,@texture1.gl_tex_info.bottom)
          glVertex3f(-512/2, -512/2, 0.0)

          glTexCoord2d(@texture1.gl_tex_info.right,@texture1.gl_tex_info.bottom)
          glVertex3f(512/2, -512/2, 0.0)

          glTexCoord2d(@texture1.gl_tex_info.right,@texture1.gl_tex_info.top)
          glVertex3f(512/2, 512/2, 0.0)

          glTexCoord2d(@texture1.gl_tex_info.left,@texture1.gl_tex_info.top)
          glVertex3f(-512/2, 512/2, 0.0)
        glEnd
      glPopMatrix

      #DRAW A TREE
      glBindTexture(GL_TEXTURE_2D, @texture2.gl_tex_info.tex_name)
      glEnable(GL_ALPHA_TEST)
      glAlphaFunc(GL_GREATER,0)

      glPushMatrix
      glTranslatef(64*3,64,0)
      glScalef(@texture2.width,@texture2.height, @texture2.height)

        glBegin(GL_QUADS)
          glTexCoord2d(@texture2.gl_tex_info.left,@texture2.gl_tex_info.bottom)
          glVertex3f(-0.5, 0.0, 0.0)

          glTexCoord2d(@texture2.gl_tex_info.right,@texture2.gl_tex_info.bottom)
          glVertex3f(0.5, 0.0, 0.0)

          glTexCoord2d(@texture2.gl_tex_info.right,@texture2.gl_tex_info.top)
          glVertex3f(0.5, 0.0, 1.0)

          glTexCoord2d(@texture2.gl_tex_info.left,@texture2.gl_tex_info.top)
          glVertex3f(-0.5, 0.0, 1.0)
        glEnd

      glDisable(GL_ALPHA_TEST)
      glPopMatrix

      #DRAW A CHARACTER
      glBindTexture(GL_TEXTURE_2D, @texture3.gl_tex_info.tex_name)
      glEnable(GL_ALPHA_TEST)
      glAlphaFunc(GL_GREATER,0)

      glPushMatrix
      glTranslatef(0,-64,1)
        glScalef(@texture3.width,@texture3.height,@texture3.height)

        glBegin(GL_QUADS)
          glTexCoord2d(@texture3.gl_tex_info.left,@texture3.gl_tex_info.bottom)
          glVertex3f(-0.5, 0.0, 0.0)

          glTexCoord2d(@texture3.gl_tex_info.right,@texture3.gl_tex_info.bottom)
          glVertex3f(0.5, 0.0, 0.0)

          glTexCoord2d(@texture3.gl_tex_info.right,@texture3.gl_tex_info.top)
          glVertex3f(0.5, 0.0, 1.0)

          glTexCoord2d(@texture3.gl_tex_info.left,@texture3.gl_tex_info.top)
          glVertex3f(-0.5, 0.0, 1.0)
        glEnd
      glDisable(GL_ALPHA_TEST)
      glPopMatrix

    end

  end

end
Parent - By jlnr (dev) Date 2022-03-27 13:13
This has already been discussed in the Discord. In general, it is probably easier to use raw OpenGL to manage textures because then you can ask for advice in general gamedev communities, not just the tiny niche that is Ruby/Gosu. If you want to use Gosu to load images into RGBA format, you can use Gosu::Image.new(...).to_blob to retrieve a binary RGBA string.
Up Topic Gosu / Gosu Exchange / Problem to tile a texture in a single quad

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill