04-03-2023, 09:02 PM
(This post was last modified: 04-14-2023, 02:55 PM by Petr.
Edit Reason: BYVAL....
)
Here is Display List Demo.
It speeds up work considerably. For example, in this program, in the BuildLists Sub, a cube is classically created using the glVertex3f and _glCoord2f commands. So it is then stored without being displayed in memory. Then, when needed, it is called into place, covered with (any) texture, and displayed. OpenGL doesn't have to calculate anything anymore, it just takes it and slaps it in place. This is a significant acceleration of work. Being able to finish this was an experience.
I ran into a problem that I ended up having to solve by using a different command. This is the _glColor3fv command. Reads three color values from the array at once. So. I am asking people who know C to take a look at this source code, the original initialization in the C language is also commented there, it is lines 20 to 43 and 172 to 177. I think I did what I could, but at end I use the classic field and the _glColor3f command, but since these mismatches will increase, I am interested in how to go about it.
Thank you.
The program requires an attached freely available texture to run.
Texture:
EDIT:
No one helped, I helped myself. Interestingly. The solution always comes to me only after I turn off the computer.
The source code of the same program is attached. This time it already uses the statement, I asked about - _glColor3fv. The solution can be seen on lines 141 to 143 and also 152 - 154. If someone can think of a more elegant solution, I'd be happy to learn.
It speeds up work considerably. For example, in this program, in the BuildLists Sub, a cube is classically created using the glVertex3f and _glCoord2f commands. So it is then stored without being displayed in memory. Then, when needed, it is called into place, covered with (any) texture, and displayed. OpenGL doesn't have to calculate anything anymore, it just takes it and slaps it in place. This is a significant acceleration of work. Being able to finish this was an experience.
I ran into a problem that I ended up having to solve by using a different command. This is the _glColor3fv command. Reads three color values from the array at once. So. I am asking people who know C to take a look at this source code, the original initialization in the C language is also commented there, it is lines 20 to 43 and 172 to 177. I think I did what I could, but at end I use the classic field and the _glColor3f command, but since these mismatches will increase, I am interested in how to go about it.
Thank you.
The program requires an attached freely available texture to run.
Code: (Select All)
_Title "Display List demo"
Declare CustomType Library
Sub gluBuild2DMipmaps (BYVAL Target As _Unsigned Long,BYVAL iFormat As Long,BYVAL Wdth As Long,BYVAL Hght As Long,BYVAL format As _Unsigned Long,BYVAL typ As _Unsigned Long,BYVAL dat As _Offset)
End Declare
$If WIN Then
$End If
Type GL_Loader ' array for loading textures
PointerGL As Long
TextureName As String
Filtering As _Unsigned _Byte
End Type
ReDim Shared GLL(0) As GL_Loader, t As Long
Dim Shared GL_InitInfo As _Byte ' is need for OpenGL init
Dim Shared ExitSignal As _Byte ' is need for correct OpenGL exit when program end
'------------------------------------------------------------------------
Type RGB
As Single R, G, B
End Type
' this is my ask -------------------------------------
'in C it was:
'static GLfloat boxcol[5][3]=
'{1.0f,0.0f,0.0f},{1.0f,0.5f,0.0f},{1.0f,1.0f,0.0f},{0.0f,1.0f,0.0f},{0.0f,1.0f,1.0f}
'};
'static GLfloat topcol[5][3]=
'{0.5f,0.0f,0.0f},{0.5f,0.25f,0.0f},{0.5f,0.5f,0.0f},{0.0f,0.5f,0.0f},{0.0f,0.5f,0.5f}
'};
'HOW CALL 3 values once in QB64 using Offset????
'-------------------------------------------------------
Dim Shared BoxCol(5) As RGB 'for cube colors
Restore BXC
For Row = 1 To 5
For Column = 1 To 3
Read Value
Select Case Column
Case 1: BoxCol(Row).R = Value
Case 2: BoxCol(Row).G = Value
Case 3: BoxCol(Row).B = Value
End Select
Next
Next
'colors for cube
BXC:
Data 1.0,0.0,0.0
Data 1.0,0.5,0.0
Data 1.0,1.0,0.0
Data 0.0,1.0,0.0
Data 0.0,1.0,1.0
Dim Shared TopCol(5) As RGB
Restore TPC
For Row = 1 To 5
For Column = 1 To 3
Read Value
Select Case Column
Case 1: TopCol(Row).R = Value
Case 2: TopCol(Row).G = Value
Case 3: TopCol(Row).B = Value
End Select
Next
Next
'colors for lid
TPC: '
Data 0.5,0.0,0.0
Data 0.5,0.25,0.0
Data 0.5,0.5,0.0
Data 0.0,0.5,0.0
Data 0.0,0.5,0.5
Dim Shared As _Unsigned Long Box, Top
Dim Shared BoxTexture(1) As _Unsigned Long
Dim Shared As Single Yrot, Xrot
Screen _NewImage(1024, 768, 32)
Print "What you see on the screen is just 1 cube copyed as more cubes and colored and textured with _glGenLists, _glNewList and _glCallList. Use arrows."
Do
i& = _KeyHit
Select Case i&
Case 20480 'arrow up
Xrot = Xrot + .4
Case 18432 'arrow down
Xrot = Xrot - .4
Case 19200 'arrow left
Yrot = Yrot - .4
Case 19712 'arrow right
Yrot = Yrot + .4
End Select
If ExitSignal Then System
_Limit 50
Loop
Sub _GL ()
' Static Xrot, Yrot
Init2
GL_Init
_glClear _GL_COLOR_BUFFER_BIT And _GL_DEPTH_BUFFER_BIT 'Clear screen and depth buffer
_glMatrixMode _GL_PROJECTION ' Set projection matrix - TRY comment this five rows and then run it. Black screen occur. For view just something then must be depth set to -1 (Z parameter in _glTranslateF)
_glLoadIdentity ' Reset matrix
_gluPerspective 45.0F, _Width / _Height, 0.1F, 100.0F ' Perspective calculation
_glMatrixMode _GL_MODELVIEW ' set modelview matrix
_glBindTexture _GL_TEXTURE_2D, BoxTexture(1)
For yloop = 1 To 6
'The following line moves the origin of the coordinates to the given point on the screen. At first glance it's a bit confusing.
' On the x-axis: We move to the right by 1.4 units so that the pyramid is in the center of the screen. Then we multiply the xloop
'variable by 2.8 and add 1.4. (We multiply by 2.8 so the cubes are not on top of each other. 2.8 is approximately their width when
' rotated 45 degrees.) Finally, we subtract yloop*1.4. This will move them to the left depending on which row we are on. If we didn't
' move them, they would line up on the left side. (And they don't look like a pyramid.)
'On the y-axis: We subtract the variable yloop from six, otherwise the pyramid would be created upside down. Then we multiply the
'result by 2.4. Otherwise the cubes will be on top of each other on the Y axis. (2.4 is roughly equal to the height of the cube).
'Then we subtract 7 so the pyramid starts at the bottom of the screen and is built from the bottom up.
'On the z-axis: We move 20 units inward. So the pyramid just fits on the screen.
xloop = 1
Do Until xloop = yloop 'Dále máme vnořený cyklus s proměnnou xloop. Je použitý pro pozici krychlí na ose x. Jejich počet závisí na tom, ve kterém řádku se nacházejí. Pokud se nacházíme v horním řádku vykreslíme jednu, ve druhém dvě, atd.
_glLoadIdentity
'// Pozice krychle na obrazovce
_glTranslatef 1.4 + (xloop * 2.8F) - (yloop * 1.4F), ((6.5F - yloop) * 2.4F) - 7.0F, -20.0F
_glRotatef 45.0 - (2.0F * yloop) + Xrot, 1.0F, 0.0F, 0.0F
_glRotatef 45.0 + Yrot, 0.0F, 1.0F, 0.0F
'We choose the color of the box (light). Note that we use glColor3fv(). - I NOT BECAUSE IT WORK NOT CORRECT.
' This function selects all three values (red, green, blue) at once to set the color. In this case, we find it in
' the boxcol field with index yloop-1. This ensures a different color for each row of the pyramid. If we used xloop-1 we would get the same colors for each column.
_glColor3f BoxCol(yloop - 1).R, BoxCol(yloop - 1).G, BoxCol(yloop - 1).B 'this work correctly
_glCallList (Top) 'draw it
_glColor3f TopCol(yloop - 1).R, TopCol(yloop - 1).G, TopCol(yloop - 1).B
' _glColor3fv _Offset(TopCol(i, yloop - 1)) '(1, yloop - 1)) ', TopCol(YLoop - 1, 2), TopCol(YLoop - 1, 3) '(yloop-1,1), topcol(yloop-1,2), topcol(yloop -1, 3) 'for color
'original C call: glColor3fv(boxcol[yloop-1])
' _glColor3fv hard uncompatible statement or i dont know how use it. He read 3 color values once from array, defined in c as [5] [3] (5 rows, 3 color values (R,G,B) on 1 row. All outputs was bad
' it try load color array as Topcol(3,5), then as Topcol(5,3), then as Topcol(15) then as _MEM (single = 4 * 15) - all that return black out (black screen) or white screen or program crashed, and if
' are colors created, are different than awaited colors. In end i use _glColor3f, because this..... so this.... i dont know how correct use it, ALSO if original C program call it here as TopCol [y-1]
' without second parameter...
' _glBindTexture _GL_TEXTURE_2D, BoxTexture(1) ' for case you load second texture, you can try use it as box lid
_glCallList (Box) ';// Vykreslení
xloop = xloop + 1
'_glBindTexture _GL_TEXTURE_2D, BoxTexture(0) 'if you use 2 textures, set previous for box back
Loop
Next
If _Exit Then
' DeleteTexture t 'if program end, first free texture from memory, then exit from GL and return to main loop
KillAllTextures
_glClear _GL_COLOR_BUFFER_BIT
ExitSignal = Not 0
Exit Sub
End If
End Sub
Sub BuildLists 'this sub build 1 box + 1 lid and save it as glList
'glVoid BuildLists
Box = _glGenLists(2)
'We will create the first sheet. We have already taken up space for two sheets and know that the box points to the beginning of the prepared memory. We will use the glNewList() command.
'The first box parameter says that we want to store the sheet in the memory it points to.
'The second GL_COMPILE parameter says that we want to pre-create the sheet in memory so that it doesn't have to be generated and recomputed every time it's rendered. GL_COMPILE is the same as programming.
'If you write a program and load it into your compiler, you have to compile it every time you want to run it. But if it is compiled into an .exe file, all that needs to be done to run it is
'mouse click on this .exe file and run it. Without compilation of course. Whatever OpenGL compiles into the display list can be used without any further recomputation. Rendering speeds up.
Top = Box + 1 '
_glNewList Top, _GL_COMPILE 'compile display list - lid
_glBegin _GL_QUADS
'ceiling
_glNormal3f 0.0F, 1.0F, 0.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f -1.0F, 1.0F, -1.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f -1.0F, 1.0F, 1.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f 1.0F, 1.0F, 1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f 1.0F, 1.0F, -1.0F
_glEnd
_glEndList
_glNewList Box, _GL_COMPILE 'compile display list - box
_glBegin _GL_QUADS
'bottom
_glNormal3f 0.0F, -1.0F, 0.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f -1.0F, -1.0F, -1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f 1.0F, -1.0F, -1.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f 1.0F, -1.0F, 1.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f -1.0F, -1.0F, 1.0F
'front
_glNormal3f 0.0F, 0.0F, 1.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f -1.0F, -1.0F, 1.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f 1.0F, -1.0F, 1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f 1.0F, 1.0F, 1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f -1.0F, 1.0F, 1.0F
'rear
_glNormal3f 0.0F, 0.0F, -1.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f -1.0F, -1.0F, -1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f -1.0F, 1.0F, -1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f 1.0F, 1.0F, -1.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f 1.0F, -1.0F, -1.0F
'right side
_glNormal3f 1.0F, 0.0F, 0.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f 1.0F, -1.0F, -1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f 1.0F, 1.0F, -1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f 1.0F, 1.0F, 1.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f 1.0F, -1.0F, 1.0F
'left side
_glNormal3f -1.0F, 0.0F, 0.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f -1.0F, -1.0F, -1.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f -1.0F, -1.0F, 1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f -1.0F, 1.0F, 1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f -1.0F, 1.0F, -1.0F
_glEnd
_glEndList
'With the glEndList() command, we announce that we are ending the creation of the list. Anything between glNewList() and glEndList()
'is part of the display list, and vice versa, if there is anything before or after it no longer belongs to it. To find out where to store
'the second display sheet, we take the value of the one already created and add one to it (we said at the beginning of the function that
'we are making 2 display sheets, so it's fine).
End Sub
Sub GL_Init
If GL_InitInfo = 0 Then
_glViewport 0, 0, _Width, _Height
GL_InitInfo = 1
End If
End Sub
Sub Init2
_glEnable _GL_TEXTURE_2D 'enable texturing
_glShadeModel _GL_SMOOTH 'smooth shadowing
_glClearColor 0.0F, 0.0F, 0.0F, 0.5F 'black background
_glClearDepth 1.0F 'Depth buffer settings
_glEnable _GL_DEPTH_TEST 'allow depth testing
_glDepthFunc _GL_LEQUAL 'depth testing type
'The following three lines turn on quick and dirty lighting. Light0 is predefined on most video cards, so it avoids inconvenience
' when setting the lights. After light0 we set the lighting. If your card does not support light0, you will see a black monitor - you need to turn off the lights. The last line adds color
' to texture mapping. If we do not turn on material coloring, the texture will always have the original color. glColor3f(r,g,b) will have no effect (in the render function.
'
_glEnable _GL_LIGHT0 'enable basic light
_glEnable _GL_LIGHTING 'enable lights
_glEnable _GL_COLOR_MATERIAL 'enable materials coloring
'Finally, we'll set perspective corrections to make the image look better. Returning true tells the program that the initialization was successful.
_glHint _GL_PERSPECTIVE_CORRECTION_HINT, _GL_NICEST 'best perspective projection
If GL_InitInfo = 0 Then
BoxTexture(1) = LoadTexture("bedna.jpg", 1)
' BoxTexture(0) = LoadTexture("czflag.jpg", 1) 'try load here next texture for box lid (then uncomment _glbindTexture in _GL SUB)
BuildLists 'create display lists
End If
End Sub
Sub DeleteTexture (nr As Long)
For P = LBound(GLL) To UBound(GLL)
If GLL(P).PointerGL = nr Then
Dim DEL As Long
DEL = GLL(P).PointerGL
_glDeleteTextures 1, _Offset(DEL)
Exit Sub
End If
Next
End Sub
Sub KillAllTextures
For P = LBound(GLL) To UBound(GLL)
If GLL(P).PointerGL > 0 Then
DeleteTexture P
GLL(P).PointerGL = 0
End If
Next P
End Sub
Function LoadTexture (image As String, Filter As _Unsigned _Byte)
If GL_InitInfo = 0 Then GL_Init
If _FileExists(image) Then
TT = 0
Do Until TT = UBound(GLL)
If GLL(TT).TextureName = image$ And GLL(TT).Filtering = Filter Then
LoadTexture = GLL(TT).PointerGL 'prevent memory leak loading next and next texture again and angain...
Exit Function
End If
TT = TT + 1
Loop
tex& = _LoadImage(image$, 32)
_ClearColor _RGB32(255, 255, 0), tex&
texinv& = _NewImage(_Width(tex&), _Height(tex&), 32)
_PutImage (0, _Height(tex&))-(_Width(tex&), 0), tex&, texinv&
ni& = _CopyImage(texinv&, 32) '_NewImage(32, 32, 32)
Dim Texture As _Unsigned Long
_glGenTextures 1, _Offset(Texture) 'generate our texture handle (reserve place in memory for new texture)
_glBindTexture _GL_TEXTURE_2D, Texture 'select our texture handle (set this texture for use)
Dim m As _MEM
m = _MemImage(texinv&)
Dim n As _MEM
n = _MemImage(ni&)
Select Case Filter
Case -1
'set our texture wrapping
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_WRAP_S, _GL_REPEAT
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_WRAP_T, _GL_REPEAT
Case 0
'set out texture filtering
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_NEAREST 'for scaling up
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_NEAREST 'for scaling down
Case 1
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR 'for scaling up
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_LINEAR 'for scaling down
Case 2 'works....not sure, if this output is correct
'gluBuild2DMipmaps(GL_TEXTURE_2D, pic->bpp/8, pic->width, pic->height, textureType, GL_UNSIGNED_BYTE, pic->data);
gluBuild2DMipmaps _GL_TEXTURE_2D, 4, _Width(ni&), _Height(ni&), _GL_RGB, _GL_UNSIGNED_BYTE, _Offset(Texture)
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR_MIPMAP_NEAREST 'for scaling up
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_LINEAR ' IF IS USED _GL_LINEAR_MIMAP_NEAREST here, program crash. Is it correct? -?-
'
_glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGB, _Width(ni&), _Height(ni&), 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, n.OFFSET
_FreeImage tex&
_MemFree n
'_FreeImage ni&
GoTo saveit
'gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
End Select
_FreeImage tex&
_glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGB, _Width(texinv&), _Height(texinv&), 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, m.OFFSET
saveit:
U = UBound(GLL)
GLL(U).PointerGL = Texture
GLL(U).TextureName = image
GLL(U).Filtering = Filter
ReDim _Preserve GLL(U + 1) As GL_Loader
_MemFree m
Else
Print "LoadTexture Error: "; image$; " - file not found."
End If
LoadTexture = Texture
End Function
Texture:
EDIT:
No one helped, I helped myself. Interestingly. The solution always comes to me only after I turn off the computer.
The source code of the same program is attached. This time it already uses the statement, I asked about - _glColor3fv. The solution can be seen on lines 141 to 143 and also 152 - 154. If someone can think of a more elegant solution, I'd be happy to learn.
Code: (Select All)
_Title "Display List demo"
Declare CustomType Library
Sub gluBuild2DMipmaps (BYVAL Target As _Unsigned Long, BYVAL iFormat As Long, BYVAL Wdth As Long, BYVAL Hght As Long, BYVAL format As _Unsigned Long, BYVAL typ As _Unsigned Long, BYVAL dat As _Offset)
End Declare
Type GL_Loader ' array for loading textures
PointerGL As Long
TextureName As String
Filtering As _Unsigned _Byte
End Type
ReDim Shared GLL(0) As GL_Loader, t As Long
Dim Shared GL_InitInfo As _Byte ' is need for OpenGL init
Dim Shared ExitSignal As _Byte ' is need for correct OpenGL exit when program end
'------------------------------------------------------------------------
Type RGB
As Single R, G, B
End Type
Dim Shared BoxCol(5) As RGB 'for cube colors
Restore BXC
For Row = 1 To 5
For Column = 1 To 3
Read Value
Select Case Column
Case 1: BoxCol(Row).R = Value
Case 2: BoxCol(Row).G = Value
Case 3: BoxCol(Row).B = Value
End Select
Next
Next
'colors for cube
BXC:
Data 1.0,0.0,0.0
Data 1.0,0.5,0.0
Data 1.0,1.0,0.0
Data 0.0,1.0,0.0
Data 0.0,1.0,1.0
Dim Shared TopCol(5) As RGB
Restore TPC
For Row = 1 To 5
For Column = 1 To 3
Read Value
Select Case Column
Case 1: TopCol(Row).R = Value
Case 2: TopCol(Row).G = Value
Case 3: TopCol(Row).B = Value
End Select
Next
Next
'colors for lid
TPC: '
Data 0.5,0.0,0.0
Data 0.5,0.25,0.0
Data 0.5,0.5,0.0
Data 0.0,0.5,0.0
Data 0.0,0.5,0.5
Dim Shared As _Unsigned Long Box, Top
Dim Shared BoxTexture(1) As _Unsigned Long
Dim Shared As Single Yrot, Xrot
Screen _NewImage(1024, 768, 32)
Print "What you see on the screen is just 1 cube copyed as more cubes and colored and textured with _glGenLists, _glNewList and _glCallList. Use arrows."
Do
i& = _KeyHit
Select Case i&
Case 20480 'arrow up
Xrot = Xrot + .4
Case 18432 'arrow down
Xrot = Xrot - .4
Case 19200 'arrow left
Yrot = Yrot - .4
Case 19712 'arrow right
Yrot = Yrot + .4
End Select
If ExitSignal Then System
_Limit 50
Loop
Sub _GL ()
' Static Xrot, Yrot
Init2
GL_Init
_glClear _GL_COLOR_BUFFER_BIT And _GL_DEPTH_BUFFER_BIT 'Clear screen and depth buffer
_glMatrixMode _GL_PROJECTION ' Set projection matrix - TRY comment this five rows and then run it. Black screen occur. For view just something then must be depth set to -1 (Z parameter in _glTranslateF)
_glLoadIdentity ' Reset matrix
_gluPerspective 45.0F, _Width / _Height, 0.1F, 100.0F ' Perspective calculation
_glMatrixMode _GL_MODELVIEW ' set modelview matrix
_glBindTexture _GL_TEXTURE_2D, BoxTexture(1)
For yloop = 1 To 6
'The following line moves the origin of the coordinates to the given point on the screen. At first glance it's a bit confusing.
' On the x-axis: We move to the right by 1.4 units so that the pyramid is in the center of the screen. Then we multiply the xloop
'variable by 2.8 and add 1.4. (We multiply by 2.8 so the cubes are not on top of each other. 2.8 is approximately their width when
' rotated 45 degrees.) Finally, we subtract yloop*1.4. This will move them to the left depending on which row we are on. If we didn't
' move them, they would line up on the left side. (And they don't look like a pyramid.)
'On the y-axis: We subtract the variable yloop from six, otherwise the pyramid would be created upside down. Then we multiply the
'result by 2.4. Otherwise the cubes will be on top of each other on the Y axis. (2.4 is roughly equal to the height of the cube).
'Then we subtract 7 so the pyramid starts at the bottom of the screen and is built from the bottom up.
'On the z-axis: We move 20 units inward. So the pyramid just fits on the screen.
xloop = 1
Do Until xloop = yloop
_glLoadIdentity
'cube position on the screen
_glTranslatef 1.4 + (xloop * 2.8F) - (yloop * 1.4F), ((6.5F - yloop) * 2.4F) - 7.0F, -20.0F
_glRotatef 45.0 - (2.0F * yloop) + Xrot, 1.0F, 0.0F, 0.0F
_glRotatef 45.0 + Yrot, 0.0F, 1.0F, 0.0F
'We choose the color of the box (light). Note that we use glColor3fv(). - I NOT BECAUSE IT WORK NOT CORRECT.
' This function selects all three values (red, green, blue) at once to set the color. In this case, we find it in
' the boxcol field with index yloop-1. This ensures a different color for each row of the pyramid. If we used xloop-1 we would get the same colors for each column.
'my solution for _glColor3fv (need 3 records at once)
ReDim Test(2) As Single
Test(0) = BoxCol(yloop - 1).R: Test(1) = BoxCol(yloop - 1).G: Test(2) = BoxCol(yloop - 1).B 'extract just 3 values, need for coloring
_glColor3fv _Offset(Test()) ' used in C original learn example
' _glColor3f BoxCol(yloop - 1).R, BoxCol(yloop - 1).G, BoxCol(yloop - 1).B 'used in previous case
_glCallList (Top) 'draw it
' _glColor3f TopCol(yloop - 1).R, TopCol(yloop - 1).G, TopCol(yloop - 1).B 'used in previous case
ReDim Test(2) As Single
Test(0) = TopCol(yloop - 1).R: Test(1) = TopCol(yloop - 1).G: Test(2) = TopCol(yloop - 1).B
_glColor3fv _Offset(Test())
' _glBindTexture _GL_TEXTURE_2D, BoxTexture(1) ' for case you load second texture, you can try use it as box lid
_glCallList (Box) ';// Vykreslení
xloop = xloop + 1
'_glBindTexture _GL_TEXTURE_2D, BoxTexture(0) 'if you use 2 textures, set previous for box back
Loop
Next
If _Exit Then
' DeleteTexture t 'if program end, first free texture from memory, then exit from GL and return to main loop
KillAllTextures
_glClear _GL_COLOR_BUFFER_BIT
ExitSignal = Not 0
Exit Sub
End If
End Sub
Sub BuildLists 'this sub build 1 box + 1 lid and save it as glList
'glVoid BuildLists
Box = _glGenLists(2)
'We will create the first sheet. We have already taken up space for two sheets and know that the box points to the beginning of the prepared memory. We will use the glNewList() command.
'The first box parameter says that we want to store the sheet in the memory it points to.
'The second GL_COMPILE parameter says that we want to pre-create the sheet in memory so that it doesn't have to be generated and recomputed every time it's rendered. GL_COMPILE is the same as programming.
'If you write a program and load it into your compiler, you have to compile it every time you want to run it. But if it is compiled into an .exe file, all that needs to be done to run it is
'mouse click on this .exe file and run it. Without compilation of course. Whatever OpenGL compiles into the display list can be used without any further recomputation. Rendering speeds up.
Top = Box + 1 '
_glNewList Top, _GL_COMPILE 'compile display list - lid
_glBegin _GL_QUADS
'ceiling
_glNormal3f 0.0F, 1.0F, 0.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f -1.0F, 1.0F, -1.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f -1.0F, 1.0F, 1.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f 1.0F, 1.0F, 1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f 1.0F, 1.0F, -1.0F
_glEnd
_glEndList
_glNewList Box, _GL_COMPILE 'compile display list - box
_glBegin _GL_QUADS
'bottom
_glNormal3f 0.0F, -1.0F, 0.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f -1.0F, -1.0F, -1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f 1.0F, -1.0F, -1.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f 1.0F, -1.0F, 1.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f -1.0F, -1.0F, 1.0F
'front
_glNormal3f 0.0F, 0.0F, 1.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f -1.0F, -1.0F, 1.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f 1.0F, -1.0F, 1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f 1.0F, 1.0F, 1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f -1.0F, 1.0F, 1.0F
'rear
_glNormal3f 0.0F, 0.0F, -1.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f -1.0F, -1.0F, -1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f -1.0F, 1.0F, -1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f 1.0F, 1.0F, -1.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f 1.0F, -1.0F, -1.0F
'right side
_glNormal3f 1.0F, 0.0F, 0.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f 1.0F, -1.0F, -1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f 1.0F, 1.0F, -1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f 1.0F, 1.0F, 1.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f 1.0F, -1.0F, 1.0F
'left side
_glNormal3f -1.0F, 0.0F, 0.0F
_glTexCoord2f 0.0F, 0.0F: _glVertex3f -1.0F, -1.0F, -1.0F
_glTexCoord2f 1.0F, 0.0F: _glVertex3f -1.0F, -1.0F, 1.0F
_glTexCoord2f 1.0F, 1.0F: _glVertex3f -1.0F, 1.0F, 1.0F
_glTexCoord2f 0.0F, 1.0F: _glVertex3f -1.0F, 1.0F, -1.0F
_glEnd
_glEndList
'With the glEndList() command, we announce that we are ending the creation of the list. Anything between glNewList() and glEndList()
'is part of the display list, and vice versa, if there is anything before or after it no longer belongs to it. To find out where to store
'the second display sheet, we take the value of the one already created and add one to it (we said at the beginning of the function that
'we are making 2 display sheets, so it's fine).
End Sub
Sub GL_Init
If GL_InitInfo = 0 Then
_glViewport 0, 0, _Width, _Height
GL_InitInfo = 1
End If
End Sub
Sub Init2
_glEnable _GL_TEXTURE_2D 'enable texturing
_glShadeModel _GL_SMOOTH 'smooth shadowing
_glClearColor 0.0F, 0.0F, 0.0F, 0.5F 'black background
_glClearDepth 1.0F 'Depth buffer settings
_glEnable _GL_DEPTH_TEST 'allow depth testing
_glDepthFunc _GL_LEQUAL 'depth testing type
'The following three lines turn on quick and dirty lighting. Light0 is predefined on most video cards, so it avoids inconvenience
' when setting the lights. After light0 we set the lighting. If your card does not support light0, you will see a black monitor - you need to turn off the lights. The last line adds color
' to texture mapping. If we do not turn on material coloring, the texture will always have the original color. glColor3f(r,g,b) will have no effect (in the render function.
'
_glEnable _GL_LIGHT0 'enable basic light
_glEnable _GL_LIGHTING 'enable lights
_glEnable _GL_COLOR_MATERIAL 'enable materials coloring
'Finally, we'll set perspective corrections to make the image look better. Returning true tells the program that the initialization was successful.
_glHint _GL_PERSPECTIVE_CORRECTION_HINT, _GL_NICEST 'best perspective projection
If GL_InitInfo = 0 Then
BoxTexture(1) = LoadTexture("bedna.jpg", 1)
' BoxTexture(0) = LoadTexture("czflag.jpg", 1) 'try load here next texture for box lid (then uncomment _glbindTexture in _GL SUB)
BuildLists 'create display lists
End If
End Sub
Sub DeleteTexture (nr As Long)
For P = LBound(GLL) To UBound(GLL)
If GLL(P).PointerGL = nr Then
Dim DEL As Long
DEL = GLL(P).PointerGL
_glDeleteTextures 1, _Offset(DEL)
Exit Sub
End If
Next
End Sub
Sub KillAllTextures
For P = LBound(GLL) To UBound(GLL)
If GLL(P).PointerGL > 0 Then
DeleteTexture P
GLL(P).PointerGL = 0
End If
Next P
End Sub
Function LoadTexture (image As String, Filter As _Unsigned _Byte)
If GL_InitInfo = 0 Then GL_Init
If _FileExists(image) Then
TT = 0
Do Until TT = UBound(GLL)
If GLL(TT).TextureName = image$ And GLL(TT).Filtering = Filter Then
LoadTexture = GLL(TT).PointerGL 'prevent memory leak loading next and next texture again and angain...
Exit Function
End If
TT = TT + 1
Loop
tex& = _LoadImage(image$, 32)
_ClearColor _RGB32(255, 255, 0), tex&
texinv& = _NewImage(_Width(tex&), _Height(tex&), 32)
_PutImage (0, _Height(tex&))-(_Width(tex&), 0), tex&, texinv&
ni& = _CopyImage(texinv&, 32) '_NewImage(32, 32, 32)
Dim Texture As _Unsigned Long
_glGenTextures 1, _Offset(Texture) 'generate our texture handle (reserve place in memory for new texture)
_glBindTexture _GL_TEXTURE_2D, Texture 'select our texture handle (set this texture for use)
Dim m As _MEM
m = _MemImage(texinv&)
Dim n As _MEM
n = _MemImage(ni&)
Select Case Filter
Case -1
'set our texture wrapping
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_WRAP_S, _GL_REPEAT
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_WRAP_T, _GL_REPEAT
Case 0
'set out texture filtering
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_NEAREST 'for scaling up
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_NEAREST 'for scaling down
Case 1
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR 'for scaling up
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_LINEAR 'for scaling down
Case 2 'works....not sure, if this output is correct
'gluBuild2DMipmaps(GL_TEXTURE_2D, pic->bpp/8, pic->width, pic->height, textureType, GL_UNSIGNED_BYTE, pic->data);
gluBuild2DMipmaps _GL_TEXTURE_2D, 4, _Width(ni&), _Height(ni&), _GL_RGB, _GL_UNSIGNED_BYTE, _Offset(Texture)
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MAG_FILTER, _GL_LINEAR_MIPMAP_NEAREST 'for scaling up
_glTexParameteri _GL_TEXTURE_2D, _GL_TEXTURE_MIN_FILTER, _GL_LINEAR ' IF IS USED _GL_LINEAR_MIMAP_NEAREST here, program crash. Is it correct? -?-
'
_glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGB, _Width(ni&), _Height(ni&), 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, n.OFFSET
_FreeImage tex&
_MemFree n
'_FreeImage ni&
GoTo saveit
'gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
End Select
_FreeImage tex&
_glTexImage2D _GL_TEXTURE_2D, 0, _GL_RGB, _Width(texinv&), _Height(texinv&), 0, _GL_BGRA_EXT, _GL_UNSIGNED_BYTE, m.OFFSET
saveit:
U = UBound(GLL)
GLL(U).PointerGL = Texture
GLL(U).TextureName = image
GLL(U).Filtering = Filter
ReDim _Preserve GLL(U + 1) As GL_Loader
_MemFree m
Else
Print "LoadTexture Error: "; image$; " - file not found."
End If
LoadTexture = Texture
End Function