1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
#include "stdafx.h"
#include "windows.h"
#include "windowsx.h"
#include "gl\gl.h"
#include "gl\glaux.h"
#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "glu32.lib")
#pragma comment (lib, "glaux.lib")
LRESULT WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wparam,LPARAM lparam);
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS w;
memset(&w,0,sizeof(WNDCLASS));
w.style = CS_HREDRAW | CS_VREDRAW;
w.lpfnWndProc = WndProc;
w.hInstance = hInstance;
w.hbrBackground = GetStockBrush(WHITE_BRUSH);
w.hCursor = LoadCursor(NULL, IDC_ARROW);
w.lpszClassName = "Test2";
RegisterClass(&w);
hwnd = CreateWindow("Test2","Test2", WS_OVERLAPPEDWINDOW,
60,60,600,480,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
void setDCPixelFormat(HDC hdc)
{
static PIXELFORMATDESCRIPTOR pfd;
memset(&pfd,0,sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
int pixelFormat;
pixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, pixelFormat, &pfd);
DescribePixelFormat(hdc, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
if (pfd.dwFlags & PFD_NEED_PALETTE)
MessageBox(NULL, "Wrong video mode", "Attention", MB_OK);
}
void init()
{
GLfloat lightAmbient[4]={0.1f,0.1f,0.1f,1.0f};
GLfloat lightDiffuse[4]={0.7f,0.7f,0.7f,1.0f};
GLfloat lightSpecular[4]={0.0f,0.0f,0.0f,1.0f};
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 0.0);
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHT0);
}
void drawscene()
{
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
auxSolidSphere(0.3);
glFlush();
}
LRESULT WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wparam,LPARAM lparam)
{
static HDC hdc;
static HGLRC hrc;
static UINT timer;
int temp;
PAINTSTRUCT ps;
GLsizei width, height;
switch (Message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
setDCPixelFormat(hdc);
hrc = wglCreateContext(hdc);
temp = wglMakeCurrent(hdc, hrc);
init();
timer = SetTimer(hwnd,1,50,NULL);
return 0;
case WM_SIZE:
width = (GLsizei)LOWORD(lparam);
height = (GLsizei)HIWORD(lparam);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, width/height, 0.1, 200.0);
gluLookAt(0, 0, -1, 0, 0, 0, 0, 1, 0);
glViewport(0, 0, width, height);
return 0;
case WM_DESTROY:
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hrc);
KillTimer(hwnd, timer);
PostQuitMessage(0);
return 0;
case WM_PAINT:
BeginPaint(hwnd, &ps);
drawscene();
EndPaint(hwnd, &ps);
case WM_TIMER:
InvalidateRect(hwnd, NULL, FALSE);
return 0;
}
return DefWindowProc(hwnd,Message,wparam,lparam);
}
|