Plasma Tunnel conversion from Delphi

Started by SToS, November 21, 2024, 08:25:23

Previous topic - Next topic

SToS

Just for fun...

SuperStrict

'//-ORIGINAL CODE (in Delphi) by--------------------------------------------
'//
'// Author      : Jan Horn
'// Email       : jhorn@Global.co.za
'// Website     : http://home.Global.co.za/~jhorn
'// date        : 8 April 2001
'// Version     : 1.0
'// Description : OpenGL Plasma tunnel
'//
'//------------------------------------------------------------------------
'
' Translated (roughly) to BlitzMax NG by SToS/A.K.A. Tantalus

Const WIN_WIDTH:UInt = 800
Const WIN_HEIGHT:UInt = 600

Type TVertex3 Final
    Field x:Float
    Field y:Float
    Field z:Float
End Type

Type TCoordUV Final
    Field u:Float
    Field v:Float
End Type

Global elapsed_time:Int
Global last_time:Int
Global tunnel_pixmap:TPixmap
Global texture_id:Int
Global pixel_data:Byte Ptr
Global tunnel_vertices:TVertex3[33,33]
Global tunnel_tcoords:TCoordUV[33,33]

Function Rad2Deg:Float(r:Float)
    Return r * 180.0 / 3.14159
End Function

Function CreateTunnel()
    For Local i:Int = 0 To 32
        For Local j:Int = 0 To 32
            tunnel_tcoords[i, j] = New TCoordUV
            tunnel_vertices[i, j] = New TVertex3
            tunnel_vertices[i, j].x = (3.0 - Float(j) / 12.0) * CosF(Rad2Deg(2.0 * 3.14159 / 32.0 * Float(i)))
            tunnel_vertices[i, j].y = (3.0 - Float(j) / 12.0) * SinF(Rad2Deg(2.0 * 3.14159 / 32.0 * Float(i)))
            tunnel_vertices[i, j].z = Float(-j)
        Next
    Next
End Function

Function glDraw()
    Local c:Float
    Local angle:Float

    glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()

    angle = (elapsed_time + last_time) / 32.0
    last_time = elapsed_time
    glTranslatef(0.0, 0.0, -4.2)

    For Local i:Int = 0 To 32
        For Local j:Int = 0 To 32
            tunnel_tcoords[i, j].u = Float(i) / 32.0 + CosF(Rad2Deg((angle + 8.0 * Float(j)) / 60.0)) / 2.0
            tunnel_tcoords[i, j].v = Float(j) / 32.0 +              (angle +       Float(j)) / 120.0
        Next
    Next

    For Local j:Int = 0 To 31
        If j > 24 Then c = 1.0 - (j - 24.0) / 10.0 Else c = 1.0
       
        glColor3f(c, c, c)

        glBegin(GL_QUADS)
        For Local i:Int = 0 To 31
            glTexCoord2f(tunnel_tcoords[i, j].U, tunnel_tcoords[i, j].v)
            glVertex3f(tunnel_vertices[i, j].X, tunnel_vertices[i, j].Y, tunnel_vertices[i, j].Z)
            glTexCoord2f(tunnel_tcoords[i+1, j].U, tunnel_tcoords[i+1, j].v)
            glVertex3f(tunnel_vertices[i+1, j].X, tunnel_vertices[i+1, j].Y, tunnel_vertices[i+1, j].Z)
            glTexCoord2f(tunnel_tcoords[i+1, j+1].U, tunnel_tcoords[i+1, j+1].v)
            glVertex3f(tunnel_vertices[i+1, j+1].X, tunnel_vertices[i+1, j+1].Y, tunnel_vertices[i+1, j+1].Z)
            glTexCoord2f(tunnel_tcoords[i, j+1].U, tunnel_tcoords[i, j+1].v)
            glVertex3f(tunnel_vertices[i, j+1].X, tunnel_vertices[i, j+1].Y, tunnel_vertices[i, j+1].Z)
        Next
        glEnd()
    Next
    glFinish()
End Function

Function glInit()
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glShadeModel(GL_SMOOTH)
    glClearDepth(1.0)
    glEnable(GL_DEPTH_TEST)
    glDepthFunc(GL_LESS)
   
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
    glEnable(GL_TEXTURE_2D)
    tunnel_pixmap = LoadPixmap("texture.bmp")
    Assert(tunnel_pixmap)
    Local pixmap:TPixmap = ConvertPixmap(tunnel_pixmap, PF_RGB888)
    Assert(pixmap)
    pixel_data = PixmapPixelPtr(pixmap)
    Assert(pixel_data)
    glGenTextures(1, Varptr texture_id)
    glBindTexture(GL_TEXTURE_2D, texture_id)
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, PixmapWidth(tunnel_pixmap), PixmapHeight(tunnel_pixmap), GL_RGB, GL_UNSIGNED_BYTE, pixel_data)

    CreateTunnel()
End Function

Function glResizeWnd(w:Int, h:Int)
    If h = 0 Then h = 1
    glViewport(0, 0, w, h)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45.0, Float(w) / Float(h), 1.0, 100.0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
End Function

Graphics WIN_WIDTH, WIN_HEIGHT, 0

glInit()
glResizeWnd(WIN_WIDTH, WIN_HEIGHT)

last_time = MilliSecs()
Repeat
    glDraw()
    Flip
    elapsed_time = MilliSecs() - last_time
Until KeyDown(KEY_ESCAPE) Or AppTerminate()


screenshot.png
plasmatunnel.zip

Derron

#1
Just a question: Is your plan to "convert examples" strictly "from language to language" or would you also incorporate the features BlitzMax NG offers?

So if they did their own "Timage"-like implementation would you use the Blitzmax "TImage" instead or their approach?

Asking as eg. that type "Type TVertex3 Final" could also become an "SVec3f" (so a struct, not a type). And changing it to SVec3F would make the code a bit shorter (as it does not require the custom type) and possibly would show to "others" how things could be done.


PS: Am not sure if you are aware of it. But if a part of a calculation contains Floats already, then the result will be a Float too. So "3 / 5" will be "0" while "3 / 5.0" will result in "0.6".

In your code you have
            tunnel_tcoords[i, j].u = Float(i) / 32.0 + CosF(Rad2Deg((angle + 8.0 * Float(j)) / 60.0)) / 2.0
            tunnel_tcoords[i, j].v = Float(j) / 32.0 +              (angle +       Float(j)) / 120.0
There you cast "Float(i)" (and j) but multiply/divide with a float/double anyways. So the cast should be not necessary (and thus the formula would become "shorter/easier to read")


But as said ... depends on what your target is - don't feel "trying to get convinced" to do things.

PS: Effect reminds me on old WinAmp-Visualizers (good old times :D)

bye
Ron



SToS

Quote from: Derron on November 22, 2024, 11:37:05Just a question: Is your plan to "convert examples" strictly "from language to language" or would you also incorporate the features BlitzMax NG offers?

Initially I'm doing whatever I can to try and learn its syntax and modules, and the best way for me is to convert examples from other languages I'm more familiar with.

As a side, does it matter? If there is a problem with me posting my trash and cluttering the forum then PLEASE let me know.

QuoteSo if they did their own "Timage"-like implementation would you use the Blitzmax "TImage" instead or their approach?

Asking as eg. that type "Type TVertex3 Final" could also become an "SVec3f" (so a struct, not a type). And changing it to SVec3F would make the code a bit shorter (as it does not require the custom type) and possibly would show to "others" how things could be done.

As I was unaware of the Vector library as I'm using the sidebar help and It is not under the Math module heading.  And this is why the docs are  :-X

QuotePS: Am not sure if you are aware of it. But if a part of a calculation contains Floats already, then the result will be a Float too. So "3 / 5" will be "0" while "3 / 5.0" will result in "0.6".

There you cast "Float(i)" (and j) but multiply/divide with a float/double anyways. So the cast should be not necessary (and thus the formula would become "shorter/easier to read")

I have had some unexpected behaviour when not implicitly casting to a type, so I start off by casting and pruning later if I feel inclined to continue adapting and example.  It's like in C if you don't suffix a float literal with f, you can get unexpected results too; I'm just being cautious.

QuoteBut as said ... depends on what your target is - don't feel "trying to get convinced" to do things.

No target, but then no one starts a post/topic based on whether they've forseen someone else wanting a post on the subject. (I've confused myself with that statement) ???

To be honest, I don't think I'll stick with BlitzMax as it's just a bit of fun.  I will as always go back to C and Assembly, it's just a passing phase.

Again, if anyone has an objection to me posting here please let me know.

Derron

Maybe you got me wrong - I was not telling you to stop posting (think it is helpful for some people, and also "google" will find the code so people looking for such stuff might find it...).
I just was not sure "why" you do things - and if you are interested in improving your BlitzMax skill to (eg learning new stuff there).

As you seem to just use NG "temporarily" it might not make much sense for "us" (the ones doing that kind of feedback) to invest time for "code specific feedback". We should then just read your code, raise eyebrows ("oh, didn't know that, ... cool!") here and then ;) - saves you from having to scan replies you are not interested in.


bye
Ron