Author Topic: OpenGL Basics  (Read 9670 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Guest
OpenGL Basics
« on: June 02, 2016, 06:34:51 AM »
Hi Charles,

you mentioned that you will use OpenGL with Oxide which gave me the idea to learn a little bit about this library. There are a lot of samples provided with Oxygenbasic, but somehow I am lacking the basics. So I started with the NeHe OpenGl tutorials as they are very well documented.

Lesson1 creates an empty OpenGL window. F1 will switch into fullscreen or windowed mode and the application waits for ESC or closing the window to exit. It is a template for the next lessons. I ported the VC code to OxygenBasic and it will work like the original code. I am not sure about two places:

In about line 157 DEVMODE dmScreenSettings is used. Must the structure be set to zeros or is this done automatically by OxygenBasic?

I used the include statements which I found in examples\OpenGL\other\OpenglChildWins1.o2bas. But I see that there are some more include files for using OpenGL. Could I use e.g. OpenglSceneFrame.inc instead and could I save some lines in the main code then?

Roland

Unfortunately the code exceeds the maximum allowed length so I will only show the two relevant places here:

Code: OxygenBasic
  1. /*
  2.  *    This Code Was Created By Jeff Molofee 2000
  3.  *    A HUGE Thanks To Fredric Echols For Cleaning Up
  4.  *    And Optimizing This Code, Making It More Flexible!
  5.  *    If You've Found This Code Useful, Please Let Me Know.
  6. *    Visit My Site At nehe.gamedev.net
  7.  */
  8.  
  9. ' ported to OxygenBasic
  10.  
  11. #case capital
  12. '
  13. $ filename "lesson1.exe"
  14. '#include "$/inc/RTL32.inc"
  15. '#include "$/inc/RTL64.inc"
  16. #include "$/inc/MinWin.inc"
  17. '
  18. s=error()
  19. if s then
  20.   print s
  21.   end
  22. end if
  23.  
  24. #define WINGDIAPI
  25. #define APIENTRY
  26. #define const
  27. typedef word wchar_t
  28. typedef sys ptrdiff_t
  29. includepath +"$/inc/glo2/"
  30. library "opengl32.dll"
  31. include once "gl/gl.h"
  32. 'include once "gl/glext.h"
  33. library "glu32.dll"
  34. include once "gl/glu.h"
  35. include once "gl/wgl.inc"
  36. library ""
  37.  
  38. crlf=chr(13,10)
  39.  
  40. sys  hDC=null    // Private GDI Device Context
  41. sys  hRC=null    // Permanent Rendering Context
  42. sys  hWnd=null   // Holds Our Window Handle
  43. sys  hInstance   // Holds The Instance Of The Application
  44.  
  45. bool keys[256]        // Array Used For The Keyboard Routine
  46. bool active=TRUE      // Window Active Flag Set To TRUE By Default
  47. bool fullscreen=TRUE  // Fullscreen Flag Set To Fullscreen Mode By Default
  48.  
  49. ....
  50.  
  51. /*  This Code Creates Our OpenGL Window.  Parameters Are:         *
  52.  *  title      - Title To Appear At The Top Of The Window         *
  53.  *  width      - Width Of The GL Window Or Fullscreen Mode        *
  54.  *  height      - Height Of The GL Window Or Fullscreen Mode      *
  55.  *  bits      - Number Of Bits To Use For Color (8/16/24/32)      *
  56.  *  fullscreenflag  - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE)  */
  57.  
  58. function CreateGLWindow (string title, int width, int height, int bits, bool fullscreenflag) as bool
  59.   GLuint    PixelFormat      // Holds The Results After Searching For A Match
  60.   WNDCLASS  wc               // Windows Class Structure
  61.   DWORD     dwExStyle        // Window Extended Style
  62.   DWORD     dwStyle          // Window Style
  63.   RECT      WindowRect       // Grabs Rectangle Upper Left / Lower Right Values
  64.   WindowRect.left=0          // Set Left Value To 0
  65.   WindowRect.right=width     // Set Right Value To Requested Width
  66.   WindowRect.top=0           // Set Top Value To 0
  67.   WindowRect.bottom=height   // Set Bottom Value To Requested Height
  68.  
  69.   fullscreen=fullscreenflag  // Set The Global Fullscreen Flag
  70.  
  71.   hInstance        = GetModuleHandle(null)               // Grab An Instance For Our Window
  72.   wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC  // Redraw On Size, And Own DC For Window.
  73.   wc.lpfnWndProc   = @WndProc                            // WndProc Handles Messages
  74.   wc.cbClsExtra    = 0                                   // No Extra Window Data
  75.   wc.cbWndExtra    = 0                                   // No Extra Window Data
  76.   wc.hInstance     = hInstance                           // Set The Instance
  77.   wc.hIcon         = LoadIcon(null, IDI_WINLOGO)         // Load The Default Icon
  78.   wc.hCursor       = LoadCursor(null, IDC_ARROW)         // Load The Arrow Pointer
  79.   wc.hbrBackground = null                                // No Background Required For GL
  80.   wc.lpszMenuName  = null                                // We Don't Want A Menu
  81.  wc.lpszClassName = strptr "OpenGL"                     // Set The Class Name
  82.  
  83.   if not RegisterClass(&wc) then                  // Attempt To Register The Window Class  
  84.     MessageBox(null,"Failed To Register The Window Class.","ERROR",MB_OK | MB_ICONEXCLAMATION)
  85.     return FALSE                                  // Return FALSE
  86.   end if
  87.  
  88.   if fullscreen then                                      // Attempt Fullscreen Mode?  
  89.     DEVMODE dmScreenSettings                              // Device Mode
  90. '    memset(&dmScreenSettings,0,sizeof(dmScreenSettings))  // Makes Sure Memory's Cleared
  91.    dmScreenSettings.dmSize=sizeof(dmScreenSettings)      // Size Of The Devmode Structure
  92.     dmScreenSettings.dmPelsWidth  = width                 // Selected Screen Width
  93.     dmScreenSettings.dmPelsHeight = height                // Selected Screen Height
  94.     dmScreenSettings.dmBitsPerPel = bits                  // Selected Bits Per Pixel
  95.     dmScreenSettings.dmFields=DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT
  96.  
  97.  
  98. ...
  99.  

.

Arnold

  • Guest
Re: OpenGL Basics
« Reply #1 on: June 02, 2016, 06:51:56 AM »
This is the code of Lesson5. It is derived from Lesson1 with some additional code and it serves as a proof of concept. And again I wonder if I maybe could use different include files to save some lines in the main program?

Roland



.

Peter

  • Guest
Re: OpenGL Basics
« Reply #2 on: June 02, 2016, 09:43:56 AM »
Hello,
Code: [Select]
include "gl.inc"
window 640,480,1
setframes 60

FontLoad "glfonts/font15.bmp"
float rtri
float rquad

while key(27)=0
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) // Clear Screen And Depth Buffer
 
glLoadIdentity()                    // Reset The Current Modelview Matrix
glTranslatef(-1.5f,0.0f,-6.0f)      // Move Left 1.5 Units And Into The Screen 6.0
glRotatef(rtri,0.0f,1.0f,0.0f)      // Rotate The Triangle On The Y axis ( NEW )

glBegin(GL_TRIANGLES)               // Start Drawing A Triangle
glColor3f ( 1.0f, 0.0f, 0.0f)           // Red
glVertex3f( 0.0f, 1.0f, 0.0f)       // Top Of Triangle (Front)
glColor3f ( 0.0f, 1.0f, 0.0f)           // Green
glVertex3f(-1.0f,-1.0f, 1.0f)       // Left Of Triangle (Front)
glColor3f ( 0.0f, 0.0f, 1.0f)           // Blue
glVertex3f( 1.0f,-1.0f, 1.0f)       // Right Of Triangle (Front)
glColor3f ( 1.0f, 0.0f, 0.0f)           // Red
glVertex3f( 0.0f, 1.0f, 0.0f)       // Top Of Triangle (Right)
glColor3f ( 0.0f, 0.0f, 1.0f)           // Blue
glVertex3f( 1.0f,-1.0f, 1.0f)       // Left Of Triangle (Right)
glColor3f ( 0.0f, 1.0f, 0.0f)           // Green
glVertex3f( 1.0f,-1.0f,-1.0f)       // Right Of Triangle (Right)
glColor3f ( 1.0f, 0.0f, 0.0f)           // Red
glVertex3f( 0.0f, 1.0f, 0.0f)       // Top Of Triangle (Back)
glColor3f ( 0.0f, 1.0f, 0.0f)           // Green
glVertex3f( 1.0f,-1.0f,-1.0f)       // Left Of Triangle (Back)
glColor3f ( 0.0f, 0.0f, 1.0f)           // Blue
glVertex3f(-1.0f,-1.0f,-1.0f)       // Right Of Triangle (Back)
glColor3f ( 1.0f, 0.0f, 0.0f)           // Red
glVertex3f( 0.0f, 1.0f, 0.0f)       // Top Of Triangle (Left)
glColor3f ( 0.0f, 0.0f, 1.0f)           // Blue
glVertex3f(-1.0f,-1.0f,-1.0f)       // Left Of Triangle (Left)
glColor3f ( 0.0f, 1.0f, 0.0f)           // Green
glVertex3f(-1.0f,-1.0f, 1.0f)       // Right Of Triangle (Left)
glEnd()                             // Done Drawing The Pyramid

glLoadIdentity()                    // Reset The Current Modelview Matrix
glTranslatef(1.5,0.0,-7.0)          // Move Right 1.5 Units And Into The Screen 7.0
glRotatef(rquad,1.0,1.0,1.0)        // Rotate The Quad On The X axis ( NEW )
glBegin(GL_QUADS)                   // Draw A Quad
glColor3f ( 0.0f, 1.0f, 0.0f)           // Set The Color To Green
glVertex3f( 1.0f, 1.0f,-1.0f)       // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f)       // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f)       // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f)       // Bottom Right Of The Quad (Top)
glColor3f ( 1.0f, 0.5f, 0.0f)           // Set The Color To Orange
glVertex3f( 1.0f,-1.0f, 1.0f)       // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f)       // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f)       // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f)       // Bottom Right Of The Quad (Bottom)
glColor3f ( 1.0f, 0.0f, 0.0f)           // Set The Color To Red
glVertex3f( 1.0f, 1.0f, 1.0f)       // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f)       // Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 1.0f)       // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 1.0f)       // Bottom Right Of The Quad (Front)
glColor3f ( 1.0f, 1.0f, 0.0f)           // Set The Color To Yellow
glVertex3f( 1.0f,-1.0f,-1.0f)       // Top Right Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f)       // Top Left Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f)       // Bottom Left Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f)       // Bottom Right Of The Quad (Back)
glColor3f ( 0.0f, 0.0f, 1.0f)           // Set The Color To Blue
glVertex3f(-1.0f, 1.0f, 1.0f)       // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f)       // Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f)       // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f)       // Bottom Right Of The Quad (Left)
glColor3f ( 1.0f, 0.0f, 1.0f)           // Set The Color To Violet
glVertex3f( 1.0f, 1.0f,-1.0f)       // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f)       // Top Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f, 1.0f)       // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f)       // Bottom Right Of The Quad (Right)
glEnd()                             // Done Drawing The Quad

Text 1,-0.128,0.36,0.032,0.064,"LESSON 5"
rtri +=1.2       
rquad-=0.8           
SwapBuffer()
Wait 10
wend
winEnd




.

Arnold

  • Guest
Re: OpenGL Basics
« Reply #3 on: June 03, 2016, 12:05:24 AM »
Hi Peter,

thank you for this code which is very helpful for me. I will use gl.inc with my studies of the tutorials too to find out the limits.

Would it make sense to use the option of fullscreen mode? I must admit switching to fullscreen mode and vice versa works terribly slow with my old notebook. (But it works). If it is not useful I will remove this option from the template's code.

Roland

Peter

  • Guest
Re: OpenGL Basics
« Reply #4 on: June 03, 2016, 03:06:15 AM »
Quote
Would it make sense to use the option of fullscreen mode?

Hi Roland,

Why not!

A laptop has no resolution of 640 x 480.
You must take a resolution of 800 x 600 or 1024 x 768 or higher.

This is fullscreen :

include "gl.inc"
Window ( 1024, 768, 0 )

Demo:
Code: [Select]
include "gl.inc"
window 1024,768,0

fnt = FontLoad "glfonts/font21.bmp"
tex = TgaLoad  "tga/titus.tga"

float rx,ry,rz
float Mt_Diffuse [3] = {0.8, 0.8, 0.8, 1.0}
float Mt_Ambient [3] = {0.3, 0.3, 0.3, 1.0}
float Mt_Specular[3] = {1.0, 1.0, 1.0, 1.0}
float Mt_Shiness [0] = {50.0}

float Lt_Position[3] = {0.0, 0.0, 10.0, 1.0}
float Lt_Diffuse [3] = {0.5, 0.5, 0.5, 0.5 }   
float Lt_Ambient [3] = {0.2, 0.2, 0.2, 1.0 }
float Lt_Specular[3] = {1.0, 1.0, 1.0, 1.0 }

glMaterialfv GL_FRONT, GL_SPECULAR , &Mt_Specular
glMaterialfv GL_FRONT, GL_SHININESS, &Mt_Shiness
glMaterialfv GL_FRONT, GL_DIFFUSE  , &Mt_Diffuse
glMaterialfv GL_FRONT, GL_AMBIENT  , &Mt_Ambient

glLightfv GL_LIGHT1,GL_DIFFUSE , &Lt_Diffuse
glLightfv GL_LIGHT1,GL_AMBIENT , &Lt_Ambient
glLightfv GL_LIGHT1,GL_SPECULAR, &Lt_Specular
glLightfv GL_LIGHT1,GL_POSITION, &Lt_Position

while Key(27)=0
Cls 0, 0, 0
glLoadIdentity
glTranslatef 0.0, 0.0, -6.0

glRotatef rx,1.0, 0.0, 0.0
glRotatef ry,0.0, 1.0, 0.0
glRotatef rz,0.0, 0.0, 1.0

glEnable GL_LIGHT1
glEnable GL_LIGHTING

glEnable GL_TEXTURE_2D
glBindTexture GL_TEXTURE_2D, tex
glBegin  GL_QUADS
glColor3f    1.0, 1.0, 0.0
glNormal3f   0.0, 1.0, 0.0 
glTexCoord2f 1.0, 1.0 : glVertex3f  1.0, 1.0,-1.0 
glTexCoord2f 0.0, 1.0 : glVertex3f -1.0, 1.0,-1.0 
glTexCoord2f 0.0, 0.0 : glVertex3f -1.0, 1.0, 1.0 
glTexCoord2f 1.0, 0.0 : glVertex3f  1.0, 1.0, 1.0 
glColor3f    1.0, 0.0, 1.0
glNormal3f   0.0,-1.0, 0.0 
glTexCoord2f 1.0, 1.0 : glVertex3f  1.0,-1.0, 1.0 
glTexCoord2f 0.0, 1.0 : glVertex3f -1.0,-1.0, 1.0 
glTexCoord2f 0.0, 0.0 : glVertex3f -1.0,-1.0,-1.0 
glTexCoord2f 1.0, 0.0 : glVertex3f  1.0,-1.0,-1.0
glColor3f    0.0, 0.0, 1.0
glNormal3f   0.0, 0.0, 1.0 
glTexCoord2f 1.0, 1.0 : glVertex3f  1.0, 1.0, 1.0 
glTexCoord2f 0.0, 1.0 : glVertex3f -1.0, 1.0, 1.0 
glTexCoord2f 0.0, 0.0 : glVertex3f -1.0,-1.0, 1.0 
glTexCoord2f 1.0, 0.0 : glVertex3f  1.0,-1.0, 1.0 
glColor3f    0.0, 1.0, 1.0
glNormal3f   0.0, 0.0,-1.0 
glTexCoord2f 0.0, 0.0 : glVertex3f  1.0,-1.0,-1.0 
glTexCoord2f 1.0, 0.0 : glVertex3f -1.0,-1.0,-1.0 
glTexCoord2f 1.0, 1.0 : glVertex3f -1.0, 1.0,-1.0 
glTexCoord2f 0.0, 1.0 : glVertex3f  1.0, 1.0,-1.0 
glColor3f    0.0, 1.0, 0.0
glNormal3f  -1.0, 0.0, 0.0 
glTexCoord2f 1.0, 1.0 : glVertex3f -1.0, 1.0, 1.0 
glTexCoord2f 0.0, 1.0 : glVertex3f -1.0, 1.0,-1.0 
glTexCoord2f 0.0, 0.0 : glVertex3f -1.0,-1.0,-1.0 
glTexCoord2f 1.0, 0.0 : glVertex3f -1.0,-1.0, 1.0 
glColor3f    1.0, 0.0, 0.0
glNormal3f   1.0, 0.0, 0.0 
glTexCoord2f 1.0, 1.0 : glVertex3f 1.0, 1.0,-1.0 
glTexCoord2f 0.0, 1.0 : glVertex3f 1.0, 1.0, 1.0 
glTexCoord2f 0.0, 0.0 : glVertex3f 1.0,-1.0, 1.0 
glTexCoord2f 1.0, 0.0 : glVertex3f 1.0,-1.0,-1.0 
glEnd

glDisable GL_LIGHTING
Text fnt,-0.160,0.350,0.032,0.032,"LIGHT CUBE"

rx +=0.6
ry +=0.5
rz +=0.4

SwapBuffer
wend
WinEnd





.
« Last Edit: June 03, 2016, 03:26:28 AM by Peter »

Arnold

  • Guest
Re: OpenGL Basics
« Reply #5 on: June 03, 2016, 04:02:13 AM »
Hi Peter,

thank you. I see that Window (width, height, 0) is fullscreen and Window(width, height, 1) is windowed.

BTW: this program only works in fullscreen mode, whereas Lesson5 with gl.inc will work in a window and also in fullscreen. Does the difference result from the images?

Roland

Peter

  • Guest
Re: OpenGL Basics
« Reply #6 on: June 03, 2016, 02:30:43 PM »
Quote
BTW: this program only works in fullscreen mode

Hi Roland,

No,  this works also in windowed

another Demo:
Code: [Select]
include "gl.inc"
window 800,600,1 
setframes 60

FontLoad "glfonts/font16.bmp" 
FontLoad "glfonts/font25.bmp" 

t1=BmpLoad "bmp/titan.bmp"   
t2=BmpLoad "bmp/apple.bmp"   
t3=BmpLoad "bmp/bat.bmp"   

float xrot,yrot

Sub Quad2D(float x, y, z)
glPushMatrix
glTranslatef x, y, z
glBegin(gl_Quads)
  glTexCoord2f  0.0, 0.0 : glVertex3f -1.0,-1.0, 0.0
  glTexCoord2f  1.0, 0.0 : glVertex3f  1.0,-1.0, 0.0
  glTexCoord2f  1.0, 1.0 : glVertex3f  1.0, 1.0, 0.0
  glTexCoord2f  0.0, 1.0 : glVertex3f -1.0, 1.0, 0.0
glEnd
glPopMatrix
End Sub

Sub Quad3D(float x, y, z, an, rx, ry, rz)
glPushMatrix
glTranslatef x, y, z
glRotatef an, rx, ry, rz
glBegin(GL_QUADS)
glTexCoord2f 0.0, 0.0 : glVertex3f -1.0,-1.0, 1.0 
glTexCoord2f 1.0, 0.0 : glVertex3f  1.0,-1.0, 1.0 
glTexCoord2f 1.0, 1.0 : glVertex3f  1.0, 1.0, 1.0 
glTexCoord2f 0.0, 1.0 : glVertex3f -1.0, 1.0, 1.0 

glTexCoord2f 1.0, 0.0 : glVertex3f -1.0,-1.0,-1.0 
glTexCoord2f 1.0, 1.0 : glVertex3f -1.0, 1.0,-1.0 
glTexCoord2f 0.0, 1.0 : glVertex3f  1.0, 1.0,-1.0 
glTexCoord2f 0.0, 0.0 : glVertex3f  1.0,-1.0,-1.0 

glTexCoord2f 0.0, 1.0 : glVertex3f -1.0, 1.0,-1.0 
glTexCoord2f 0.0, 0.0 : glVertex3f -1.0, 1.0, 1.0 
glTexCoord2f 1.0, 0.0 : glVertex3f  1.0, 1.0, 1.0 
glTexCoord2f 1.0, 1.0 : glVertex3f  1.0, 1.0,-1.0 

glTexCoord2f 1.0, 1.0 : glVertex3f -1.0,-1.0,-1.0 
glTexCoord2f 0.0, 1.0 : glVertex3f  1.0,-1.0,-1.0 
glTexCoord2f 0.0, 0.0 : glVertex3f  1.0,-1.0, 1.0 
glTexCoord2f 1.0, 0.0 : glVertex3f -1.0,-1.0, 1.0 

glTexCoord2f 1.0, 0.0 : glVertex3f  1.0,-1.0,-1.0 
glTexCoord2f 1.0, 1.0 : glVertex3f  1.0, 1.0,-1.0 
glTexCoord2f 0.0, 1.0 : glVertex3f  1.0, 1.0, 1.0 
glTexCoord2f 0.0, 0.0 : glVertex3f  1.0,-1.0, 1.0 

glTexCoord2f 0.0, 0.0 : glVertex3f -1.0,-1.0,-1.0 
glTexCoord2f 1.0, 0.0 : glVertex3f -1.0,-1.0, 1.0 
glTexCoord2f 1.0, 1.0 : glVertex3f -1.0, 1.0, 1.0 
glTexCoord2f 0.0, 1.0 : glVertex3f -1.0, 1.0,-1.0 
glEnd
glPopMatrix
End Sub

while Key(27)=0
Cls 0.0, 0.0, 0.0
glLoadIdentity
glTranslatef 0.0, 0.0, -10.0
glBegin(gl_Quads)
  glColor3f   1.0, 0.0, 1.0
  glVertex3f -1.0,-1.0, 0.0
  glColor3f   1.0, 1.0, 1.0
  glVertex3f  1.0,-1.0, 0.0
  glVertex3f  1.0, 1.0, 0.0
  glColor3f   1.0, 1.0, 1.0
  glVertex3f -1.0, 1.0, 0.0
glEnd

glEnable GL_TEXTURE_2D
glBindTexture GL_TEXTURE_2D, t2
Quad2D -3, 0, -1
glBindTexture GL_TEXTURE_2D, t3
Quad2D  3, 0, -1

glBindTexture GL_TEXTURE_2D, t1
Quad3D(-6, 4,-6, xrot,1,0,1)
Quad3D( 6,-4,-6, yrot,0,1,0)

glColor3ub 255,128,0
Text 1, -0.064,0.340, 0.032,0.064, "HELLO " 
glColor3ub 255,255,0
Text 1, -0.064,0.288, 0.032,0.064, " EAT  " 
glColor3ub 255,255,255
Text 1, -0.064, 0.240,0.032,0.064, "APPLES" 
Text 2, -0.384,-0.400,0.064,0.064, "opengl demo "

xrot +=1.0
yrot +=1.0

SwapBuffer
wend
winEnd

maximize this demo!


.

Charles Pegge

  • Guest
Re: OpenGL Basics
« Reply #7 on: June 03, 2016, 11:40:46 PM »
Hi Roland,

Unlike C, OxygenBasic initializes all local variables to nulls. So you can omit this step when porting script from C.

OpenglSceneFrame.inc has many useful features, including Anti-Alias screen mode selection, low CPU usage, jpeg snap-shot, and object picking, which you are likely to need later on. It took me years to get it right, including substantial help from Mike Lobanovsky.

Unfortunately Opengl and 3d graphics are never going to be simple, but most programs require a similar base system.
« Last Edit: June 03, 2016, 11:48:19 PM by Charles Pegge »

Arnold

  • Guest
Re: OpenGL Basics
« Reply #8 on: June 05, 2016, 12:02:10 AM »
Hi Peter,

this might be interesting. I tried several dimensions with my notebook and had these results with the above mentioned light cube demo:

include "gl.inc"

'window 720,480,1   'fails
'window 960,480,1   'fails
'window 1024,768,1  'fails
'window 1280,760,1  'fails

window 640,480,1  'success
'window 800,600,1  'success

...

I do not know the reason for the differences. Maybe my graphics card lacks some capabilities or I use the wrong settings. It is only a reminder that these differences can happen.

Roland

Arnold

  • Guest
Re: OpenGL Basics
« Reply #9 on: June 05, 2016, 12:36:53 AM »
Hi Charles,

yes, OpenglSceneFrame.inc will be too much oversized for my purpose of porting the NeHe tutorials to OxygenBasic. But I also found /inc/WinUtil.inc with the Opengl option and I am experimenting with it. If I manage to combine the function MainWindow properly with the lesson1 framework this will save a lot of lines in the main code.

Roland

Peter

  • Guest
Re: OpenGL Basics
« Reply #10 on: June 05, 2016, 04:38:32 AM »
Quote
this might be interesting. I tried several dimensions with my notebook and had these results with the above mentioned light cube demo:

Hi Roland

If you drive OpenGl3D, the ratio must match.
If you drive Opengl2D, you must not be careful.

Demo OpenGl 2D:
Code: [Select]
include "ogl.inc"
window 720,720,1
enable GL_POINT_SMOOTH

single x=300, y=200, e=0.05, dx, dy, tx, ty
sys i, j

while key(27)=0
cls 0,0,0
for j=0 to scrH() step 40
for i=0 to scrW() step 40
    color 255, 255, 255, 128
    circle i, j, 40, 1
next
next

tx = xMouse()-10
dx = tx-x
if abs(dx) >1
   x = x+dx*e
end if
ty = yMouse()-10
dy = ty-y
if abs(dy) >1
   y = y+dy*e
end if
color 255,194,183,200
circle x, y, 40, 2
color 255,224,203,200
circle x, y, 30, 2
color 255,244,255,200
fillcircle x, y, 20

color 255,44,23,128
fillcircle scrW()/2, scrH()/2,75
color 23,44,255,128
fillcircle scrW()/2, scrH()/2-150,75
color 23,255,44,128
fillcircle scrW()/2, scrH()/2+150,75
setcaption "frames: " + getframes
redraw
wait 10
wend
winEnd





.

Arnold

  • Guest
Re: OpenGL Basics
« Reply #11 on: October 20, 2016, 06:03:41 AM »
Hi Charles,

as always I will need some help.
By exploring consoleG.inc I found that this is in fact a complete library to develop all kind of OpenGl applications and as it is included in GraphUtil.inc there are still some more possibilities provided.

I tried out this small example:
Code: OxygenBasic
  1. $ Title "OpenGl Framework"
  2.  
  3. % width     300
  4. % height    100
  5.  
  6. 'screen dimensions
  7. '% width = GetSystemMetrics (SM_CXSCREEN)
  8. '% height = GetSystemMetrics (SM_CYSCREEN)
  9.  
  10. '% ExplicitMain
  11.                                 ,
  12. '% ScaleUp
  13. '% PlaceCentral
  14. '% AnchorCentral
  15. '% NoEscape
  16.  
  17. 'include "$/inc/consoleG.inc"
  18. includepath "$/inc/"
  19. include "GraphUtil.inc"
  20.  
  21. sub main()
  22.   print "Hello World from OpenGl"
  23. end sub
  24.  
  25. 'BeginScript 'does nothing?
  26. EndScript  'starts MainWindow width, height, WS_OVERLAPPEDWINDOW
  27.  
  28. 'MainWindow width, height, WS_POPUP  'Alt-F4 or Esc
  29.  

What does %% ExplicitMain and %% ScaleUp do? Can %% PlaceCentral and %% AchorCentral be used together? What is the purpose of BeginScript?

The problem with this example is the changing size of the font. Can I influence this or can I add additional code for using a fixed sized font? You have already provided:
BuildFont, print3D, gprint in OpenglSceneFrame.inc
print, output in ConsoleG.inc

If I cannot use these functions I would like to use and add this code from NeHe lesson 13 somehow:
Code: [Select]
GLuint  base                // Base Display List For The Font Set

sub BuildFont()                                        // Build Our Bitmap Font
    sys font                                           // Windows Font ID
    sys oldfont                                        // Used For Good House Keeping

    base = glGenLists(255)                          // Storage For Characters

    font = CreateFont(    -24,                         // Height Of Font
                        0,                             // Width Of Font
                        0,                             // Angle Of Escapement
                        0,                             // Orientation Angle
                        FW_BOLD,                       // Font Weight
                        FALSE,                         // Italic
                        FALSE,                         // Underline
                        FALSE,                         // Strikeout
                        ANSI_CHARSET,                  // Character Set Identifier
                        OUT_TT_PRECIS,                 // Output Precision
                        CLIP_DEFAULT_PRECIS,           // Clipping Precision
                        ANTIALIASED_QUALITY,           // Output Quality
                        FF_DONTCARE|DEFAULT_PITCH,     // Family And Pitch
                        "Courier New")                 // Font Name

    oldfont = SelectObject(wglGetCurrentDC(), font)           // Selects The Font We Want
    wglUseFontBitmapsA(wglGetCurrentDC(), 32, 255-32, base)   // Builds Characters Starting At Character 32
    SelectObject(wglGetCurrentDC(), oldfont)                  // Selects The Font We Want
    DeleteObject(font)                                        // Delete The Font
end sub

sub KillFont()                                          // Delete The Font List
    glDeleteLists(base, 255)                            // Delete All Characters
end sub

sub glPrint(char *text)                           // Custom GL "Print" Routine
   if len(text) = 0 then return                        // If There's No Text

   glPushAttrib(GL_LIST_BIT)                           // Pushes The Display List Bits
   glListBase(base - 32)                               // Sets The Base Character to 32
   glCallLists(len(text), GL_UNSIGNED_BYTE, text)      // Draws The Display List Text
   glPopAttrib()                                       // Pops The Display List Bits
end sub

What would be the best way to add some more functionality? E.g. I would like to use a F(num) key to change the Windows style and toggle (ShowWindow) between Fullscreen and original size of the window.

Roland

Charles Pegge

  • Guest
Re: OpenGL Basics
« Reply #12 on: October 21, 2016, 12:32:17 AM »
Hi Roland,

%PlaceCentral and %AnchorCentral locates 0,0 at the centre of the window, otherwise 0,0 locates to the top left margin.

%ScaleUp enlarges the image as the window is stretched, otherwise enlargement is limited.

%ExplicitMain requires your code to call MainWindow (via Endscript). This ensures that the Program traverses any global variable initializations and any classes you may define,

BeginScript is reserved for future functionality

I have not explored changing the windows style, but you can do so by overriding EndScript:
currently:   MainWindow width,height,WS_OVERLAPPEDWINDOW

Here is an example of Text tranformations (with F4 function key)

Code: [Select]
  % Title "ConsoleG Demo"
  % Animated
 '% ScaleUp
  % PlaceCentral
  % AnchorCentral
  include "$\inc\ConsoleG.inc"

  BeginScript

  sub main
  ========

  pushstate
  scale 2,3
  color 1,.5,0,1
  printl "Hello"
  scale 2, 1.25
  if key(VK_F4) then rotatez -30
  printl "World!"
  popstate

  end sub 'main

  EndScript


.
« Last Edit: October 21, 2016, 12:50:13 AM by Charles Pegge »

Arnold

  • Guest
Re: OpenGL Basics
« Reply #13 on: October 21, 2016, 03:43:29 PM »
Hi Charles,

based on your example it is already possible to experiment a little bit with the World. I added some options for the arrow keys and for n, m:
Code: [Select]
  % Title "ConsoleG Demo"
  % Animated
 '% ScaleUp
  % PlaceCentral
  % AnchorCentral
  include "$\inc\ConsoleG.inc"
 
  'Keys: Esc, arrow-keys, n,m, F4

  BeginScript

  sub main
  ========
  static int z, y, x
 
  pushstate
  scale 2, 3
  color 1,.5,0,1
  print "Hello "

  scale 2, 1.25
  if key(VK_UP) then x-=2
  if key(VK_DOWN) then x+=2
  if key(VK_RIGHT) then y-=2
  if key(VK_LEFT) then y+=2
  if key(77) then z-=2      'm,M
  if key(78) then z+=2      'n,N
  if key(VK_F4) then x=0 : y=0 : z=0

  rotatex x
  rotatey y
  rotatez z
 
  print "World!"
  popstate

  end sub 'main

  EndScript

Some keys can be used simultaneously. Unfortunately I have not yet found out how to use the timer functions correctly which you already generated. Is there a simple way to start and stop moving the World?

Roland

Charles Pegge

  • Guest
Re: OpenGL Basics
« Reply #14 on: October 23, 2016, 12:28:09 PM »
Hi Roland,

There are some deep issues concerning reliable timing, which I am attempting to address right now. I don't think the windows timer should be used at all, nor should one rely on the 60hz/30hz frame rate.

Multimedia time is a distinct possibility

https://msdn.microsoft.com/en-us/library/windows/desktop/dd743612(v=vs.85).aspx

When you say 'stop the world' do you mean freeze animation or do you mean shut down?