Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: neumanix on April 13, 2007
-
Hi everyone! I've been playing around with some OpenGL and found it quite annoying to have to use Screen or Screenres to open a screen that OpenGL can use.
I guess it loads the whole gfxlib when you just need one function. The resulting .exe gets bloated, although comresses to around 50K or so, but that's still a lot if you want to do a 64K intro or something.
So... I searched around a bit and suddenly found someone here using GLFW. I searched the forum and didn't find any discussion about GLFW so I decided to give this tip... might be
useful for some of you.
GLFW is a framework that has alot of neat features, just visit http://glfw.sourceforge.net/ and read all about it.
The incude file and lib should already be in your FREEBASIC folders.
A little example:
#include "gl/glfw.bi" ' also includes "gl.bi" and "glu.bi" automatically
glfwinit()
if glfwopenwindow(640,480,8,8,8,8,32,8,glfw_fullscreen)=0 then ' glfw_window for windowed mode
glfwterminate()
end -1
endif
' normal OpenGL setup follow here
while not glfwgetkey(glfw_key_esc) and glfwGetWindowParam(GLFW_OPENED)
' do your stuff
glfwswapbuffers() ' use instead of flip
wend
glfwterminate()
end
Using this results in a much smaller .exe. I did a simple program with a few spinning objects and it compiled to 32K and compressed to 13K using kkrunchy!
I hope this is good news for someone :)
-
...
Using this results in a much smaller .exe. I did a simple program with a few spinning objects
and it compiled to 32K and compressed to 13K using kkrunchy!
...
Would be interesting to see your the spinning objects in motion. Would you mind sharing the exe
with us! And thanks for pointing out to the GLFW. Had a quick look at it before - but totally for-
get about it!
-
Sure, here is the source as well. Just found out about the glfwGetMousePos function, so I implemented that as well :)
For some reason it now compiled even smaller, 25K! And compressed to 10K!!!
Btw, never mind the corny swedish comments in the source... if you're not swedish ofcourse :)
#include "gl/glfw.bi"
const gfxw = 640
const gfxh = 480
declare sub cube()
declare sub plane()
dim as integer sphere_rot, mx, my
'---------------------------------------------------------------
'Här följer init-koden. No more gfxlib!!!
'---------------------------------------------------------------
glfwinit()
if glfwopenwindow(gfxw,gfxh,8,8,8,8,24,8,glfw_fullscreen)=0 then
glfwterminate()
end -1
endif
glviewport 0,0,gfxw,gfxh ' så att GL och skärmen stämmer överens
glmatrixmode gl_projection ' välj projection matrix
glloadidentity ' och nollställ den
gluperspective 45.0 , gfxw/gfxh , 0.1 , 100.0 ' nice perspective (gfxw/gfxh+.2 för full widescreen)
glmatrixmode gl_modelview ' välj modelview matrix
glloadidentity ' och nollställ den också
glshademodel gl_smooth ' sätt på smooth shading
glenable gl_depth_test ' på med depth testing
gldepthfunc gl_lequal
glcleardepth 1.0
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ' ännu finare perspective
glenable gl_cull_face
'--------------------------------------------------------
'---------LIGHTING SETUP---------------------------------
'--------------------------------------------------------
dim ambient_light(3) as single = {0.5 , 0.7 , 1.0 , 1.0}
dim diffuse_light(3) as single = {1.0 , 1.0 , 1.0 , 1.0}
dim light1_pos(3) as single = {0.0 , 0.0 , 5.0 , 1.0}
dim sphere_light_diffuse(3) as single = {1.0 , 0.0 , 0.0 , 1.0}
gllightfv gl_light1, gl_ambient, @ambient_light(0)
gllightfv gl_light1, gl_position, @light1_pos(0)
gllightfv gl_light1, gl_diffuse, @diffuse_light(0)
glLightf gl_light1, gl_constant_attenuation, 1.0
glenable gl_lighting
glenable gl_light1
glenable gl_blend
glBlendFunc gl_src_color , gl_one
glenable gl_color_material ' color tracking på
glcolormaterial gl_front , gl_ambient_and_diffuse
'---------------------------------------------------------------------------
'----------DET VAR INITIALISERINGEN, NEDAN FÖLJER HUVUDLOOPEN---------------
'---------------------------------------------------------------------------
while not glfwgetkey(glfw_key_esc) and glfwGetWindowParam(GLFW_OPENED)
glclear gl_color_buffer_bit or gl_depth_buffer_bit ' CLS
glloadidentity
glfwgetmousepos(@mx,@my)
glcolor3f 0.5 , 0.5 , 0.5
plane()
gltranslatef -0.5,0,-5
glrotatef mx,0,1,0
glrotatef my,1,0,0
glcolor3f 0.5 , 0.5 , 1.0
cube()
glloadidentity
gltranslatef 2.0 , 0 , -5
glrotatef sphere_rot,0,1,1
glcolor3f 1.0 , 0.5 , 0.5
glusphere glunewquadric, 0.5 , 3 , 16
glflush
sphere_rot=sphere_rot+1
if sphere_rot>360 then sphere_rot=sphere_rot-360
'flip
glfwswapbuffers()
wend
glfwterminate()
end
'---------------------------------------------
' för front facing quads, sätt vertices "motsols", alltså counter-clockwise
' föreställ att man står framför quaden i 3d space
'-------------------------------------------------
sub cube()
glbegin gl_quads
'top
glnormal3f 0.0 , 1.0 , 0.0
glvertex3f -1.0 , 1.0 , 1.0
glvertex3f 1.0 , 1.0 , 1.0
glvertex3f 1.0 , 1.0 , -1.0
glvertex3f -1.0 , 1.0 , -1.0
'bottom
glnormal3f 0.0 , -1.0 , 0.0
glvertex3f -1.0 , -1.0 , -1.0
glvertex3f 1.0 , -1.0 , -1.0
glvertex3f 1.0 , -1.0 , 1.0
glvertex3f -1.0 , -1.0 , 1.0
' left
glnormal3f -1.0 , 0.0 , 0.0
glvertex3f -1.0 , 1.0 , 1.0
glvertex3f -1.0 , 1.0 , -1.0
glvertex3f -1.0 , -1.0 , -1.0
glvertex3f -1.0 , -1.0 , 1.0
' right
glnormal3f 1.0 , 0.0 , 0.0
glvertex3f 1.0 , 1.0 , 1.0
glvertex3f 1.0 , -1.0 , 1.0
glvertex3f 1.0 , -1.0 , -1.0
glvertex3f 1.0 , 1.0 , -1.0
' front
glnormal3f 0.0 , 0.0 , 1.0
glvertex3f -1.0 , 1.0 , 1.0
glvertex3f -1.0 , -1.0 , 1.0
glvertex3f 1.0 , -1.0 , 1.0
glvertex3f 1.0 , 1.0 , 1.0
' back
glnormal3f 0.0 , 0.0 , -1.0
glvertex3f 1.0 , 1.0 , -1.0
glvertex3f 1.0 , -1.0 , -1.0
glvertex3f -1.0 , -1.0 , -1.0
glvertex3f -1.0 , 1.0 , -1.0
glend
end sub
sub plane()
glbegin gl_polygon
glvertex3f 1.0 , -5.0 , 0.0
glvertex3f 30.0 , -5.0 , -50.0
glvertex3f -30.0 , -5.0 , -50.0
glvertex3f -1.0 , -5.0 , 0.0
glend
end sub
-
Neat going Neumanix, and thanks for posting it, and I'm sure it's going to be very handy for people.
Cheers and nice one,
Clyde.
-
Thanks Clyde! Yes, I hope it will be useful. Although I'm quite new to both OpenGL and Freebasic, I can see some potential in GLFW.
I know that producing small exes is important for many, and I find it challenging too, although being a rookie :)
It will sure take a while before I can produce something intro-like, but I'll try to learn as much as possible.
More about GLFW... it supports keyboard, mouse and joystick input, high precision timers, opengl extensions, image/texture loading (although only .tga) and even
some fancy stuff like multithreading. There are functions for setting refresh rate and enabling/disabling Vsync and lots more.
There are some good user and reference guides in .pdf format on the website. It is C syntax but that's quite easy to translate to FB.
Gotta go play with this some more now...
-
Thanks for adding the exe. Looks very nice. Guess I will have a look on GLFW
again. K++ !!!
-
Nice one the roundish thing spun really fast so I added these lines in the while loop
Start = Timer 'starta en timer
Do 'vänta 10 millisec eller en hundradels sekund
Loop Until (Timer - Start) > 0.01
I'm still at a very basic level in freebasic :(
Edit: kul med svenska komentarer :D
-
Paul, it seems that you have Vsync set to "always off" in the gfx driver. That's not such a good idea. If you set it to "default on" or "application preferable" or somethink like that and add the following to the code:
glfwopenwindowhint(glfw_refresh_rate, 60) ' 60Hz refresh !!! OBS, försiktigt här så man inte pajar skärmen !!!
if glfwopenwindow(gfxw,gfxh,0,0,0,0,0,0,glfw_fullscreen)=0 then ' nollorna = låt GL bestämma rgb,alpha,depth,static bits. Annars 8,8,8,8,24,8
glfwterminate()
end -1
endif
glfwswapinterval(1) ' WAIT FOR VERTICAL RETRACE!!!
The first line set's the refresh rate, it's optional but use if you want to set an exact rate
The last line turns Vsync on.
Let me know if this works for you.
This should work with the driver set to anything BUT "always off"
p.s. jag är finlandssvensk, bor i Finland, snackar svenska :)
-
Yep, I agree with neumanix, for some reason some cards have v sync turned off only on specific resolutions too. It's best to leave it on and have the application control it.
-
Thx for trying to help me with the above example but im to thick to get it working :(
I can't find any vsync setting anywhere in my catalyst control cenyter.