hi…
My code:
#pragma comment(lib, “opengl32.lib”)
#pragma comment(lib, “glu32.lib”)
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include
using namespace std;#define WIN32_LEAN_AND_MEAN
bool RegisterWinClass(HINSTANCE &, WNDPROC, LPSTR);
bool CreateWinEx(HWND &, HINSTANCE &, LPSTR, LPSTR);
bool SetupPixelFormat(HDC &);
void InitialOGLScreen(int, int);
bool InitialOGL(HDC &, HWND &, HGLRC &, RECT &);
void Render();
WPARAM MainLoop();
LRESULT CALLBACK MainMsg(HWND, UINT, WPARAM, LPARAM);HDC g_hDc = NULL;
HWND g_hWnd = NULL;
HGLRC g_hGLrc = NULL;
PAINTSTRUCT g_ps;
RECT g_rect;int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
{
if(!RegisterWinClass(hInstance, MainMsg, “OGLTest3”))
{
return -1;
}if(!CreateWinEx(g_hWnd, hInstance, “OGLTest3”, “OpenGL Test 3”))
{
return -2;
}if(!InitialOGL(g_hDc, g_hWnd, g_hGLrc, g_rect))
{
return -3;
}ShowWindow(g_hWnd, SW_SHOWNORMAL);
UpdateWindow(g_hWnd);
SetFocus(g_hWnd);return (int)MainLoop();
}bool RegisterWinClass(HINSTANCE &hInstance, WNDPROC wndProc, LPSTR className)
{
WNDCLASSEX wcx;
wcx.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.hInstance = hInstance;
wcx.lpfnWndProc = wndProc;
wcx.lpszClassName = className;wcx.hbrBackground = NULL;
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hIconSm = LoadIcon(NULL, IDI_WINLOGO);wcx.cbClsExtra = NULL;
wcx.cbWndExtra = NULL;
wcx.lpszMenuName = NULL;if(!RegisterClassEx(&wcx))
{
return false;
}return true;
}bool CreateWinEx(HWND &hWnd, HINSTANCE &hInstance, LPSTR className, LPSTR winCaption)
{
hWnd = CreateWindowEx(NULL,
className,
winCaption,
WS_OVERLAPPEDWINDOW,
0,
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);if(!hWnd)
{
return false;
}return true;
}void Paint(HWND &hWnd, PAINTSTRUCT &ps)
{
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}bool SetupPixelFormat(HDC &hDc)
{
PIXELFORMATDESCRIPTOR pfd = {0};
int iPixelFormat = 0;pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.dwLayerMask = PFD_MAIN_PLANE;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 16; // or 16
pfd.cDepthBits = 16; // or 16
pfd.cAccumBits = 0;
pfd.cStencilBits = 0;if((iPixelFormat = ChoosePixelFormat(hDc, &pfd)) == FALSE)
{
return false;
}if(SetPixelFormat(hDc, iPixelFormat, &pfd) == FALSE)
{
return false;
}return true;
}void InitialOGLScreen(int width, int height)
{
if(height == 0)
{
height = 1;
}glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 1.0f, 150.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}bool InitialOGL(HDC &hDc, HWND &hWnd, HGLRC &hGLrc, RECT &rect)
{
hDc = GetDC(hWnd);if(!SetupPixelFormat(hDc))
{
return false;
}hGLrc = wglCreateContext(hDc);
if(wglMakeCurrent(hDc, hGLrc) == FALSE)
{
return false;
}glEnable(GL_DEPTH_TEST);
GetClientRect(hWnd, &rect);
InitialOGLScreen(rect.right, rect.bottom);
return true;
}void Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing The Triangle
glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 Units
glColor3f(0.5f,0.5f,1.0f); // Set The Color To Blue One Time Only
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd();SwapBuffers(g_hDc);
//SwapBuffers(hDC);
}WPARAM MainLoop()
{
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
bool done = false;while(!done)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
done = true;
break;
}TranslateMessage(&msg); DispatchMessage(&msg); } else { Render(); }
}
return msg.wParam;
}LRESULT CALLBACK MainMsg(HWND hWnd, UINT uiMsg, WPARAM wParam,
LPARAM lParam)
{
switch(uiMsg)
{
case WM_PAINT:
{
BeginPaint(hWnd, &g_ps);
EndPaint(hWnd, &g_ps);
break;
}
case WM_SIZE:
{
InitialOGLScreen(LOWORD(lParam), HIWORD(wParam));
GetClientRect(hWnd, &g_rect);
break;
}
case WM_CLOSE:
{
PostQuitMessage(0);
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
{
break;
}
}return DefWindowProc(hWnd, uiMsg, wParam, lParam);
}
The problem is there are no objects appear on the screen… and the whole screen just in black color