📄 lesson42.cpp
字号:
{
mx=int(rand()%(width/2))*2; // Pick A New Random X Position
my=int(rand()%(height/2))*2; // Pick A New Random Y Position
}
while (tex_data[((mx+(width*my))*3)]==0); // Keep Picking A Random Position Until We Find
} // One That Has Already Been Tagged (Safe Starting Point)
dir=int(rand()%4); // Pick A Random Direction
if ((dir==0) && (mx<=(width-4))) // If The Direction Is 0 (Right) And We Are Not At The Far Right
{
if (tex_data[(((mx+2)+(width*my))*3)]==0) // And If The Room To The Right Has Not Already Been Visited
{
UpdateTex(mx+1,my); // Update The Texture To Show Path Cut Out Between Rooms
mx+=2; // Move To The Right (Room To The Right)
}
}
if ((dir==1) && (my<=(height-4))) // If The Direction Is 1 (Down) And We Are Not At The Bottom
{
if (tex_data[((mx+(width*(my+2)))*3)]==0) // And If The Room Below Has Not Already Been Visited
{
UpdateTex(mx,my+1); // Update The Texture To Show Path Cut Out Between Rooms
my+=2; // Move Down (Room Below)
}
}
if ((dir==2) && (mx>=2)) // If The Direction Is 2 (Left) And We Are Not At The Far Left
{
if (tex_data[(((mx-2)+(width*my))*3)]==0) // And If The Room To The Left Has Not Already Been Visited
{
UpdateTex(mx-1,my); // Update The Texture To Show Path Cut Out Between Rooms
mx-=2; // Move To The Left (Room To The Left)
}
}
if ((dir==3) && (my>=2)) // If The Direction Is 3 (Up) And We Are Not At The Top
{
if (tex_data[((mx+(width*(my-2)))*3)]==0) // And If The Room Above Has Not Already Been Visited
{
UpdateTex(mx,my-1); // Update The Texture To Show Path Cut Out Between Rooms
my-=2; // Move Up (Room Above)
}
}
UpdateTex(mx,my); // Update Current Room
}
void Draw (void) // Our Drawing Routine
{
RECT rect; // Holds Coordinates Of A Rectangle
GetClientRect(g_window->hWnd, &rect); // Get Window Dimensions
int window_width=rect.right-rect.left; // Calculate The Width (Right Side-Left Side)
int window_height=rect.bottom-rect.top; // Calculate The Height (Bottom-Top)
// Update Our Texture... This Is The Key To The Programs Speed... Much Faster Than Rebuilding The Texture Each Time
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, tex_data);
glClear (GL_COLOR_BUFFER_BIT); // Clear Screen
for (int loop=0; loop<4; loop++) // Loop To Draw Our 4 Views
{
glColor3ub(r[loop],g[loop],b[loop]); // Assign Color To Current View
if (loop==0) // If We Are Drawing The First Scene
{
// Set The Viewport To The Top Left. It Will Take Up Half The Screen Width And Height
glViewport (0, window_height/2, window_width/2, window_height/2);
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
// Set Up Ortho Mode To Fit 1/4 The Screen (Size Of A Viewport)
gluOrtho2D(0, window_width/2, window_height/2, 0);
}
if (loop==1) // If We Are Drawing The Second Scene
{
// Set The Viewport To The Top Right. It Will Take Up Half The Screen Width And Height
glViewport (window_width/2, window_height/2, window_width/2, window_height/2);
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
// Set Up Perspective Mode To Fit 1/4 The Screen (Size Of A Viewport)
gluPerspective( 45.0, (GLfloat)(width)/(GLfloat)(height), 0.1f, 500.0 );
}
if (loop==2) // If We Are Drawing The Third Scene
{
// Set The Viewport To The Bottom Right. It Will Take Up Half The Screen Width And Height
glViewport (window_width/2, 0, window_width/2, window_height/2);
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
// Set Up Perspective Mode To Fit 1/4 The Screen (Size Of A Viewport)
gluPerspective( 45.0, (GLfloat)(width)/(GLfloat)(height), 0.1f, 500.0 );
}
if (loop==3) // If We Are Drawing The Fourth Scene
{
// Set The Viewport To The Bottom Left. It Will Take Up Half The Screen Width And Height
glViewport (0, 0, window_width/2, window_height/2);
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
// Set Up Perspective Mode To Fit 1/4 The Screen (Size Of A Viewport)
gluPerspective( 45.0, (GLfloat)(width)/(GLfloat)(height), 0.1f, 500.0 );
}
glMatrixMode (GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity (); // Reset The Modelview Matrix
glClear (GL_DEPTH_BUFFER_BIT); // Clear Depth Buffer
if (loop==0) // Are We Drawing The First Image? (Original Texture... Ortho)
{
glBegin(GL_QUADS); // Begin Drawing A Single Quad
// We Fill The Entire 1/4 Section With A Single Textured Quad.
glTexCoord2f(1.0f, 0.0f); glVertex2i(window_width/2, 0 );
glTexCoord2f(0.0f, 0.0f); glVertex2i(0, 0 );
glTexCoord2f(0.0f, 1.0f); glVertex2i(0, window_height/2);
glTexCoord2f(1.0f, 1.0f); glVertex2i(window_width/2, window_height/2);
glEnd(); // Done Drawing The Textured Quad
}
if (loop==1) // Are We Drawing The Second Image? (3D Texture Mapped Sphere... Perspective)
{
glTranslatef(0.0f,0.0f,-14.0f); // Move 14 Units Into The Screen
glRotatef(xrot,1.0f,0.0f,0.0f); // Rotate By xrot On The X-Axis
glRotatef(yrot,0.0f,1.0f,0.0f); // Rotate By yrot On The Y-Axis
glRotatef(zrot,0.0f,0.0f,1.0f); // Rotate By zrot On The Z-Axis
glEnable(GL_LIGHTING); // Enable Lighting
gluSphere(quadric,4.0f,32,32); // Draw A Sphere
glDisable(GL_LIGHTING); // Disable Lighting
}
if (loop==2) // Are We Drawing The Third Image? (Texture At An Angle... Perspective)
{
glTranslatef(0.0f,0.0f,-2.0f); // Move 2 Units Into The Screen
glRotatef(-45.0f,1.0f,0.0f,0.0f); // Tilt The Quad Below Back 45 Degrees.
glRotatef(zrot/1.5f,0.0f,0.0f,1.0f); // Rotate By zrot/1.5 On The Z-Axis
glBegin(GL_QUADS); // Begin Drawing A Single Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
glEnd(); // Done Drawing The Textured Quad
}
if (loop==3) // Are We Drawing The Fourth Image? (3D Texture Mapped Cylinder... Perspective)
{
glTranslatef(0.0f,0.0f,-7.0f); // Move 7 Units Into The Screen
glRotatef(-xrot/2,1.0f,0.0f,0.0f); // Rotate By -xrot/2 On The X-Axis
glRotatef(-yrot/2,0.0f,1.0f,0.0f); // Rotate By -yrot/2 On The Y-Axis
glRotatef(-zrot/2,0.0f,0.0f,1.0f); // Rotate By -zrot/2 On The Z-Axis
glEnable(GL_LIGHTING); // Enable Lighting
glTranslatef(0.0f,0.0f,-2.0f); // Translate -2 On The Z-Axis (To Rotate Cylinder Around The Center, Not An End)
gluCylinder(quadric,1.5f,1.5f,4.0f,32,16); // Draw A Cylinder
glDisable(GL_LIGHTING); // Disable Lighting
}
}
glFlush (); // Flush The GL Rendering Pipeline
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -