Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: Shockwave 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.
'
' 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
-
How about this one, ~7kb :P
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
-
:) k+
-
I've forgot to remove this line, its not necessary.
dim shared hwnd as HWND
-
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
-
Build up your faces like this; :)
(You can have loads of quads in between the glbegin and glend
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
-
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
-
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!
-
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.
-
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. :)
'
' 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
-
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
-
Hmm, cheers Jim :)
The perspective certainly looks a lot better at 90
-
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:
-
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.
-
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.