Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Help!

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2003
    Posts
    4

    Help!

    Hi,
    I'm really new to OpenGL, and I ran into a problem I cant seem to fix. I am working out of a book, and one of the chapters in the book is how to initialize an OpenGl session, create a red triangle, and rotate it. I typed in the code exactly from the book (I think) and all I get is the window, but no triangle. Please tell me what is wrong with my code!

    Code:
    ///Triangle.cpp
    #define WIN32_LEAN_AND_MEAN
    // Includes

    #include <windows.h>
    #include <gl/gl.h>
    #include <gl/glu.h>
    #include <gl/glaux.h>

    // Global Variables

    float angle = 0.0f;
    HDC g_HDC;


    // Enable OpenGL

    void SetupPixelFormat(HDC hDC)
    {
    int nPixelFormat;

    static PIXELFORMATDESCRIPTOR pfd = {
    sizeof(PIXELFORMATDESCRIPTOR),
    1,
    PFD_DRAW_TO_WINDOW |
    PFD_SUPPORT_OPENGL |
    PFD_DOUBLEBUFFER,
    PFD_TYPE_RGBA,
    32,
    0, 0, 0, 0, 0, 0,
    0,
    0,
    0,
    0, 0, 0, 0,
    16,
    0,
    0,
    PFD_MAIN_PLANE,
    0,
    0, 0, 0 };
    nPixelFormat = ChoosePixelFormat(hDC, &pfd);

    SetPixelFormat(hDC, nPixelFormat, &pfd);
    }

    LRESULT CALLBACK
    WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
    {
    static HDC hDC;
    static HGLRC hRC;
    char string[] = "Hello World!";
    int height, width;

    switch ( message ) {

    case WM_CREATE:
    hDC = GetDC(hWnd);
    g_HDC = hDC;
    SetupPixelFormat(hDC);

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

    return 0;
    break;

    case WM_CLOSE:

    wglMakeCurrent(hDC, NULL);
    wglDeleteContext(hRC);

    PostQuitMessage( 0 );

    return 0;
    break;

    case WM_SIZE:
    height = HIWORD(lParam);
    width = LOWORD(wParam);

    if (height==0)
    {
    height=1;
    }
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height, 1.0f, 1000.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    return 0;
    break;

    default:
    break;

    }

    return (DefWindowProc(hWnd, message, wParam, lParam));

    }// WinMain

    int WINAPI
    WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
    {
    WNDCLASSEX wc;
    MSG msg;
    BOOL bQuit = FALSE;
    HWND hWnd;

    // register window class
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground = NULL;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "MyClass";
    wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

    if (!RegisterClassEx(&wc))
    return 0;

    // create main window
    hWnd = CreateWindowEx(
    NULL
    "MyClass",
    "Triangle",
    WS_OVERLAPPEDWINDOW | WS_VISIBLE |
    WS_SYSMENU | WS_CLIPCHILDREN |
    WS_CLIPSIBLINGS,
    100, 100,
    400, 400,
    NULL,
    NULL,
    hInstance,
    NULL );

    if (!hWnd)
    return 0;

    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);

    // program main loop
    while ( !bQuit ) {

    // check for messages
    PeekMessage( &msg, NULL, 0, 0, PM_REMOVE );

    // handle or dispatch messages
    if ( msg.message == WM_QUIT )
    {
    bQuit = TRUE;
    }
    else
    {
    // OpenGL animation code
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    angle = angle + 0.1f;
    if (angle >= 360.0f)
    angle = 0.0f;
    glTranslatef(0.0f,0.0f,-5.0f);
    glRotatef(angle, 0.0f, 0.0f, 1.0f);

    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_TRIANGLES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 0.0f);
    glEnd();

    SwapBuffers(g_HDC);

    TranslateMessage(&msg);
    DispatchMessage(&msg);

    }

    }

    return msg.wParam;

    }

  2. #2
    Intern Contributor
    Join Date
    Apr 2003
    Location
    Turin, Italy
    Posts
    94

    Re: Help!

    hi dude...
    i just gave a fasto look to your code (i am at work and my boss doesn't really like me having fun on GL forums). However... if i were u i'd try to get rid of the gltranslatef (0,0,-5).

    for the little i saw, u don't have any gluLook at function in your code, so the camera should be stick in the origin, focused on the z axis, positive facing.
    I am afraid u are actually drawing the triangle, but outside the screen.
    Alternatively u may add AFTER the glLoadIdentity part a nice
    gluLookAt (0,0,5,0,0,-5,0,1,0);
    so u are quite sure that your camera is aiming on the triangle.
    Hope it will solve
    Bye The Gunslinger

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2003
    Posts
    4

    Re: Help!

    Hey,
    thanks for the help, but it STILL doesnt work! Anyway, here is my GL code right now.......

    //Begin OpenGl code
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt (0,0,5,0,0,-5,0,1,0);

    angle = angle + 0.1f;
    if (angle >= 360.0f)
    angle = 0.0f;
    glRotatef(angle, 0.0f, 0.0f, 1.0f);

    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_TRIANGLES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 0.0f);
    glEnd();

    SwapBuffers(g_HDC);

    TranslateMessage(&msg);
    DispatchMessage(&msg);

    [This message has been edited by Z (edited 12-17-2003).]

  4. #4
    Intern Contributor
    Join Date
    Jul 2002
    Location
    Seri Kembangan, Selangor, Malaysia
    Posts
    92

    Re: Help!

    quad:
    ---------------------------------------------
    //Begin OpenGl code
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt (0,0,5,0,0,-5,0,1,0);

    angle = angle + 0.1f;
    if (angle >= 360.0f)
    angle = 0.0f;
    glRotatef(angle, 0.0f, 0.0f, 1.0f);

    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_TRIANGLES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 0.0f);
    glEnd();

    SwapBuffers(g_HDC);

    ---------------------------------------------

    Try to put glPushMatrix()and glPopMatrix();
    It will look like....

    //Begin OpenGl code
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt (0,0,5,0,0,-5,0,1,0);

    angle = angle + 0.1f;
    if (angle >= 360.0f)
    angle = 0.0f;
    glPushMatrix();
    glRotatef(angle, 0.0f, 0.0f, 1.0f);

    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_TRIANGLES);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 0.0f);
    glEnd();
    glPopMatrix();

    SwapBuffers(g_HDC);

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2003
    Posts
    4

    Re: Help!

    still doesnt work! maybe its my compiler... I'm using Dev-C++....
    If anyone has any suggestions PLEASE reply!

    thanks in advance,
    ~Z~

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •