Author Topic: Here's a simple glsl demo.  (Read 14389 times)

0 Members and 1 Guest are viewing this topic.

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Here's a simple glsl demo.
« on: July 15, 2006 »
It's a metablob demo using a glsl vertex shader. It wont run on old cards, so I wouldn't bother with it, if you do have an older card.
By the way, nice forum!  :cheers:

Code: [Select]
Option Explicit
Randomize Timer
'$Static

'$INCLUDE: "\gl\gl.bi"
'$INCLUDE: "\gl\glu.bi"
'$INCLUDE: "\gl\glext.bi"
'$INCLUDE: "\gl\glfw.bi"

Const As Integer True = -1, False = Not True


Type DisplayMode
  Â  W As Uinteger
  Â  H As Uinteger
  Â  R_BITS As Uinteger
  Â  G_BITS As Uinteger
  Â  B_BITS As Uinteger
  Â  A_BITS As Uinteger
  Â  D_BITS As Uinteger
  Â  S_BITS As Uinteger
  Â  MODE As Uinteger
  Â  GlVer As Zstring Ptr
  Â  As Single FOVy, Aspect, zNear, zFar
End Type


Type Balls
  Â  As Single R, G, B
  Â  As Single x, Dx, y, Dy, Rad
End Type



Declare Sub Init_GL_Window( Display As DisplayMode )
Declare Sub Set_Ortho( Display As DisplayMode )
Declare Sub Drop_Ortho( Display As DisplayMode )
Declare Sub Init_Balls( Ball() As Balls, Display As DisplayMode)

Const MAX_BALLS As Integer = 25

Dim As Integer Use_Shader = True
Dim Ball(1 To Max_Balls) As Balls
Dim Display As DisplayMode
Init_GL_Window Display

#Define Midx (Display.w \ 2)
#Define Midy (Display.h \ 2)

Dim glCreateShaderObjectARBÂ  Â As PFNglCreateShaderObjectARBPROCÂ  Â = Cptr(PFNGLCREATESHADEROBJECTARBPROC, glfwGetProcAddress( "glCreateShaderObjectARB" ))
Dim glShaderSourceARBÂ  Â  Â  Â  Â As PFNglShaderSourceARBPROCÂ  Â  Â  Â  Â = Cptr(PFNGLSHADERSOURCEARBPROC, glfwGetProcAddress( "glShaderSourceARB" ))
Dim glGetShaderSourceARBÂ  Â  Â  As PFNglGetShaderSourceARBPROCÂ  Â  Â  = Cptr(PFNGLGetSHADERSOURCEARBPROC, glfwGetProcAddress( "glGetShaderSourceARB" ))
Dim glCompileShaderARBÂ  Â  Â  Â  As PFNGLCompileShaderARBPROCÂ  Â  Â  Â  = Cptr(PFNglCompileShaderARBPROC, glfwGetProcAddress( "glCompileShaderARB" ))
Dim glDeleteObjectARBÂ  Â  Â  Â  Â As PFNGLDeleteObjectARBPROCÂ  Â  Â  Â  Â = Cptr(PFNglDeleteObjectARBPROC, glfwGetProcAddress( "glDeleteObjectARB" ))
Dim glCreateProgramObjectARBÂ  As PFNglCreateProgramObjectARBPROCÂ  = Cptr(PFNglCreateProgramObjectARBPROC, glfwGetProcAddress( "glCreateProgramObjectARB" ))
Dim glAttachObjectARBÂ  Â  Â  Â  Â As PFNglAttachObjectARBPROCÂ  Â  Â  Â  Â = Cptr(PFNglAttachObjectARBPROC, glfwGetProcAddress( "glAttachObjectARB" ))
Dim glUseProgramObjectARBÂ  Â  Â As PFNglUseProgramObjectARBPROCÂ  Â  Â = Cptr(PFNglUseProgramObjectARBPROC, glfwGetProcAddress( "glUseProgramObjectARB" ))
Dim glLinkProgramARBÂ  Â  Â  Â  Â  As PFNglLinkProgramARBPROCÂ  Â  Â  Â  Â  = Cptr(PFNglLinkProgramARBPROC, glfwGetProcAddress( "glLinkProgramARB" ))
Dim glValidateProgramARBÂ  Â  Â  As PFNglValidateProgramARBPROCÂ  Â  Â  = Cptr(PFNglValidateProgramARBPROC, glfwGetProcAddress( "glValidateProgramARB" ))
Dim glGetObjectParameterivARB As PFNglGetObjectParameterivARBPROC = Cptr(PFNglGetObjectParameterivARBPROC, glfwGetProcAddress( "glGetObjectParameterivARB" ))
Dim glGetInfoLogARBÂ  Â  Â  Â  Â  Â As PFNglGetInfoLogARBPROCÂ  Â  Â  Â  Â  Â = Cptr(PFNglGetInfoLogARBPROC, glfwGetProcAddress( "glGetInfoLogARB" ))

If glfwExtensionSupported( "GL_ARB_shader_objects" ) = 0 Then
  Â  Print "Error: ARB shader objects extension not supported."
  Â  Sleep 1000
  Â  End -1
End If

If( glCreateShaderObjectARB = 0 ) Then
  Â  Print "Error: glCreateShaderObjectARB not present."
  Â  Sleep 1000
  Â  End -1
