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(PIXELFORMATDESCRIPTOR),&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;
}