Author Topic: Really simple Opengl example.  (Read 7992 times)

0 Members and 1 Guest are viewing this topic.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Really simple Opengl example.
« on: January 18, 2008 »
Now, I know that I have not set up anything properly here really, but Freebasic makes it even simpler than C++ to initialise a window for opengl drawing.

Downside? It takes more room than tinyptc already.

So bearing in mind that there are a lot of things I never set up here (because I am still learning), I thought I'd post my progress, it opens an opengl screen and draws a large riangle.


Please don't ask for an exe, this is for the freebasic guys so they know how to open a gl window with no troubles.

Code: [Select]
'
' This is a test to initially open a window in freebasic for Opengl and
' just simply put a triangle onto it.
'
' By Shockwave
'
'===============================================================================


    OPTION STATIC
    OPTION EXPLICIT

    #INCLUDE "GL/gl.bi"
    #INCLUDE "GL/glu.bi"
   
'   INCLUDE WINDOWS.BI AFTER GL, GLU.   

    #INCLUDE "WINDOWS.BI"

'===============================================================================   
' MODE 20 = 1024*768 - 24 BIT - DEFAULT PAGES -
' FLAG = 3 (2 for gl drawing + 1 for full screen) FOR GL DRAWING
'===============================================================================

    SCREEN 19,24,,3
   
WHILE((GETASYNCKEYSTATE(VK_ESCAPE)<> -32767) )   

'   DRAW A TRIANGLE;

  GLBEGIN GL_TRIANGLES   
   
        GLCOLOR3F   1.0, 1.0, 0.0           ' YELLOW
    GLVERTEX3F  0.0, 1.0, 0.0           ' TOP
    GLCOLOR3F   0.0, 1.0, 1.0           ' TURQOISE
    GLVERTEX3F  1.0, -1.0, 0.0          ' RIGHT
    GLCOLOR3F   1.0, 0.0, 1.0           ' PURPLE    
            GLVERTEX3F -1.0, -1.0, 0.0          ' LEFT
   
    GLEND
   
    FLIP
   
'   CLEAR SCREEN + DEPTH BUFFER   

    GLCLEAR GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT     
   
WEND
END
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: Really simple Opengl example.
« Reply #1 on: January 18, 2008 »
How about this one, ~7kb  :P


Code: [Select]
option explicit
option static

'-------------------------------------
' Includes
'-------------------------------------
#include "windows.bi"
#include "GL/gl.bi"

dim shared pfd as PIXELFORMATDESCRIPTOR
dim shared hdc as hDC
declare sub InitOGL()


InitOGL()


while(GetAsyncKeyState(VK_ESCAPE)<>-32767)
    glClear(GL_DEPTH_BUFFER_BIT + GL_COLOR_BUFFER_BIT )


  GLBEGIN GL_TRIANGLES   
   
        GLCOLOR3F   1.0, 1.0, 0.0           ' YELLOW
    GLVERTEX3F  0.0, 1.0, 0.0           ' TOP
    GLCOLOR3F   0.0, 1.0, 1.0           ' TURQOISE
    GLVERTEX3F  1.0, -1.0, 0.0          ' RIGHT
    GLCOLOR3F   1.0, 0.0, 1.0           ' PURPLE    
            GLVERTEX3F -1.0, -1.0, 0.0          ' LEFT
   
    GLEND
   

    SwapBuffers(hDC)
wend


sub InitOGL()
pfd.cColorBits = 32
    pfd.cDepthBits = 32
pfd.dwFlags    = PFD_SUPPORT_OPENGL + PFD_DOUBLEBUFFER
    hDC = GetDC(CreateWindow("edit", 0,WS_POPUP+WS_VISIBLE+WS_MAXIMIZE,0, 0, 0 , 0, 0, 0, 0, 0))
SetPixelFormat ( hDC, ChoosePixelFormat ( hDC, @pfd) , @pfd )
wglMakeCurrent ( hDC, wglCreateContext(hDC) )
    ShowCursor(FALSE)
end sub
« Last Edit: January 18, 2008 by rbraz »
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Really simple Opengl example.
« Reply #2 on: January 18, 2008 »
:) k+
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: Really simple Opengl example.
« Reply #3 on: January 18, 2008 »
I've forgot to remove this line, its not necessary.
Code: [Select]
dim shared hwnd as HWND

Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Really simple Opengl example.
« Reply #4 on: January 18, 2008 »
Okay guys, I understand the code, fairly straightforward, but are the GLVERTEX commands pixel based, as they only use -1 or 0 or 1 -, how would i draw a box, i know it needs four vertex points, but how do i resize n stuff like that?

Sorry to be a bit dim but I have NEVER touched opengl before!

Thanks for the post though - excellent work shockwave (for posting) and rbraz (for your contribution).

Cheers
Drew
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Really simple Opengl example.
« Reply #5 on: January 18, 2008 »
Build up your faces like this; :)

(You can have loads of quads in between the glbegin and glend
Code: [Select]
GLBEGIN GL_QUADS

'FACE 1:
            GLCOLOR3F   1.0, 0.0, 0.0           ' RED
            GLVERTEX3F -1, 1,1
            GLCOLOR3F   0.0, 1.0, 0.0           ' GREEN     
            GLVERTEX3F  1, 1,1
            GLCOLOR3F   0.0, 0.0,1.0           ' BLUE
            GLVERTEX3F  1, 1,-1
            GLCOLOR3F   1.0, 1.0, 0.0           ' YELLOW     
            GLVERTEX3F -1, 1,-1

GLEND
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Really simple Opengl example.
« Reply #6 on: January 19, 2008 »
The default matrices that OpenGL uses map the entire window from (-1,-1) in the bottom left to (+1,+1) in the top right.

Jim
Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: Really simple Opengl example.
« Reply #7 on: January 19, 2008 »
Shock, LOL!  I guess my PM to you is moot and academic.  You can forget about it.  It isn't geared for small sizes. Hahahahahahaha

Nice example guys!
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Really simple Opengl example.
« Reply #8 on: January 19, 2008 »
Your PM is most certainly not moot and academic :) There are loads of things I would like to ask you mate and I will be following the advice that you gave me about upgrading my ide and fb package because it makes sense. I'll hopefully get to speak to you soon.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Really simple Opengl example.
« Reply #9 on: January 19, 2008 »
Still having fun messing around with this.

I haven't used the example you gave yet Rbraz, sorry.. I can't see how to control the window size yet, or to set the aspect ratio of the window yet. I will play with it soon though I promise and post my understanding of it so others can learn. :)

Code: [Select]
'
' By Shockwave
'
'===============================================================================


    OPTION STATIC
    OPTION EXPLICIT

    #INCLUDE "GL/gl.bi"
    #INCLUDE "GL/glu.bi"
   
'   INCLUDE WINDOWS.BI AFTER GL, GLU.   

    #INCLUDE "WINDOWS.BI"

'===============================================================================   
' MODE 20 = 1024*768 - 24 BIT - DEFAULT PAGES -
' FLAG = 3 (2 for gl drawing + 1 for full screen) FOR GL DRAWING
'===============================================================================

    SCREEN 20,24,,3
    dim as single quad1,quad2
glViewport 0, 0, 1024, 768                          '' Reset The Current Viewport
glMatrixMode GL_PROJECTION                          '' Select The Projection Matrix
glLoadIdentity                                      '' Reset The Projection Matrix
gluPerspective 50.0, 1024.0/768.0, 0.1, 100.0       '' Calculate The Aspect Ratio Of The Window
glMatrixMode GL_MODELVIEW                           '' Select The Modelview Matrix
glLoadIdentity                                      '' Reset The Modelview Matrix

'' All Setup For OpenGL Goes Here
glShadeModel GL_SMOOTH                              '' Enable Smooth Shading
glClearColor 0.0, 0.0, 0.0, 0.02                     '' Black Background
glClearDepth 1.0                                    '' Depth Buffer Setup
glEnable GL_DEPTH_TEST                              '' Enables Depth Testing
glDepthFunc GL_LEQUAL                               '' The Type Of Depth Testing To Do
glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST    '' Really Nice Perspective Calculations

dim xrot as single            '' X Rotation
dim yrot as single            '' Y Rotation
dim zrot as single            '' Z Rotation

    dim shared LightAmbient(0 to 3) as single => {0.1, 0.2, 0.3,0.5}   '' Ambient Light is 20% white
    dim shared LightDiffuse(0 to 3) as single => {0.2, 0.3, 0.6,0.5}   '' Diffuse Light is white
    dim shared LightPosition(0 to 2) as single =>{0.0, 0.0, 5.0    }  '' Position is somewhat in front of screen
   
    dim shared DEF1(0 to 3) as single => {1.0, 12.0, 1.0,0.15}  '' Position is somewhat in front of screen
    dim shared DEF2(0 to 3) as single => {1.0, 1.0, 12.0,0.15}  '' Position is somewhat in front of screen
    dim shared DEF3(0 to 3) as single => {12.0, 1.0, 1.0,0.15}  '' Position is somewhat in front of screen


    glLightfv( GL_LIGHT1, GL_AMBIENT, @LightAmbient(0))        '' Load Light-Parameters Into GL_LIGHT1
glLightfv( GL_LIGHT1, GL_DIFFUSE, @LightDiffuse(0))
    glLightfv( GL_LIGHT1, GL_POSITION, @LightPosition(0))


    glEnable(GL_LIGHTING)
glEnable(GL_LIGHT1)
    GLSHADEMODEL (GL_SMOOTH)
    GLENABLE(GL_BLEND)
    GLBLENDFUNC(GL_ONE,GL_DST_ALPHA)
    DIM AS SINGLE XX,YY,ZZ
   
   
WHILE((GETASYNCKEYSTATE(VK_ESCAPE)<> -32767) )   

         XX=.5*SIN(TIMER*3)
         YY=.5*SIN(TIMER*2.1)
        ZZ=(1*SIN(TIMER*2.4))-6

        GLCLEAR GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT   
        GLFLUSH()
    glLoadIdentity                                          '' Reset The Current Modelview Matrix
   
glTranslatef(XX, YY, ZZ)
glRotatef(xrot, 1.0, 0.0, 0.0)
glRotatef(yrot, 0.0, 1.0, 0.0)
glRotatef(zrot, 0.0, 0.0, 1.0)
   
  GLBEGIN GL_QUADS

    GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@DEF1(0))

            GLVERTEX3F  1, 1,-1
            GLNORMAL3F    0.0,0.0,-1.0           
            GLVERTEX3F  1,-1,-1
            GLNORMAL3F    0.0,0.0,-1.0           
            GLVERTEX3F -1,-1,-1
            GLVERTEX3F -1, 1,-1 
            GLNORMAL3F    0.0,0.0,-1.0           
            GLCOLOR4F   0.0,1.0,0.0,1.0
            GLNORMAL3F    0.0,0.0,-1.0


            GLNORMAL3F    0.0,0.0,1.0                       
            GLVERTEX3F -1, 1,1
            GLNORMAL3F    0.0,0.0,1.0       
            GLVERTEX3F  1, 1,1
            GLNORMAL3F    0.0,0.0,1.0       
            GLVERTEX3F  1,-1,1
            GLNORMAL3F    0.0,0.0,1.0       
            GLVERTEX3F -1,-1,1
           
    GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@DEF2(0))


            GLNORMAL3F    0.0,1.0,0.0   
            GLVERTEX3F  1, 1,1
            GLNORMAL3F   0.0,1.0,0.0                       
            GLVERTEX3F -1, 1,1
            GLNORMAL3F    0.0,1.0,0.0   
            GLVERTEX3F -1, 1,-1
            GLNORMAL3F    0.0,1.0,0.0   
            GLVERTEX3F  1, 1,-1


           
            GLNORMAL3F    0.0,-1.0,0.0         
            GLVERTEX3F  1, -1,-1
            GLNORMAL3F    0.0,-1.0,0.0         
            GLVERTEX3F -1, -1,-1
            GLNORMAL3F    0.0,-1.0,0.0                       
            GLVERTEX3F -1, -1,1
            GLNORMAL3F    0.0,-1.0,0.0         
            GLVERTEX3F  1, -1,1


    GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@DEF3(0))

            GLNORMAL3F  1.0,0.0,0.0                       
            GLVERTEX3F  1, 1,1
            GLNORMAL3F  1.0,0.0,0.0         
            GLVERTEX3F  1, -1,1
            GLNORMAL3F  1.0,0.0,0.0         
            GLVERTEX3F  1, -1,-1
            GLNORMAL3F  1.0,0.0,0.0         
            GLVERTEX3F  1, 1,-1

            GLNORMAL3F  -1.0,0.0,0.0                       
            GLVERTEX3F  -1, 1,1
            GLNORMAL3F  -1.0,0.0,0.0       
            GLVERTEX3F  -1, -1,1
            GLNORMAL3F  -1.0,0.0,0.0       
            GLVERTEX3F  -1, -1,-1
            GLNORMAL3F  -1.0,0.0,0.0       
            GLVERTEX3F  -1, 1,-1


    GLEND
   
    FLIP
   


xrot = xrot + 0.07
yrot = yrot + 0.06
zrot = zrot + 0.05
 
WEND
END
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Really simple Opengl example.
« Reply #10 on: January 19, 2008 »
Window size
   glViewport 0, 0, 1024, 768
By default the viewport is the same size as the window when the OpenGL device is created unless you change it.

Aspect ratio
   glMatrixMode GL_PROJECTION
   gluPerspective 50.0, 1024.0/768.0, 0.1, 100.0

50.0 is an odd value for the Field of View.  I usually set mine to (90*screen_height)/screen_width, which gives the 'natural' 90degree horizontal view angle.

Jim
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Really simple Opengl example.
« Reply #11 on: January 19, 2008 »
Hmm, cheers Jim :)

The perspective certainly looks a lot better at 90
Shockwave ^ Codigos
Challenge Trophies Won:

Offline .:] Druid [:.

  • freebasic n00b
  • Pentium
  • *****
  • Posts: 563
  • Karma: 47
    • View Profile
    • Intro-Inferno
Re: Really simple Opengl example.
« Reply #12 on: January 20, 2008 »
hmmm...and now you start opengl on freebasic...like I have time NOW to start playing with it myself :D
That's a great initiative, I'll probably follow it and learn from it!

 :goodpost:
[sheep]: im sure he wants to goto prison.. they didnt get him last time.. he was promised a big cock up his arse.. and no doubt looking forward to it.. lets hope he gets his wish this year.

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: Really simple Opengl example.
« Reply #13 on: January 20, 2008 »
I've been wondering (not criticizing, just curious) , why are you still using "option static" and "option explicit"? Putting those two lines in requires that you use either -lang qb or -lang deprecated in order to compile it, but by doing that you lose many of the new OO (and multithreading with lang qb) features of the compiler.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Really simple Opengl example.
« Reply #14 on: January 20, 2008 »
I've been wondering (not criticizing, just curious) , why are you still using "option static" and "option explicit"? Putting those two lines in requires that you use either -lang qb or -lang deprecated in order to compile it, but by doing that you lose many of the new OO (and multithreading with lang qb) features of the compiler.

Because I did not like the way Freebasic is evolving from a structured language into more of an oop language I had not upgraded freenasic and had been using fb1.5 (this will change). Also Rbraz's extension Tinyptc_ext was written for FB1.5.

Option static made fixed size arrays which worked faster.
Option Explicit means that everything needs to be defined which leads to better programming, I prefer to have to declare everything as it helps with debugging.

I am not a huge fan of OOP. Just a personal matter of preference.
Shockwave ^ Codigos
Challenge Trophies Won: