Compiling in FB with OpenGL

The forum for all of your Freebasic needs!

Moderators: Pete, Mods

Post Reply
User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Compiling in FB with OpenGL

Post by Codemss » Wed Dec 26, 2007 6:07 pm

I tried to compile Rel's code that he included with a tut on opengl in freebasic in QBE issue #7(<a href="http://www.petesqbsite.com/sections/exp ... l">Link</a>), but it doesn't work when I compile it. It gives me tons of errors. One of them is that 'SDL_quit' is not recognized. Maybe the SDL or OpenGL lib isn't properly loaded? I just use -lang qb and -s gui in the command line(also tried without them so that shouldnt be the problem, but do I need to add something?) I downloaded SDL.dll and OpenGL.dll already.

Greets,

Codemss.
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL

User avatar
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt » Wed Dec 26, 2007 10:01 pm

Note that it's highly unlikely that you would even need to download opengl.dll. Video cards come with opengl drivers already...that is, if your card was made within the last 10 years or so. :D

Don't forget that FB has gone through a ton of changes since that issue. A TON. Most things broke between 0.15 and 0.16. I don't know what version was being used at the time of that QBE issue but it's highly likely that you'd need that version to recompile it.

User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Post by Codemss » Thu Dec 27, 2007 6:44 am

Yes, you're right, but it seems like the compiler doesn't even load the SDL lib, because it doesn't even recognize 'SDL_quit'. That is really weird, because including the libs is just:

Code: Select all

'$include: "\sdl\sdl.bi"                'sdl function and constants declaration
'$include: "\gl\gl.bi"                  'OpenGL funks and consts
'$include: "\gl\glu.bi"                 'GL standard utility lib
Right? And further, nearly all the code is SDL or OpenGL. And that code gives errors too. So, I think the libs arent loaded correctly. Does anyone how to do that? Or, does anyone have experience with OpenGL in FB 0.18 (latest version)?

EDIT:
The compiling does work with -lang deprecated in the compiler line. FB indeed does not recognize '$include. How do you include files with -lang FB? Is there a difference between:
fbc prog.bas
and:
fbc prog.bas -lang FB
?

Codemss
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL

MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows » Thu Dec 27, 2007 7:16 am

just change those include to read as follows

Code: Select all

#include "\sdl\sdl.bi"                'sdl function and constants declaration
#include "\gl\gl.bi"                  'OpenGL funks and consts
#include "\gl\glu.bi"                 'GL standard utility lib

When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com

User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Post by Codemss » Thu Dec 27, 2007 9:26 am

Thanks! Sorry for being a FB n00B :P.
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL

MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows » Thu Dec 27, 2007 11:40 am

hehe it's no problem :-). If you don't ask you won't know ;-). Now, you know. hehe.

Got more questions don't hesitate ok?
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com

User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat » Fri Dec 28, 2007 10:11 am

#include once "GL/gl.bi"
#include once "GL/glu.bi"

That's what I do. I just learned how to use OpenGL.
For any grievances posted above, I blame whoever is in charge . . .

User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Post by Codemss » Sun Dec 30, 2007 7:00 am

Nice that there is a FB subforum. Hehe, you will get flooded by my questions about FB! Burger2227, hide yourself. hehe :P.

But, to respond on your post, Mentat:
Probably both ways are possible. Where do you learn OpenGL from?
I think OpenGL is a bit complicated, but it gives many possibilities and is very fast. What do you think?
Greets,

Codemss
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL

User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat » Sun Dec 30, 2007 10:25 am

At first I thought OpenGL was too hard. But I opened up lesson 5 of the NeHe tutorials, read it, copied and pasted the first big chunk of code onto a new source for me.

Code: Select all

#include once "GL/gl.bi"
#include once "GL/glu.bi"
   
screen 18,16,,2                                   'FB screen           

glViewport 0,0,640,480                        'screen mode
glMatrixMode GL_PROJECTION              'projection model
glLoadIdentity                                     'resets the matrix
gluPerspective 45.0, 640.0/480.0, 0.1, 100.0
glMatrixMode GL_MODELVIEW              'viewing model
glLoadIdentity                                     'resets viewing matrix

glClearColor 0.0,0.0,0.0,0.5              'background color
glClearDepth 1.0                              'Clears depth, for z axis
glEnable GL_DEPTH_TEST                 ' Enables Depth Testing
glDepthFunc GL_LEQUAL                   ' The Type Of Depth Testing To Do
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST
At first I had no clue what it did. All I knew is that it worked well. After repeated copying and pasting and reading, I figured out what each line does.

Next is the DO loop for whatever you want to draw. the first thing to write down is FLIP!!!!!!!!!!! It will be right before LOOP WHILE. If you don't, your system will crash and you'll have to reboot. I forgot 3 times, and each time my screen had the resolution of 40*40, sometimes went blank, and I had to reboot manually. There won't be any permanrnt damage, just an anoying inconvenience. Remember FLIP right before you loop around.

And in the begining of the loop is glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT and then glLoadIdentity. The last thing resets your screen (sort of like cls, but it does much more, such as keeping everything from running away).

Now the looping code looks like

Code: Select all

do
    glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
    glLoadIdentity


    flip
loop until multikey(1)      'escape key
In that space is the steak, the part where you get to DRAW!
Here's how you draw a triangle

Code: Select all

glTranslatef 0,0,-3           'moves into the screen three units

glBegin GL_TRIANGLES
     glColor3f 0,0,1              'Blue, in the form of RGB
     glVertex3f 0,1,0            'top vertice, in the form of (x,y,z)

     glColor3f 0,1,0              'Green
     glVertex3f 1,0,0             'bottom right

     glColor3f 1,0,0              'Red
     glVertex3f -1,0,0           'bottom left
glEnd
Put it together and you get

Code: Select all

#include once "GL/gl.bi"
#include once "GL/glu.bi"
   
screen 18,16,,2                                   'FB screen           

glViewport 0,0,640,480                        'screen mode
glMatrixMode GL_PROJECTION              'projection model
glLoadIdentity                                     'resets the matrix
gluPerspective 45.0, 640.0/480.0, 0.1, 100.0
glMatrixMode GL_MODELVIEW              'viewing model
glLoadIdentity                                     'resets viewing matrix

glClearColor 0.0,0.0,0.0,0.5              'background color
glClearDepth 1.0                              'Clears depth, for z axis
glEnable GL_DEPTH_TEST                 ' Enables Depth Testing
glDepthFunc GL_LEQUAL                   ' The Type Of Depth Testing To Do
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST

do
    glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
    glLoadIdentity

    glTranslatef 0,0,-3           'moves into the screen three units

    glBegin GL_TRIANGLES
        glColor3f 0,0,1              'Blue, in the form of RGB
        glVertex3f 0,1,0            'top vertice, in the form of (x,y,z)

        glColor3f 0,1,0              'Green
        glVertex3f 1,0,0             'bottom right

        glColor3f 1,0,0              'Red
        glVertex3f -1,0,0           'bottom left
    glEnd
    flip
loop until multikey(1)      'escape key
For more, go to the NeHe website and there are nehe tutorials that come with the FB ide (you may have to dig a little bit).

And prepare to be amazed. I've got some source code for a small maze (no collision detection).

Code: Select all

#include once "GL/gl.bi"
#include once "GL/glu.bi"
   
screen 18,16,,2

dim as single x=18   'player's x coordinate
dim as single z=21  'player's z coordinate
dim bob as single   'bob angle
dim as short angle=90  'player's facing angle
dim as byte wid=2         'width of the boxes
dim map(2,10,10) as byte  'map array, (level,row,column)
const conv as single=3.14159265/180  'conversion factor for degrees to radians

glViewport 0,0,640,480                  'screen mode
glMatrixMode GL_PROJECTION          'projection model
glLoadIdentity                      'does something to the models
gluPerspective 45.0, 640.0/480.0, 0.1, 100.0
glMatrixMode GL_MODELVIEW           'viewing model
glLoadIdentity

glClearColor 0.0,0.0,0.0,0.5        'background color?
glClearDepth 1.0                    'Clears depth
glEnable GL_DEPTH_TEST                         ' Enables Depth Testing
glDepthFunc GL_LEQUAL                          ' The Type Of Depth Testing To Do
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST

for m=1 to 2
    for r=1 to 10
        for c=1 to 10
            read map(m,r,c)
        next c
    next r
next m

