Android Question 3D Tin Model using OpenGL

Terradrones

Active Member
I have a TIN Model consisting of Triangles as defined by their Vertexes. I would like to display this as a 3D Model using OpenGL, but my code is full of errors. If somebody could please give me a push in the right direction. Here is my code:

B4X:
Sub Render3D
    If GlSV.DeviceAPILevel < 8 Then
        MsgboxAsync("OpenGL Is Not Supported On This Device", "OpenGL Error")
        Panels
        Panel1.Visible=True
    End If
    Panels
    Panel6.Visible = True
    Pnl3DView.Visible = True
    ProgressDialogShow("Generating 3D Model...")
    Sleep(0)
    CalculateScalingFactors
    GetVertices

    GlSV.Initialize2(GlSV.RENDERMODE_WHEN_DIRTY, "glsv", 16, 0)
    Pnl3DView.AddView(GlSV, 0, 0, Pnl3DView.Width, Pnl3DView.Height)
    ProgressDialogHide
End Sub

Sub Butt3_Click
'    Timer1.Enabled = False
    Panels
    Panel1.Visible=True
End Sub

Sub GLSV_Draw(gl As GL2)
    Try
        gl.glClear(Bit.Or(gl.GL_COLOR_BUFFER_BIT, gl.GL_DEPTH_BUFFER_BIT))
'        gl.glLoadIdentity()
'        gl.glTranslatef(0, 0, -5)
'        gl.glRotatef(EyeAngle * (180 / 3.14159), 0, 1, 0)
        
        DrawTINModel(gl)
    Catch
        Log(LastException)
    End Try
End Sub

Sub glsv_SurfaceChanged(gl As GL2, width As Int, height As Int)
    Log("Changed")
    Try
        gl.glViewport(0, 0, width, height)
        Dim ratio As Float = width / height
        gl.gluPerspective(45, ratio, 1, 100)
    Catch
        Log(LastException)
    End Try
End Sub
    
Sub glsv_SurfaceCreated(gl As GL2)
    Log("Created")
    Try
        gl.glClearColor(0, 0, 0, 1)
        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glDepthFunc(gl.GL_LESS)
    Catch
        Log(LastException)
    End Try
End Sub

Sub Timer1_Tick
    EyeAngle = EyeAngle + 0.1
    If EyeAngle > (2 * 3.14159) Then EyeAngle = EyeAngle - (2 * 3.14159)
    GlSV.RequestRender
End Sub

Sub GetVertices
    Dim i As Int
    
    i=0
    vertixes.Initialize
    Do While i<=CGlobals.MaxTins
        Y1=ConvertPhysicToCADX(CGlobals.Tin3D(i).East)
        X1=ConvertPhysicToCADY(CGlobals.Tin3D(i).North)
        vertixes.Add(Array As Float(X1, Y1, CGlobals.Tin3D(i).Elev)) ' x, y, height
        
        Y1=ConvertPhysicToCADX(CGlobals.Tin3D(i).East1)
        X1=ConvertPhysicToCADY(CGlobals.Tin3D(i).North1)
        vertixes.Add(Array As Float(X1, Y1, CGlobals.Tin3D(i).Elev1)) ' x, y, he
        
        Y1=ConvertPhysicToCADX(CGlobals.Tin3D(i).East2)
        X1=ConvertPhysicToCADY(CGlobals.Tin3D(i).North2)
        vertixes.Add(Array As Float(X1, Y1, CGlobals.Tin3D(i).Elev2)) ' x, y, he
        i=i+1
    Loop
    
    MinX=PntMinX
    MinY=PntMinY
    MaxX=PntMaxX
    MaxY=PntMaxY
    MinZ=CGlobals.ZMinCont
    MaxZ=CGlobals.ZMaxCont
    
End Sub

Sub CalculateScalingFactors
    ' Determine the range of your world coordinates
    Dim rangeX As Float = PntMaxX - PntMinX
    Dim rangeY As Float = PntMaxY - PntMinY
    Dim rangeZ As Float = CGlobals.ZMaxCont- CGlobals.ZMinCont

    ' Determine the dimensions of your screen or viewport
    Dim screenWidth As Float = Pnl3DView.Width
    Dim screenHeight As Float = Pnl3DView.Height

    ' Calculate the scaling factors
    Dim scaleFactorX As Float = screenWidth / rangeX
    Dim scaleFactorY As Float = screenHeight / rangeY

    ' Use the minimum of the two scaling factors to maintain aspect ratio
    Dim scaleFactor As Float = Min(scaleFactorX, scaleFactorY)

    ' Apply the same scaling factor to all axes
    ScaleX1 = scaleFactor
    ScaleY1 = scaleFactor
    ScaleZ1 = scaleFactor ' Assuming Z scaling is proportional to X and Y
End Sub

Sub DrawTINModel(gl As GL2)

'    If vertixes.Size < 3 Then
'        Log("Not enough vertices to draw a triangle")
'        Return
'    End If
'
'    Try
'        Dim jo As JavaObject = gl
'        jo.RunMethod("glEnableClientState", Array(gl.GL_VERTEX_ARRAY)) ' Enable vertex array
'
'        ' Define vertex pointer
'        jo.RunMethod("glVertexPointer", Array(3, gl.GL_FLOAT, 0, vertixes)) ' Assuming vertices is a Float array
'
'        ' Draw the vertices as triangles
'        jo.RunMethod("glDrawArrays", Array(gl.GL_TRIANGLES, 0, vertixes.Size / 3))
'
'        jo.RunMethod("glDisableClientState", Array(gl.GL_VERTEX_ARRAY)) ' Disable vertex array
'    Catch
'        Log("Exception during drawing: " & LastException)
'    End Try
End Sub

And my screen is black.

Thanks
Michael
 

MasterGy

Member
I have a TIN Model consisting of Triangles as defined by their Vertexes. I would like to display this as a 3D Model using OpenGL, but my code is full of errors. If somebody could please give me a push in the right direction. Here is my code:

B4X:
Sub Render3D
    If GlSV.DeviceAPILevel < 8 Then
        MsgboxAsync("OpenGL Is Not Supported On This Device", "OpenGL Error")
        Panels
        Panel1.Visible=True
    End If
    Panels
    Panel6.Visible = True
    Pnl3DView.Visible = True
    ProgressDialogShow("Generating 3D Model...")
    Sleep(0)
    CalculateScalingFactors
    GetVertices

    GlSV.Initialize2(GlSV.RENDERMODE_WHEN_DIRTY, "glsv", 16, 0)
    Pnl3DView.AddView(GlSV, 0, 0, Pnl3DView.Width, Pnl3DView.Height)
    ProgressDialogHide
End Sub

Sub Butt3_Click
'    Timer1.Enabled = False
    Panels
    Panel1.Visible=True
End Sub

Sub GLSV_Draw(gl As GL2)
    Try
        gl.glClear(Bit.Or(gl.GL_COLOR_BUFFER_BIT, gl.GL_DEPTH_BUFFER_BIT))
'        gl.glLoadIdentity()
'        gl.glTranslatef(0, 0, -5)
'        gl.glRotatef(EyeAngle * (180 / 3.14159), 0, 1, 0)
       
        DrawTINModel(gl)
    Catch
        Log(LastException)
    End Try
End Sub

Sub glsv_SurfaceChanged(gl As GL2, width As Int, height As Int)
    Log("Changed")
    Try
        gl.glViewport(0, 0, width, height)
        Dim ratio As Float = width / height
        gl.gluPerspective(45, ratio, 1, 100)
    Catch
        Log(LastException)
    End Try
End Sub
   
Sub glsv_SurfaceCreated(gl As GL2)
    Log("Created")
    Try
        gl.glClearColor(0, 0, 0, 1)
        gl.glEnable(gl.GL_DEPTH_TEST)
        gl.glDepthFunc(gl.GL_LESS)
    Catch
        Log(LastException)
    End Try
End Sub

Sub Timer1_Tick
    EyeAngle = EyeAngle + 0.1
    If EyeAngle > (2 * 3.14159) Then EyeAngle = EyeAngle - (2 * 3.14159)
    GlSV.RequestRender
End Sub

Sub GetVertices
    Dim i As Int
   
    i=0
    vertixes.Initialize
    Do While i<=CGlobals.MaxTins
        Y1=ConvertPhysicToCADX(CGlobals.Tin3D(i).East)
        X1=ConvertPhysicToCADY(CGlobals.Tin3D(i).North)
        vertixes.Add(Array As Float(X1, Y1, CGlobals.Tin3D(i).Elev)) ' x, y, height
       
        Y1=ConvertPhysicToCADX(CGlobals.Tin3D(i).East1)
        X1=ConvertPhysicToCADY(CGlobals.Tin3D(i).North1)
        vertixes.Add(Array As Float(X1, Y1, CGlobals.Tin3D(i).Elev1)) ' x, y, he
       
        Y1=ConvertPhysicToCADX(CGlobals.Tin3D(i).East2)
        X1=ConvertPhysicToCADY(CGlobals.Tin3D(i).North2)
        vertixes.Add(Array As Float(X1, Y1, CGlobals.Tin3D(i).Elev2)) ' x, y, he
        i=i+1
    Loop
   
    MinX=PntMinX
    MinY=PntMinY
    MaxX=PntMaxX
    MaxY=PntMaxY
    MinZ=CGlobals.ZMinCont
    MaxZ=CGlobals.ZMaxCont
   
End Sub

Sub CalculateScalingFactors
    ' Determine the range of your world coordinates
    Dim rangeX As Float = PntMaxX - PntMinX
    Dim rangeY As Float = PntMaxY - PntMinY
    Dim rangeZ As Float = CGlobals.ZMaxCont- CGlobals.ZMinCont

    ' Determine the dimensions of your screen or viewport
    Dim screenWidth As Float = Pnl3DView.Width
    Dim screenHeight As Float = Pnl3DView.Height

    ' Calculate the scaling factors
    Dim scaleFactorX As Float = screenWidth / rangeX
    Dim scaleFactorY As Float = screenHeight / rangeY

    ' Use the minimum of the two scaling factors to maintain aspect ratio
    Dim scaleFactor As Float = Min(scaleFactorX, scaleFactorY)

    ' Apply the same scaling factor to all axes
    ScaleX1 = scaleFactor
    ScaleY1 = scaleFactor
    ScaleZ1 = scaleFactor ' Assuming Z scaling is proportional to X and Y
End Sub

Sub DrawTINModel(gl As GL2)

'    If vertixes.Size < 3 Then
'        Log("Not enough vertices to draw a triangle")
'        Return
'    End If
'
'    Try
'        Dim jo As JavaObject = gl
'        jo.RunMethod("glEnableClientState", Array(gl.GL_VERTEX_ARRAY)) ' Enable vertex array
'
'        ' Define vertex pointer
'        jo.RunMethod("glVertexPointer", Array(3, gl.GL_FLOAT, 0, vertixes)) ' Assuming vertices is a Float array
'
'        ' Draw the vertices as triangles
'        jo.RunMethod("glDrawArrays", Array(gl.GL_TRIANGLES, 0, vertixes.Size / 3))
'
'        jo.RunMethod("glDisableClientState", Array(gl.GL_VERTEX_ARRAY)) ' Disable vertex array
'    Catch
'        Log("Exception during drawing: " & LastException)
'    End Try
End Sub

And my screen is black.

Thanks
Michael
I'm happy to help with opengl, but unfortunately I don't understand your code. If there was only enough in your code that generates the lines or points, so it creates the graphic data, then I can try to build a renderer on top of it.
 
Upvote 0
Top