Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: ninogenio 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.
-
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
-
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,
#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
-
Still don't really understand ???
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...
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
-
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 =)
#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
-
Glad you figured it out :)
-
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
-
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
-
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
-
cool i might investigate pbuffers at a later point but this seems to do its job fine anyway
-
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.
-
We're discussing GL_EXT_framebuffer_object here http://dbfinteractive.com/index.php?topic=1884.0 (http://dbfinteractive.com/index.php?topic=1884.0) :)
Jim