Author Topic: opengl texture question  (Read 5018 times)

0 Members and 1 Guest are viewing this topic.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
opengl texture question
« on: May 08, 2007 »
when textureing in opengl is it possible to do it via packed rgb data instead of individual bytes its for my render to texture as its a bit of a pain having to keep track of all the individual rgb data every frame.

the closest i got to it working was gl_rgb , gl_short_5_6_5 but thats 2 bytes so it doesnt work.

i cant think of a way to get it working but i would have thought it was possible?

cheers for any help.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: opengl texture question
« Reply #1 on: May 08, 2007 »
I don't understand what you mean.  If you have data in memory as r,g,b,a or r,g,b whether it's 32bit values or 4 8bit ones, you can just point glTexImage2D at the data.  GL_BGRA_EXT or GL_ARGB is what you're looking for for 32 bits.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: opengl texture question
« Reply #2 on: May 08, 2007 »
here is a bit of code to try to show what im getting at i dont know if this can be done or not though,
 
Code: [Select]
#Include "GenioGl\GenioGl.bi"

GenioGraphics( 800 , 600 , 32 , WINDOWED )



Type KeyTracker

      Key As Ubyte ptr

      Esckey as Uinteger
      Up As uinteger
      Down As Uinteger
      KLeft As Uinteger
      KRight As Uinteger
      Z As Uinteger

End Type

Declare     sub          GenioCheckKeys       ( Keys As KeyTracker )
Declare     Function   CreateTextureBuffer  ( Byval TWidth As Double , Byval THeight As Double ) As GLuByte PTR
Declare     Function   CopyTexture            ( Destination As GLuint , Byval Source As GLuint PTR )

Dim Shared As GLuint PTR TextureBuffer
Dim Shared As GLuint Texture

TextureBuffer = CreateTextureBuffer( 512 , 512 )

glGenTextures( 1, @Texture )
glBindTexture( GL_TEXTURE_2D , Texture )     
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D( GL_TEXTURE_2D , 0 , GL_RGB , 512 , 512 , 0 , GL_RGB , GL_UNSIGNED_SHORT_5_6_5 , 0)

Dim     Shared  Key      As     KeyTracker


Do

      GenioClear()
      GenioCheckKeys( Key )


      For Y = 0 To 512 * 512
             TextureBuffer[Y] = rgb(255,0,0)
      Next
     
      glBindTexture( GL_TEXTURE_2D , Texture )
      glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 512, 512 , GL_RGB , GL_UNSIGNED_SHORT_5_6_5 , TextureBuffer)

      GenioBltSprite( Texture , 0 , 0 , 800 , 600 )

      GenioRender()

LOOP WHILE Key.esckey <> 1
If ( TextureBuffer ) Then Deallocate( TextureBuffer )



Function CreateTextureBuffer( Byval TWidth As Double , Byval THeight As Double ) As GLuint PTR

            Dim Ddata as GLuint ptr
            Ddata = callocate ( TWidth * THeight )
            Return Ddata

End Function



sub GenioCheckKeys( Keys As KeyTracker )

      SDL_PumpEvents
      Keys.Key = SDL_GetKeyState(0)
      Keys.Esckey = Peek( Keys.Key + SDLK_ESCAPE )
      Keys.Up = peek( Keys.Key + SDLK_UP )
      Keys.Down = peek( Keys.Key + SDLK_DOWN )
      Keys.KLeft = peek( Keys.Key + SDLK_LEFT )
      Keys.KRight = peek( Keys.Key + SDLK_RIGHT )
      Keys.Z = peek( Keys.Key + SDLK_Z )

end sub

so what im trying to do each frame is update my texture like this

texture[y]= rgb(255,0,0)

instead of

texture[y] = 255
texture[y+1] = 0
texture[y+2] = 0
« Last Edit: May 08, 2007 by ninogenio »
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: opengl texture question
« Reply #3 on: May 09, 2007 »
Still don't really understand  ???
Code: [Select]
Ddata = callocate ( TWidth * THeight )
This line isn't allocating enough space.  You want 512x512 texture in either 24 or 32 bits then you have to allocate 3 or 4 times as much space as you have done.
Why are you using GL_UNSIGNED_SHORT_5_6_5?  Your texture at the moment is 512x512 bytes, so when you load it, it'd have to be only a palette or luminance texture.  If you allocate the right amount of space 512x512x3 for RGB then you'd just use GL_RGB.

What does your function rgb() do?
Why would you need to do this...
Code: [Select]
texture[y] = 255
texture[y+1] = 0
texture[y+2] = 0
if texture is an array of uinteger?  If you use 24bit textures, then you have to deal with it as bytes, since there's no 3byte type (and rightly so!).

If texture is an array of uinteger, then you'd allocate 512x512x4 and use GL_RGBA.  Then your colour is just one 32bit value.  Be sure to set the alpha to 0xff (255) - if you set it to 0 the texture will be totally transparent.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: opengl texture question
« Reply #4 on: May 09, 2007 »
Doh,

