Author Topic: OGL Cube with windows.[BMAX]  (Read 3201 times)

0 Members and 1 Guest are viewing this topic.

Offline JumpMan

  • C= 64
  • **
  • Posts: 45
  • Karma: 11
    • View Profile
OGL Cube with windows.[BMAX]
« on: October 01, 2007 »
Hi everybody
I have been spending what ever time I have free to myself(almost none) to learn OGL. I have been going to websites and found some tutorials. To make it more complicated for me to learn, Most of them are in cpp of which I know little of. I have been forced to learn as I go. To make a long story short, I just want to share some of the things I am learning. I can't remember where I got this demo from but I converted it to BMax.  It took me a long time sence there is not much help at Bmax for OGL. if any body have some suggestions on what I can improve or how I can do it better  I would apreciate it.
Code: [Select]
Framework BRL.GLGraphics
Import BRL.EventQueue
Import BRL.PNGLoader

'-----------------------------------------------------------------------------
'           Name: ogl_alpha_blending_texture.cpp
'         Author: Kevin Harris
'  Last Modified: 03/25/05
'    Description: This sample demonstrates how To perform alpha blending using
'                 the alpha channel of a standard .png texture. For proper
'                 alpha blending, the sample uses a cull-mode sorting trick
'                 To ensure the sides of the textured cube get rendered in
'                 back-To-front order.
'
'   Control Keys: b - Toggle blending
'                 s - Toggle usage of cull-mode sorting trick
'                 Up Arrow - Move the test cube closer
'                 Down Arrow - Move the test cube away
'-----------------------------------------------------------------------------
SuperStrict
Const stric% = 1
Const WIN32_LEAN_AND_MEAN% = 1

'
Type Vertex
    Field tu#, tv#;
    Field x#, y#, z#;
End Type
Type point
Field x%,y%
End Type

Global g_bBlending% = True;
Global g_bSortUsingCullModeTrick% = True;
Global ptLastMousePosit:point = New point
Global ptCurrentMousePosit:point = New point
Global bMousing%;
Global oldx%,oldy%
Global g_fDistance# = -4.5;
Global g_fSpinX#    = 0.0;
Global g_fSpinY#    = 0.0;
Global Checkimage:Byte[256,256,4]
Global Texname:Int
Global g_textureID% = -1
Global g_cubeVertices:Float[] = [..
.. ' Front Face
 0.0, 0.0, -1.0, -1.0, 1.0,..       ' Bottom Left Of The Texture And Quad
 1.0, 0.0,  1.0, -1.0, 1.0,..       ' Bottom Right Of The Texture And Quad
 1.0, 1.0,  1.0,  1.0, 1.0,..       ' Top Right Of The Texture And Quad
 0.0, 1.0, -1.0,  1.0, 1.0,..       ' Top Left Of The Texture And Quad
.. ' Back Face
 1.0, 0.0, -1.0, -1.0, -1.0,..      ' Bottom Right Of The Texture And Quad
 1.0, 1.0, -1.0,  1.0, -1.0,..      ' Top Right Of The Texture And Quad
 0.0, 1.0,  1.0,  1.0, -1.0,..      ' Top Left Of The Texture And Quad
 0.0, 0.0,  1.0, -1.0, -1.0,..      ' Bottom Left Of The Texture And Quad
.. ' Top Face
 0.0, 1.0, -1.0,  1.0, -1.0,..      ' Top Left Of The Texture And Quad
 0.0, 0.0, -1.0,  1.0,  1.0,..      ' Bottom Left Of The Texture And Quad
 1.0, 0.0,  1.0,  1.0,  1.0,..      ' Bottom Right Of The Texture And Quad
 1.0, 1.0,  1.0,  1.0, -1.0,..      ' Top Right Of The Texture And Quad
.. ' Bottom Face
 1.0, 1.0, -1.0, -1.0, -1.0,..      ' Top Right Of The Texture And Quad
 0.0, 1.0,  1.0, -1.0, -1.0,..      ' Top Left Of The Texture And Quad
 0.0, 0.0,  1.0, -1.0,  1.0,..      ' Bottom Left Of The Texture And Quad
 1.0, 0.0, -1.0, -1.0,  1.0,..      ' Bottom Right Of The Texture And Quad
.. ' Right face
 1.0, 0.0,  1.0, -1.0, -1.0,..      ' Bottom Right Of The Texture And Quad
 1.0, 1.0,  1.0,  1.0, -1.0,..      ' Top Right Of The Texture And Quad
 0.0, 1.0,  1.0,  1.0,  1.0,..      ' Top Left Of The Texture And Quad
 0.0, 0.0,  1.0, -1.0,  1.0,..      ' Bottom Left Of The Texture And Quad
.. ' Left Face
 0.0, 0.0, -1.0, -1.0, -1.0,..      ' Bottom Left Of The Texture And Quad
 1.0, 0.0, -1.0, -1.0,  1.0,..      ' Bottom Right Of The Texture And Quad
 1.0, 1.0, -1.0,  1.0,  1.0,..      ' Top Right Of The Texture And Quad
 0.0, 1.0, -1.0,  1.0, -1.0]      ' Top Left Of The Texture And Quad

'-----------------------------------------------------------------------------
' PROTOTYPES
'-----------------------------------------------------------------------------
'Int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
'   LPSTR lpCmdLine, Int nCmdShow);
'LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
'void loadTexture(void);
'void init(void);
'void render(void);
'void shutDown(void);

'-----------------------------------------------------------------------------
' Name: WinMain()
' Desc: The application's entry point
'-----------------------------------------------------------------------------

GLGraphics 800,600
oldx = MouseX()
oldy = MouseY()

init();
Repeat

callback();
render();
GLDrawText("B - blending ",10,1)
GLDrawText("s - cull sorting",10,15)
GLDrawText("up/down arrow - aproach",10,30)
GLDrawText("(esc) to Exit",10,45)
Flip()
Forever 'Until KeyDown(KEY_ESCAPE)

End

'-----------------------------------------------------------------------------
' Name: WindowProc()
' Desc: The window's message handler
'-----------------------------------------------------------------------------
Function CALLBACK%()

    Select True
Case KeyHit(KEY_B)
            g_bBlending = Not g_bBlending;
        Case KeyHit(KEY_S)
            g_bSortUsingCullModeTrick = Not g_bSortUsingCullModeTrick;
        Case KeyDown(KEY_ESCAPE)
End
        Case KeyDown(KEY_UP) ' Up Arrow Key
             g_fDistance :- 0.1;
        Case KeyDown(KEY_DOWN)'  Down Arrow Key
            g_fDistance :+ 0.1;
End Select
Select PollEvent()

        Case EVENT_MOUSEDOWN
ptLastMousePosit.x = ptCurrentMousePosit.x = MouseX()
            ptLastMousePosit.y = ptCurrentMousePosit.y = MouseY()
bMousing = True;
Case EVENT_MOUSEMOVE

ptCurrentMousePosit.x = MouseX()
ptCurrentMousePosit.y = MouseY()

If( bMousing )

g_fSpinX :- (ptCurrentMousePosit.x - ptLastMousePosit.x);
g_fSpinY :- (ptCurrentMousePosit.y - ptLastMousePosit.y);
EndIf

ptLastMousePosit.x = ptCurrentMousePosit.x;
            ptLastMousePosit.y = ptCurrentMousePosit.y;
End Select
If Not MouseDown(1) bMousing = False;

End Function

'-----------------------------------------------------------------------------
' Name: loadTexture()
' Desc:
'-----------------------------------------------------------------------------
Function loadTexture( )
Local PointeurImg:Byte Ptr
Local TexWidth%
Local TexHeight%
Local tex01:TPixmap=LoadPixmap("radiation_box.png")
TexWidth=tex01.Width
TexHeight=tex01.Height
PointeurImg=PixmapPixelPtr(tex01,0,0)
Local pp%=0
For Local y%=TexHeight-1 To 0 Step -1
For Local x%=0 To TexWidth-1
If x> 28 And x< (TexWidth-29) And y > 28 And y<(TexHeight-29)
Checkimage[y,x,0]=PointeurImg[pp]
Checkimage[y,x,1]=PointeurImg[pp+1]
Checkimage[y,x,2]=PointeurImg[pp+2]
Checkimage[y,x,3]=160
Else
Checkimage[y,x,0]=PointeurImg[pp]
Checkimage[y,x,1]=PointeurImg[pp+1]
Checkimage[y,x,2]=PointeurImg[pp+2]
Checkimage[y,x,3]=255
EndIf
pp=pp+3
Next
Next
tex01=Null
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glGenTextures(1, Varptr Texname)
glBindTexture(GL_TEXTURE_2D, Texname)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TexWidth, TexHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, Checkimage)
End Function


'-----------------------------------------------------------------------------
' Name: init()
' Desc:
'-----------------------------------------------------------------------------
Function init()
loadTexture();
glClearColor( 0.35, 0.53, 0.7, 1.0 );
glEnable( GL_TEXTURE_2D );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, 640.0 / 480.0, 0.1, 100.0);
End Function


'-----------------------------------------------------------------------------
' Name: render()
' Desc:
'-----------------------------------------------------------------------------
Function render( )

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    glTranslatef( 0.0, 0.0, g_fDistance );
    glRotatef( -g_fSpinY, 1.0, 0.0, 0.0 );
    glRotatef( -g_fSpinX, 0.0, 1.0, 0.0 );

Rem
    
     Transparency sorting For our cube...
    
     If you have a single transparent Object, Or multiple transparent objects
     which do Not overlap in screen space (i.e., each screen pixel is touched
     by at most one of the transparent objects), there's a sorting short-cut
     which can be used under certain conditions.
    
     If your transparent objects are closed, convex, And viewed from the
     outside, culling may be used To draw the back-facing polygons prior To
     the front-facing polygons. This will accomplish the same thing
     as sorting your objects Or polygons into back-To-front order.
     Fortunately For us, our cube is a perfect candidate For this sorting
     trick.
    
     On the other hand, If we can't use the cull-mode sorting trick, we would
     need To sort our objects manually, which would require us To transform
     the geometry into eye-space so we could compare their Final position
     along the z axis. Only Then, could we could render them in the proper
     back-To-front order For alpha blending.
    
     Also, If transparent objects intersect in any way, the individual
     triangles of the objects touching will have To be sorted And drawn
     individually from back-To-front. And is some rare cases, triangles that
     intersect each other may have To be broken into smaller triangles so they
     no longer intersect Or blending artifacts will persist regardless of our
     sorting efforts.
    
     It’s plain To see, transparency sorting can become a big, hairy mess real quick.
    
     http:www.opengl.org/resources/tutorials/sig99/advanced99/notes/node204.html
    
EndRem
If( g_bBlending = True )

'        
'         Use the texture's alpha channel to blend it with whatever’s already
'         in the frame-buffer.
'        

glDisable( GL_DEPTH_TEST );

        glEnable( GL_BLEND );
        glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

        glBindTexture( GL_TEXTURE_2D, texname);

       If( g_bSortUsingCullModeTrick = True )
   
'            
'             Use the cull-mode sorting trick For convex non-overlapping
'             geometry.
'            

            glEnable( GL_CULL_FACE );

'            
'             Render the cube but only render the back-facing polygons.
'            

            glCullFace( GL_FRONT );

            glInterleavedArrays( GL_T2F_V3F, 0, g_cubeVertices );
glDrawArrays( GL_QUADS, 0, 24 );

'            
'             Render the cube again, but this time we only render the
'             front-facing polygons.
'            

            glCullFace( GL_BACK );

           glInterleavedArrays( GL_T2F_V3F, 0, g_cubeVertices );
           glDrawArrays( GL_QUADS, 0, 24 );

           glDisable( GL_CULL_FACE );
      
        Else
'        
'            
'             Do no sorting And hope For the best. From certain viewing
'             positions the cube's sides will appear sorted correctly, but this
'             is typically rare And the cube will Not look Right most of the
'             time.
'            

            glInterleavedArrays( GL_T2F_V3F, 0, g_cubeVertices );
            glDrawArrays( GL_QUADS, 0, 24 );
        EndIf'
'
Else
'
'        
'         Render the cube, but do no blending...
'        

glDisable( GL_BLEND );
glEnable( GL_DEPTH_TEST );

        glBindTexture( GL_TEXTURE_2D, Texname);
        glInterleavedArrays( GL_T2F_V3F, 0, g_cubeVertices );
        glDrawArrays( GL_QUADS, 0, 24 );
EndIf

End Function


<edit>
forgot:
hold left mouse button down and move  mouse to rotate cube.

executable included.
« Last Edit: October 01, 2007 by JumpMan »

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: OGL Cube with windows.[BMAX]
« Reply #1 on: October 01, 2007 »
It looks pretty well coded to me.  I don't really have anything to add :)

Jim
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17426
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: OGL Cube with windows.[BMAX]
« Reply #2 on: October 02, 2007 »
Runs really nice, thanks for sharing your source Jumpman :)
Shockwave ^ Codigos
Challenge Trophies Won: