Michael Clease
30-06-2007, 01:08
had a play around with kryton9 code and thought back to my amiga days.
This seems to work sometimes and not others.
enjoy.
Feel free to tweak. Use Left and RIght to adjust frequency.
2012-02-13 - Updated to fix the message loop and some minor improvements
Nice Effect. :)
If you're interested it Ran at 32 FPS on my Laptop which hasn't got a Middle Mouse Button. :D
Michael Clease
30-06-2007, 01:40
i just disabled all the timing stuff it only runs at 79 FPS
Try changing the blocksize to a larger number.
Petr Schreiber
30-06-2007, 19:13
Hi,
thanks for the code! I love SIN function in all its forms and uses ;)
On my PC with onboard card it runs also at fixed 32FPS.
Bye,
Petr
P.S. One thing - try to always use "TBGL_UseTexture 1/0" instead of "glEnable/glDisable ( %GL_TEXTURE_2D )", it is better optimised for TBGL pipeline. In this case it would not improve performance, but when dealing with models it could ( little bit, but yes :) ).
Why ? Model buffers take care of being independent on states and they do auto cleanup, internal variables help to optimize that better than via OpenGL driver queries. Performance difference is not dramatic, but when you need to get every frame from your card :) ...
P.P.S While TBGLiing your code I did something that now it runs on 64 FPS :D
' Sin Wave V1.0.0.02
' thx to Kryton9 for pointing me in that direction
' based on the great base code from
' Demonstration of rendering to texture
' Copyright (C) 2005 Julien Guertault
'
' Abraxas 2007
'
Uses "TBGL"
USES "UI"
#INCLUDE "%APP_INCLUDEPATH%\thinbasic_gl.inc"
#INCLUDE "%APP_INCLUDEPATH%\thinbasic_glu.inc"
%ScrnWidth = 640 ' Window width
%ScrnHeight = 480 ' Window Height
%BlockSizeX = 2 ' Size of block
%SIZEX = 640 ' size of texture X
%SIZEY = 72 ' Size of texture Y
%TexTile = %SIZEx/%BlockSizeX ' For the Texture slices
dim texture(3 * %SIZEX * %SIZEY) as GLvoid
dim texture_id as GLuint = 1
DIM sMsg as string
DIM WinTITLE AS STRING VALUE "Sine Wave - press Middle mouse to quit"
DIM Message AS STRING VALUE "Sine Wave"
DIM X AS SINGLE VALUE 0
DIM Y AS SINGLE VALUE 0
dim n as SINGLE value 0
DIM tx AS SINGLE value 0
DIM m as SINGLE value 0
DIM R AS SINGLE VALUE 0
DIM a as SINGLE VALUE 0
DIM b as SINGLE VALUE 0
DIM Freq as SINGLE VALUE 0
Dim ThisTime, LastTime, TimeDelta as dword
DIM EXITFLAG AS BYTE VALUE 0
DIM hWnd AS DWORD VALUE = TBGL_CreateWindowEx(WinTITLE, %ScrnWidth , %ScrnHeight, 32, 0) ' Creates OpenGL window, it returns handle
DIM hFont AS DWORD = Font_Create("Courier New", 72) ' Define the Font I want to use
TBGL_BuildFont hFont ' Build the Font
TBGL_ClearFrame ' Prepares clear frame
TBGL_ShowWindow ' Shows the window
'/* Texture setting */
tbgl_UseTexture 1
tbgl_BindTexture texture_id
glTexImage2D (%GL_TEXTURE_2D, 0, %GL_RGB, %SIZEX, %SIZEY, 0, %GL_RGB, %GL_UNSIGNED_BYTE, texture)
glTexParameterF (%GL_TEXTURE_2D, %GL_TEXTURE_MIN_FILTER, %GL_LINEAR)
glTexParameterF (%GL_TEXTURE_2D, %GL_TEXTURE_MAG_FILTER, %GL_LINEAR)
glTexParameterF (%GL_TEXTURE_2D, %GL_TEXTURE_WRAP_S, %GL_CLAMP)
glTexParameterF (%GL_TEXTURE_2D, %GL_TEXTURE_WRAP_T, %GL_CLAMP)
while tbgl_isWindow(hWnd)
LastTime = ThisTime
ThisTime = GetTickCount
TimeDelta= ThisTime-LastTime
If TimeDelta = 0 THEN TimeDelta = 1
TBGL_RenderMatrix2D
TBGL_ClearFrame ' Prepares clear frame
' TBGL_ResetMatrix ' Not realy needed after CLEARFRAME
TBGL_Camera 0,0,1,0,0,0 ' Setups camera to look from 0,0,1 to 0,0,0
' TBGL_Color 0, 0, 0 ' Setting color this way affects even font
TBGL_Translate 0,0,1 '
glDISABLE (%GL_TEXTURE_2D) ' Disable Textures
' IF a < 1 THEN b = +1 ' add a bit of wobble
' IF a > 40 THEN b = -1
'
' a += b
TBGL_Color 255,255,255 ' Setting color this way affects even font
TBGL_PrintFont message, a, 1, 0 ' Output message to buffer
'/* Copy buffer to texture */
glCopyTexSubImage2D(%GL_TEXTURE_2D, 0, 0, 0, 0, 0, %SizeX, %SIZEY )
tbgl_UseTexture 1
tbgl_BindTexture texture_id
for n = 1 to %ScrnWidth/%BlockSizeX
R += Freq ' sine frequency
tx += 1/%TexTile
tbgl_BeginPoly %GL_QUADS
tbgl_TexCoord2D ( tx-1/%TexTile, 0) : tbgl_Vertex ( %BlockSizeX*n-%BlockSizeX , %ScrnHeight/2+10*SIN(r+m))
tbgl_TexCoord2D ( tx , 0) : tbgl_Vertex ( %BlockSizeX*n , %ScrnHeight/2+10*SIN(R+m))
tbgl_TexCoord2D ( tx , 1) : tbgl_Vertex ( %BlockSizeX*n , %ScrnHeight/2+10*SIN(R+m)+100)
tbgl_TexCoord2D ( tx-1/%TexTile, 1) : tbgl_Vertex ( %BlockSizeX*n-%BlockSizeX , %ScrnHeight/2+10*SIN(R+m)+100)
tbgl_EndPoly
next
tx = 0 ' reset my sub textile counter
r = 0 ' reset ripple
m += 0.1 ' phase shift my sine wave
TBGL_DrawFrame ' Swaps the buffers - displays rendered image
TBGL_SetWindowTitle(hWnd, "Sine Wave - press ESC to quit - Running at " & TBGL_GetFramerate & " FPS Freq " & Freq )
if tbgl_GetWindowKeyState( hWnd, %VK_LEFT) THEN
IF Freq > 0.01 THEN Freq -=0.001
ENDIF
if tbgl_GetWindowKeyState( hWnd, %VK_RIGhT) THEN
IF Freq < 0.2 THEN Freq +=0.001
ENDIF
if tbgl_GetWindowKeyState( hWnd, %VK_ESCAPE) THEN exitFLAG = %TRUE
If ExitFLAG = %TRUE THEN EXIT WHILE
if TimeDelta > 1000/70 then sleep 1000/70 '-TimeDelta
wend
TBGL_KillFont
TBGL_DestroyWindow ' Closes OpenGL window
You know I was looking at this and thinking wow this would be a great start to a dynamic text texture editor. Have some controls to adjust parameters, color, expression, size, rotation and then save the info out to an ini file and then write a subroutine that could load from the ini, this sub would be called once, then a render subroutine that can be used to draw the effect. I guess the same could be done with regular textures too. So two powerful effects could be universal to use in all our apps.
I would do it, but I started enough projects that I need to finish and make universal use too for out programs.
Michael Clease
01-07-2007, 01:22
Petr your version doesnt work on my machine can you post the the tbasic file so i confirm its not just a cut and paste problem.
It worked on my computer just selecting and pasting to let you know. Probably just missed getting something in the copy as you said.
Petr Schreiber
01-07-2007, 11:25
Hi Abraxas,
please find attached version here.
But it is not better than yours, just few lines changed :)
Bye,
Petr
All the examples in this thread are giving errors. To save time, just look at the previous post's attachment from Petr.
Abraxas_SinWave.tbasic
It says a parenthesis is missing when there is one?
Michael Hartlef
19-06-2010, 06:37
yes, I can confirm that the parser is choking on this.
ErosOlmi
19-06-2010, 10:09
Problem is that since 2007 thinBasic has changed the way it passes arrays to external functions.
It expect the initial element to which pass the reference
If you want to make it working with current 1.8.x version change from
glTexImage2D(%GL_TEXTURE_2D, 0, %GL_RGB, %SIZEX, %SIZEY, 0, %GL_RGB, %GL_UNSIGNED_BYTE, texture)
to
glTexImage2D(%GL_TEXTURE_2D, 0, %GL_RGB, %SIZEX, %SIZEY, 0, %GL_RGB, %GL_UNSIGNED_BYTE, texture(1))
when you pass an array as parameter to an external function.
I will see if I can improve.
Eros
ErosOlmi
19-06-2010, 10:27
I think I've fixed.
If no (x) is specified in passing an array/matrix, (1) will be assumed.
Will be present in next update