PDA

View Full Version : Oh wow, got it working, dynamic textures



kryton9
20-06-2007, 13:11
I stayed up all night and finally got it working. I found this c program for making dynamic textures in opengl.

I have been hacking away at it over and over till I got it working. Woohoo!!! I am so excited.

This is neat, but the code is messy as you see me commenting and trying different things. The c source is in there but it is a mess.

To excited to clean it up at the moment, but will later. For now here it is, it draws a cube, it then makes that a texture and you see it update
while the cube rotates around.

I will clean it up later and make it easier to follow, but this is neat that it works so well and is not slow at all!!

Michael Clease
20-06-2007, 13:35
thats really good (did make me feel a bit sick if you watch it to close)

I had a quick look when into it when you set the challenge.

I will play around and see what happens.

thx

ErosOlmi
20-06-2007, 13:55
Hey ken, this is really great.
What a good example. Petr will be scared when he will be back :D

This script is also very interesting for me because you have mainly used direct DLL interface to OpenGL. I have the impression I can get some more speed improvements on this aspect so your code is perfect for my tests.

Thanks a lot
Eros

RobertoBianchi
20-06-2007, 14:12
Kryton,

amazing! With little more than a hundred code lines you made a good animation.

Congratulations,
Roberto

matthew
20-06-2007, 15:39
Nice Programme.

I thought I'd attempt to Port it to Basic4GL, lol.

You can see it here (http://www.gamedesignnovice.com/forum/index.php?topic=762.msg4348#new). :)

Petr Schreiber
20-06-2007, 19:36
:o :o :o,

amazing work kryton9!
I am hardly searching words! I am leaving back to the mountains, where I will spent rest of life on meditations, because new GL boss is here ;) Great!

Did you know render to texture is fastest approach to perform motion blur too ?
Just save few frames back and than render it all at once ! I think NFS: Most Wanted has similar approach for example.


Bye,
Petr

kryton9
20-06-2007, 20:12
Thanks guys, glad you all got excited by the possibilities too! It just shows how cool thinBasic is and how well tbgl is written.

Welcome back Petr. I never thought about it for blur sounds great!

Matthew, I haven't played with 4gl, but will install it and try out your program. Just by looking at the link you did a great job cleaning up the mess!!!

I am sure now that this technique works and performance is not a factor, we will see lots of cool programs coming in the future!!

kryton9
20-06-2007, 23:38
Here is a neat looking effect that I came up with tinkering with the base code.

I am sure more will come, but this looks pretty nice so I thought I would share it before I lost it with changes.

You can change some attributes on the fly:

up and down arrow, the speed of the rotation lesser the speed value the faster it goes

left and right arrow the size of the plane being rendered.

spacebar resets to default

escape to exit

just for fun, you can comment out the cube() that is right above TBGL_DrawFrame and put in plane() to see it that way.

Also you could change the render to buffer to cube() and the last one to plane()

so lots of looks with little changes.

Petr Schreiber
20-06-2007, 23:43
Very nice demo Kent,

just the movements could be framerate dependant and so running the same on all PCs.
Looks impressive!

Bye,
Petr

kryton9
20-06-2007, 23:50
Thanks Petr, we crossed paths on posts. I made a few more additions to the instructions and code parts to comment and uncomment.
Since the user can adjust the speed, I didn't mess with getting the framerate for all.

But thanks for reminding me for future non speed adjustable demos.

Petr Schreiber
21-06-2007, 00:34
kryton,

no problem with that speed sync stuff,
just searching mistakes on perfect script :)


Bye,
Petr

Michael Hartlef
21-06-2007, 19:24
Nice. But sadly the only TBGL connection is the CreateWindow command.

Petr Schreiber
21-06-2007, 19:32
Mike,

krytons script could be quite rapidly converted to look more TBGLish. But it works perfectly even in almost pure GL too.
Just some function need to be used from headers ( and thats the reason why I maintain them along with module ).

If you would like to see some parts as single TBGL commands, please let me know ;)


Bye,
Petr

Michael Hartlef
21-06-2007, 20:23
Yes, the part where you can modify a TBGL texture that is in memory. Or build one like KEnt and then use that as a TBGL texture.

kryton9
21-06-2007, 21:32
All I can say is that in trying all of the languages I have been trying lately, it is hard to match thinBasic. TBGL really makes opengl fun to use And they are both under development. Already it is my favorite language to use. And keeps getting better and better every day. So I appreciate all you guys have done.

I was lured by OOP programming, but when you see the reality... I think the reality is that programmers like to write and create objects. They are fun to make. But it seems very few objects get used by others. In Delphi, with the components, it is different. THey are useful and do get used. But in c++. Everyone has written their own 3d math library. Why? Why not use one great library? Because of the headaches of doing so and also, like I said, I think people like making objects.

With udt's and global variables, you can have much nicer looking code that is easier to go back to in the future. I am still learning the other languages because I know when I get a small device, I would like to write for it. If it wasn't for that thinBasic would be my only focus.

Anyways just wanted to give a big thanks to you guys.

Petr Schreiber
21-06-2007, 21:45
Thanks kryton :),

I am very happy you feel it like this. And it is pleasure to see how you get used to tB and master it more and more.

Here is just TBGLified version of your latest render2tex code:


' Demo 2 of Rendered Text by Kryton9
' based on the great base code from
' Demonstration of rendering to texture
' Copyright (C) 2005 Julien Guertault

' -- Just TBGLified a bit by Psch

Uses "TBGL"

#INCLUDE "%APP_INCLUDEPATH%\thinbasic_gl.inc"
#INCLUDE "%APP_INCLUDEPATH%\thinbasic_glu.inc"

%SIZE = 256

dim texture(3 * %SIZE * %SIZE) as GLvoid
dim texture_id as GLuint = 1
dim window_width as LONG = 500
dim window_height as LONG = 500
dim speed as double = 75
dim sizze as double = 2
dim hWnd as dword

hWnd = TBGL_CreateWindowEX("Render to texture - press ESC to quit", window_width, window_height, 32, 0)
TBGL_ShowWindow

tbgl_UseTexture 1
tbgl_BindTexture texture_id

glTexImage2D (%GL_TEXTURE_2D, 0, %GL_RGB, %SIZE, %SIZE, 0, %GL_RGB, %GL_UNSIGNED_BYTE, texture)
glTexParameteri (%GL_TEXTURE_2D, %GL_TEXTURE_MIN_FILTER, %GL_LINEAR)
glTexParameteri (%GL_TEXTURE_2D, %GL_TEXTURE_MAG_FILTER, %GL_LINEAR)
glTexParameteri (%GL_TEXTURE_2D, %GL_TEXTURE_WRAP_S, %GL_CLAMP)
glTexParameteri (%GL_TEXTURE_2D, %GL_TEXTURE_WRAP_T, %GL_CLAMP)

dim FrameRate as number
tbgl_GetAsyncKeyState(-1) ' Resets key status before checking

while tbgl_IsWindow(hWnd)

FrameRate = tbgl_GetFrameRate

tbgl_ClearFrame
tbgl_Translate (0, 0, -10)
tbgl_Rotate (30, 1, 0, 0)
tbgl_Rotate (GetTickCount/speed, 0, 1, 0)

'/* Define a view-port adapted to the texture */
glMatrixMode(%GL_PROJECTION)
tbgl_resetMatrix
gluPerspective (20, 1, 5, 15)
glViewport(0, 0, %SIZE, %SIZE)
glMatrixMode(%GL_MODELVIEW)

'/* Render to buffer */
tbgl_BackColor 255, 175, 1, 0
glClear (%GL_COLOR_BUFFER_BIT or %GL_DEPTH_BUFFER_BIT)
plane()
'Cube ()
glFlush ()

'/* Copy buffer to texture */
glCopyTexSubImage2D(%GL_TEXTURE_2D, 0, 5, 5, 0, 0, %SIZE - 10, %SIZE - 10)

'/* Render to screen */
glMatrixMode(%GL_PROJECTION)
tbgl_resetMatrix
gluPerspective (20, window_width / window_height, 5, 15)
tbgl_Viewport(0, 0, 1, 1)
glMatrixMode(%GL_MODELVIEW)
tbgl_BackColor 0,0,0,0
glClear (%GL_COLOR_BUFFER_BIT or %GL_DEPTH_BUFFER_BIT)
'plane()
Cube ()

TBGL_DrawFrame

if tbgl_GetWindowKeyState( hWnd, %VK_ESCAPE) THEN exit while
if tbgl_GetWindowKeyState( hWnd, %VK_UP) THEN speed = iif( speed <= 0, 1, speed + 1/FrameRate)
if tbgl_GetWindowKeyState( hWnd, %VK_DOWN) THEN speed -= 1/FrameRate
if tbgl_GetWindowKeyState( hWnd, %VK_RIGHT) THEN sizze += 5/FrameRate
if tbgl_GetWindowKeyState( hWnd, %VK_LEFT) THEN sizze -= 5/FrameRate
if tbgl_GetWindowKeyState( hWnd, %VK_SPACE) THEN
sizze = 2
speed = 75
end if

tbgl_setwindowtitle (hwnd,"ESC to quit speed = "+format$(speed,"0.00")+" size = "+format$(sizze,"0.00"))

wend

TBGL_DestroyWindow ' Closes TBGL window


sub Cube()

tbgl_BeginPoly %GL_QUADS

tbgl_TexCoord2d (0, 0) : tbgl_Vertex (-1, -1, -1)
tbgl_TexCoord2d (0, 1) : tbgl_Vertex (-1, -1, 1)
tbgl_TexCoord2d (1, 1) : tbgl_Vertex (-1, 1, 1)
tbgl_TexCoord2d (1, 0) : tbgl_Vertex (-1, 1, -1)

tbgl_TexCoord2d (0, 0) : tbgl_Vertex ( 1, -1, -1)
tbgl_TexCoord2d (0, 1) : tbgl_Vertex ( 1, -1, 1)
tbgl_TexCoord2d (1, 1) : tbgl_Vertex ( 1, 1, 1)
tbgl_TexCoord2d (1, 0) : tbgl_Vertex ( 1, 1, -1)

tbgl_TexCoord2d (0, 0) : tbgl_Vertex (-1, -1, -1)
tbgl_TexCoord2d (0, 1) : tbgl_Vertex (-1, -1, 1)
tbgl_TexCoord2d (1, 1) : tbgl_Vertex ( 1, -1, 1)
tbgl_TexCoord2d (1, 0) : tbgl_Vertex ( 1, -1, -1)

tbgl_TexCoord2d (0, 0) : tbgl_Vertex (-1, 1, -1)
tbgl_TexCoord2d (0, 1) : tbgl_Vertex (-1, 1, 1)
tbgl_TexCoord2d (1, 1) : tbgl_Vertex ( 1, 1, 1)
tbgl_TexCoord2d (1, 0) : tbgl_Vertex ( 1, 1, -1)

tbgl_TexCoord2d (0, 0) : tbgl_Vertex (-1, -1, -1)
tbgl_TexCoord2d (0, 1) : tbgl_Vertex (-1, 1, -1)
tbgl_TexCoord2d (1, 1) : tbgl_Vertex ( 1, 1, -1)
tbgl_TexCoord2d (1, 0) : tbgl_Vertex ( 1, -1, -1)

tbgl_TexCoord2d (0, 0) : tbgl_Vertex (-1, -1, 1)
tbgl_TexCoord2d (0, 1) : tbgl_Vertex (-1, 1, 1)
tbgl_TexCoord2d (1, 1) : tbgl_Vertex ( 1, 1, 1)
tbgl_TexCoord2d (1, 0) : tbgl_Vertex ( 1, -1, 1)

tbgl_EndPoly
end sub

sub plane()
tbgl_BeginPoly %GL_QUADS
tbgl_Color 255, 0, 0 : tbgl_TexCoord2d 0,0 : tbgl_Vertex -sizze , -sizze, 0
tbgl_Color 0 , 255, 0 : tbgl_TexCoord2d 0,1 : tbgl_Vertex sizze , -sizze, 0
tbgl_Color 0 , 0, 255 : tbgl_TexCoord2d 1,1 : tbgl_Vertex sizze , sizze, 0
tbgl_Color 255, 200, 0 : tbgl_TexCoord2d 1,0 : tbgl_Vertex -sizze , sizze, 0
tbgl_EndPoly
tbgl_Color 255, 175, 0
end sub


Bye,
Petr

kryton9
22-06-2007, 01:45
Petr, very nice.

I am going to simplify my new commands and do them in chunks to work out ideas. No sense trying to write a bunch of subroutines till we agree on a sweet interface scheme.

I will do the lines demo tonight. Been thinking about it all day while out doing errands. I am ready to code now. Hope to have something by your time tomorrow morning.