December 09, 2019, 08:50:28 AM

Author Topic: Begin2D and End2D not working right.  (Read 369 times)

Offline JBR

  • Jr. Member
  • **
  • Posts: 42
Begin2D and End2D not working right.
« on: July 17, 2019, 09:31:03 PM »
Hi, I'm creating a textured square sphere and it works perfect unless I use Begin2D - End2D where it messes up all my textures.

I'm coming from the Elite code from Krischan.

Could some people try?



Code: [Select]
SuperStrict
 
Import "minib3d.bmx"
 
Graphics3D 1920/1.5, 1080/1.5, 32, 2
 
ClearTextureFilters()
 

Global G_Camera:TCamera = CreateCamera()
PositionEntity G_Camera, 0, 0, -30




Global G_Planet_Mesh:TMesh[6]
Global G_Planet_Surf:TSurface[6]

Global G_Planet_Texture:TTexture[6]
Global G_Planet_Pixmap:TPixmap[6]

Global t_s% = 128*2


For Local i% = 0 To 6-1
G_Planet_Mesh:TMesh[i%] = CreateMesh()
G_Planet_Surf:TSurface[i%] = CreateSurface(G_Planet_Mesh:TMesh[i%])

G_Planet_Texture:TTexture[i%] = CreateTexture( t_s%, t_s%, 1 + 8 ) ' colour + mipmapped
G_Planet_Pixmap:TPixmap[i%] = CreatePixmap( t_s%, t_s%, PF_RGBA8888)

Local pixels:Int Ptr = Int(G_Planet_Pixmap:TPixmap[i%].pixels) ' fill the pixmaps with red shit with black edge
For Local y% = 0 To t_s%-1
For Local x% = 0 To t_s%-1
pixels[x%+y%*t_s%] = $FF000000 | Rand(0,$ffffff) ' totally random
Next
Next



PixmapToTexture(G_Planet_Pixmap:TPixmap[i%], G_Planet_Texture:TTexture[i%])


EntityTexture G_Planet_Mesh:TMesh[i%], G_Planet_Texture:TTexture[i%]

Next




Global G_Side_Size:Int = 16

For Local face% = 0 To 5

For Local y# = 1 To -1  Step -0.125
For Local x# = -1 To 1 Step 0.125

Local z# = -1

'we have x#,y#,z#
'----------------

Local x2# = x# * x#
Local y2# = y# * y#
Local z2# = z# * z#

Local sx# = x# * Sqr( 1 - y2#/2.0 - z2#/2.0 + y2#*z2#/3.0 )
Local sy# = y# * Sqr( 1 - x2#/2.0 - z2#/2.0 + x2#*z2#/3.0 )
Local sz# = z# * Sqr( 1 - x2#/2.0 - y2#/2.0 + x2#*y2#/3.0 )

Local size% = 15

Local u#    = ((x#+1)/2.0) ' MAY NEED TO USE SX#, SY#
Local v#    = ((y#-1)/-2.0)

AddVertex( G_Planet_Surf:TSurface[face%], sx#*size%, sy#*size%, sz#*size%, u#, v# )

Next
Next

Local mult% = G_Side_Size+1

For Local y% = 0 To G_Side_Size-1
For Local x% = 0 To G_Side_Size-1

AddTriangle( G_Planet_Surf:TSurface[face%], y%*mult% + x%, y%*mult% + x%+1, (y%+1)*mult% + x%+1 )

AddTriangle( G_Planet_Surf:TSurface[face%], y%*mult% + x%, (y%+1)*mult% + x%+1, (y%+1)*mult% + x% )

VertexColor( G_Planet_Surf:TSurface[face%], y%*mult% + x%, 255,0,0 )
Next
Next

Select face%

Case 0 rotatemesh G_Planet_Mesh:TMesh[face%], 0,0,0
Case 1 rotatemesh G_Planet_Mesh:TMesh[face%], 0,90,0
Case 2 rotatemesh G_Planet_Mesh:TMesh[face%], 0,180,0
Case 3 rotatemesh G_Planet_Mesh:TMesh[face%], 0,270,0

Case 4 rotatemesh G_Planet_Mesh:TMesh[face%], 90,0,0 ' NORTH
Case 5 rotatemesh G_Planet_Mesh:TMesh[face%], 270,0,0 ' SOUTH

Default
EndSelect
Next





Global G_Time%

While Not AppTerminate() Or Not Confirm( "Terminate? "+String(G_TIME%) )

G_Time% = MilliSecs()


For Local j% = 0 To 5
PixmapToTexture(G_Planet_Pixmap:TPixmap[j%], G_Planet_Texture:TTexture[j%])
Next

For Local i% = 0 To 6-1
turnentity G_Planet_Mesh[i%], .2, .1, .3
Next



updateworld()
renderworld()

' Begin2D()
'
' DrawText String(MilliSecs()-G_Time%),100,100
'
' End2D()

Flip True
G_Time% = MilliSecs()-G_Time%
Wend



FlushKeys()
End




'--------------------------------
' creates a texture from a pixmap
'--------------------------------
Function PixmapToTexture(pixmap:TPixmap, tex:TTexture)
 
        If PixmapFormat(pixmap) <> PF_RGBA8888 Then pixmap = ConvertPixmap(pixmap, PF_RGBA8888)
 
        ' OpenB3D only function to copy pixmap to texture
        'BufferToTex tex, PixmapPixelPtr(pixmap, 0, 0)
 
        ' MiniB3D workaround to copy pixmap to texture
        glBindTexture (GL_TEXTURE_2D, tex.gltex[0])
        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, pixmap.width, pixmap.Height, GL_RGBA, GL_UNSIGNED_BYTE, PixmapPixelPtr(pixmap, 0, 0))


End Function
' --------------------------------------------------------------------------------
' Fixed BeginMax2D()
' --------------------------------------------------------------------------------
Function Begin2D()
 
        Local x:Int, y:Int, w:Int, h:Int
        GetViewport(x, y, w, h)
 
        'glPopClientAttrib()
        'glPopAttrib()
        glMatrixMode(GL_MODELVIEW)
        glPopMatrix()
        glMatrixMode(GL_PROJECTION)
        glPopMatrix()
        glMatrixMode(GL_TEXTURE)
        glPopMatrix()
        glMatrixMode(GL_COLOR)
        glPopMatrix()
       
        glDisable(GL_LIGHTING)
        glDisable(GL_DEPTH_TEST)
        glDisable(GL_SCISSOR_TEST)
        glDisable(GL_FOG)
        glDisable(GL_CULL_FACE)
 
        glMatrixMode GL_TEXTURE
        glLoadIdentity
 
        glMatrixMode GL_PROJECTION
        glLoadIdentity
        glOrtho 0, GraphicsWidth(), GraphicsHeight(), 0, -1, 1
 
        glMatrixMode GL_MODELVIEW
        glLoadIdentity
 
        SetViewport x, y, w, h
 
        Local MaxTex:Int
        glGetIntegerv(GL_MAX_TEXTURE_UNITS, Varptr(MaxTex))
 
        For Local Layer:Int = 0 Until MaxTex
 
                glActiveTexture(GL_TEXTURE0+Layer)
 
                glDisable(GL_TEXTURE_CUBE_MAP)
                glDisable(GL_TEXTURE_GEN_S)
                glDisable(GL_TEXTURE_GEN_T)
                glDisable(GL_TEXTURE_GEN_R)
 
                glDisable(GL_TEXTURE_2D)
 
        Next
 
        glActiveTexture(GL_TEXTURE0)
 
        glViewport(0, 0, GraphicsWidth(), GraphicsHeight())
        glScissor(0, 0, GraphicsWidth(), GraphicsHeight())
 
        glEnable GL_BLEND
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glEnable(GL_TEXTURE_2D)
 
End Function
 
' --------------------------------------------------------------------------------
' Fixed EndMax2D()
' --------------------------------------------------------------------------------
Function End2D()
 
        ' save the Max2D settings for later
        'glPushAttrib(GL_ALL_ATTRIB_BITS)
        'glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS)
        glMatrixMode(GL_MODELVIEW)
        glPushMatrix()
        glMatrixMode(GL_PROJECTION)
        glPushMatrix()
        glMatrixMode(GL_TEXTURE)
        glPushMatrix()
        glMatrixMode(GL_COLOR)
        glPushMatrix()
               
        glDisable(GL_TEXTURE_CUBE_MAP)
        glDisable(GL_TEXTURE_GEN_S)
        glDisable(GL_TEXTURE_GEN_T)
        glDisable(GL_TEXTURE_GEN_R)
 
        glDisable(GL_TEXTURE_2D)
        glDisable(GL_BLEND)
 
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''TGlobal.EnableStates()
        'glDisable(GL_TEXTURE_2D)
 
        ' ' only needed for OpenB3D
        'TGlobal.alpha_enable[0] = 0    ' alpha blending was disabled by Max2d (GL_BLEND)
        'TGlobal.blend_mode[0] = 1      ' force alpha blending
        'TGlobal.fx1[0] = 0                     ' full bright/surface normals was enabled by EnableStates (GL_NORMAL_ARRAY)
        'TGlobal.fx2[0] = 1                     ' vertex colors was enabled by EnableStates (GL_COLOR_ARRAY)
 
        glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR)
        glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_TRUE)
 
        glClearDepth(1.0)
        glDepthFunc(GL_LEQUAL)
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
 
        glAlphaFunc(GL_GEQUAL, 0.5)
 
        ' ' only needed for OpenB3D
        'For Local cam:TCamera=EachIn TCamera.cam_list
               
                 ' active camera - was if cam.hide[0]=0
                 'If cam = TGlobal.camera_in_use
               
                        ' fog with Max2d fix
                '       cam.UpdateFog()
                '       Exit
                       
                'EndIf
               
        'Next
 
End Function

Offline JBR

  • Jr. Member
  • **
  • Posts: 42
Re: Begin2D and End2D not working right.
« Reply #1 on: July 18, 2019, 07:09:40 PM »
Hi added TextureBlend to each texture and all is working!

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 409
Re: Begin2D and End2D not working right.
« Reply #2 on: July 19, 2019, 03:56:06 AM »
You could replace the Minib3d Begin/end functions with the new ones in Openb3dmax. You'll need to remove the fog code and render state flags, and anything with {0} after it.