OpenGL shader examples

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

OpenGL shader examples

Post by pestery »

Hi all. I've made a couple of OpenGL shader examples. One is a simple triangle on the screen example. The other is 3D and uses a maths library I've been building (also included). I've put comments and labels everywhere to try and explain everything. I don't think I've used any depreciated OpenGL functions (I tried not too).

One thing I should mention is that the examples required OpenGL 2.0 or above. I know some on the forum are running older hardware so its worth mentioning.

Anyway, here's the download link, http://www.mediafire.com/file/wlrwrro93 ... Shader.zip

Also, this is a webpage that I found very useful. It has a really good explanation of everything shaders, and its also up to date. When I was searching for shader info and examples most of what I could find was useful, but out of data (used depreciated function, etc). http://duriansoftware.com/joe/An-intro- ... tents.html
Last edited by pestery on Nov 07, 2011 11:26, edited 4 times in total.
Aave
Posts: 128
Joined: Jun 13, 2008 19:55
Location: Helsinki, Finland

Post by Aave »

Great to see some newer OpenGL examples. Extra points for using VBOs and going beyond the old OpenGL 2.0 and using GLSL from OpenGL 3.1 (note for people with 2.0 only: see the readme).

EDIT: Examples run fine on Linux, but I needed to add a definition of NULL to shader.bi:

Code: Select all

#Ifndef NULL
#Define NULL 0
#EndIf
podvornyak
Posts: 148
Joined: Nov 12, 2007 11:46
Location: Russia

Post by podvornyak »

Damn good Pestery!
Aave wrote: EDIT: Examples run fine on Linux, but I needed to add a definition of NULL to shader.bi:
or incode constant

Code: Select all

const NULL = 0
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Post by pestery »

Glad you like it :-)

I've updated the original link. I've added the #define NULL as suggested by Aave. I've also slightly updated the maths.bas file, and added a my standard image loader thingy.

And, test3 now exists! Uses textures and blending. Enjoy.
joseywales72
Posts: 206
Joined: Aug 27, 2005 2:02
Location: Istanbul, Turkey

Post by joseywales72 »

Thanks for the examples. They are very useful.
There is something wrong with test3 though. It gives this when I try to compile.

Code: Select all

/usr/include/freebasic/crt/sys/linux/types.bi(96) error 4: Duplicated definition in 'type int64_t as integer'
I'm on Archlinux. The other examples compile and run fine.
Thanks again.
Anil
podvornyak
Posts: 148
Joined: Nov 12, 2007 11:46
Location: Russia

Post by podvornyak »

At ubuntu 10.04 64 with 0.22 work fine.
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Post by pestery »

Hi Anil. I've fixed the bug so it should work now. The bug was actually in my OpenGL headers, which I guess your using :-)

I've updated the download link in the other post. Thanks for letting me know.
http://www.freebasic.net/forum/viewtopi ... highlight=
joseywales72
Posts: 206
Joined: Aug 27, 2005 2:02
Location: Istanbul, Turkey

Post by joseywales72 »

I downloaded and set up your updated headers and everything is ok now. Thank you for the fix and most importantly for the headers themselves.
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Post by pestery »

Hi all. Back again with another update of the examples.

I noticed a bug in my maths library that I'd bundled with the shader examples. I've tested this new version a lot and I'm pretty sure its bug free. Its also a lot longer because I've added a heap on new datatypes, functions and operators.

If anyone is using the bundled API files for FbEdit then I've updated those too. I added some stuff for framebuffer object functions. Also made a couple of small changes to the existing examples to make them automatically select the shader files.

Lastly, I've added a forth example which has a nice glow effect. The example also uses framebuffer objects as well, though because they are only available in GL 3.0 there are also fallback commands in the example. I've tested it and it runs fine on both GL 2.1 and GL 4.2.

Enjoy :-)
Aave
Posts: 128
Joined: Jun 13, 2008 19:55
Location: Helsinki, Finland

Post by Aave »

I get compile error form all tests:

Code: Select all

OpenGL_Shader/inc/shader.bi(282) error 41: Variable not declared, GL_MAX_COLOR_ATTACHMENTS in 'glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, @opengl_info.fbo_attach)'
If I hard code that into an arbitrary value, I can build and run tests 1-3. However test 4 gives:

Code: Select all

test4.bas(496) error 41: Variable not declared, GL_RENDERBUFFER in 'glBindRenderbuffer(GL_RENDERBUFFER, .fbo_depth)'
test4.bas(502) error 41: Variable not declared, GL_FRAMEBUFFER in 'glBindFramebuffer(GL_FRAMEBUFFER, .fbo)'
test4.bas(506) error 41: Variable not declared, GL_COLOR_ATTACHMENT0 in 'glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, .texture[i], 0)'
test4.bas(510) error 41: Variable not declared, GL_DEPTH_ATTACHMENT in 'glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, .fbo_depth)'
test4.bas(514) error 41: Variable not declared, GL_FRAMEBUFFER_COMPLETE in 'If check <> GL_FRAMEBUFFER_COMPLETE Then'
test4.bas(598) error 41: Variable not declared, GL_COLOR_ATTACHMENT1 in 'Dim As GLenum drawbuf(0 To 1) = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1}'
test4.bas(696) error 41: Variable not declared, GL_COLOR_ATTACHMENT2 in 'glDrawBuffer GL_COLOR_ATTACHMENT2'
One other thing, it would be nice if you could name the bas and dir differently, e.g. test1.bas and test1-data. This is because on linux, exes don't generally have any suffix (.exe), so if I command: fbc test1.bas, it won't work as it tries to create and exe named 'test1' but there's already a directory by that name. Of course I can work around this with fbc test1.bas -x test1exe, but it's slightly annoying.
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Post by pestery »

Thanks for letting me know Aave. I've fixed up the folder names to make it easier on Linux. I've also added a bit to make sure those constants get defined if they are not present. I think I got them all :-)

Regarding the constants not being defined, you might want to get the updated version of glext.bi that I did a while ago. Using it should stop any other problems with things not being defined. http://www.freebasic.net/forum/viewtopi ... highlight=
Aave
Posts: 128
Joined: Jun 13, 2008 19:55
Location: Helsinki, Finland

Post by Aave »

pestery wrote:I've also added a bit to make sure those constants get defined if they are not present. I think I got them all :-)
Not quite :P

Code: Select all

test4.bas(510) error 41: Variable not declared, GL_DEPTH_ATTACHMENT in 'glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, .fbo_depth)'
test4.bas(514) error 41: Variable not declared, GL_FRAMEBUFFER_COMPLETE in 'If check <> GL_FRAMEBUFFER_COMPLETE Then'
However, I did as you suggested and downloaded updated gl headers and was able to compile it.
Post Reply