I know there are already a few similar things out there, with commands other than OpenGL, but here's the OpenGL version. Waving flag.
Change the texture file name on row 153.
Change the texture file name on row 153.
Code: (Select All)
_Title "3D Flag in OpenGL"
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
'look to init2 sub - changes in _gl_polygon_mode
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
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
'----------------------------------------------------------------------------------
Dim Shared Points(45, 45, 3), Texture~&
Dim Shared As Single Xrot, Yrot, Zrot, Wiggle_count, Hold
' The next two cycles initialize our grid. In order to get the correct index, we have to divide the control
' transformation of the loop by five (ie 45/9=5). I subtract 4.4 from each coordinate to center the wave at the
' origin of the coordinates. The same effect can be achieved with the help of displacement, but I prefer this method.
' The points (x, y, 2) value is made up of the sine value. The sin() function needs radians, so we take the
' value in degrees, which is our x/5 multiplied by forty, and recalculate it using the formula (radians=2*PI*degrees/360). - or we can using _D2R
x = 0
Do Until x > 44
Do Until y > 44
Points(x, y, 0) = ((x / 5.0F) - 4.5F)
Points(x, y, 1) = ((y / 5.0F) - 4.5F)
Points(x, y, 2) = (Sin((((x / 5.0F) * 40.0F) / 360.0F) * _Pi * 2.0F))
y = y + 1
Loop
x = x + 1
y = 0
Loop
Screen _NewImage(1024, 768, 32)
Do
If ExitSignal Then System
_Limit 50
Loop
Sub _GL ()
Static X, Y, float_x, float_y, float_xb, float_yb, wiggle_count
Init2
GL_Init
_glClear _GL_COLOR_BUFFER_BIT And _GL_DEPTH_BUFFER_BIT 'Clear screen and depth buffer
_glLoadIdentity '();// Reset matice
_glTranslatef 0.0F, 0.0F, -12.0F ' shift to depth to screen
_glRotatef Xrot, 1.0F, 0.0F, 0.0F 'rotation on X axis
_glRotatef Yrot, 0.0F, 1.0F, 0.0F 'rotation on Y axis
_glRotatef Zrot, 0.0F, 0.0F, 1.0F 'rotation on Z axis
_glBindTexture _GL_TEXTURE_2D, Texture~& ' set texture
_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)
_gluPerspective 45.0F, _Width / _Height, 0.1F, 100.0F ' Perspective calculation
'-----------------------------------
'Note that the squares are drawn clockwise. This means that the front surface you see will be filled and the back will be a wireframe.
'If we were to draw the squares counterclockwise, the wireframe would be on the front side.
_glBegin _GL_QUADS
X = 0
Y = 0
Do Until X > 43
Do Until Y > 43
float_x = X / 44.0F
float_y = Y / 44.0F
float_xb = (X + 1) / 44.0F
float_yb = (Y + 1) / 44.0F
'set points
_glTexCoord2f float_x, float_y
_glVertex3f Points(X, Y, 0), Points(X, Y, 1), Points(X, Y, 2)
_glTexCoord2f float_x, float_yb
_glVertex3f Points(X, Y + 1, 0), Points(X, Y + 1, 1), Points(X, Y + 1, 2)
_glTexCoord2f float_xb, float_yb
_glVertex3f Points(X + 1, Y + 1, 0), Points(X + 1, Y + 1, 1), Points(X + 1, Y + 1, 2)
_glTexCoord2f float_xb, float_y
_glVertex3f Points(X + 1, Y, 0), Points(X + 1, Y, 1), Points(X + 1, Y, 2)
Y = Y + 1
Loop
X = X + 1
Y = 0
Loop
_glEnd
X = 0
Y = 0
' With even rendering in order, we move the coordinates in the field to the neighboring coordinates and thus also move the wave a little next to it.
' We gradually store the entire first column (outer cycle) in an auxiliary variable. We then move the wave a bit by simply assigning each element
' to its neighbor, and finally assign the stored edge value to the opposite end of the image. This creates the impression that when one wave
' disappears, a new one immediately begins to appear, but programmatically it is the end of the old one :-) In simple terms, we have only one wave, which
' moves to the beginning after leaving the image. Finally, we reset the wiggle_count to zero to keep the animation running.
If wiggle_count = 2 Then
Do Until Y > 44
X = 0
Hold = Points(0, Y, 2)
Do Until X > 43
Points(X, Y, 2) = Points(X + 1, Y, 2)
X = X + 1
Loop
Points(44, Y, 2) = Hold
Y = Y + 1
Loop
wiggle_count = 0
End If
wiggle_count = wiggle_count + 1
Xrot = Xrot + 0.3F
Yrot = Yrot + 0.2F
Zrot = Zrot + 0.4F
If _Exit Then
DeleteTexture Texture~& 'if program end, first free texture from memory, then exit from GL and return to main loop
_glClear _GL_COLOR_BUFFER_BIT
ExitSignal = Not 0
Exit Sub
End If
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 'allow texture maping
_glBlendFunc _GL_SRC_ALPHA, _GL_ONE 'blending type for transparency
_glClearColor 0.0, 0.0, 0.0, 0.5 'Black background
_glClearDepth 1.0F ' depth buffer settings
_glDepthFunc _GL_LESS ' depth testing type
_glEnable _GL_DEPTH_TEST ' enable depth testing
_glShadeModel _GL_SMOOTH 'allow smooth shading
_glHint _GL_PERSPECTIVE_CORRECTION_HINT, _GL_NICEST ' best perspective projection
_glPolygonMode _GL_BACK, _GL_FILL 'Front side filled with polygons
_glPolygonMode _GL_FRONT, _GL_LINE 'Back side filled with grid
' GL_FILL determines the classic drawing of polygons, GL_LINES draws only edge lines, with GL_POINTS only the top points could be seen.
' Which side of the polygon is the front and which is the back cannot be clearly determined, just rotate it and it's the other way around.
' That is why the convention arose that polygons whose vertices were entered counter-clockwise during rendering are inverted.
If GL_InitInfo = 0 Then
Texture~& = LoadTexture("vlajka.png", 1)
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
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? -?-
_FreeImage tex&
_MemFree n
'_FreeImage ni&
GoTo saveit
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