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 - Minimal Code

  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin OpenGL Texture Mapping - Minimal Code

    The minimal OpenGL code example I just posted is a good vehicle for showing how to implement texture mapping in your PowerBASIC application.

    In this example, a single rectangle (quad) is drawn and an image (from a
    file) is mapped to the rectangle.

    A discussion thread is here.

    Two new, short procedures are added to the OpenGL minimal code I posted to implement texture mapping.

    CreateTexture simply creates an array of color values needed by OpenGL. It uses only PowerBASIC statements - no OpenGL commands.

    InitializeMapping enables texture mapping and binds the color array to the OpenGL scene.

    Finally, the DrawScene procedure now includes statements which tie
    each corner of the image to each corner (vertex) of the rectangle (quad) that is drawn.

    Here's the simple output - an image drawn onto a rectangle.

    I'll be putting a texture mapping tutorial online later today that goes into more detail.

    'Compilable Example:
    #Compile Exe
    #Dim All
    #Include ""
    #Include ""
    #Include ""
    Global hDlg, hDC, hRC, hBMP As DWord
    Global BMPData() As Long
    Function PBMain() As Long
      Dialog New Pixels, 0, "OpenGL Example",,, 320, 240,%WS_OverlappedWindow To hDlg
      Dialog Show Modal hdlg Call dlgproc
    End Function
    CallBack Function dlgproc()
       Select Case CB.Msg
          Case %WM_InitDialog : GetRenderContext
          Case %WM_Size       : ResizeScene Lo(Word, CB.lParam), Hi(Word, CB.lParam)
       End Select
    End Function
    Sub GetRenderContext
       hDC = GetDC(hDlg)                                      'DC for dialog
       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 ResizeScene (w As Long, h As Long)
       glViewport 0, 0, w, h             'resize viewport to match window size
       glLoadIdentity                    'reset the projection matrix
       gluPerspective 45, w/h, 0.1, 100  'calculate the aspect ratio of the Window
    End Sub
    Sub DrawScene
       glBegin %gl_quads
          glTexCoord2f 0.0, 0.0: glVertex3f -1.0, -1.0, -5.0  ' Bottom Left Of The Texture And Quad
          glTexCoord2f 1.0, 0.0: glVertex3f  1.0, -1.0, -5.0  ' Bottom Right Of The Texture And Quad
          glTexCoord2f 1.0, 1.0: glVertex3f  1.0,  1.0, -5.0  ' Top Right Of The Texture And Quad
          glTexCoord2f 0.0, 1.0: glVertex3f -1.0,  1.0, -5.0  ' Top Left Of The Texture And Quad
       SwapBuffers hDC        'display the buffer (image)
    End Sub
    Sub CreateTexture
       Local i,j,imgW,imgH As Long, sFileName As String
       sFileName = "cowgirl.bmp"
       imgW=100 : imgH = 100
       Graphic Bitmap Load sFileName, imgW, imgH To hBMP
       Graphic Attach hBMP, 0
       Dim BMPData(imgW-1,imgH-1)
       For i = 0 to imgW-1
          For j = 0 to imgH-1                                                                                   
             Graphic Get Pixel (i,j) To BMPData(i,imgH-1-j)
          Next j
       Next i
    End Sub
    Sub InitializeMapping
       glEnable %gl_texture_2D
       glBindTexture %gl_texture_2D, 1
       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, BMPData(0)
    End Sub
    Updated 6/14 to remove these lines from InitializeMapping procedure to go a bit more minimal:
    glTexEnvf %gl_texture_env, %gl_texture_env_mode, %gl_modulate
    glTexParameteri %gl_texture_2d,%gl_texture_mag_filter,%gl_linear
    Last edited by Gary Beene; 14 Jun 2010, 08:15 PM.

  • #2
    Hi Gary!!

    ok deleted!!


    Jurgen Huhn

    Last edited by Jurgen Huhn; 17 Jun 2010, 09:49 AM. Reason: Source code forum is not for discussions


    • #3
      Since the source code forum is not for discussions, let's head over to the discussion thread and talk about your question.