PDA

View Full Version : Why my code doesn't show nothing?



09-03-2000, 09:12 AM
I've written a program that tries the basics of OpenGL, but problem is it shows nothing, but code looks correct (I don't claim it is, I just say it looks correct).

Please help!

Please send a correction to mike14@inter.net.il.
Thanks in advance.

Code:
//OpenGL program.

#define NAME "OpenGLappWnd1"
#define TITLE "OpenGL program."

#include<windows.h>
#include<gl\gl.h>
#include<gl\glu.h>
///variables that will be used in the program,

HWND hwnd;
HINSTANCE hInstance;
HDC hdc;
HGLRC hglrc;

//////

/////// functions that will be used in a program

LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);

///////
void Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glBegin(GL_TRIANGLES);
glTranslatef(-0.2,0,0);
glColor3i(1,0,0);
glVertex3f(0,1,1);
glColor3i(0,1,0);
glVertex3f(-1,-0.5,1);
glColor3i(0,0,1);
glVertex3f(1,-0.5,1);
glEnd();
glPopMatrix();
glFlush();
SwapBuffers(hdc);
}

void InitGL()
{
glViewport(0,0,640,480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(55,640/480,0.1,10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);

}
BOOL InitPixel(HDC hDC)
{
PIXELFORMATDESCRIPTOR pfd;
int Pixel;
ZeroMemory(&pfd, sizeof(pfd));
pfd.nSize=sizeof(pfd);
pfd.nVersion=1;
pfd.dwFlags=PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL;
pfd.iPixelType=PFD_TYPE_RGBA;
pfd.cColorBits=32;
pfd.cDepthBits=16;
pfd.iLayerType=PFD_MAIN_PLANE;
Pixel=ChoosePixelFormat(hDC,&pfd);
SetPixelFormat(hDC, Pixel, &pfd);
DescribePixelFormat(hDC,Pixel,sizeof(PIXELFORMATDE SCRIPTOR),&pfd);
ReleaseDC(hwnd,hDC);
return TRUE;
}

WINAPI WinMain(HINSTANCE hThisInst,HINSTANCE hPrevInst,
LPSTR lpszArgs,int nCmdLine)
{
MSG msg;
WNDCLASS wc;

hInstance=hThisInst;//saved in global variable for future use in other functions

wc.style=CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc=MainWndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground= (HBRUSH) GetStockObject(BLACK_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName=NAME;

RegisterClass(&wc);

hwnd = CreateWindow(NAME,
TITLE,
WS_OVERLAPPEDWINDOW,
0,0,640,480,
HWND_DESKTOP,
NULL,
hThisInst,
NULL);
hdc=GetDC(hwnd);
InitPixel(hdc);
hglrc=wglCreateContext(hdc);
wglMakeCurrent(hdc,hglrc);
InitGL();
ShowWindow(hwnd,SW_NORMAL);
UpdateWindow(hwnd);

while( GetMessage(&msg, NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return TRUE;
}

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
switch(message)
{
case WM_PAINT:
Render();
BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
wglMakeCurrent(NULL,NULL);
wglDeleteContext(hglrc);
ReleaseDC(hwnd,hdc);
PostQuitMessage(0);
break;

default : return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}

Dang
09-03-2000, 02:44 PM
I don't know what language that is. Is that c++? From a c standpoint, you don't even have an int main() function that I can find.

bugatti
09-03-2000, 11:17 PM
add glTranslatef(0.0,0.0,-5.0);
between glClear(...) and glPushMatrix()
in Render()

Bob
09-04-2000, 06:24 AM
Beginer: Transformationcalls is not allower between a glBegin/glEnd pair. So, either remove, or move that glTranslatef in your Render function. And you don't have any errorchecking code. Maybe you don't even get a valid rendering context, and therefore won't see anything. So, check the result of ALL functions for errorcodes. Just to make sure this is/isn't the problem, add a unsigned char vendor=glGetString(GL_VENDOR) or similar after calling InitGL, and see what you get. A NULL-string means no context.

Dang: This is C. But the entry point in Win32 environement is called WinMain.

Pete Bassett
09-04-2000, 06:34 AM
Originally posted by Dang:
I don't know what language that is. Is that c++? From a c standpoint, you don't even have an int main() function that I can find.

This is a Windows app. They begin with WinMain

Pete

masterpoi
09-04-2000, 08:37 AM
you forgot to call the render function in you message loop :-)

09-04-2000, 09:35 AM
Originally posted by masterpoi:
you forgot to call the render function in you message loop :-)

Check WM_PAINT message.

09-04-2000, 09:49 AM
Originally posted by Bob:
Beginer: Transformationcalls is not allower between a glBegin/glEnd pair. So, either remove, or move that glTranslatef in your Render function. And you don't have any errorchecking code. Maybe you don't even get a valid rendering context, and therefore won't see anything. So, check the result of ALL functions for errorcodes. Just to make sure this is/isn't the problem, add a unsigned char vendor=glGetString(GL_VENDOR) or similar after calling InitGL, and see what you get. A NULL-string means no context.

Dang: This is C. But the entry point in Win32 environement is called WinMain.

I recieve an error : "Non portable pointer convertion" when I declare vendor as unsgined char and then save result form glGetString to it. What is return type of glGetString, and how should I declare vendor?
Thanks in advance.

P.S. Thanks to all of you guys, I realy appreciate ANY help.

Bob
09-05-2000, 02:29 AM
Not completely sure if it's suppose to be an unsinged char, but I definitely know i forgot to add a '*'.

unsigned char *vendor = glGetString(GL_VENDOR)

Maybe this works better.

09-05-2000, 08:49 AM
Originally posted by Bob:
Not completely sure if it's suppose to be an unsinged char, but I definitely know i forgot to add a '*'.

unsigned char *vendor = glGetString(GL_VENDOR)

Maybe this works better.

Thanks a lot, but I already solved the problem. I thought that I can check if context exist's by using glClearColor. What I mean is I didn't set the clear color, so it had to be default one (Black or transparent, I'm not sure which). So I used white as clear color and saw white background and black triangle in the middle. Then I understood that the problem was my wrong usage of glColor3i. I actually thougt I know how to use it, but now I realized I over-thougt myself. I tried glColor3f (that I know how to use) and got the colors. Code is still buggy, because when I move a window it's contents goes away. I'll debug it, I think I know what's the problem there.

Thanks to everybody for giving me tips and pointing my mistakes. Thanks a lot.
I'll be glad to help if some-one would need my help.
Thanks once again and bye.