i was thinking that GL_UNSIGNED_BYTE would only take a byte ptr and not a uinteger one it works now. rgb is just a fb function that shifts the colors me being lasy lol.

how far does the opengl rabbit hole go? im finding my learning is never ending =)

Code: [Select]
#Include "GenioGl\GenioGl.bi"

GenioGraphics( 800 , 600 , 32 , WINDOWED )



Type KeyTracker

      Key As Ubyte ptr

      Esckey as Uinteger
      Up As uinteger
      Down As Uinteger
      KLeft As Uinteger
      KRight As Uinteger
      Z As Uinteger

End Type

Declare     sub          GenioCheckKeys       ( Keys As KeyTracker )
Declare     Function   CreateTextureBuffer  ( Byval TWidth As Double , Byval THeight As Double ) As GLuint PTR
Declare     Function   CopyTexture            ( Destination As GLuint , Byval Source As GLuint PTR )

Dim Shared As GLuint PTR TextureBuffer
Dim Shared As GLuint Texture

TextureBuffer = CreateTextureBuffer( 512 , 512 )

glGenTextures( 1, @Texture )
glBindTexture( GL_TEXTURE_2D , Texture )     
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D( GL_TEXTURE_2D , 0 , GL_RGB , 512 , 512 , 0 , GL_BGRA , GL_UNSIGNED_BYTE , 0)

Dim     Shared  Key      As     KeyTracker


Do

      GenioClear()
      GenioCheckKeys( Key )


      For Y = 0 To 512 * 512
             TextureBuffer[Y] = rgbA(255,0,0,255)
      Next
     
      glBindTexture( GL_TEXTURE_2D , Texture )
      glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 512, 512 , GL_BGRA , GL_UNSIGNED_BYTE , TextureBuffer)

      GenioBltSprite( Texture , 0 , 0 , 800 , 600 )

      GenioRender()

LOOP WHILE Key.esckey <> 1
If ( TextureBuffer ) Then Deallocate( TextureBuffer )



Function CreateTextureBuffer( Byval TWidth As Double , Byval THeight As Double ) As GLuint PTR

            Dim Ddata as GLuint ptr
            Ddata = callocate ( TWidth * THeight * 4 )
            Return Ddata

End Function



sub GenioCheckKeys( Keys As KeyTracker )

      SDL_PumpEvents
      Keys.Key = SDL_GetKeyState(0)
      Keys.Esckey = Peek( Keys.Key + SDLK_ESCAPE )
      Keys.Up = peek( Keys.Key + SDLK_UP )
      Keys.Down = peek( Keys.Key + SDLK_DOWN )
      Keys.KLeft = peek( Keys.Key + SDLK_LEFT )
      Keys.KRight = peek( Keys.Key + SDLK_RIGHT )
      Keys.Z = peek( Keys.Key + SDLK_Z )

end sub
Challenge Trophies Won:

Offline ferris

  • Pentium
  • *****
  • Posts: 841
  • Karma: 84
    • View Profile
    • Youth Uprising Home
Re: opengl texture question
« Reply #5 on: May 10, 2007 »
Glad you figured it out :)
http://iamferris.com/
http://youth-uprising.com/

Where the fun's at.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: opengl texture question
« Reply #6 on: May 10, 2007 »
Important to note that unless you're changing the texture every frame, you only need to glTexImage2D once before the loop.  Loading textures is slow, so you need to avoid it.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: opengl texture question
« Reply #7 on: May 10, 2007 »
yeah im using this for render to multi textures other wize id prebuild 2dmipmaps. which leads me too is this the fastest render to texture methode there is i mean i cant complain as in getting a few hundred fps at full screen but just wonderd if anything was faster
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: opengl texture question
« Reply #8 on: May 10, 2007 »
You're talking about software rendering and using it as a texture, and I think this is the best you can do.

You can use 'pbuffers' to use OpenGL to render textures that can then be used in another render, but I don't know much about that.

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: opengl texture question
« Reply #9 on: May 10, 2007 »
cool i might investigate pbuffers at a later point but this seems to do its job fine anyway
Challenge Trophies Won:

Offline ferris

  • Pentium
  • *****
  • Posts: 841
  • Karma: 84
    • View Profile
    • Youth Uprising Home
Re: opengl texture question
« Reply #10 on: May 14, 2007 »
you might also want to look into framebuffer objects, which although a more 'modern' card is required to take advantage them, they are incredibly fast compared to texcopyimage2d and such.
http://iamferris.com/
http://youth-uprising.com/

Where the fun's at.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: opengl texture question
« Reply #11 on: May 14, 2007 »
We're discussing GL_EXT_framebuffer_object here http://dbfinteractive.com/index.php?topic=1884.0 :)

Jim
Challenge Trophies Won: