Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

OpenGL Texture Mapping - Rotating Cube (Multiple Images)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin OpenGL Texture Mapping - Rotating Cube (Multiple Images)

    The earlier posting I made of a rotating cube used a single image for all faces. This example shows how to select the image that you put on each face.




    Code:
    'Compilable Example:
    #Compile Exe
    #Dim All
    #Include "win32api.inc"
    #Include "gl.inc"
    #Include "glu.inc"
    
    %ID_Timer = 1000 : %ID_Label = 1002 : %ID_Graphic = 1003
    
    Global hDlg, hDC, hRC, hLabel, hBMP1, hBMP2 As DWord
    Global scalefactor As Single
    Global BMPData1(), BMPData2(), imgW, imgH As Long
    
    Function PBMain() As Long
       CreateTexture
       Dialog New Pixels, 0, "OpenGL Example",,, 400, 200,%WS_OverlappedWindow To hDlg
       Control Add Graphic, hDlg, %ID_Graphic, "", 10,10,imgW,imgH
       Graphic Attach hDlg, %ID_Graphic
       Graphic Copy hBMP2, 0
       Control Add Label, hdlg, %ID_Label,"",120,10,imgW,imgH, %WS_Child Or %WS_Visible Or %SS_Sunken Or %SS_Notify
       Dialog Show Modal hdlg Call dlgproc
    End Function
    
    CallBack Function dlgproc()
       Local pt As Point
       Local XDelta, YDelta As Single
       Static SpinInWork,XLast,YLast As Long
    
       Select Case CB.Msg
          Case %WM_InitDialog : GetRenderContext
                                InitializeScene
                                SetTimer(hDlg, %ID_Timer, 50, %NULL)
                                ScaleFactor = 1
          Case %WM_Timer      : DrawScene 1,1,0  'redraw with rotation on all 3 axes
          Case %WM_Paint      : DrawScene 0,0,0  'redraw with no rotation
          Case %WM_Size       : Control Set Size hDlg, %ID_Label, Lo(Word, CB.lParam)-130, Hi(Word, CB.lParam)-20
                                ResizeScene Lo(Word, CB.lParam)-130, Hi(Word, CB.lParam)-20
                                DrawScene 0,0,0  'redraw with no rotation
          Case %WM_Close      : wglmakecurrent %null, %null 'unselect rendering context
                                wgldeletecontext hRC        'delete the rendering context
                                releasedc hDlg, hDC         'release device context
          Case %WM_MouseWheel
             Select Case Hi(Integer,CB.wParam)
                Case > 0  : ScaleFactor = ScaleFactor + 0.1 : DrawScene 0,0,0
                Case < 0  : ScaleFactor = ScaleFactor - 0.1 : DrawScene 0,0,0
             End Select
          Case %WM_SetCursor
             GetCursorPos pt          'p.x and p.y are in screen coordinates
             ScreenToClient hDlg, pt  'p.x and p.y are now dialog client coordinates
             If GetDlgCtrlID(ChildWindowFromPoint( hDlg, pt.x, pt.y )) <> %ID_Label Then Exit Function
             Select Case Hi(Word, CB.lParam)
                Case %WM_LButtonDown
                   KillTimer CB.Hndl, %ID_Timer
                   SpinInWork = 1
                   GetCursorPos pt              'pt has xy screen coordinates
                   ScreenToClient hDC, pt       'pt now has dialog client coordinates
                   XLast = Pt.x
                   YLast = Pt.y
                Case %WM_MouseMove
                   If SpinInWork Then
                      GetCursorPos pt           'pt has xy screen coordinates
                      ScreenToClient hDC, pt    'pt now has dialog client coordinates
                      XDelta = XLast - Pt.x
                      YDelta = YLast - Pt.y
                      DrawScene -YDelta, -XDelta, 0
                      XLast = pt.x
                      YLast = pt.y
                   End If
                Case %WM_LButtonUp
                   SetTimer(hDlg, %ID_Timer, 50, %NULL)
                   SpinInWork = 0
             End Select
       End Select
    End Function
    
    Sub GetRenderContext
       Local pfd As PIXELFORMATDESCRIPTOR   'pixel format properties for device context
       pfd.nSize       =  SizeOf(PIXELFORMATDESCRIPTOR)
       pfd.nVersion    =  1
       pfd.dwFlags     = %pfd_draw_to_window Or %pfd_support_opengl Or %pfd_doublebuffer
       pfd.dwlayermask = %pfd_main_plane
       pfd.iPixelType  = %pfd_type_rgba
       pfd.ccolorbits  = 24
       pfd.cdepthbits  = 24
    
       Control Handle hdlg, %ID_Label To hLabel
       hDC = GetDC(hLabel)
       SetPixelFormat(hDC, ChoosePixelFormat(hDC, pfd), pfd)  'set properties of device context
       hRC = wglCreateContext (hDC)                           'get rendering context
       wglMakeCurrent hDC, hRC                                'make the RC current
    End Sub
    
    Sub InitializeScene
       InitializeMapping
       glClearColor 0,0,0,0     'sets color to be used with glClear
       glClearDepth 1           'sets zvalue to be used with glClear
       glEnable %gl_depth_test                             'enable depth testing
       glHint %gl_perspective_correction_hint, %gl_nicest  'best quality rendering
    End Sub
    
    Sub ResizeScene (w As Long, h As Long)
       glViewport 0, 0, w, h               'resize viewport to match window size
       glMatrixMode %gl_projection         'select the projection matrix
       glLoadIdentity                    'reset the projection matrix
       gluPerspective 45, w/h, 0.1, 100  'set frustum using viewport aspect ratio
       glMatrixMode %gl_modelview          'select the modelview matrix
    End Sub
    
    Sub DrawScene (dx As Single, dy As Single, dz As Single)
       Static anglex, angley, anglez As Single
       glClear %gl_color_buffer_bit Or %gl_depth_buffer_bit  'clear buffers
       glLoadIdentity               'clear the modelview matrix
    
       gluLookAt 0,0,6,0,0,0,0,1,0
    
       glScalef scalefactor, scalefactor, scalefactor
    
       anglex = anglex + dx : glRotatef anglex, 1,0,0
       angley = angley + dy : glRotatef angley, 0,1,0
       anglez = anglez + dz : glRotatef anglez, 0,0,1
    
       DrawBox
    
       SwapBuffers hDC              'display the buffer (image)
    End Sub
    
    Sub InitializeMapping
       glEnable %gl_texture_2D
       glTexEnvf %gl_texture_env, %gl_texture_env_mode, %gl_modulate
       glBindTexture %gl_texture_2D, 1
       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_linear
       glTexParameteri %gl_texture_2d,%gl_texture_min_filter,%gl_linear
       glTexImage2D %gl_texture_2D, 0, %gl_rgb, 100, 100, 0, %gl_rgba, %gl_unsigned_byte, BMPData1(0,0)
    
       glBindTexture %gl_texture_2D, 2
       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_linear
       glTexParameteri %gl_texture_2d,%gl_texture_min_filter,%gl_linear
       glTexImage2D %gl_texture_2D, 0, %gl_rgb, 100, 100, 0, %gl_rgba, %gl_unsigned_byte, BMPData2(0,0)
    End Sub
    
    Sub DrawBox
       glBindTexture %gl_texture_2D, 2
       glBegin %gl_quads
           ' Front Face
           glTexCoord2f 0.0, 0.0: glVertex3f -1.0, -1.0, 1.0       ' Bottom Left Of The Texture And Quad
           glTexCoord2f 1.0, 0.0: glVertex3f  1.0, -1.0, 1.0       ' Bottom Right Of The Texture And Quad
           glTexCoord2f 1.0, 1.0: glVertex3f  1.0,  1.0, 1.0       ' Top Right Of The Texture And Quad
           glTexCoord2f 0.0, 1.0: glVertex3f -1.0,  1.0, 1.0       ' Top Left Of The Texture And Quad
       glEnd
       glBindTexture %gl_texture_2D, 2
       glBegin %gl_quads
           ' Back Face
           glTexCoord2f 1.0, 0.0: glVertex3f -1.0, -1.0, -1.0      ' Bottom Right Of The Texture And Quad
           glTexCoord2f 1.0, 1.0: glVertex3f -1.0,  1.0, -1.0      ' Top Right Of The Texture And Quad
           glTexCoord2f 0.0, 1.0: glVertex3f  1.0,  1.0, -1.0      ' Top Left Of The Texture And Quad
           glTexCoord2f 0.0, 0.0: glVertex3f  1.0, -1.0, -1.0      ' Bottom Left Of The Texture And Quad
       glEnd
       glBindTexture %gl_texture_2D, 1
       glBegin %gl_quads
           ' Top Face
           glTexCoord2f 0.0, 1.0: glVertex3f -1.0,  1.0, -1.0      ' Top Left Of The Texture And Quad
           glTexCoord2f 0.0, 0.0: glVertex3f -1.0,  1.0,  1.0      ' Bottom Left Of The Texture And Quad
           glTexCoord2f 1.0, 0.0: glVertex3f  1.0,  1.0,  1.0      ' Bottom Right Of The Texture And Quad
           glTexCoord2f 1.0, 1.0: glVertex3f  1.0,  1.0, -1.0      ' Top Right Of The Texture And Quad
       glEnd
       glBindTexture %gl_texture_2D, 1
       glBegin %gl_quads
           ' Bottom Face
           glTexCoord2f 1.0, 1.0: glVertex3f -1.0, -1.0, -1.0      ' Top Right Of The Texture And Quad
           glTexCoord2f 0.0, 1.0: glVertex3f  1.0, -1.0, -1.0      ' Top Left Of The Texture And Quad
           glTexCoord2f 0.0, 0.0: glVertex3f  1.0, -1.0,  1.0      ' Bottom Left Of The Texture And Quad
           glTexCoord2f 1.0, 0.0: glVertex3f -1.0, -1.0,  1.0      ' Bottom Right Of The Texture And Quad
       glEnd
       glBindTexture %gl_texture_2D, 1
       glBegin %gl_quads
           ' Right face
           glTexCoord2f 1.0, 0.0: glVertex3f  1.0, -1.0, -1.0      ' Bottom Right Of The Texture And Quad
           glTexCoord2f 1.0, 1.0: glVertex3f  1.0,  1.0, -1.0      ' Top Right Of The Texture And Quad
           glTexCoord2f 0.0, 1.0: glVertex3f  1.0,  1.0,  1.0      ' Top Left Of The Texture And Quad
           glTexCoord2f 0.0, 0.0: glVertex3f  1.0, -1.0,  1.0      ' Bottom Left Of The Texture And Quad
       glEnd
       glBindTexture %gl_texture_2D, 2
       glBegin %gl_quads
           ' Left Face
           glTexCoord2f 0.0, 0.0: glVertex3f -1.0, -1.0, -1.0      ' Bottom Left Of The Texture And Quad
           glTexCoord2f 1.0, 0.0: glVertex3f -1.0, -1.0,  1.0      ' Bottom Right Of The Texture And Quad
           glTexCoord2f 1.0, 1.0: glVertex3f -1.0,  1.0,  1.0      ' Top Right Of The Texture And Quad
           glTexCoord2f 0.0, 1.0: glVertex3f -1.0,  1.0, -1.0      ' Top Left Of The Texture And Quad
       glEnd
    End Sub
    
    Sub GetBitMapDimensions(sFileName$, w As Long, h As Long)
       Open sFileName$ For BINARY As #1
       Get #1, 19, w
       Get #1, 23, h
       Close #1
    End Sub
    
    Sub CreateTexture
       Local i,j As Long, sFileName As String
    
       sFileName = "cowgirl.bmp"
       GetBitMapDimensions(sFileName, imgW, imgH)
       Graphic Bitmap Load sFileName, imgW, imgH To hBMP1
       Graphic Attach hBMP1, 0
       Dim BMPData1(imgW-1,imgH-1)
       For i = 0 To imgW-1
          For j = 0 To imgH-1
             Graphic Get Pixel (i,j) To BMPData1(i,j)
          Next j
       Next i
    
       sFileName = "garyfaceb.bmp"
       GetBitMapDimensions(sFileName, imgW, imgH)
       Graphic Bitmap Load sFileName, imgW, imgH To hBMP2
       Graphic Attach hBMP2, 0
       Dim BMPData2(imgW-1,imgH-1)
       For i = 0 To imgW-1
          For j = 0 To imgH-1
             Graphic Get Pixel (i,j) To BMPData2(i,imgH-1-j)
          Next j
       Next i
    End Sub
    
    
    'gbs_00616
    Last edited by Gary Beene; 16 Jun 2010, 01:21 PM.
Working...
X