End If

If( glCreateProgramObjectARB = 0 ) Then
  Â  Print "Error: glCreateProgramObjectARB not present."
  Â  Sleep 1000
  Â  End -1
End If

If( glShaderSourceARB = 0 ) Then
  Â  Print "Error: glShaderSourceARB not present."
  Â  Sleep 1000
  Â  End -1
End If


If( GL_ARB_shading_language_100 = 0 ) Then
  Â  Print "Error: This program required GL_ARB_shading_language_100."
  Â  Sleep 1000
  Â  End -1
End If


Dim As Integer Line_Cnt
Dim As String Shader_Text, tString
Dim As Integer i, x, y, Scale = 8
Dim As Integer  Dist
Dim As GlHandleARB Vertex_Shader, Shader_Program
Dim As Gluint Shader_Compile_Success


Read Line_Cnt
For i = 1 To Line_Cnt
  Â Read tString
  Â Shader_Text + = tString + Chr( 13, 10 )
Next


Dim As GLcharARB Ptr table(0) => { Strptr( Shader_Text ) }
Vertex_Shader = glCreateShaderObjectARB( GL_VERTEX_SHADER )
glShaderSourceARB( Vertex_Shader, 1, @table(0), 0 )
glCompileShaderARB( Vertex_Shader )

glGetObjectParameterivARB(Vertex_Shader, GL_OBJECT_COMPILE_STATUS_ARB, @Shader_Compile_Success )
If Shader_Compile_Success = 0 Then
  Â  Dim As Gluint infologsize
  Â  glGetObjectParameterivARB( Vertex_Shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, @infoLogSize)
  Â  Dim As GlByte infolog(InfoLogSize)
  Â  glGetInfoLogARB(Vertex_Shader, InfoLogSize, 0, @infoLog(0))
  Â  tString=""
  Â  For i = 0 To InfoLogSize-1
  Â  Â  Â  tString+=Chr(InfoLog(i))
  Â  Next
  Â  
  Â  Print "Vertex Shader Infolog error message:"
  Â  Print tString
End If


Shader_Program = GlCreateProgramObjectARB()
glAttachObjectARB( Shader_Program, Vertex_Shader )


glLinkProgramARB( Shader_Program )
GlValidateProgramARB( Shader_Program )


glGetObjectParameterivARB( Shader_Program, GL_OBJECT_VALIDATE_STATUS_ARB, @Shader_Compile_Success )
If Shader_Compile_Success = 0 Then
  Â  Beep
  Â  Print "GLSL program failed to compile!"
  Â  Sleep 1000
End If


If GL_VERTEX_PROGRAM_POINT_SIZE_ARB = 0 Then
  Â  Beep
  Â  Print "GL_VERTEX_PROGRAM_POINT_SIZE_ARB required for program to run correctly!"
Else
  Â  GLEnable GL_VERTEX_PROGRAM_POINT_SIZE_ARB
End If


Set_Ortho( Display )
'Drop_Ortho( Display )
Init_Balls( Ball(), Display )


Do
  Â  
  Â  If GlfwGetKey(GLFW_KEY_SPACE) Then
  Â  Â  Â  Use_Shader Xor = True
  Â  Â  Â  Do While GlfwGetKey(GLFW_KEY_SPACE)
  Â  Â  Â  Â  Â  GlfwPollEvents()
  Â  Â  Â  Loop  Â  
  Â  End If
  Â  
  Â  If Use_Shader Then
  Â  Â  Â  glUseProgramObjectARB( Shader_Program )
  Â  Else
  Â  Â  Â  glUseProgramObjectARB( False )
  Â  End If
  Â  
  Â  GlClear GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT
  Â  
  Â  For i = 1 To MAX_BALLS
  Â  Â  Â  Ball(i).x += Ball(i).dx
  Â  Â  Â  Ball(i).y += Ball(i).dy
  Â  Â  Â  
  Â  Â  Â  If Ball(i).x>360 Then
  Â  Â  Â  Â  Â  Ball(i).dx -= (1+Rnd*.5)
  Â  Â  Â  End If
  Â  Â  Â  
  Â  Â  Â  If Ball(i).x<280 Then
  Â  Â  Â  Â  Â  Ball(i).dx += (1+Rnd*.5)
  Â  Â  Â  End If
  Â  Â  Â  
  Â  Â  Â  If Ball(i).y>280 Then
  Â  Â  Â  Â  Â  Ball(i).dy -= (1+Rnd*.5)
  Â  Â  Â  End If
  Â  Â  Â  
  Â  Â  Â  If Ball(i).y<200 Then
  Â  Â  Â  Â  Â  Ball(i).dy += (1+Rnd*.5)
  Â  Â  Â  End If
  Â  Â  Â  
  Â  Next
  Â  
  Â  GlBegin GL_QUADS
  Â  For y = Display.h To Scale Step -Scale
  Â  Â  Â  For x = Scale To Display.w Step Scale
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  Dim As Single R,G,B
  Â  Â  Â  Â  Â  Dim As Integer Hits
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  For i = 1 To MAX_BALLS
  Â  Â  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  Â  Â  Dist = ((y-Ball(i).y)^2 + (x-Ball(i).x)^2)/Ball(i).Rad
  Â  Â  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  Â  Â  If Dist<= Ball(i).Rad Then
  Â  Â  Â  Â  Â  Â  Â  Â  Â  R += (Ball(i).Rad-Dist)/Ball(i).R
  Â  Â  Â  Â  Â  Â  Â  Â  Â  G += (Ball(i).Rad-Dist)/Ball(i).G
  Â  Â  Â  Â  Â  Â  Â  Â  Â  B += (Ball(i).Rad-Dist)/Ball(i).B
  Â  Â  Â  Â  Â  Â  Â  Â  Â  Hits-=1
  Â  Â  Â  Â  Â  Â  Â  End If
  Â  Â  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  Next
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  If Hits>0 Then
  Â  Â  Â  Â  Â  Â  Â  R/=Hits
  Â  Â  Â  Â  Â  Â  Â  G/=Hits
  Â  Â  Â  Â  Â  Â  Â  B/=Hits
  Â  Â  Â  Â  Â  End If
  Â  Â  Â  Â  Â  GlEnable GL_BLEND
  Â  Â  Â  Â  Â  GlBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
  Â  Â  Â  Â  Â  GlColor4F R, G, B, 0
  Â  Â  Â  Â  Â  GlVertex3f X, Y, -Y / 500
  Â  Â  Â  Â  Â  GlColor4F R, G, B, 0
  Â  Â  Â  Â  Â  GlVertex3f X-Scale, Y, -Y / 500
  Â  Â  Â  Â  Â  GlColor4F R, G, B, 0
  Â  Â  Â  Â  Â  GlVertex3f X-Scale, Y-Scale, -( Y - Scale ) / 500
  Â  Â  Â  Â  Â  GlColor4F R, G, B, 0
  Â  Â  Â  Â  Â  GlVertex3f X, Y-Scale, -( Y - Scale ) / 500
  Â  Â  Â  Â  Â  
  Â  Â  Â  Next
  Â  Next
  Â  GlEnd
  Â  
  Â  
  Â  GlfwSwapBuffers
Loop Until glfwGetKey( GLFW_KEY_ESC )

GlDeleteObjectArb( Vertex_Shader )
GlDeleteObjectArb( Shader_Program )
GlfwTerminate()
End


Sub Init_Balls( Ball() As Balls, Display As DisplayMode)
  Â  Dim As Integer i
  Â  
  Â  For i = 1 To MAX_BALLS
  Â  Â  Â  Ball(i).x = 320+Rnd*250
  Â  Â  Â  Ball(i).y = 240+Rnd*150
  Â  Â  Â  Ball(i).dx = 1+Rnd*10
  Â  Â  Â  Ball(i).dy = 1+Rnd*10
  Â  Â  Â  If Int(Rnd*10) = 5 Then
  Â  Â  Â  Â  Â  Ball(i).dx = -Ball(i).dx
  Â  Â  Â  Â  Â  If Int(Rnd*2) = 1 Then Ball(i).dy = -Ball(i).dy
  Â  Â  Â  End If
  Â  Â  Â  
  Â  Â  Â  Ball(i).Rad = 64+Rnd*64
  Â  Â  Â  
  Â  Â  Â  Ball(i).R = 64+Int(Rnd*255)
  Â  Â  Â  Ball(i).G = 64+Int(Rnd*255)
  Â  Â  Â  Ball(i).B = 64+Int(Rnd*255)
  Â  Next
  Â  
End Sub


Sub Set_Ortho( Display As DisplayMode )
  Â  GlMatrixMode( Gl_Projection )
  Â  GlPushMatrix
  Â  GlLoadIdentity
  Â  GlMatrixMode( Gl_Modelview )
  Â  GlPushMatrix
  Â  GlLoadIdentity
  Â  GlOrtho( 0, Display.W, 0, Display.H, -1, 1 )
End Sub


Sub Drop_Ortho( Display As DisplayMode )
  Â  GlMatrixMode( Gl_Projection )
  Â  GlPopMatrix
  Â  GlMatrixMode( Gl_Modelview )
  Â  GlPopMatrix
End Sub

Sub Init_GL_Window( Display As DisplayMode )
  Â  Display.W  Â  Â = 640
  Â  Display.H  Â  Â = 480
  Â  Display.R_BITS= 8
  Â  Display.G_BITS= 8
  Â  Display.B_BITS= 8
  Â  Display.A_BITS= 8
  Â  Display.D_BITS= 24
  Â  Display.S_BITS= 8
  Â  Display.MODE  = GLFW_WINDOW
  Â  
  Â  If glfwInit() Then
  Â  Â  Â  'Successful!
  Â  Else  Â  
  Â  Â  Â  Print "Failed to initialize GLFW!"
  Â  Â  Â  Sleep 1000
  Â  Â  Â  End
  Â  End If
  Â  
  Â  
  Â  If  glfwOpenWindow( _
  Â  Display.W  Â  Â , _
  Â  Display.H  Â  Â , _
  Â  Display.R_BITS, _
  Â  Display.G_BITS, _
  Â  Display.B_BITS, _
  Â  Display.A_BITS, _
  Â  Display.D_BITS, _
  Â  Display.S_BITS, _
  Â  Display.MODE  ) _
  Â  Then
  Â  GlfwSwapInterval 1
  Â  Display.GlVer = glGetString(GL_VERSION)
Else
  Â  GlfwTerminate()
  Â  End
End If
'To check, use... glfwGetWindowParam()


' OpenGL specific stuff...
glViewport 0, 0, Display.W, Display.H
glMatrixMode GL_PROJECTIONÂ  Â  Â  Â  
glLoadIdentity  Â  
Display.FOVy = 45.0
Display.Aspect = Display.W / Display.H
Display.znear = 1
Display.zfar = 1000
gluPerspective Display.FOVy, Display.Aspect, Display.zNear, Display.zFar

glShadeModel GL_SMOOTH
glClearColor 0.0, 0.0, 0.0, 0.0
glClearDepth 1.0
glEnable GL_DEPTH_TEST
glDepthFunc GL_LEQUALÂ  Â  
glEnable GL_COLOR_MATERIAL

glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
glPolygonMode GL_FRONT_AND_BACK, GL_POINT
glEnable GL_CULL_FACE
End Sub


Data 13
Data "void main(void)"
Data "{"
Data "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
Data "vec4 V = gl_ModelViewMatrix * gl_Vertex;"
Data "gl_FrontColor = gl_Color;"
Data "float ptSize = length(V);"
Data "ptSize *= ptSize;"
Data "gl_PointSize = (gl_Color * 10.0);"
Data "if (gl_PointSize || 0 )"
Data "{"
Data "gl_FrontColor = gl_Color * (gl_PointSize/64);"
Data "}"
Data "}"
« Last Edit: July 16, 2006 by Dr_D »
The Dr. is INsane!!!

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Here's a simple glsl demo.
« Reply #1 on: July 16, 2006 »
Thanks for posting the routine Dr_D  :) Unfortunately it stops compiling at line 10 for me, trying to kluge it makes the compilation fale at various declarations throughout the listing, when it get to the shader definitions I gave up. Shame because I have shaders on this card and I'd really love to see it.
Soo.. If anyone can get this to compile could they attach an exe of it to their post please? Thanks!
And by the way  :hi: to the board Dr_D, nice to see you here and I hope that you stick around.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: Here's a simple glsl demo.
« Reply #2 on: July 16, 2006 »
Yo doc!!!

I just took a screenie!!!!

BTW, this forum is kool cuz you can post screenies as attachment.
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Here's a simple glsl demo.
« Reply #3 on: July 16, 2006 »
Ah, thanks for posting the screen Rel, it looks excellent.
Could someone attach an exe so I can try running it please?
Thanks!  :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Here's a simple glsl demo.
« Reply #4 on: July 16, 2006 »
Definately need to try this when I get home, my parents gfx card doesnt do OGL too well.
Looks way smart from the screenie, and nice one DR D. :)
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: Here's a simple glsl demo.
« Reply #5 on: July 16, 2006 »
Thanks guys! ;D It overloads after a while though... I mean the balls will eventually start flying way off the screen because I didn't limit their movement. I'm working on another version that also uses a fragment shader, but it's obvious that I've still got alot to learn. :|| I'll make an exe of this one though. It should be very small.
The Dr. is INsane!!!

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Here's a simple glsl demo.
« Reply #6 on: July 16, 2006 »
Cool, you can just attach it to your post (as long as it is in a zip or rar file). Thanks :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: Here's a simple glsl demo.
« Reply #7 on: July 16, 2006 »
Ok. I attached a zip to the first post.  :cheers:
The Dr. is INsane!!!

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: Here's a simple glsl demo.
« Reply #8 on: July 17, 2006 »
Ok. I attached a zip to the first post.  :cheers:

Awesome ... very cool fx !
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Here's a simple glsl demo.
« Reply #9 on: July 17, 2006 »
very cool shader work mate!
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Here's a simple glsl demo.
« Reply #10 on: July 17, 2006 »
Thanks very much for attaching the exe, I managed to run it however it was odd. My card / drivers are obviously out of date so the program wouldnt run properly, I got a dos window with some errors, however I got a good idea of what it would look like moving because another window was opened with the blob effect running as just dots, looked cool like that, I'm just gutted I can't see it for real :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline zparticle

  • Atari ST
  • ***
  • Posts: 168
  • Karma: 11
    • View Profile
    • ScottShaver2000
Re: Here's a simple glsl demo.
« Reply #11 on: July 17, 2006 »
Vertex Shader Infolog error message:
ERROR: 0:8: 'assign' :  cannot convert from '4-component vector of float' to 'Po
intSize float'
ERROR: 0:9: '||' :  wrong operand types  no operation '||' exists that takes a l
eft-hand operand of type 'PointSize float' and a right operand of type 'const in
t' (or there is no acceptable conversion)
ERROR: 0:11: '/' :  wrong operand types  no operation '/' exists that takes a le
ft-hand operand of type 'PointSize float' and a right operand of type 'const int
' (or there is no acceptable conversion)
ERROR: 3 compilation errors.  No code generated.


GLSL program failed to compile!

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Here's a simple glsl demo.
« Reply #12 on: July 17, 2006 »
That's the same as I got. I still managed to get some metablobs constructed from dots displayed though.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: Here's a simple glsl demo.
« Reply #13 on: July 18, 2006 »
Dr_D attempts to make shader...

GFX card says... :whack:


hehehe, I told you I had alot to learn. I'm still reading. ;)
The Dr. is INsane!!!

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Here's a simple glsl demo.
« Reply #14 on: July 18, 2006 »
With the exe version everything looked pretty smart DR_D Dude :)
Perhaps it's a FB compiler issue.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Ghost^BHT

  • Clueless and Happy
  • ^GVY
  • Pentium
  • ******
  • Posts: 931
  • Karma: 49
  • BYTE ME!
    • View Profile
Re: Here's a simple glsl demo.
« Reply #15 on: July 18, 2006 »
Got the same errors as Relsoft and the movings dots - and I believe my GL shader libarary is up to date/

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Here's a simple glsl demo.
« Reply #16 on: July 18, 2006 »
It's not just my shader library then? :)
Please post any further updates Dr_D , it'd be nice to help you sort out this compatibility problem if we can.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: Here's a simple glsl demo.
« Reply #17 on: July 26, 2006 »
This is kind of a twist on the same thing... sort of. It's not real pretty, it just blurs a texture. It does show how to use a fragment shader. It also uses the point size extension from the other demo. ;)

Code: [Select]
Option Explicit
Randomize Timer
'$Static

'$INCLUDE: "\gl\gl.bi"
'$INCLUDE: "\gl\glu.bi"
'$INCLUDE: "\gl\glext.bi"
'$INCLUDE: "\gl\glfw.bi"

Const MAX_BALLS As Integer = 50
Const Depth As Single =Â  .503

Type DisplayMode
  Â  W As Uinteger
  Â  H As Uinteger
  Â  R_BITS As Uinteger
  Â  G_BITS As Uinteger
  Â  B_BITS As Uinteger
  Â  A_BITS As Uinteger
  Â  D_BITS As Uinteger
  Â  S_BITS As Uinteger
  Â  MODE As Uinteger
  Â  GlVer As Zstring Ptr
  Â  As Single FOVy, Aspect, zNear, zFar
End Type

Type Vector2D
  Â  As Single X,Y
End Type

Type Balls
  Â  As Single R, G, B
  Â  As Single x, Dx, y, Dy,z, Dz, Rad
End Type



Declare Sub Init_GL_Window( Display As DisplayMode )
Declare Sub Set_Ortho( Display As DisplayMode )
Declare Sub Drop_Ortho( Display As DisplayMode )
Declare Sub Init_Ball( Ball As Balls, Display As DisplayMode)

Dim Ball(1 To Max_Balls) As Balls
Dim Display As DisplayMode
Init_GL_Window Display

#Define Midx (Display.w \ 2)
#Define Midy (Display.h \ 2)


Dim Buffer As GLUINT, tData As Uinteger Ptr
glGenTextures 1, @Buffer
GlBindTexture GL_TEXTURE_2D, Buffer
glTexImage2D( GL_TEXTURE_2D, 0, 4, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0)
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST )
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST )


Dim glCreateShaderObjectARBÂ  Â As PFNglCreateShaderObjectARBPROCÂ  Â = Cptr(PFNGLCREATESHADEROBJECTARBPROC, glfwGetProcAddress( "glCreateShaderObjectARB" ))
Dim glShaderSourceARBÂ  Â  Â  Â  Â As PFNglShaderSourceARBPROCÂ  Â  Â  Â  Â = Cptr(PFNGLSHADERSOURCEARBPROC, glfwGetProcAddress( "glShaderSourceARB" ))
Dim glGetShaderSourceARBÂ  Â  Â  As PFNglGetShaderSourceARBPROCÂ  Â  Â  = Cptr(PFNGLGetSHADERSOURCEARBPROC, glfwGetProcAddress( "glGetShaderSourceARB" ))
Dim glCompileShaderARBÂ  Â  Â  Â  As PFNGLCompileShaderARBPROCÂ  Â  Â  Â  = Cptr(PFNglCompileShaderARBPROC, glfwGetProcAddress( "glCompileShaderARB" ))
Dim glDeleteObjectARBÂ  Â  Â  Â  Â As PFNGLDeleteObjectARBPROCÂ  Â  Â  Â  Â = Cptr(PFNglDeleteObjectARBPROC, glfwGetProcAddress( "glDeleteObjectARB" ))
Dim glCreateProgramObjectARBÂ  As PFNglCreateProgramObjectARBPROCÂ  = Cptr(PFNglCreateProgramObjectARBPROC, glfwGetProcAddress( "glCreateProgramObjectARB" ))
Dim glAttachObjectARBÂ  Â  Â  Â  Â As PFNglAttachObjectARBPROCÂ  Â  Â  Â  Â = Cptr(PFNglAttachObjectARBPROC, glfwGetProcAddress( "glAttachObjectARB" ))
Dim glUseProgramObjectARBÂ  Â  Â As PFNglUseProgramObjectARBPROCÂ  Â  Â = Cptr(PFNglUseProgramObjectARBPROC, glfwGetProcAddress( "glUseProgramObjectARB" ))
Dim glLinkProgramARBÂ  Â  Â  Â  Â  As PFNglLinkProgramARBPROCÂ  Â  Â  Â  Â  = Cptr(PFNglLinkProgramARBPROC, glfwGetProcAddress( "glLinkProgramARB" ))
Dim glValidateProgramARBÂ  Â  Â  As PFNglValidateProgramARBPROCÂ  Â  Â  = Cptr(PFNglValidateProgramARBPROC, glfwGetProcAddress( "glValidateProgramARB" ))
Dim glGetObjectParameterivARB As PFNglGetObjectParameterivARBPROC = Cptr(PFNglGetObjectParameterivARBPROC, glfwGetProcAddress( "glGetObjectParameterivARB" ))
Dim glGetInfoLogARBÂ  Â  Â  Â  Â  Â As PFNglGetInfoLogARBPROCÂ  Â  Â  Â  Â  Â = Cptr(PFNglGetInfoLogARBPROC, glfwGetProcAddress( "glGetInfoLogARB" ))

