Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


OpenGL Triangle not being displayed


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 MindOfCorruption97   Members   -  Reputation: 140

Like
0Likes
Like

Posted 26 December 2012 - 02:43 PM

Hello gents, I require some assistance. For some reason, my win32 program refuses to display a triangle. I've tried to find ways to fix it, but my attempts seem to result in failures.
Here is the code to my program.

 

My window class.

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

#include "EditorLink.h"

const char g_szClassName[] = "Window";
BOOL quit = FALSE;

void EnableOpenGL(HDC hDc,HGLRC hRc);
void DisableOpenGL(HDC hDc,HGLRC hRc);

Editor Edit;

LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            quit = TRUE;
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd,msg,wParam,lParam);
    }
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR CmdLine,int nCmdShow)
{
    WNDCLASS wc;
    HWND hWnd;
    HGLRC hRc;
    HDC hDc;
    MSG msg;
    
    wc.lpszClassName = g_szClassName;
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.hInstance = hInstance;
    wc.style = CS_OWNDC;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.lpfnWndProc = WndProc;
    wc.lpszMenuName = NULL;
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.hIcon = LoadIcon(NULL,NULL);
    RegisterClass(&wc);
    
    hWnd = CreateWindow(
            g_szClassName,
            "OPENGL PRACTICE",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT,CW_USEDEFAULT,700,700,
            NULL,NULL,hInstance,NULL
            );
    
    ShowWindow(hWnd,nCmdShow);
    UpdateWindow(hWnd);
    
    EnableOpenGL(hDc,hRc);
    Edit.init();
    
    while(!quit)
    {
        if(GetMessage(&msg,NULL,0,0) > 0)
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            Edit.display();
            SwapBuffers(hDc);
        }
    }
    
    DisableOpenGL(hDc,hRc);
    DestroyWindow(hWnd);
    
    return msg.wParam;
}


void EnableOpenGL(HDC hDc,HGLRC hRc)
{
    int nFormat;
    
    PIXELFORMATDESCRIPTOR pfd =
    {
        sizeof(PIXELFORMATDESCRIPTOR),
        1,
        PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA,
        0,0,0,0,0,0,0,0,0,0,0,0,0,
        24,
        8,
        0,
        PFD_MAIN_PLANE,
        0,0,0,0
    };
    
    nFormat = ChoosePixelFormat(hDc,&pfd);
    SetPixelFormat(hDc,nFormat,&pfd);
    
    hRc = wglCreateContext(hDc);
    wglMakeCurrent(hDc,hRc);
}

void DisableOpenGL(HDC hDc,HGLRC hRc)
{
    wglMakeCurrent(NULL,NULL);
    wglDeleteContext(hRc);
}

 

And my class where I entered the display data.

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

#include "EditorLink.h"

void Editor::init()
{
    glClearColor(0.0,0.0,0.0,1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45,640.0/480.0,1.0,500.0);
    glMatrixMode(GL_MODELVIEW);
}

void Editor::display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    
    glBegin(GL_TRIANGLES);
    glVertex3f(0.0,2.0,-5.0);
    glVertex3f(-2.0,-2.0,-5.0);
    glVertex3f(2.0,-2.0,-5.0);
    glEnd();
}

 

I don't know what is wrong. So some help would be nice.

Thank you and have a nice day.



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8570

Like
2Likes
Like

Posted 26 December 2012 - 03:27 PM

You're passing hDc and hRc to EnableOpenGL by value but expect the function to change the values in your main function since you're using those variables later. Pass them by reference instead if you expect the function to change the original variables and not the parameter variables local to the function itself. You're also not creating the device context anywhere; you need to call GetDC to actually get the value for hDc.

 

You're missing many of these errors because you're not checking the return values from the functions. You definitely should start checking for errors from the functions you call; they are there for a reason.



#3 MindOfCorruption97   Members   -  Reputation: 140

Like
0Likes
Like

Posted 26 December 2012 - 04:19 PM

You're passing hDc and hRc to EnableOpenGL by value but expect the function to change the values in your main function since you're using those variables later. Pass them by reference instead if you expect the function to change the original variables and not the parameter variables local to the function itself. You're also not creating the device context anywhere; you need to call GetDC to actually get the value for hDc.

 

You're missing many of these errors because you're not checking the return values from the functions. You definitely should start checking for errors from the functions you call; they are there for a reason.

Thank you for that. You saved me from future errors with my program.

But after looking at the code bit.

I switched

GetMessage(&msg,NULL,0,0) > 0

with

 

PeekMessage(&msg,NULL,0,0,PM_REMOVE)

 

making the program work. Thank you for giving me more insight on opengl.

:D






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS