📄 tutorial_42.htm
字号:
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre>void ReshapeGL (int width, int height) <font class="theme">// 当窗口移动或者大小改变时重新调整窗口</font>
{
glViewport (0, 0, (GLsizei)(width), (GLsizei)(height)); <font class="theme">// 重置当前视口</font>
}
</pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">下
一步我们添加一些代码用于监视擦除窗口背景的Windows消息(WM_ERASEBKGND).如果它被调用,我们截取它并返回0,这样就阻止了窗口背
景被擦除,并让我们自己来调整主窗口大小,这样就没有了我们以前常见的那种恼人的闪烁。如果你还不明白我的意思,删掉 case
WM_ERASEBKGND: 和 return 0; 你自己比较就能知道有何不同。</td>
<td class="r"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre>LRESULT CALLBACK WindowProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
DWORD tickCount; <font class="theme">// 保存当前的时间</font>
__int64 timer; <font class="theme">// 记录时间</font>
<font class="theme">// 返回窗口结构</font>
GL_Window* window = (GL_Window*)(GetWindowLong (hWnd, GWL_USERDATA));
switch (uMsg) <font class="theme">// 处理消息</font>
{
case WM_ERASEBKGND: <font class="theme">// 检测Windows是否去擦除背景</font>
return 0; <font class="theme">// 跳过直接返回</font>
</pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">在WinMain函数中,我们需要修改窗口标题并设置分辨率至1024x768.如果由于某种原因你的显示器不能支持到1024x768,你可以设置低一点的分辨率,但是牺牲了一些细节。</td>
<td class="r"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre> window.init.width = 1024; <font class="theme">// 宽</font>
window.init.height = 768; <font class="theme">// 高</font>
</pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">现在该是对lesson42.cpp文件动手术的时候了(主要代码)...<br>
我们以包含标准头文件和库文件作为开始吧.</td>
<td class="r"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre>#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include "NeHeGL.h"
#pragma comment( lib, "opengl32.lib" )
#pragma comment( lib, "glu32.lib" )
GL_Window* g_window;
Keys* g_keys; </pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">然后我们声明一些我们打算在整个程序中都要用到的全局变量。<br>
mx和my纪录了当前所在迷宫中的房间。每个房间都被墙隔开(因此房间都是2个单元大小的部分)。<br>
with和height是用来建立纹理需要的。它也是迷宫的宽和高。让迷宫和贴图的大小一致的原因是使迷宫中的象素和纹理中的象素一一对应。我倾向于把宽和高都设成256,尽管这要花更长的时间来建立迷宫。<br>
如果你的显卡能支持处理大型贴图。可以试着以2次幂增加这个值(256, 512,
1023)。确保这个值不至于太大。如果这个主窗口的宽度有1024个象素,并且每个视口的大小都是主窗口的一半,相应的你应该设置你的贴图宽度也是窗口
宽度的一半。如果你使贴图宽度为1024象素,但你的视口大小只有512,空间不足于容纳贴图中所有得象素,这样每两个象素就会重叠在一起。贴图的高度也
作同样处理:高度是窗口高度的1/2. 当然你还必须四舍五入到2的幂。</td>
<td class="r"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre>int mx,my; <font class="theme">// 循环变量</font>
const width = 128; <font class="theme">// 迷宫大小</font>
const height = 128; </pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">dong用来跟踪迷宫是否被建完,后面有这个更详细的解释。<br>
sp用来确认空格键是否处于按下状态。通过按空格健,迷宫就会被重置,然后程序将重新开始画一个新的迷宫。如果我们不去检测空格键是否处于按下状态,迷宫会在空格键按下的瞬间被重置很多次。这个值确保迷宫只被重置一次。</td>
<td class="r"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre>BOOL done; <font class="theme">// 迷宫是否被建完</font>
BOOL sp; </pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">r[4]
保存了4个随机的红色分量值,g[4]保存了4个随机的绿色分量值,b[4]保存了4个随机的兰色分量值。这些值赋给各个视口不同的颜色。第一个视口颜色
为r[0],g[0],b[0]。请注意每一个颜色都是一个字节的值,而不是常用的浮点值。我这里用字节是因为产生0-255的随机值比产生0.0f-
1.0f的浮点值更容易。<br>
tex_data指向我们的贴图数据。</td>
<td class="r"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="bc" width="100%"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="br"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<pre>BYTE r[4], g[4], b[4]; <font class="theme">// 随机的颜色</font>
BYTE *tex_data; <font class="theme">// 保存纹理数据</font>
</pre>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="tl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="tc" width="100%"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="100%"></td>
<td class="tr"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="l"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
<td class="back3" valign="top" width="100%">xrot,yrot和zrot是旋转3d物体用到的变量。<br>
最后,我们声明一个二次曲面物体,这样我们可以用gluCylinder和gluSphere来画圆柱和球体,这比手工绘制这些物体容易多了。</td>
<td class="r"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td></tr></tbody></table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td class="bl"><img alt="" src="Tutorial_42_files/blank1.gif" height="28" width="28"></td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -