Author Topic: OpenGL - Draw to Texture  (Read 5406 times)

0 Members and 1 Guest are viewing this topic.

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
OpenGL - Draw to Texture
« 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:
Challenge Trophies Won:

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: OpenGL - Draw to Texture
« Reply #1 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 !
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: OpenGL - Draw to Texture
« Reply #2 on: May 01, 2007 »

Nice work  rbraz.  :)

have a question.

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

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: OpenGL - Draw to Texture
« Reply #3 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.
« Last Edit: May 01, 2007 by ninogenio »
Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: OpenGL - Draw to Texture
« Reply #4 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.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: OpenGL - Draw to Texture
« Reply #5 on: May 02, 2007 »
yeah i was speaking from why i use it mate but of course your right for rbraz`s example ;)
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: OpenGL - Draw to Texture
« Reply #6 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.

Challenge Trophies Won:

Offline Emil_halim

  • Atari ST
  • ***
  • Posts: 248
  • Karma: 21
    • View Profile
    • OgreMagic Library
Re: OpenGL - Draw to Texture
« Reply #7 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.

Offline Optimus

  • DBF Aficionado
  • ******
  • Posts: 2456
  • Karma: 128
    • View Profile
    • Optimouse Demo Site
Re: OpenGL - Draw to Texture
« Reply #8 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.
Challenge Trophies Won: