Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: Rbz on April 30, 2007

Title: OpenGL - Draw to Texture
Post by: Rbz on April 30, 2007
For those who want to create a texture and use it in opengl, here's a simple code to do that job.

Any problems, let me know...

 :cheers:
Title: Re: OpenGL - Draw to Texture
Post by: benny! on April 30, 2007
Always like pieces of sources showing one separated fx or technique with
an running exe included. Karma up for sharing. Thx mate !
Title: Re: OpenGL - Draw to Texture
Post by: Emil_halim on May 01, 2007

Nice work  rbraz.  :)

have a question.

why you use Push/Pop Matrix instead of using  LoadIdentity for starting transformation ?
Title: Re: OpenGL - Draw to Texture
Post by: ninogenio on May 01, 2007
@emil i personaly (cant speak fot rbraz though) use push and pop becasue i think its good to keep a record of what in the modelview its handy for me because i load my camera matrices in the modelveiw at the start of a new frame and just mutiply them by each of my model matrices pushing and poping as i go down.

@rbraz i think vain was after a render to texture like this mate not sure 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 GLuByte 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_byte, 0)

Dim     Shared  Key      As     KeyTracker


Do

      GenioClear()
      GenioCheckKeys( Key )


      For Y = 0 To (512 * 512 * 3) Step 3
             TextureBuffer[Y] = Rnd(1)*255
             TextureBuffer[Y+1] = Rnd(1)*255
             TextureBuffer[Y+2] = Rnd(1)*255
      Next
     
      glBindTexture( GL_TEXTURE_2D , Texture )
      glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 512, 512 , GL_RGB , 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 GLuByte PTR

            Dim Ddata as GLuByte ptr
            Ddata = callocate ( ( TWidth * THeight )  * 3 * Len( integer ) )
            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

this wont compile as ive not included my engine but it would be easy to make it work.
ive only just banged this up for something im working on so dont go mad if its not right lol.

here is the exe.
Title: Re: OpenGL - Draw to Texture
Post by: Emil_halim on May 02, 2007

Yes  Nino , that is ok in the case of  having more that model , but the case here is just draw a cube so that I asked.

As you already know , using Push/Pop instead of LoadIdentity  will take more time for that simple example , there is no reason to keep track of ModelView matrix in his example.
Title: Re: OpenGL - Draw to Texture
Post by: ninogenio on May 02, 2007
yeah i was speaking from why i use it mate but of course your right for rbraz`s example ;)
Title: Re: OpenGL - Draw to Texture
Post by: Rbz on May 02, 2007

Nice work  rbraz.  :)

have a question.

why you use Push/Pop Matrix instead of using  LoadIdentity for starting transformation ?


Emil dude, this part of my code are just a copy&paste from other project, and I don't think this change will make it more faster (how much nano seconds :) ) .

Anyway the subject of my post was how to create a texture and use it.

Title: Re: OpenGL - Draw to Texture
Post by: Emil_halim on May 02, 2007

Ok rbraz , but be careful we measure  the time by femto Second  not nano. :)

Thank you for posting your work for letting others to learn.
Title: Re: OpenGL - Draw to Texture
Post by: Optimus on May 03, 2007
That's great and easy. I downloaded the code and devc on a netcafe and tried to send a plasma effect and it's smooth. I always wondered in the past if it's easy and fast to do perpixel manipulations with the 3d acceleration this way but I guess this was a speed issue in the very past only? It was a myth but now I've checked it easilly ;). Lol, it's really great, I guess I'll start generating some textures for my next intro just for fun.