Skip to main content
gamedev.net
🔒 Locked

Problems with this beginning OpenGL program

Started by tamakala Aug 6, 2008 at 6:26 PM 11 replies 2.4k views
Original Post
tamakala
tamakala
Can anyone help me ?
#include <windows.h>
#include <gl\gl.h>
LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
    switch(msg)
	{  
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd,msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR,int nShowCmd)
{
    WNDCLASSEX wc;
	wc.cbClsExtra = 0;
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wc.hIconSm = LoadIcon(hInstance,(LPCTSTR)IDI_APPLICATION);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "MyWindowClassName";
	wc.lpszMenuName = NULL;
	wc.style = 0;
	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL,TEXT("This program requires Windows NT!"),TEXT("Error"),MB_ICONERROR);
		return 0;
	}
	HWND hWnd = CreateWindowEx(
		0,wc.lpszClassName,
		TEXT("My Window Name"),
		WS_OVERLAPPEDWINDOW | WS_VISIBLE,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		NULL,NULL,hInstance,NULL);
	ShowWindow(hWnd,nShowCmd);
	UpdateWindow(hWnd);
	HDC hDC = GetDC(hWnd);
	HGLRC hRC = wglCreateContext(hDC);
	wglMakeCurrent(hDC,hRC);
	MSG msg;
	while(msg.message!=WM_QUIT)
    {
        if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            glClearColor(0.0, 0.0, 0.0, 0.0);
            glClear(GL_COLOR_BUFFER_BIT);
            glColor3f(1.0, 1.0, 1.0);
            glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
            glBegin(GL_POLYGON);
            glVertex2f(-0.5, -0.5);
            glVertex2f(-0.5, 0.5);
            glVertex2f(0.5, 0.5);
            glVertex2f(0.5, -0.5);
            glEnd();
            glFlush();
        }
    }
    wglMakeCurrent (NULL, NULL);
    wglDeleteContext (hRC);
    ReleaseDC (hWnd, hDC);
    UnregisterClass("MyWindowClassName", wc.hInstance );
	return 0;
}
I compile it successfully, but it does not run as expected. It only shows main window, and nothing else happens (the window screen is supposed to clear with black color and a white shape should appear)
santonel
santonel
You need to call SwapBuffers() after you're done drawing. Right now it's just drawing to the back buffer but you need to call SwapBuffers to flip the back buffer to the screen so it can be seen.
tamakala
tamakala
It still does not work
glClearColor(0.0, 0.0, 0.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0, 1.0, 1.0);glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);glBegin(GL_POLYGON);glVertex2f(-0.5, -0.5);glVertex2f(-0.5, 0.5);glVertex2f(0.5, 0.5);glVertex2f(0.5, -0.5);glEnd();SwapBuffers(hDC);glFlush();

Can anyone help me ?
Anyway, when I change
SwapBuffers(hDC);
by
SwapBuffers(wglGetCurrentDC());
it still does not work
Barking_Mad
Barking_Mad
Check your near and far plane values, i think they should be reversed (i.e. your looking the wrong way).
openGL Coordinate system has the z axis negative with depth, so your far clipping plane is +1 and your near is -1 which puts your object out of view frustum. Either that or your polygon is back-to-front because your planes are inverted and its not visible because your looking at the backface. Reversing the polygon vertex sequence or inverting the planes should fix either way.

See here for further info

I could be wrong.

[Edited by - Barking_Mad on August 7, 2008 5:22:21 AM]
datalurkur
datalurkur
If this is your first OpenGL program, it might be beneficial for you to start with some basecode that you know works. Nehe.gamedev.net has lots of good basecode to work with. Once you're more comfortable with the way OpenGL works, then maybe you can start messing with window code (which is arguably the most gruesome part of starting with GL).
Sanzennin
Sanzennin
Quote:
Original post by V-man
I don't see a call to SetPixelFormat

I dont see call to SetPixelFormat in many of my projects =D

Anyways, I dont get it why people keep using I dont like it not a bit, Id rather add a check that checks if the operating system is windows, and then tells user that this system is too viruscontaniable, and closes for its own safety.

Anyways, say, whats the color of the background you see? "nothing else happens" cant be true, since there will be something in the background. Is it A) Black B) White C) Transparent D)Something that changes shape constantly, like broken telly E) something else?
tamakala
tamakala
Thank you a lot, the color is B)white
And whatever color I change for glClear, the color of the window is still white
I do not use NEHE because the tutorial in NEHE is a little bit compicated. I need something more simple so I only use opengl.org Blue Book.
I know window is not a good OS but I will not choose any Linux distro because I need the simplest way first. It is not good if I begin with Linux, find document about setup and programing in Linux environment. I will learn later but first of all I desire my first openGL program must work.
Somebody, can you compile the source code, run it and show me why my code goes wrong ? Thank you again.
tamakala
tamakala
Thank you, I already try it, but it still does not work.
My code comes from OpenGL Blue Book, so I don't think the problem is far and near, there must be something else
Anyway, I compiled with both Dev-Cpp and Visual Studio 6.0, but the problem still exists
tamakala
tamakala
so no one knows the problem ? too sad. After two weeks this one is unable to fix, after I try every possible way, even with Pixel Fomat
empirical2
empirical2
Ill take you through what I did to get it to work. There were two issues with it.


The first is i broke it just before the message loop and found the hardware context was 0. So wglCreateContext(hDC) was failing. Inserting the pixel format code before hand made this return a valid value.

Second, it still did not work, I saw that swap buffers was not in so I added that.

I now have a black screen with a white rectangle in it. This is what you were after?

Source code below.

#include "stdafx.h"#pragma comment (lib,"GLaux.lib")//Links the libs in VC6 automaticaly#pragma comment (lib,"OpenGL32.lib")//Laziness on my part you can remove#pragma comment (lib,"GLu32.lib")#include <windows.h>#include <gl\gl.h>LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam){    switch(msg)	{  	case WM_DESTROY:		PostQuitMessage(0);		break;	}	return DefWindowProc(hWnd,msg,wParam,lParam);}int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR,int nShowCmd){    WNDCLASSEX wc;	wc.cbClsExtra = 0;	wc.cbSize = sizeof(WNDCLASSEX);	wc.cbWndExtra = 0;	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);	wc.hCursor = LoadCursor(NULL,IDC_ARROW);	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);	wc.hIconSm = LoadIcon(hInstance,(LPCTSTR)IDI_APPLICATION);	wc.hInstance = hInstance;	wc.lpfnWndProc = WndProc;	wc.lpszClassName = "MyWindowClassName";	wc.lpszMenuName = NULL;	wc.style = 0;	if(!RegisterClassEx(&wc))	{		MessageBox(NULL,TEXT("This program requires Windows NT!"),TEXT("Error"),MB_ICONERROR);		return 0;	}	HWND hWnd = CreateWindowEx(		0,wc.lpszClassName,		TEXT("My Window Name"),		WS_OVERLAPPEDWINDOW | WS_VISIBLE,		CW_USEDEFAULT,		CW_USEDEFAULT,		CW_USEDEFAULT,		CW_USEDEFAULT,		NULL,NULL,hInstance,NULL);	ShowWindow(hWnd,nShowCmd);	UpdateWindow(hWnd);	HDC hDC = GetDC(hWnd);//Begin Added Pixel Format code	GLuint PixelFormat;						// Holds The Results After Searching For A Match				static	PIXELFORMATDESCRIPTOR pfd={sizeof(PIXELFORMATDESCRIPTOR),1,PFD_DRAW_TO_WINDOW |PFD_SUPPORT_OPENGL |PFD_DOUBLEBUFFER,PFD_TYPE_RGBA,24,0, 0, 0, 0, 0, 0,0,0,0,0, 0, 0, 0,		16,								// 16Bit Z-Buffer (Depth Buffer)		0,0,PFD_MAIN_PLANE,0,0, 0, 0};		if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))		{			MessageBox(hWnd,"No good pixel format","Error",0);			return 0;		}		//Activate rendering context		SetPixelFormat(hDC,PixelFormat,&pfd);//End Added Code	HGLRC hRC = wglCreateContext(hDC);	wglMakeCurrent(hDC,hRC);	MSG msg;	while(msg.message!=WM_QUIT)    {        if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))        {            TranslateMessage(&msg);            DispatchMessage(&msg);        }        else        {            glClearColor(0.0, 0.0, 0.0, 0.0);            glClear(GL_COLOR_BUFFER_BIT);            glColor3f(1.0, 1.0, 1.0);            glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);            glBegin(GL_POLYGON);            glVertex2f(-0.5, -0.5);            glVertex2f(-0.5, 0.5);            glVertex2f(0.5, 0.5);            glVertex2f(0.5, -0.5);            glEnd();            SwapBuffers(hDC);//Added this & got rid of Flush, not needed        }    }    wglMakeCurrent (NULL, NULL);    wglDeleteContext (hRC);    ReleaseDC (hWnd, hDC);    UnregisterClass("MyWindowClassName", wc.hInstance );	return 0;}
tamakala
tamakala
IT WORKS, FINALLY !
THANK YOU A LOT, MR empirical2 (sorry i am not sure you are a lady or gentleman)
And thank everyone as well

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.