Wednesday, 19 September 2012

LAB 1


BIT20203: Graphics Programming

LAB SHEET 1

Title                          : First Step to Graphics Programming using OpenGL
Objectives           : At the end of the session, students are able to:
                                          i.         Install Visual C++
                                         ii.         Setup OpenGL
                                       iii.         Create simple graphics
Duration               : 2 Hours
Tasks                       :

1.     Download and install Microsoft Visual C++ 2008 Express Edition from the following website:
http://www.microsoft.com/express/vc/

2.     Install Windows SDK for Windows Server 2008 and .NET Framework 3.5 which contains the main OpenGL libraries.

a.     Download the Windows SDK for Windows Server 2008 and .NET Framework  from the Microsoft website

3.     Install the GLUT Libraries
a.     Download the GLUT from  Nate Robbins website:

b.     Unzip them and do the following:
                                               i.     Copy all the .h files into the C:\Program Files\Microsoft SDKs\Windows\v6.1\Include\GL folder. This should be glut.h, freeglut.h, freeglut_ext.h, and freeglut_std.h
                                             ii.     Copy all the .lib files into the C:\Program Files\Microsoft SDKs\Windows\v6.1\Lib folder. This should be freeglut.lib and glut32.lib.
                                            iii.     Copy all the .dll files into the C:\Windows\system32 folder. This should be freeglut.dll and glut32.dll

4.     Run Visual C++ and create a new project:
a.     Under the File menu select New → Project (Ctrl+Shift+N)
b.     Select Win32 Project, enter a Name, and click OK
c.      In the Wizard click Next, then check the box next to Empty Project, and click Finish

5.     Add a new source file for the project
a.     Under the Project menu select Add New Item (Ctrl+Shift+A)
b.     Select C++ File (.cpp), enter a Name, and click OK

6.     Link to the OpenGL libraries
a.     Under the Project menu select Project Properties (Alt+F7) at the bottom
b.     Select Configuration Properties → Linker → Input from the navigation panel on the left
c.     Select All Configurations from the Configuration drop-down box at the top of the dialog. This ensures you are changing the settings for both the Debug and Release configurations
d.     Type “opengl32.lib glu32.lib” in Additional Dependencies and click OK

7.     Write the following code.
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

HWND    hWnd;
HDC     hDC;
HGLRC   hRC;

// Set up pixel format for graphics initialization
void SetupPixelFormat()
{
    PIXELFORMATDESCRIPTOR pfd, *ppfd;
    int pixelformat;

    ppfd = &pfd;

    ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
    ppfd->nVersion = 1;
    ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    ppfd->dwLayerMask = PFD_MAIN_PLANE;
    ppfd->iPixelType = PFD_TYPE_COLORINDEX;
    ppfd->cColorBits = 16;
    ppfd->cDepthBits = 16;
    ppfd->cAccumBits = 0;
    ppfd->cStencilBits = 0;

    pixelformat = ChoosePixelFormat(hDC, ppfd);
    SetPixelFormat(hDC, pixelformat, ppfd);
}

// Initialize OpenGL graphics
void InitGraphics()
{
    hDC = GetDC(hWnd);

    SetupPixelFormat();

    hRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC, hRC);

    glClearColor(0, 0, 0, 0.5);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
}

// Resize graphics to fit window
void ResizeGraphics()
{
    // Get new window size
    RECT rect;
    GetClientRect(hWnd, &rect);
    int width = rect.right;
    int height = rect.bottom;

    GLfloat aspect = (GLfloat) width / height;

    // Adjust graphics to window size
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, aspect, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

// Draw frame
void DrawGraphics()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Set location in front of camera
    glLoadIdentity();
    glTranslated(0, 0, -10);

    // Draw a square
    glBegin(GL_QUADS);
    glColor3d(1, 0, 0);
    glVertex3d(-2, 2, 0);
    glVertex3d(2, 2, 0);
    glVertex3d(2, -2, 0);
    glVertex3d(-2, -2, 0);
    glEnd();

    // Show the new scene
    SwapBuffers(hDC);
}

// Handle window events and messages
LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM  wParam, LPARAM  lParam)
{
    switch (uMsg)
    {
    case WM_SIZE:
        ResizeGraphics();
        break;

    case WM_CLOSE:
        DestroyWindow(hWnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    // Default event handler
    default:
        return DefWindowProc (hWnd, uMsg, wParam, lParam);
        break;
    }
     return 1;
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

    const LPCWSTR appname = TEXT("OpenGL Sample");

    WNDCLASS wndclass;
    MSG      msg;

    // Define the window class
    wndclass.style         = 0;
    wndclass.lpfnWndProc   = (WNDPROC)MainWndProc;
    wndclass.cbClsExtra    = 0;
    wndclass.cbWndExtra    = 0;
    wndclass.hInstance     = hInstance;
    wndclass.hIcon         = LoadIcon(hInstance, appname);
    wndclass.hCursor       = LoadCursor(NULL,IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wndclass.lpszMenuName  = appname;
    wndclass.lpszClassName = appname;

    // Register the window class
    if (!RegisterClass(&wndclass)) return FALSE;

    // Create the window
    hWnd = CreateWindow(
            appname,
            appname,
            WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            800,
            600,
            NULL,
            NULL,
            hInstance,
            NULL);

    if (!hWnd) return FALSE;

    // Initialize OpenGL
    InitGraphics();

    // Display the window
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    // Event loop
    while (1)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)
        {
            if (!GetMessage(&msg, NULL, 0, 0)) return TRUE;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        DrawGraphics();
    }

        wglDeleteContext(hRC);
        ReleaseDC(hWnd, hDC);
}


8.     Compile and run. If everything is correct you should see a red square when running the code


~ end ~



ALTERNATIVES (in case above didnt work)


Setting Up Compilers
  • Windows Using MS Visual C++Installing GLUT
    1. Most of the following files (ie. OpenGL and GLU) will already be present if you have installed MS Visual C++ v5.0 or later. The following GLUT files will need to be copied into the specified directories.
    2. To install:
Compiling OpenGL/GLUT Programs
  1. Create a new project:
    • choose File | New from the File Menu
    • select the Projects tab
    • choose Win32 Console Application
    • fill in your Project name
  2. Designate library files for the linker to use:
    • choose Project | Settings from the File Menu
    • under Object/library modules: enter "opengl32.lib glu32.lib glut32.lib"
  3. Add/Create files to the project:
    • choose Project | Add to Project | Files from the File menu
    • add the required program files
  4. Build and Execute



No comments:

Post a Comment