Jump to content
  • Advertisement
Sign in to follow this  
MindOfCorruption97

OpenGL OpenGL Triangle not being displayed

This topic is 2097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!