Author Topic: Wishful Thinking  (Read 6437 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Wishful Thinking
« on: October 14, 2011, 12:43:06 PM »
If this Basic developer forum becomes popular as a shared resource, it would be cool if everyone would join forces and collaboratively create the next perfect Basic language. There is a lot of talent that are already members of this forum. I invited Paul Turley and Victor (FB author) but I'm not sure they will join us. There is still open invitations to Peter van Eerten (BaCon/GTK-Server), Kevin Diggins (MrBcx), Thomas Larsen and Eros. (declined for the moment - busy) If you know of a Basic developer or active project contributor, have them send me a e-mail to support@scriptbasic.org with the requested user name for the forum.
« Last Edit: October 14, 2011, 01:02:23 PM by JRS »

Marcus

  • Guest
Re: Wishful Thinking
« Reply #1 on: October 25, 2011, 07:49:36 AM »
Creating the perfect basic probably wouldn't be that difficult. If it just supports loading of dll:s (and whatever the linux and mac equivalents are), we just need one expert on every possible area that can implement wrapper libraries. The only problem is what the language itself should support. Does, for example OOP, belong in BASIC? I don't think so, but I guess many people do.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: Wishful Thinking
« Reply #2 on: October 25, 2011, 08:35:07 AM »
I like the Gambas approach to building a better Basic. They have a core ScriptBasic like interpreter and everything is built on top of that as shared objects. If you have a Linux install, (Ubuntu is best as that is what the author uses) you might want to give Gambas 3 a try and check out how that project is assembled.

I could see using SB as the core with it's easy to use extension module API but use a Rapid-Q like syntax model rather than VB.

« Last Edit: October 25, 2011, 08:47:46 AM by JRS »

Marcus

  • Guest
Re: Wishful Thinking
« Reply #3 on: October 25, 2011, 08:56:52 AM »
Yeah, I like Gambas (I accidentally installed it once because I thought the name was short for game basic ...).

But expert programmers would still be needed in many areas to make the perfect basic :) If something like this was to be released, one couldn't count on "the community" to do the work. "Everything"'d need to be there in the first official release - else it wouldn't grow, as people usually prefer specific purpose basics.

If I was to be involved with something like TPB (The Perfect Basic) I woldn't go near the compiler/interpreter part. Nor would I have anything to do with making database programming easy etc. I'm only good at one thing, and that's algorithms for computer graphics, which probably wouldn't be needed in the first place as everyone have trust in SDL, OpenGL etc :)


Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: Wishful Thinking
« Reply #4 on: October 25, 2011, 09:02:53 AM »
Quote
I'm only good at one thing, and that's algorithms for computer graphics, which probably wouldn't be needed in the first place as everyone have trust in SDL, OpenGL etc

That is Gambas's approach. Provide the engine and use industry standard libraries to build it out.

Gambas Component List

  • gb - Internal native classes
  • gb.cairo - Cairo graphic library
  • gb.chart - Chart drawing
  • gb.compress - Compression & decompression
  • gb.crypt - MD5/DES crypting
  • gb.db - Database access
  • gb.db.firebird - Firebird database driver
  • gb.db.form - Data bound controls
  • gb.db.mysql - MySQL database driver
  • gb.db.odbc - ODBC database driver
  • gb.db.postgresql - PostgreSQL database driver
  • gb.db.sqlite2 - SQLite 2 database driver
  • gb.db.sqlite3 - SQLite 3 database driver
  • gb.debug - Application debugger helper
  • gb.desktop - Desktop-neutral routines from Portland project
  • gb.dbus - D-Bus interface
  • gb.eval - Expression evaluator
  • gb.eval.highlight - Syntax highlighter routines
  • gb.form - More controls for graphical components
  • gb.form.dialog - Enhanced standard dialogs
  • gb.form.mdi - Multi Document Interface
  • gb.gtk - GTK+ toolkit
  • gb.gtk.ext - GTK+ toolkit extension
  • gb.gtk.opengl - OpenGL with GTK+ toolkit
  • gb.gtk.svg - GTK+ SVG renderer
  • gb.gui - QT/GTK+ switcher component
  • gb.image - Image management
  • gb.image.effect - Image filtering component
  • gb.image.imlib - Image routines from the Imlib2 library
  • gb.image.io - Image loading and saving
  • gb.info - Information about components
  • gb.ldap - OpenLDAP client
  • gb.mysql - MySQL specific routines
  • gb.net - Network programming
  • gb.net.curl - Network high-level protocols management
  • gb.net.smtp - SMTP client
  • gb.opengl - 3D programming with OpenGL
  • gb.opengl.glsl - OpenGL shaders management
  • gb.option - GNU command option parser
  • gb.pcre - Perl-compatible Regular Expression Matching
  • gb.pdf - PDF renderer based on Poppler library
  • gb.qt4 - QT4 toolkit
  • gb.qt4.ext - QT4 toolkit extension
  • gb.qt4.webkit - QT4 WebKit component
  • gb.qt4.opengl - OpenGL with QT4 toolkit
  • gb.report - Report designer
  • gb.sdl - SDL library
  • gb.sdl.sound - SDL sound & CD-ROM management
  • gb.settings - Application settings management
  • gb.signal - Signals management
  • gb.v4l - Video capture
  • gb.vb - Visual Basic compatibility
  • gb.xml - XML tools based on libxml
  • gb.xml.rpc - XML-RPC protocol
  • gb.xml.xslt - XSLT tools based on libxslt
  • gb.web - Web applications tools

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: Wishful Thinking
« Reply #5 on: October 25, 2011, 09:15:19 AM »
A Rapid-Q like multi-platform Basic using a fork of Gambas components and SB seems doable to me. If I could just get an example of interfacing SB to a Gambas component, it should be smooth sailing from there.

Marcus

  • Guest
Re: Wishful Thinking
« Reply #6 on: October 25, 2011, 09:25:57 AM »
I really should shut up until I've checked out gambas for real and that entire list. But I'll just ask a question instead. I see that gambas supports opengl. Does that mean you can write something like:

Code: [Select]
' Open a window at position 0, 0 of the size 640x480 for 32 bit opengl rendering with a 90 degree view.
opengl create window 0, 0, 640, 480, 32, 90

rather than:

Code: [Select]
/*
 * This Code Was Created By Jeff Molofee 2000
 * A HUGE Thanks To Fredric Echols For Cleaning Up
 * And Optimizing This Code, Making It More Flexible!
 * If You've Found This Code Useful, Please Let Me Know.
 * Visit My Site At nehe.gamedev.net
 */

#include <windows.h> // Header File For Windows
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library

HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application

bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc

GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}

glViewport(0,0,width,height); // Reset The Current Viewport

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}

int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // 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
return TRUE; // Initialization Went OK
}

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
return TRUE; // Everything Went OK
}

GLvoid KillGLWindow(GLvoid) // Properly Kill The Window
{
if (fullscreen) // Are We In Fullscreen Mode?
{
ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
ShowCursor(TRUE); // Show Mouse Pointer
}

if (hRC) // Do We Have A Rendering Context?
{
if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?
{
MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}

if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
{
MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL; // Set RC To NULL
}

if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC
{
MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL; // Set DC To NULL
}

if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
{
MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // Set hWnd To NULL
}

if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class
{
MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL; // Set hInstance To NULL
}
}

/* This Code Creates Our OpenGL Window.  Parameters Are: *
 * title - Title To Appear At The Top Of The Window *
 * width - Width Of The GL Window Or Fullscreen Mode *
 * height - Height Of The GL Window Or Fullscreen Mode *
 * bits - Number Of Bits To Use For Color (8/16/24/32) *
 * fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */
 
BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat; // Holds The Results After Searching For A Match
WNDCLASS wc; // Windows Class Structure
DWORD dwExStyle; // Window Extended Style
DWORD dwStyle; // Window Style
RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
WindowRect.left=(long)0; // Set Left Value To 0
WindowRect.right=(long)width; // Set Right Value To Requested Width
WindowRect.top=(long)0; // Set Top Value To 0
WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height

fullscreen=fullscreenflag; // Set The Global Fullscreen Flag

hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
wc.cbClsExtra = 0; // No Extra Window Data
wc.cbWndExtra = 0; // No Extra Window Data
wc.hInstance = hInstance; // Set The Instance
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
wc.hbrBackground = NULL; // No Background Required For GL
wc.lpszMenuName = NULL; // We Don't Want A Menu
wc.lpszClassName = "OpenGL"; // Set The Class Name

if (!RegisterClass(&wc)) // Attempt To Register The Window Class
{
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if (fullscreen) // Attempt Fullscreen Mode?
{
DEVMODE dmScreenSettings; // Device Mode
memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

// Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
// If The Mode Fails, Offer Two Options.  Quit Or Use Windowed Mode.
if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen=FALSE; // Windowed Mode Selected.  Fullscreen = FALSE
}
else
{
// Pop Up A Message Box Letting User Know The Program Is Closing.
MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
return FALSE; // Return FALSE
}
}
}

if (fullscreen) // Are We Still In Fullscreen Mode?
{
dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
dwStyle=WS_POPUP; // Windows Style
ShowCursor(FALSE); // Hide Mouse Pointer
}
else
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
}

AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size

// Create The Window
if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window
"OpenGL", // Class Name
title, // Window Title
dwStyle | // Defined Window Style
WS_CLIPSIBLINGS | // Required Window Style
WS_CLIPCHILDREN, // Required Window Style
0, 0, // Window Position
WindowRect.right-WindowRect.left, // Calculate Window Width
WindowRect.bottom-WindowRect.top, // Calculate Window Height
NULL, // No Parent Window
NULL, // No Menu
hInstance, // Instance
NULL))) // Dont Pass Anything To WM_CREATE
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
bits, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)  
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};

if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

ShowWindow(hWnd,SW_SHOW); // Show The Window
SetForegroundWindow(hWnd); // Slightly Higher Priority
SetFocus(hWnd); // Sets Keyboard Focus To The Window
ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen

if (!InitGL()) // Initialize Our Newly Created GL Window
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}

return TRUE; // Success
}

LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
UINT uMsg, // Message For This Window
WPARAM wParam, // Additional Message Information
LPARAM lParam) // Additional Message Information
{
switch (uMsg) // Check For Windows Messages
{
case WM_ACTIVATE: // Watch For Window Activate Message
{
if (!HIWORD(wParam)) // Check Minimization State
{
active=TRUE; // Program Is Active
}
else
{
active=FALSE; // Program Is No Longer Active
}

return 0; // Return To The Message Loop
}

case WM_SYSCOMMAND: // Intercept System Commands
{
switch (wParam) // Check System Calls
{
case SC_SCREENSAVE: // Screensaver Trying To Start?
case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
return 0; // Prevent From Happening
}
break; // Exit
}

case WM_CLOSE: // Did We Receive A Close Message?
{
PostQuitMessage(0); // Send A Quit Message
return 0; // Jump Back
}

case WM_KEYDOWN: // Is A Key Being Held Down?
{
keys[wParam] = TRUE; // If So, Mark It As TRUE
return 0; // Jump Back
}

case WM_KEYUP: // Has A Key Been Released?
{
keys[wParam] = FALSE; // If So, Mark It As FALSE
return 0; // Jump Back
}

case WM_SIZE: // Resize The OpenGL Window
{
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));  // LoWord=Width, HiWord=Height
return 0; // Jump Back
}
}

// Pass All Unhandled Messages To DefWindowProc
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

int WINAPI WinMain( HINSTANCE hInstance, // Instance
HINSTANCE hPrevInstance, // Previous Instance
LPSTR lpCmdLine, // Command Line Parameters
int nCmdShow) // Window Show State
{
MSG msg; // Windows Message Structure
BOOL done=FALSE; // Bool Variable To Exit Loop

// Ask The User Which Screen Mode They Prefer
if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
{
fullscreen=FALSE; // Windowed Mode
}

// Create Our OpenGL Window
if (!CreateGLWindow("NeHe's OpenGL Framework",640,480,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}

while(!done) // Loop That Runs While done=FALSE
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
{
if (msg.message==WM_QUIT) // Have We Received A Quit Message?
{
done=TRUE; // If So done=TRUE
}
else // If Not, Deal With Window Messages
{
TranslateMessage(&msg); // Translate The Message
DispatchMessage(&msg); // Dispatch The Message
}
}
else // If There Are No Messages
{
// Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
if (active) // Program Active?
{
if (keys[VK_ESCAPE]) // Was ESC Pressed?
{
done=TRUE; // ESC Signalled A Quit
}
else // Not Time To Quit, Update Screen
{
DrawGLScene(); // Draw The Scene
SwapBuffers(hDC); // Swap Buffers (Double Buffering)
}
}

if (keys[VK_F1]) // Is F1 Being Pressed?
{
keys[VK_F1]=FALSE; // If So Make Key FALSE
KillGLWindow(); // Kill Our Current Window
fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode
// Recreate Our OpenGL Window
if (!CreateGLWindow("NeHe's OpenGL Framework",640,480,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}
}
}
}

// Shutdown
KillGLWindow(); // Kill The Window
return (msg.wParam); // Exit The Program
}

Because that's the whole point with basic. Giving people access to some dll isn't enough. Every supported thing needs to be wrapped  up really damn good.

(In naalaa it's as easy like that to create a window with a 3d view.)



« Last Edit: October 25, 2011, 09:28:00 AM by Marcus »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: Wishful Thinking
« Reply #7 on: October 25, 2011, 09:37:26 AM »
Here is a Gambas gears example. (runs smooth as silk)

Code: [Select]
' Gambas module file

' GambasGears
' Released under GPL v2 or later
' aka glxgears for gambas :)
' based on gears.c by Brian Paul / Mark J. Kilgard
'
' Code : Bodard Fabien & Carlier Laurent
'

Private screen As New Window(True) As "Screen"
Private gearlists As Integer

Private angle As Float
Private Frames As Integer
Private fTime As Float

Public Sub Main()

  With screen
    .Resizable = True
    .Width = 480
    .Height = 480
    .Show()
  End With

  fTime = Timer

End

Public Sub Screen_Open()

  Dim red As Float[]
  Dim green As Float[]
  Dim blue As Float[]
  Dim pos As Float[]

  red = [0.8, 0.1, 0.0, 0.2]
  green = [0.0, 0.8, 0.2, 0.8]
  blue = [0.1, 0.1, 0.8, 1.0]

  ' we enable lights, depth test, cull face
  Gl.Lightfv(Gl.LIGHT0, Gl.POSITION, [5.0, 5.0, 10.0, 0.0])
  Gl.Enable(Gl.CULL_FACE)
  Gl.Enable(Gl.LIGHTING)
  Gl.Enable(Gl.LIGHT0)
  Gl.Enable(Gl.DEPTH_TEST)
  Gl.ClearDepth(1.0)

  ' We need 3 displaylists for the 3 gears
  gearlists = Gl.GenLists(3)

  Gl.NewList(gearlists, Gl.COMPILE)
  Gl.Materialfv(Gl.FRONT, Gl.AMBIENT_AND_DIFFUSE, red)
  Gear(1.0, 4.0, 1.0, 20, 0.7)
  Gl.EndList()
  Gl.NewList(gearlists + 1, Gl.COMPILE)
  Gl.Materialfv(Gl.FRONT, Gl.AMBIENT_AND_DIFFUSE, green)
  Gear(0.5, 2.0, 2.0, 10, 0.7)
  Gl.EndList()
  Gl.NewList(gearlists + 2, Gl.COMPILE)
  Gl.Materialfv(Gl.FRONT, Gl.AMBIENT_AND_DIFFUSE, blue)
  Gear(1.3, 2.0, 0.5, 10, 0.7)
  Gl.EndList()
  Gl.Enable(Gl.NORMALIZE)

End

Public Sub Screen_resize()

  Gl.Viewport(0, 0, Screen.Width, Screen.Height)
  Gl.MatrixMode(Gl.PROJECTION)
  Gl.LoadIdentity()
  Gl.Frustum(-1.0, 1.0, - (Screen.Height / Screen.Width), (Screen.Height / Screen.Width), 5.0, 60.0)
  Gl.MatrixMode(Gl.MODELVIEW)
  Gl.LoadIdentity()
  Gl.Translatef(0.0, 0.0, -40.0)

End

Public Sub Screen_Draw()

  Dim calc As Float

  angle += 0.1

  Gl.Clear(Gl.COLOR_BUFFER_BIT Or Gl.DEPTH_BUFFER_BIT)

  Gl.PushMatrix()

  Gl.Rotatef(20, 1.0, 0.0, 0.0)
  Gl.Rotatef(30, 0.0, 1.0, 0.0)
  Gl.Rotatef(0, 0.0, 0.0, 1.0)

  Gl.PushMatrix()
  Gl.Translatef(-3.0, -2.0, 0.0)
  Gl.Rotatef(angle, 0.0, 0.0, 1.0)
  Gl.CallList(gearlists)
  Gl.PopMatrix()

  Gl.PushMatrix()
  Gl.Translatef(3.1, -2.0, 0.0)
  Gl.Rotatef((-2.0 * angle) - 9.0, 0.0, 0.0, 1.0)
  Gl.CallList(gearlists + 1)
  Gl.PopMatrix()

  Gl.PushMatrix()
  Gl.Translatef(-3.1, 4.2, 0.0)
  Gl.Rotatef((-2.0 * angle) - 25.0, 0.0, 0.0, 1.0)
  Gl.CallList(gearlists + 2)
  Gl.PopMatrix()

  Gl.PopMatrix()

  If (Timer > (fTime + 1)) Then
    Inc fTime
    Print Screen.Framerate; " FPS"
  Endif

End

Public Sub Screen_Close()

  Gl.DeleteLists(gearLists, 3)

End

Public Sub Screen_keyPress()

  Debug Key.Code;; Key.Text
  If (key.code = key.F1) Then screen.Fullscreen = Not screen.Fullscreen
  If (key.Code = key.Esc) Then Screen.Close()

End

Public Sub Screen_MouseMove()

  Gl.Rotatef(Mouse.StartY - Mouse.Y, 0, 0, 1)
  Gl.Rotatef(Mouse.StartX - Mouse.X, 1, 0, 0)

End

Public Sub Gear(inner_radius As Float, outer_radius As Float, width As Float, teeth As Integer, tooth_depth As Float)

  Dim i As Integer
  Dim r0 As Float
  Dim r1 As Float
  Dim r2 As Float
  Dim angle As Float
  Dim da As Float
  Dim u As Float
  Dim v As Float
  Dim fLen As Float

  r0 = inner_radius
  r1 = outer_radius - tooth_depth / 2.0
  r2 = outer_radius + tooth_depth / 2.0

  da = 2.0 * Pi / teeth / 4.0

  Gl.ShadeModel(Gl.FLAT)
  Gl.Normal3f(0.0, 0.0, 1.0)

  ' Draw front face
  Gl.Begin(Gl.QUAD_STRIP)
  For i = 0 To teeth
    angle = i * 2.0 * Pi / teeth
    Gl.Vertexf(r0 * Cos(angle), r0 * Sin(angle), width * 0.5)
    Gl.Vertexf(r1 * Cos(angle), r1 * Sin(angle), width * 0.5)
    If i < teeth Then
      Gl.Vertexf(r0 * Cos(angle), r0 * Sin(angle), width * 0.5)
      Gl.Vertexf(r1 * Cos(angle + 3 * da), r1 * Sin(angle + 3 * da), width * 0.5)
    Endif
  Next
  Gl.End()

  ' Draw front sides of teeth
  Gl.Begin(Gl.QUADS)
  da = 2.0 * Pi / teeth / 4.0
  For i = 0 To teeth - 1
    angle = i * 2.0 * Pi / teeth
    Gl.Vertexf(r1 * Cos(angle), r1 * Sin(angle), width * 0.5)
    Gl.Vertexf(r2 * Cos(angle + da), r2 * Sin(angle + da), width * 0.5)
    Gl.Vertexf(r2 * Cos(angle + 2 * da), r2 * Sin(angle + 2 * da), width * 0.5)
    Gl.Vertexf(r1 * Cos(angle + 3 * da), r1 * Sin(angle + 3 * da), width * 0.5)
  Next
  Gl.End()

  Gl.Normal3f(0.0, 0.0, -1.0)

  ' Draw back face
  Gl.Begin(Gl.QUAD_STRIP)
  For i = 0 To teeth
    angle = i * 2.0 * Pi / teeth
    Gl.Vertexf(r1 * Cos(angle), r1 * Sin(angle), - width * 0.5)
    Gl.Vertexf(r0 * Cos(angle), r0 * Sin(angle), - width * 0.5)
    If i < teeth Then
      Gl.Vertexf(r1 * Cos(angle + 3 * da), r1 * Sin(angle + 3 * da), - width * 0.5)
      Gl.Vertexf(r0 * Cos(angle), r0 * Sin(angle), - width * 0.5)
    Endif
  Next
  Gl.End()

  ' Draw back sides of teeth
  Gl.Begin(Gl.QUADS)
  da = 2.0 * Pi / teeth / 4.0
  For i = 0 To teeth - 1
    angle = i * 2.0 * Pi / teeth
    Gl.Vertexf(r1 * Cos(angle + 3 * da), r1 * Sin(angle + 3 * da), - width * 0.5)
    Gl.Vertexf(r2 * Cos(angle + 2 * da), r2 * Sin(angle + 2 * da), - width * 0.5)
    Gl.Vertexf(r2 * Cos(angle + da), r2 * Sin(angle + da), - width * 0.5)
    Gl.Vertexf(r1 * Cos(angle), r1 * Sin(angle), - width * 0.5)
  Next
  Gl.End()

  ' Draw outward faces of teeth
  Gl.Begin(Gl.QUAD_STRIP)
  For i = 0 To teeth - 1
    angle = i * 2.0 * Pi / teeth
    Gl.Vertexf(r1 * Cos(angle), r1 * Sin(angle), width * 0.5)
    Gl.Vertexf(r1 * Cos(angle), r1 * Sin(angle), - width * 0.5)
    u = r2 * Cos(angle + da) - r1 * Cos(angle)
    v = r2 * Sin(angle + da) - r1 * Sin(angle)
    fLen = Sqr(u * u + v * v)
    u /= fLen
    v /= fLen
    Gl.Normal3f(v, - u, 0.0)
    Gl.Vertexf(r2 * Cos(angle + da), r2 * Sin(angle + da), width * 0.5)
    Gl.Vertexf(r2 * Cos(angle + da), r2 * Sin(angle + da), - width * 0.5)
    Gl.Normal3f(Cos(angle), Sin(angle), 0.0)
    Gl.Vertexf(r2 * Cos(angle + 2 * da), r2 * Sin(angle + 2 * da), width * 0.5)
    Gl.Vertexf(r2 * Cos(angle + 2 * da), r2 * Sin(angle + 2 * da), - width * 0.5)
    u = r1 * Cos(angle + 3 * da) - r2 * Cos(angle + 2 * da)
    v = r1 * Sin(angle + 3 * da) - r2 * Sin(angle + 2 * da)
    Gl.Normal3f(v, - u, 0.0)
    Gl.Vertexf(r1 * Cos(angle + 3 * da), r1 * Sin(angle + 3 * da), width * 0.5)
    Gl.Vertexf(r1 * Cos(angle + 3 * da), r1 * Sin(angle + 3 * da), - width * 0.5)
    Gl.Normal3f(Cos(angle), Sin(angle), 0.0)
  Next
  Gl.Vertexf(r1 * Cos(0), r1 * Sin(0), width * 0.5)
  Gl.Vertexf(r1 * Cos(0), r1 * Sin(0), - width * 0.5)
  Gl.End()

  Gl.ShadeModel(Gl.SMOOTH)

  ' Draw inside radius cylinder
  Gl.Begin(Gl.QUAD_STRIP)
  For i = 0 To teeth
    angle = i * 2.0 * Pi / teeth
    Gl.Normal3f(- Cos(angle), - Sin(angle), 0.0)
    Gl.Vertexf(r0 * Cos(angle), r0 * Sin(angle), - width * 0.5)
    Gl.Vertexf(r0 * Cos(angle), r0 * Sin(angle), width * 0.5)
  Next
  Gl.End()

End


Marcus

  • Guest
Re: Wishful Thinking
« Reply #8 on: October 25, 2011, 09:43:01 AM »
Actually ... that's good enough for me ;D  The same could be done in naalaa with less code and better visuals, but naaalaa doesn't use OpenGL so it's a fair court. Marcus is hereby pleased with gambas.