do
    glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
    glLoadIdentity
    glRotatef 90-angle,0,1,0    
    glTranslatef -x,-1-.2*sin(5*bob*conv),-z
    
    'map
    for m=0 to 1    'm is offset back 1
    for r=1 to 10
        for c=1 to 10
            if map(m+1,c,r)=1 then
                glBegin GL_QUADS
                    glColor3f 1.0,1.0,0.0
                    glVertex3f wid*(r-.5),wid+wid*m,wid*(c+.5) 'front, yellow
                    glVertex3f wid*(r+.5),wid+wid*m,wid*(c+.5)
                    glVertex3f wid*(r+.5),wid*m  ,wid*(c+.5)
                    glVertex3f wid*(r-.5),wid*m  ,wid*(c+.5)
                    
                    glColor3f 1.0,0.0,0.0       'back face, red
                    glVertex3f wid*(r-.5),wid+wid*m,wid*(c-.5)
                    glVertex3f wid*(r-.5),wid*m  ,wid*(c-.5)
                    glVertex3f wid*(r+.5),wid*m  ,wid*(c-.5)
                    glVertex3f wid*(r+.5),wid+wid*m,wid*(c-.5)
                    
                    glColor3f 0.0,1.0,1.0       'left face,
                    glVertex3f wid*(r-.5),wid+wid*m,wid*(c-.5)
                    glVertex3f wid*(r-.5),wid+wid*m,wid*(c+.5)
                    glVertex3f wid*(r-.5),wid*m  ,wid*(c+.5)
                    glVertex3f wid*(r-.5),wid*m  ,wid*(c-.5)
                    
                    glColor3f 0.0,0.0,1.0       'right face, blue
                    glVertex3f wid*(r+.5),wid*m  ,wid*(c-.5)
                    glVertex3f wid*(r+.5),wid*m  ,wid*(c+.5)
                    glVertex3f wid*(r+.5),wid+wid*m,wid*(c+.5)
                    glVertex3f wid*(r+.5),wid+wid*m,wid*(c-.5)
                glEnd
            end if
        next c
    next r
    next m

    
    'ground
    glBegin GL_QUADS
        glColor3f 0.0,0.5,0.0   'dark green
        glVertex3f .5*wid  ,0.0,wid*10.5
        glVertex3f wid*10.5,0.0,wid*10.5
        glVertex3f wid*10.5,0.0,wid*.5
        glVertex3f .5*wid  ,0.0,wid*.5
    glEnd
    'ceiling
    glBegin GL_QUADS
        glColor3f .8,.8,1.0   'light blue
        glVertex3f .5*wid  ,wid,wid*10.5
        glVertex3f wid*10.5,wid,wid*10.5
        glVertex3f wid*10.5,wid,wid*.5
        glVertex3f .5*wid  ,wid,wid*.5
    glEnd
    '2nd ceiling
    glBegin GL_QUADS
        glColor3f .8,.8,1.0   'light blue
        glVertex3f .5*wid  ,wid*2,wid*10.5
        glVertex3f wid*10.5,wid*2,wid*10.5
        glVertex3f wid*10.5,wid*2,wid*.5
        glVertex3f .5*wid  ,wid*2,wid*.5
    glEnd
    
    'walk forward
    if multikey(80) then 
        let x=x-.1*cos(-angle*conv)
        let z=z-.1*sin(-angle*conv)
        let bob=bob+1
    end if
    'walk backward
    if multikey(72) then 
        let x=x+.1*cos(-angle*conv)
        let z=z+.1*sin(-angle*conv)
        let bob=bob-1
    end if
    'turn left
    if multikey(75) then let angle=angle+2
    'turn right
    if multikey(77) then let angle=angle-2
    
    if bob>=360 then let bob=1
    
    flip
loop while not(multikey(1))

end

'level 1
'    1,2,3,4,5,6,7,8,9,0
data 1,1,1,1,1,1,1,1,1,1 :'1
data 1,0,0,0,0,0,0,0,0,1 :'2
data 1,1,0,1,1,1,1,1,0,1 :'3
data 1,0,0,1,0,0,0,0,0,1 :'4
data 1,0,1,0,0,1,1,1,0,1 :'5
data 1,0,1,0,1,1,0,1,0,1 :'6
data 1,0,1,0,1,0,0,0,0,1 :'7
data 1,0,1,1,1,0,1,1,0,1 :'8
data 1,0,0,1,0,0,0,1,0,1 :'9
data 1,1,1,1,1,1,1,1,0,1 :'10

'level 2
data 1,1,1,1,1,1,1,1,1,1
data 1,0,0,0,0,0,0,0,0,0
data 1,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,1
data 1,0,0,0,0,0,0,0,0,1
data 1,1,1,1,1,1,1,1,1,1
You can change wid around. There shouldn't be any problems.
For any grievances posted above, I blame whoever is in charge . . .

User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Post by Codemss » Thu Jan 03, 2008 1:43 pm

nice! you are much further with OpenGL then I. I got heavily confused when I tried to learn how you open a opengl window :P. Well, I will try it later.
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL

User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat » Thu Jan 03, 2008 1:55 pm

I learned it over Christmas. As for making the window, I copy and paste the first blocks before the loop. For my purposes, it's always the same.

All you are doing is telling openGL "I want a screen that does 3d graphics, is this wide and this tall, and can do some really fancy math." The matrice loadings just are for the 'camera' and space, and for the math that comes with it.
For any grievances posted above, I blame whoever is in charge . . .

User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Post by Codemss » Sun Jan 06, 2008 12:22 pm

I printed your post. (it was 6 pages with the code :P). I kinda understand it now. You post is lika tutorial. :P.

I think I will try to make some rotating poly's soon. I'll post it here. Thanx for your help.
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL

User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Post by Codemss » Sun Feb 03, 2008 9:03 am

When I browsed through some OpenGL apps on the internet, it seems to me that most of them are quite slow. IS this just because a lack of optimization? I used to think that OpenGL works at blazing speed.
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL

User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat » Sun Feb 03, 2008 10:49 am

It isn't because of limitiations. I think it's resource management on OpenGL's part. Low polygon counts don't need as much speed, so it seems that it's slow. I had a small maze using GL, yet it ran at the same speed as when I ran just a rotating triangle.
For any grievances posted above, I blame whoever is in charge . . .

User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Post by Codemss » Mon Feb 04, 2008 1:44 pm

Nah I mean more the programs on Nehe that use special things like mipmapping, antialiasing and that kind of stuff.
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL

User avatar
Xerol
Veteran
Posts: 81
Joined: Tue Jan 04, 2005 6:27 pm
Location: Timonium, MD
Contact:

Post by Xerol » Tue Feb 05, 2008 2:08 am

There seems to be an extreme slowness problem with certain video cards and opengl mipmaps in FB. A year and I haven't been able to exactly figure it out.

One thing to start using early is glPushMatrix and glPopMatrix. glLoadIdentity is relatively slow (push and pop matrix are actually just pointer swaps, while loadidentity has to copy a whole matrix). These let you quickly reset the transforms when rendering different sets of objects. A related thing to exploit is that the transform matrix "stacks" up, so if you need to render a bunch of objects evenly spaced apart, you can exploit this.

Assume cube_list and sphere_list are display lists containing a cube and sphere respectively. The first block draws cubes at (1,0,1), (2,0,1)-(2,0,2), (3,0,1)-(3,0,3), (4,0,1)-(4,0,4), and (5,0,1)-(5,0,5).

The second block doesn't pop the matrix back to the starting state after each j-loop, so the spheres show up at (1,1,1), (2,1,2), (2,1,3), (3,1,4), (3,1,5), (3,1,6), (4,1,7), (4,1,8), (4,1,9), (4,1,10), (5,1,11), (5,1,12), (5,1,13), (5,1,14), and (5,1,15).

Code: Select all

glLoadIdentity()

'Draw a bunch of cubes
glPushMatrix()
  for i = 1 to 5
    glTranslatef 1.0, 0.0, 0.0
    glPushMatrix() 'Matrix contains a transform i units to the right here
      for j = 1 to i
        glTranslatef 0.0, 0.0, 1.0
        glCallList(cube_list)
      next j
    glPopMatrix() 'Restores the matrix at the beginning, meaning the next time the j-loop is called, the z-transform is reset to 0, and can then be looped again
  next i
glPopMatrix()

'without the push/pop (or another glLoadIdentity) the next object rendered would be at 5,0,5 (plus whatever other transforms are applied). Instead we're back at the identity matrix, and have saved a relatively slow copy.

glTranslatef 0.0, 1.0, 0.0 'The spheres will appear on a layer above the cubes
glPushMatrix()
  for i = 1 to 5
    glTranslatef 1.0, 0.0, 0.0
    for j = 1 to i
      glTranslatef 0.0, 0.0, 1.0
      glCallList(sphere_list)
    next j
  next i
glPopMatrix()

'even though there's no more rendering to be done, the whole loop is contained for consistency and to make it easier to add more rendering if we need it
Keep in mind that display lists work as if the code in the list was just pasted in place of the glCallList call, so if you've put any transforms inside a display list and have funky output, keep that in mind. Basically, keep track of where the transforms are, otherwise you might end up with objects rendering somewhere you don't want them to. glPushMatrix and glPopMatrix help immensely in this.

Edit: This code isn't tested, but it should work if you put it inside a render loop and have the right display lists set up.
If you need music composed in MP3, WAV, or MIDI format, please contact me via email.

Xerol's Music - Updated Regularly!

User avatar
burger2227
Veteran
Posts: 2425
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Watch him!

Post by burger2227 » Sun Feb 10, 2008 6:51 pm

Codemss is a know borrower of code. He then makes his own programs with it and wants credit if you use it. He used to hound the QB sites with tons of questions, so I guess it's your turn.

At least HE is learning something!

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Re: Watch him!

Post by Mentat » Sun Feb 10, 2008 7:10 pm

burger2227 wrote:... He used to hound the QB sites with tons of questions...
:D
Okay, who hasn't? I hounded FreeBasic.net, Y!A, and here too. And I'm by far not alone.
For any grievances posted above, I blame whoever is in charge . . .

User avatar
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt » Sun Feb 10, 2008 7:57 pm

burger2227 is a known troll.

Post Reply