4,448
社区成员
发帖
与我相关
我的任务
分享
class CBox
{
private:
int mWindowWidth;
int mWindowHeight;
float mAngle;
float s;
float c;
public:
CBox();
~CBox();
bool init();
bool ShutDown();
void SetProjection(int width, int height);
void Prepare(float fAngle);
void Render();
void Light();
};
float light[] = {1.0, 0.0, 0.0, 1.0};//灯光颜色
float position[] = {0.0, 1.0, 0.0, 1.0};//灯光位置,为点光源
float direction[] = {-1.0, -1.0, 0.0};//灯光方向
void CBox::Light()//设置灯光
{
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light);
glLightfv(GL_LIGHT0, GL_SPECULAR, light);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 0.0);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 30.0);
glEnable(GL_LIGHT0);
}
float material1[] = {0.0, 0.0, 0.0, 1.0};//材质1不反射任何颜色
float material2[] = {1.0, 1.0, 1.0, 1.0};//材质2反射所以颜色
float x = 1.0;
void CBox::Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glLoadIdentity();
gluLookAt(2.0, -2.0, 0.0, -1.0, 1.0, 0.0, 0.0, 0.0, 1.0);
Light();
glMaterialfv(GL_FRONT,GL_AMBIENT, material1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, material1);
glMaterialfv(GL_FRONT, GL_SPECULAR, material2);//使平面只有反射光
glBegin(GL_QUADS);//在X轴-1出垂直X轴画一个平面
glNormal3f(1.0, 0.0, 0.0);
glVertex3f(-x, x, x);
glVertex3f(-x, x, -x);
glVertex3f(-x, -x, -x);
glVertex3f(-x, -x, x);
glEnd();
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX windowClass;
HWND hwnd;
MSG msg;
DWORD dwExStyle;
DWORD dwStyle;
RECT windowRect;
windowRect.left = (long)0;
windowRect.right = (long)windowWidth;
windowRect.top = (long)0;
windowRect.bottom = (long)windowHeight;
memset(&windowClass, 0, sizeof(WNDCLASSEX));
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = MainWindowProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = NULL;
windowClass.lpszClassName = "OpenGL";
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
if( !RegisterClassEx(&windowClass))
return 0;
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle = WS_OVERLAPPEDWINDOW;
AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);
hwnd = CreateWindowEx(NULL, "OpenGL",
"myOpenGL", dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
0, 0,
windowRect.right - windowRect.left,
windowRect.bottom - windowRect.top,
NULL, NULL, hInstance, NULL);
hDC = GetDC(hwnd);
mBox = new CBox;
if( !hwnd )
return 0;
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
mBox->init();
while( exiting )
{
mBox->Prepare(0.0f);
mBox->Render();
SwapBuffers(hDC);
while( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) )
{
if( !GetMessage(&msg, NULL, 0, 0) )
{
// exiting = true;
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}