opengl_20programming
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
opengl_20programming [2018/03/31 13:19] – external edit 127.0.0.1 | opengl_20programming [2024/01/05 00:22] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 5: | Line 5: | ||
\\ This article explains how to interface with the **OpenGL** 3D graphics standard from //BBC BASIC for Windows//. It is //not// an OpenGL tutorial; you will need to refer to the [[http:// | \\ This article explains how to interface with the **OpenGL** 3D graphics standard from //BBC BASIC for Windows//. It is //not// an OpenGL tutorial; you will need to refer to the [[http:// | ||
===== Declarations ===== | ===== Declarations ===== | ||
- | \\ In order to use OpenGL from BBC BASIC you will need to access the **OPENGL32.DLL** library supplied with Windows. You should include code similar to the following in your program (e.g. in an initialisation routine):\\ \\ | + | \\ In order to use OpenGL from BBC BASIC you will need to access the **OPENGL32.DLL** library supplied with Windows. You should include code similar to the following in your program (e.g. in an initialisation routine): |
+ | |||
+ | <code bb4w> | ||
SYS " | SYS " | ||
SYS " | SYS " | ||
Line 11: | Line 13: | ||
SYS " | SYS " | ||
SYS " | SYS " | ||
- | Here only one standard OpenGL function **glClear** has been listed, but you should include every function that your program requires (the full list runs to more than 100 functions so you probably won't want to include every one). In this example each function address is assigned to a variable whose name is the function name enclosed in 'back quotes' | + | </ |
+ | |||
+ | Here only one standard OpenGL function **glClear** has been listed, but you should include every function that your program requires (the full list runs to more than 100 functions so you probably won't want to include every one). In this example each function address is assigned to a variable whose name is the function name enclosed in 'back quotes' | ||
+ | |||
+ | <code bb4w> | ||
SYS " | SYS " | ||
SYS " | SYS " | ||
+ | </ | ||
+ | |||
Again only one function has been listed in this example; there are nearly 40 in all.\\ \\ Note that in many cases there are two alternative functions, one accepting // | Again only one function has been listed in this example; there are nearly 40 in all.\\ \\ Note that in many cases there are two alternative functions, one accepting // | ||
===== Initialisation ===== | ===== Initialisation ===== | ||
- | \\ Once you have declared the OpenGL functions you need to use, you can start writing your program proper. The first step is to set up your main output window to the size you require. You can do that using any of the normal methods provided in BBC BASIC: the **MODE** statement, the **VDU 23,22** command or via the Windows API using **SYS**.\\ \\ The next step is to select the wanted **pixel format**, for example the number of bits-per-pixel for the colour and for the //depth map//. This is a two-stage process: you first request a // | + | \\ Once you have declared the OpenGL functions you need to use, you can start writing your program proper. The first step is to set up your main output window to the size you require. You can do that using any of the normal methods provided in BBC BASIC: the **MODE** statement, the **VDU 23,22** command or via the Windows API using **SYS**.\\ \\ The next step is to select the wanted **pixel format**, for example the number of bits-per-pixel for the colour and for the //depth map//. This is a two-stage process: you first request a // |
+ | |||
+ | <code bb4w> | ||
_PFD_MAIN_PLANE = 0 | _PFD_MAIN_PLANE = 0 | ||
_PFD_TYPE_RGBA = 0 | _PFD_TYPE_RGBA = 0 | ||
Line 48: | Line 58: | ||
SYS `wglCreateContext`, | SYS `wglCreateContext`, | ||
SYS `wglMakeCurrent`, | SYS `wglMakeCurrent`, | ||
+ | </ | ||
+ | |||
This code requests an //indexed// (paletted) colour selection with 8 bits per pixel, i.e. 256 different colours in all. Alternatively it could have requested, for example, a pixel type of **_PFD_TYPE_RGBA** and a **pfd.cColorBits& | This code requests an //indexed// (paletted) colour selection with 8 bits per pixel, i.e. 256 different colours in all. Alternatively it could have requested, for example, a pixel type of **_PFD_TYPE_RGBA** and a **pfd.cColorBits& | ||
===== Cleanup ===== | ===== Cleanup ===== | ||
- | \\ When you exit your program you should delete the **rendering context** and **device context** created above. You can do that using code similar to the following:\\ \\ | + | \\ When you exit your program you should delete the **rendering context** and **device context** created above. You can do that using code similar to the following: |
+ | |||
+ | <code bb4w> | ||
DEF PROCcleanup | DEF PROCcleanup | ||
ghRC% += 0 : IF ghRC% SYS `wglDeleteContext`, | ghRC% += 0 : IF ghRC% SYS `wglDeleteContext`, | ||
ghDC% += 0 : IF ghDC% SYS " | ghDC% += 0 : IF ghDC% SYS " | ||
ENDPROC | ENDPROC | ||
- | You should place this procedure out of the way, for example at the end of your program after the **END** statement.\\ \\ You will want to incorporate **ON ERROR** and **ON CLOSE** statements to ensure that PROCcleanup is executed even if your program is terminated unexpectedly: | + | </ |
+ | |||
+ | You should place this procedure out of the way, for example at the end of your program after the **END** statement.\\ \\ You will want to incorporate **ON ERROR** and **ON CLOSE** statements to ensure that PROCcleanup is executed even if your program is terminated unexpectedly: | ||
+ | |||
+ | <code bb4w> | ||
ON CLOSE PROCcleanup : QUIT | ON CLOSE PROCcleanup : QUIT | ||
ON ERROR PROCcleanup : SYS " | ON ERROR PROCcleanup : SYS " | ||
- | \\ | + | </ |
===== OpenGL code ===== | ===== OpenGL code ===== | ||
- | \\ Once you have executed the Windows and BBC BASIC specific code above, you can commence the OpenGL code proper. The main considerations in making OpenGL calls from //BBC BASIC for Windows// are related to passing floating-point values, since normally only integer values can be passed //by value// using the **SYS** statement.\\ \\ To pass a single-precision (32-bit) floating-point value use the **FN_f4** function in the [[http:// | + | \\ Once you have executed the Windows and BBC BASIC specific code above, you can commence the OpenGL code proper. The main considerations in making OpenGL calls from //BBC BASIC for Windows// are related to passing floating-point values, since normally only integer values can be passed //by value// using the **SYS** statement.\\ \\ To pass a single-precision (32-bit) floating-point value use the **FN_f4** function in the [[http:// |
+ | |||
+ | <code bb4w> | ||
SYS `glTranslatef`, | SYS `glTranslatef`, | ||
- | To pass a double-precision (64-bit) floating-point value use the **FN_dl** and **FN_dh** functions listed at the end of this article. For example when calling the **glTranslated** function, which takes three double-precision floating-point values, you would use code similar to the following:\\ \\ | + | </ |
+ | |||
+ | To pass a double-precision (64-bit) floating-point value use the **FN_dl** and **FN_dh** functions listed at the end of this article. For example when calling the **glTranslated** function, which takes three double-precision floating-point values, you would use code similar to the following: | ||
+ | |||
+ | <code bb4w> | ||
SYS `glTranslated`, | SYS `glTranslated`, | ||
- | Note particularly that each //double// parameter must be passed as a pair of values, the first using **FN_dl** and the second using **FN_dh**. When there is a choice, passing single-precision values will usually be easier.\\ \\ Some OpenGL functions require an array of values. In this case it is easier to pass a double-precision array, since BBC BASIC for Windows supports this data type natively (in ***FLOAT64** mode). For example to call **glLoadMatrixd** you would use code similar to the following:\\ \\ | + | </ |
- | DIM matrix(3, | + | |
- | matrix() = a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 | + | Note particularly that each //double// parameter must be passed as a pair of values, the first using **FN_dl** and the second using **FN_dh**. When there is a choice, passing single-precision values will usually be easier.\\ \\ Some OpenGL functions require an array of values. In this case it is easier to pass a double-precision array, since BBC BASIC for Windows supports this data type natively (by using the # suffix). For example to call **glLoadMatrixd** you would use code similar to the following: |
- | matrix() *= 1.0 | + | |
- | SYS `glLoadMatrixd`, | + | <code bb4w> |
- | Note that for this to work your program must be in ***FLOAT64** mode.\\ \\ | + | DIM matrix#(3,3) |
+ | matrix#() = a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 | ||
+ | matrix#() *= 1.0 | ||
+ | SYS `glLoadMatrixd`, | ||
+ | </ | ||
+ | |||
===== Rendering ===== | ===== Rendering ===== | ||
- | \\ OpenGL under Windows uses a // | + | \\ OpenGL under Windows uses a // |
+ | |||
+ | <code bb4w> | ||
_GL_DEPTH_BUFFER_BIT = &0100 | _GL_DEPTH_BUFFER_BIT = &0100 | ||
_GL_COLOR_BUFFER_BIT = &4000 | _GL_COLOR_BUFFER_BIT = &4000 | ||
Line 83: | Line 116: | ||
SYS " | SYS " | ||
UNTIL FALSE | UNTIL FALSE | ||
- | \\ | + | </ |
===== Support functions ===== | ===== Support functions ===== | ||
- | \\ The functions listed below are used when passing double-precision floating-point values:\\ \\ | + | \\ The functions listed below are used when passing double-precision floating-point values: |
+ | |||
+ | <code bb4w> | ||
REM Convert to 8-byte double (low 4 bytes) | REM Convert to 8-byte double (low 4 bytes) | ||
DEF FN_dl(A#) | DEF FN_dl(A#) | ||
Line 95: | Line 131: | ||
A#*=1.0# | A#*=1.0# | ||
=!(^A#+4) | =!(^A#+4) | ||
+ | </ |
opengl_20programming.1522502370.txt.gz · Last modified: 2024/01/05 00:17 (external edit)