📄 quadrics.cpp
字号:
MessageBox(NULL,"Unable to create device context", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
// set the pixel format we want
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // size of structure
1, // default version
PFD_DRAW_TO_WINDOW | // window drawing support
PFD_SUPPORT_OPENGL | // OpenGL support
PFD_DOUBLEBUFFER, // double buffering support
PFD_TYPE_RGBA, // RGBA color mode
bits, // 32 bit color mode
0, 0, 0, 0, 0, 0, // ignore color bits, non-palettized mode
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 buffer
PFD_MAIN_PLANE, // main drawing plane
0, // reserved
0, 0, 0 }; // layer masks ignored
GLuint pixelFormat;
// choose best matching pixel format
if (!(pixelFormat = ChoosePixelFormat(g_hdc, &pfd)))
{
MessageBox(NULL, "Can't find an appropriate pixel format", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
// set pixel format to device context
if(!SetPixelFormat(g_hdc, pixelFormat,&pfd))
{
MessageBox(NULL, "Unable to set pixel format", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
// create the OpenGL rendering context
if (!(g_hrc = wglCreateContext(g_hdc)))
{
MessageBox(NULL, "Unable to create OpenGL rendering context", "Error",MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
// now make the rendering context the active one
if(!wglMakeCurrent(g_hdc, g_hrc))
{
MessageBox(NULL,"Unable to activate OpenGL rendering context", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
// show the window in the forground, and set the keyboard focus to it
ShowWindow(g_hwnd, SW_SHOW);
SetForegroundWindow(g_hwnd);
SetFocus(g_hwnd);
// set up the perspective for the current screen size
ResizeScene(width, height);
// do one-time initialization
if (!InitializeScene())
{
MessageBox(NULL, "Initialization failed", "Error", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
return TRUE;
} // end SetupWindow()
/*****************************************************************************
KillWindow()
Deletes the DC, RC, and Window, and restores the original display.
*****************************************************************************/
BOOL KillWindow()
{
// restore the original display if we're in fullscreen mode
if (g_isFullscreen)
{
ChangeDisplaySettings(NULL, 0);
ShowCursor(TRUE);
}
// if we have an RC, release it
if (g_hrc)
{
// release the RC
if (!wglMakeCurrent(NULL,NULL))
{
MessageBox(NULL, "Unable to release rendering context", "Error", MB_OK | MB_ICONINFORMATION);
}
// delete the RC
if (!wglDeleteContext(g_hrc))
{
MessageBox(NULL, "Unable to delete rendering context", "Error", MB_OK | MB_ICONINFORMATION);
}
g_hrc = NULL;
}
// release the DC if we have one
if (g_hdc && !ReleaseDC(g_hwnd, g_hdc))
{
MessageBox(NULL, "Unable to release device context", "Error", MB_OK | MB_ICONINFORMATION);
g_hdc = NULL;
}
// destroy the window if we have a valid handle
if (g_hwnd && !DestroyWindow(g_hwnd))
{
MessageBox(NULL, "Unable to destroy window", "Error", MB_OK | MB_ICONINFORMATION);
g_hwnd = NULL;
}
// unregister our class so we can create a new one if we need to
if (!UnregisterClass(WND_CLASS_NAME, g_hInstance))
{
MessageBox(NULL, "Unable to unregister window class", "Error", MB_OK | MB_ICONINFORMATION);
g_hInstance = NULL;
}
return TRUE;
} // end KillWindow()
/*****************************************************************************
ResizeScene()
Called once when the application starts and again every time the window is
resized by the user.
*****************************************************************************/
GLvoid ResizeScene(GLsizei width, GLsizei height)
{
// avoid divide by zero
if (height==0)
{
height=1;
}
// set the viewport to the new dimensions
glViewport(0, 0, width, height);
// select the projection matrix and clear it out
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// set the perspective with the appropriate aspect ratio
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 2500.0f);
// select modelview matrix
glMatrixMode(GL_MODELVIEW);
} // end ResizeScene()
/*****************************************************************************
InitializeScene()
Performs one-time application-specific setup. Returns FALSE on any failure.
*****************************************************************************/
BOOL InitializeScene()
{
// enable the zbuffer
glEnable(GL_DEPTH_TEST);
// enable lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// enable using glColor to change material properties
// we'll use the default glColorMaterial setting (ambient and diffuse)
glEnable(GL_COLOR_MATERIAL);
// set the default blending function
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// set up the fog parameters for reflections
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogf(GL_FOG_START, -100.0);
glFogf(GL_FOG_END, 100.0);
// enable line anti-aliasing and make the lines slightly bigger than default
glEnable(GL_LINE_SMOOTH);
glLineWidth(1.5f);
// create a normal quadric (uses default settings)
g_normalObject = gluNewQuadric();
// create an object to use with the wireframe draw stile
g_wireframeObject = gluNewQuadric();
gluQuadricDrawStyle(g_wireframeObject, GLU_LINE);
// created an object that generates texture coordinates
g_texturedObject = gluNewQuadric();
gluQuadricTexture(g_texturedObject, GL_TRUE);
// create an object that uses flat shading
g_flatshadedObject = gluNewQuadric();
gluQuadricNormals(g_flatshadedObject, GLU_FLAT);
// load the textures we need
LoadTexture("rocket.bmp", g_rocketTexture);
LoadTexture("fire.bmp", g_fireTexture);
LoadTexture("stars.bmp", g_starsTexture);
return TRUE;
} // end InitializeScene()
/*****************************************************************************
DisplayScene()
The work of the application is done here. This is called every frame, and
handles the actual rendering of the scene.
*****************************************************************************/
BOOL DisplayScene()
{
// see if the player is turning
if (KEY_DOWN(VK_LEFT))
g_viewAngle -= 2.0;
if (KEY_DOWN(VK_RIGHT))
g_viewAngle += 2.0;
if (KEY_DOWN('Z'))
g_elevationAngle += 2.0;
if (KEY_DOWN('A'))
g_elevationAngle -= 2.0;
// calculate the player's angle of rotation in radians
float rad = float(3.14159 * g_viewAngle / 180.0f);
// if the player is pressing up or down, update their x and z coordinates
// appropriately based on their view angle and speed.
if (KEY_DOWN(VK_UP))
{
g_playerPos[2] += sin(rad) * DEFAULT_SPEED;
g_playerPos[0] += cos(rad) * DEFAULT_SPEED;
}
if (KEY_DOWN(VK_DOWN))
{
g_playerPos[2] -= sin(rad) * DEFAULT_SPEED;
g_playerPos[0] -= cos(rad) * DEFAULT_SPEED;
}
// don't allow the player to wander past the "edge of the world"
if (g_playerPos[0] < -(WORLD_SIZE-50))
g_playerPos[0] = -(WORLD_SIZE-50);
if (g_playerPos[0] > (WORLD_SIZE-50))
g_playerPos[0] = (WORLD_SIZE-50);
if (g_playerPos[2] < -(WORLD_SIZE-50))
g_playerPos[2] = -(WORLD_SIZE-50);
if (g_playerPos[2] > (WORLD_SIZE-50))
g_playerPos[2] = (WORLD_SIZE-50);
// use the players view angle to correctly set up the view matrix
g_lookAt[0] = float(g_playerPos[0] + 100*cos(rad));
g_lookAt[2] = float(g_playerPos[2] + 100*sin(rad));
rad = float (3.13149 * g_elevationAngle / 180.0f);
g_lookAt[1] = float (g_playerPos[1] + 100 * sin(rad));
// clear the modelview matrix
glLoadIdentity();
// setup the view matrix
gluLookAt(
g_playerPos[0], g_playerPos[1], g_playerPos[2],
g_lookAt[0], g_lookAt[1], g_lookAt[2],
0.0, 1.0, 0.0
);
// just clear the depth buffer, no need to clear the color buffer since
// we write to the whole thing
glClear(GL_DEPTH_BUFFER_BIT);
// position the light
GLfloat pos[4] = { 5.0, 5.0, 5.0, 0.0 };
glLightfv(GL_LIGHT0, GL_POSITION, pos);
// rotation is used for animation
static GLfloat rotation = 0.0;
// it's increased by one every frame
rotation += 1.0;
// and ranges between 0 and 360
if (rotation > 360.0)
rotation = 0.0;
// draw all of our objects in their normal position
DrawNormalObjects(rotation);
DrawWireframeObjects(rotation);
DrawTexturedObjects(rotation);
DrawFlatshadedObjects(rotation);
// draw the sky
DrawStars();
// enable fog and then draw everything inverted to get
// a reflection effect
glPushMatrix();
glEnable(GL_FOG);
glScalef(1.0, -1.0, 1.0);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
DrawNormalObjects(rotation);
DrawWireframeObjects(rotation);
DrawTexturedObjects(rotation);
DrawFlatshadedObjects(rotation);
glDisable(GL_FOG);
DrawStars();
glPopMatrix();
// draw the grid representing the ground
DrawGround();
return TRUE;
} // end DisplayScene()
/*****************************************************************************
Cleanup()
Called at the end of successful program execution.
*****************************************************************************/
BOOL Cleanup()
{
// delete every valid quadric object
if (g_normalObject)
gluDeleteQuadric(g_normalObject);
if (g_wireframeObject)
gluDeleteQuadric(g_wireframeObject);
if (g_texturedObject)
gluDeleteQuadric(g_texturedObject);
if (g_flatshadedObject)
gluDeleteQuadric(g_flatshadedObject);
return TRUE;
} // end Cleanup()
/*****************************************************************************
DrawGround()
Draw a grid of blue lines to represent the ground.
*****************************************************************************/
GLvoid DrawGround()
{
// enable blending for anti-aliased lines
glEnable(GL_BLEND);
// set the color to a bright blue
glColor3f(0.5f, 0.7f, 1.0f);
// draw the lines
glBegin(GL_LINES);
for (int x = -WORLD_SIZE; x < WORLD_SIZE; x += 6)
{
glVertex3i(x, 0, -WORLD_SIZE);
glVertex3i(x, 0, WORLD_SIZE);
}
for (int z = -WORLD_SIZE; z < WORLD_SIZE; z += 6)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -