Original Post
I wrote a simple wiggle (wgl) app and the window opens but it doesnt show the triangle I made with : glBegin(GL_TRIANGLES); // draws triangle glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(1.0f, 0.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.0f); glEnd(); btw how do you set the number of units in glVertex3f(0.0f, 0.0f, 0.0f); for the x y and z axis is + or - one pixel? ... Here is the full source sorry to dump it like this, but I dont know why it isnt showing the triangle!
// Win32 OpenGL Program
#define WIN32_LEAN_AND_MEAN // trims off win32 libraries
// includes
#include <windows.h> // standard Windows app include
#include <gl/gl.h> // standard OpenGL include
#include <gl/glu.h> // OpenGL Utilities
/////////////// Global Variables ////////////////////
HDC g_HDC;
float angle = 0.0f;
void SetupPixelFormat(HDC hDC);
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
// the main entry point to your program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLiner, int nShowCmd)
{
WNDCLASSEX windowClass; // windows class
HWND hwnd; // window handle
MSG msg; // message
bool done; // flag saying when your app is complete
// fill out the windows class structure
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL,IDC_ARROW);
windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "MyClass";
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
// register the windows class
if (!RegisterClassEx(&windowClass))
{
return 0;
}
// class registered, so now create your window
hwnd = CreateWindowEx(NULL, // extended style
"MYCLASS", // class name
"A REAL WINDOWS APP", // app name
WS_OVERLAPPEDWINDOW | // window style
WS_VISIBLE |
WS_SYSMENU,
100, 100, // x and y coordinates
400,400, // width , height
NULL, // handle to parent
NULL, // handle menu
hInstance, // application instance
NULL); // no extra params
// check if window creation failed (hwnd would equal NULL)
if (!hwnd)
return 0;
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
done = false; // intialize the loop condition variable
// main message loop
while (!done)
{
PeekMessage(&msg, hwnd, NULL, NULL , PM_REMOVE);
if (msg.message == WM_QUIT) // do you recieve a WM_QUIT message?
done = true; // if so, time to quit the app
else
{
// do rendering here
//clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
angle = angle + 1.0f; // increase rotation value
if (angle >= 360.0f)
angle = 0;
glTranslatef(0.0f,0.0f,5.0f); // move back five units
glRotatef(angle, 0.0f , 0.0f, 1.0f); // rotate along z axis
glColor3f(1.0f, 1.0f, 0.0f); // set color to red
glBegin(GL_TRIANGLES); // draws triangle
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glEnd();
SwapBuffers(g_HDC); // swap buffers
TranslateMessage(&msg); // translate and dispatch to event queue
DispatchMessage(&msg);
}
}
return msg.wParam;
}
// Windows procedures function
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HGLRC hRC;
static HDC hDC; // device context
char string[] = "Hello , World"; // text to be displayed
int width, height;
switch(message)
{
case WM_CREATE: // windows creation
{
hDC = GetDC(hwnd); // get current window's device context
g_HDC = hDC;
SetupPixelFormat(hDC); // call your pixel format setup function
// create rendering context....................
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
return 0; // return to message loop
break;
}
case WM_CLOSE: // close message
{
// deselect rendering context and delete it
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
PostQuitMessage(0); // send quit message
return 0;
break;
}
case WM_SIZE:
{
height = HIWORD(lParam); // retrieve width and height
width = LOWORD(lParam);
if (height==0)
height = 1;
}
// reset the vieport to new dimensions
glViewport(0, 0, width , height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// calculate aspect ratio of window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);
glMatrixMode(GL_MODELVIEW); // set modelview matrix
glLoadIdentity(); // reset modelview matrix
return 0;
break;
default:
break;
}
// pass all unhandles messages to DefWindowProc
return (DefWindowProc(hwnd,message,wParam,lParam));
}
// function to set pixel fromat for the device context
void SetupPixelFormat(HDC hDC) {
int nPixelFormat;
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // size of the structure
1, // version, always set to 1
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // support double buffering
PFD_TYPE_RGBA, // RGBA color mode
32, // go for 32 bit color mode
0, 0, 0, 0, 0, 0, // ignore color bits, not used
0, // no alpha buffer
0, // ignore shift bit
0, // no accumulation buffer
0, 0, 0, 0, // ignore accumulation bits
16, // 16 bit z buffer size
0, // no stencil buffer
0, // no auxiliary plane
PFD_MAIN_PLANE, // main drawing plane
0, // reserved
0, 0, 0}; // layer masks ignored
// choose best matching pixel format retun index
nPixelFormat = ChoosePixelFormat(hDC, &pfd);
// set pixel format to device context
SetPixelFormat(hDC, nPixelFormat, &pfd);
}