Dim glGetUniformLocationARB As PFNglGetUniformLocationARBPROC = Cptr(PFNglGetUniformLocationARBPROC, glfwGetProcAddress( "glGetUniformLocationARB" ))
Dim glUniform1iARB As PFNglUniform1iARBPROC = Cptr(PFNglUniform1iARBPROC, glfwGetProcAddress( "glUniform1iARB" ))
Dim glUniform2fvARB As PFNglUniform2fvARBPROC = Cptr(PFNglUniform2fvARBPROC, glfwGetProcAddress( "glUniform2fvARB" ))

If glfwExtensionSupported( "GL_ARB_shader_objects" ) = 0 Then
  Â  Print "Error: ARB shader objects extension not supported."
  Â  Sleep 1000
  Â  End -1
End If

If( glCreateShaderObjectARB = 0 ) Then
  Â  Print "Error: glCreateShaderObjectARB not present."
  Â  Sleep 1000
  Â  End -1
End If

If( glCreateProgramObjectARB = 0 ) Then
  Â  Print "Error: glCreateProgramObjectARB not present."
  Â  Sleep 1000
  Â  End -1
End If

If( glShaderSourceARB = 0 ) Then
  Â  Print "Error: glShaderSourceARB not present."
  Â  Sleep 1000
  Â  End -1
End If



Dim As Integer Line_Cnt
Dim As String Shader_Text, tString
Dim As Integer i, x, y, Scale = 8
Dim As Integer  Dist
Dim As GlHandleARB Vertex_Shader, Fragment_Shader, Vertex_Program, Fragment_Program
Dim As Gluint Shader_Compile_Success

Restore Vertex_Shader_Data
Read Line_Cnt
For i = 1 To Line_Cnt
  Â  Read tString
  Â  Shader_Text + = tString + Chr( 13, 10 )
Next
Print "Vertex Shader" + + Chr( 13, 10 )
Print Shader_Text

Dim As GLcharARB Ptr table(0) => { Strptr( Shader_Text ) }
Vertex_Shader = glCreateShaderObjectARB( GL_VERTEX_SHADER )
glShaderSourceARB( Vertex_Shader, 1, @table(0), 0 )
glCompileShaderARB( Vertex_Shader )

glGetObjectParameterivARB(Vertex_Shader, GL_OBJECT_COMPILE_STATUS_ARB, @Shader_Compile_Success )
If Shader_Compile_Success = 0 Then
  Â  Dim As Gluint infologsize
  Â  glGetObjectParameterivARB( Vertex_Shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, @infoLogSize)
  Â  Dim As GlByte infolog(InfoLogSize)
  Â  glGetInfoLogARB(Vertex_Shader, InfoLogSize, 0, @infoLog(0))
  Â  tString=""
  Â  For i = 0 To InfoLogSize-1
  Â  Â  Â  tString+=Chr(InfoLog(i))
  Â  Next
  Â  
  Â  Print "Vertex Shader Infolog error message :("
  Â  Print tString
End If


Restore Fragment_Shader_Data
Shader_Text = ""
tString=""
Read Line_Cnt
For i = 1 To Line_Cnt
  Â  Read tString
  Â  Shader_Text += tString + Chr( 13, 10 )
Next
Print "Fragment Shader"Â  + Chr( 13, 10 )
Print Shader_Text

Dim As GLcharARB Ptr Frag_table(0) => { Strptr( Shader_Text ) }
Fragment_Shader = glCreateShaderObjectARB( GL_FRAGMENT_SHADER )
glShaderSourceARB( Fragment_Shader, 1, @Frag_table(0), 0 )
glCompileShaderARB( Fragment_Shader )


glGetObjectParameterivARB(Fragment_Shader, GL_OBJECT_COMPILE_STATUS_ARB, @Shader_Compile_Success )
If Shader_Compile_Success = 0 Then
  Â  Dim As Gluint infologsize
  Â  glGetObjectParameterivARB( Fragment_Shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, @infoLogSize)
  Â  Dim As GlByte infolog(InfoLogSize)
  Â  glGetInfoLogARB(Fragment_Shader, InfoLogSize, 0, @infoLog(0))
  Â  tString=""
  Â  For i = 0 To InfoLogSize-1
  Â  Â  Â  tString+=Chr(InfoLog(i))
  Â  Next
  Â  
  Â  Print "Fragment Shader Infolog error message :("
  Â  Print tString
End If


Vertex_Program = GlCreateProgramObjectARB()
glAttachObjectARB( Vertex_Program, Vertex_Shader )
glLinkProgramARB( Vertex_Program )

Fragment_Program = GlCreateProgramObjectARB()
glAttachObjectARB( Fragment_Program, Fragment_Shader )
glLinkProgramARB( Fragment_Program )


GlValidateProgramARB( Vertex_Program )
glGetObjectParameterivARB( Vertex_Program, GL_OBJECT_VALIDATE_STATUS_ARB, @Shader_Compile_Success )
If Shader_Compile_Success = 0 Then
  Â  Beep
  Â  Print "ARGHHHH!!!! GLSL program failed to compile. :("
  Â  Sleep 1000
End If

GlValidateProgramARB( Fragment_Program )
glGetObjectParameterivARB( Fragment_Program, GL_OBJECT_VALIDATE_STATUS_ARB, @Shader_Compile_Success )
If Shader_Compile_Success = 0 Then
  Â  Beep
  Â  Print "ARGHHHH!!!! GLSL program failed to compile. :("
  Â  Sleep 1000
End If



Dim As GLINT Sampler0Loc = glGetUniformLocationARB( Fragment_Program, Strptr("sampler0") )
Dim As GLINT tc_offsetLoc = glGetUniformLocationARB( Fragment_Program, Strptr("tc_offset") )


Dim As Vector2D tc_offset(8)

i=0
For y = -1 To 1
  Â  For x = -1 To 1
  Â  Â  Â  tc_offset(i).x = x/Depth
  Â  Â  Â  tc_offset(i).y = y/Depth
  Â  Â  Â  i+=1
  Â  Next
  Â  
Next
GLEnable GL_VERTEX_PROGRAM_POINT_SIZE_ARB

Dim Sphere As GLUquadricObj Ptr
Sphere = gluNewQuadric
gluQuadricDrawStyle Sphere, GLU_FILL
gluQuadricNormals Sphere, GLU_SMOOTH
gluQuadricOrientation Sphere, GLU_OUTSIDE

Dim As Single LightPos(2) => {0, 0, 100}

GlEnable GL_LIGHTING
GlEnable GL_Light0
glLightfv GL_LIGHT0, GL_POSITION, @LightPos(0)




For i = 1 To Max_Balls
  Â  Init_Ball( Ball(i), Display )
  Â  Ball(i).y = -i
Next



Do
  Â  
  Â  
  Â  glUseProgramObjectARB( Vertex_Program )
  Â  GlViewPort ( 0, 0, 512, 512 )
  Â  
  Â  GlClear( GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT)
  Â  GlMatrixMode GL_Modelview
  Â  GlLoadIdentity
  Â  GlEnable GL_LIGHTING
  Â  glLightfv GL_LIGHT0, GL_POSITION, @LightPos(0)
  Â  GluLookat (0,0,100, 0,0,-1, 0,1,0)
  Â  glPolygonMode GL_FRONT, GL_POINT'FILL
  Â  For i = 1 To Max_Balls  Â  Â  Â  
  Â  Â  Â  Ball(i).x += Ball(i).dx
  Â  Â  Â  Ball(i).y += Ball(i).dy
  Â  Â  Â  Ball(i).z += Ball(i).dz
  Â  Â  Â  
  Â  Â  Â  Ball(i).dy-=.1
  Â  Â  Â  
  Â  Â  Â  If Ball(i).y<-100 Then
  Â  Â  Â  Â  Â  Init_Ball( Ball(i), Display )
  Â  Â  Â  End If

  Â  Â  Â  GlPushMatrix
  Â  Â  Â  GlTranslateF Ball(i).X, Ball(i).Y, Ball(i).Z
  Â  Â  Â  GlRotateF Ball(i).y*50, 1, 0, 0
  Â  Â  Â  GlColor4F Ball(i).R, Ball(i).G, Ball(i).B, 1
  Â  Â  Â  gluSphere Sphere, Ball(i).Rad, 16, 16
  Â  Â  Â  GlPopMatrix
  Â  Next
  Â  
  Â  GlEnable GL_TEXTURE_2D
  Â  glBindTexture(GL_TEXTURE_2D, Buffer)
  Â  glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 512, 512)
  Â  
  Â  GlClear GL_COLOR_BUFFER_BIT
  Â  glUseProgramObjectARB( Fragment_Program )
  Â  'glUseProgramObjectARB( 0 )
  Â  glUniform1iARB(Sampler0Loc, 0 )
  Â  glUniform2fvARB(tc_offsetLoc, 9, @tc_offset(0).X)
  Â  GlViewPort ( 0, 0, Display.W, Display.H )
  Â  glPolygonMode GL_FRONT, GL_FILL
  Â  Set_Ortho( Display )
  Â  glBindTexture(GL_TEXTURE_2D, Buffer)  Â  
  Â  GlDisable GL_LIGHTING
  Â  GlColor3F 1,1,1
  Â  GlBegin GL_QUADS
  Â  GlTexCoord2D 1,0
  Â  GlVertex3F Display.W,0,0
  Â  
  Â  GlTexCoord2D 1,1
  Â  GlVertex3F Display.W,Display.H,0
  Â  
  Â  GlTexCoord2D 0,1
  Â  GlVertex3F 0,Display.H,0
  Â  
  Â  GlTexCoord2D 0,0
  Â  GlVertex3F 0,0,0
  Â  GlEnd
  Â  
  Â  Drop_Ortho( Display )
  Â  GlDisable GL_TEXTURE_2D
  Â  
  Â  GlfwSwapBuffers
Loop Until glfwGetKey( GLFW_KEY_ESC )

GluDeleteQuadric Sphere
GlDeleteObjectArb( Vertex_Shader )
GlDeleteObjectArb( Fragment_Shader )
GlDeleteObjectArb( Vertex_Program )
GlDeleteObjectArb( Fragment_Program )
GlfwTerminate()
End


Sub Init_Ball( Ball As Balls, Display As DisplayMode)
  Â  
  Â  Â  Â  Ball.x = -5+Rnd*10
  Â  Â  Â  Ball.y = -25'+Rnd*30
  Â  Â  Â  Ball.z = -50+(RND*20)
  Â  Â  Â  
  Â  Â  Â  Ball.dx = Ball.x/10
  Â  Â  Â  Ball.dy = 2+Rnd*2
  Â  Â  Â  Ball.dz = -Ball.z/50
  Â  Â  Â  
  Â  Â  Â  Ball.Rad = 10'5+Rnd*10

  Â  Â  Â  Ball.R = .5+Rnd
  Â  Â  Â  Ball.G = .5+Rnd
  Â  Â  Â  Ball.B = .5+Rnd
End Sub


Sub Set_Ortho( Display As DisplayMode )
  Â  GlMatrixMode( Gl_Projection )
  Â  GlPushMatrix
  Â  GlLoadIdentity
  Â  GlMatrixMode( Gl_Modelview )
  Â  GlPushMatrix
  Â  GlLoadIdentity
  Â  GlOrtho( 0, Display.W, 0, Display.H, -1, 1 )
End Sub


Sub Drop_Ortho( Display As DisplayMode )
  Â  GlMatrixMode( Gl_Projection )
  Â  GlPopMatrix
  Â  GlMatrixMode( Gl_Modelview )
  Â  GlPopMatrix
End Sub

Sub Init_GL_Window( Display As DisplayMode )
  Â  Display.W  Â  Â = 800
  Â  Display.H  Â  Â = 600
  Â  Display.R_BITS= 8
  Â  Display.G_BITS= 8
  Â  Display.B_BITS= 8
  Â  Display.A_BITS= 8
  Â  Display.D_BITS= 24
  Â  Display.S_BITS= 8
  Â  Display.MODE  = GLFW_WINDOW
  Â  
  Â  If glfwInit() Then
  Â  Â  Â  'Successful!
  Â  Else  Â  
  Â  Â  Â  Print "Failed to initialize GLFW!"
  Â  Â  Â  Sleep 1000
  Â  Â  Â  End
  Â  End If
  Â  
  Â  
  Â  If  glfwOpenWindow( _
  Â  Display.W  Â  Â , _
  Â  Display.H  Â  Â , _
  Â  Display.R_BITS, _
  Â  Display.G_BITS, _
  Â  Display.B_BITS, _
  Â  Display.A_BITS, _
  Â  Display.D_BITS, _
  Â  Display.S_BITS, _
  Â  Display.MODE  ) _
  Â  Then
  Â  GlfwSwapInterval 1
  Â  Display.GlVer = glGetString(GL_VERSION)
Else
  Â  GlfwTerminate()
  Â  End
End If
'To check, use... glfwGetWindowParam()

' OpenGL specific stuff...
glViewport 0, 0, Display.W, Display.H
glMatrixMode GL_PROJECTIONÂ  Â  Â  Â  
glLoadIdentity  Â  
Display.FOVy = 45.0
Display.Aspect = Display.W / Display.H
Display.znear = 1
Display.zfar = 1000
gluPerspective Display.FOVy, Display.Aspect, Display.zNear, Display.zFar

glShadeModel GL_SMOOTH
glClearColor 0.0, 0.0, 0.0, 0.0
glClearDepth 1.0
glEnable GL_DEPTH_TEST
glDepthFunc GL_LEQUAL
glEnable GL_COLOR_MATERIAL

glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
glPolygonMode GL_FRONT, GL_POINT
glEnable GL_CULL_FACE
End Sub


Vertex_Shader_Data:
Data 10
Data "void main(void)"
Data "{"
Data "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
Data "vec4 V = gl_ModelViewMatrix * gl_Vertex;"
Data "gl_FrontColor = gl_Color;"
Data "float ptSize = length(V);"
Data "ptSize *= ptSize;"
Data "gl_PointSize =Â  (ptSize/1000.0);"
Data "gl_FrontColor = gl_FrontColor - (gl_PointSize / 16.0);"
Data "}"

Fragment_Shader_Data:
Data 11
Data "uniform sampler2D sampler0;"
Data "uniform vec2 tc_offset[9];"
Data "void main(void)"
Data "{"
Data "vec4 sample[9];"
Data "for (int i = 0; i < 9; i++)"
Data "{"
Data "sample[i]Â  = texture2D(sampler0, gl_TexCoord[0].st + tc_offset[i]);"
Data "}"
Data "gl_FragColor  = (sample[0] + sample[1] + sample[2] + sample[3] + sample[4] + sample[5] + sample[6] + sample[7] + sample[8])/16.0;"
Data "}"
The Dr. is INsane!!!

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Here's a simple glsl demo.
« Reply #18 on: July 26, 2006 »
Again I'm unable to run, everyone else will have to tell me how cool this is !
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Here's a simple glsl demo.
« Reply #19 on: July 26, 2006 »
I can't get it to accept the gl commands proceeding @numbers.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won: