📄 lesson42.cpp
字号:
/***************************************************************************************************************
* * *
* Lesson 42: Multiple Viewports * Created: 05/17/2003 *
* * *
* This Program Was Written By Jeff Molofee (NeHe) * Runs Much Faster (Many Useless Loops Removed) *
* From http://nehe.gamedev.net. * *
* * Maze Code Is Still Very Unoptimized. Speed Can Be *
* I Wanted To Create A Maze, And Was Able To Find * Increased Considerably By Keeping Track Of Cells *
* Example Code, But Most Of It Was Uncommented And * That Have Been Visited Rather Than Randomly *
* Difficult To Figure Out. * Searching For Cells That Still Need To Be Visited. *
* * *
* This Is A Direct Conversion Of Basic Code I Wrote * This Tutorial Demonstrates Multiple Viewports In A *
* On The Atari XE Many Years Ago. * Single Window With Both Ortho And Perspective Modes *
* * Used At The Same Time. As Well, Two Of The Views *
* It Barely Resembles The Basic Code, But The Idea * Have Lighting Enabled, While The Other Two Do Not. *
* Is Exactly The Same. * *
* *********************************************************
* Branches Are Always Made From An Existing Path *
* So There Should Always Be A Path Through The Maze *
* Although It Could Be Quite Short :) *
* *
* Do Whatever You Want With This Code. If You Found *
* It Useful Or Have Made Some Nice Changes To It, *
* Send Me An Email: nehe@connect.ab.ca *
* *
*******************************************************/
#include <windows.h> // Header File For Windows
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include "NeHeGL.h" // Header File For NeHeGL
#pragma comment( lib, "opengl32.lib" ) // Search For OpenGL32.lib While Linking
#pragma comment( lib, "glu32.lib" ) // Search For GLu32.lib While Linking
GL_Window* g_window; // Window Structure
Keys* g_keys; // Keyboard
// User Defined Variables
int mx,my; // General Loops (Used For Seeking)
const width = 128; // Maze Width (Must Be A Power Of 2)
const height = 128; // Maze Height (Must Be A Power Of 2)
BOOL done; // Flag To Let Us Know When It's Done
BOOL sp; // Spacebar Pressed?
BYTE r[4], g[4], b[4]; // Random Colors (4 Red, 4 Green, 4 Blue)
BYTE *tex_data; // Holds Our Texture Data
GLfloat xrot, yrot, zrot; // Use For Rotation Of Objects
GLUquadricObj *quadric; // The Quadric Object
void UpdateTex(int dmx, int dmy) // Update Pixel dmx, dmy On The Texture
{
tex_data[0+((dmx+(width*dmy))*3)]=255; // Set Red Pixel To Full Bright
tex_data[1+((dmx+(width*dmy))*3)]=255; // Set Green Pixel To Full Bright
tex_data[2+((dmx+(width*dmy))*3)]=255; // Set Blue Pixel To Full Bright
}
void Reset (void) // Reset The Maze, Colors, Start Point, Etc
{
ZeroMemory(tex_data, width * height *3); // Clear Out The Texture Memory With 0's
srand(GetTickCount()); // Try To Get More Randomness
for (int loop=0; loop<4; loop++) // Loop So We Can Assign 4 Random Colors
{
r[loop]=rand()%128+128; // Pick A Random Red Color (Bright)
g[loop]=rand()%128+128; // Pick A Random Green Color (Bright)
b[loop]=rand()%128+128; // Pick A Random Blue Color (Bright)
}
mx=int(rand()%(width/2))*2; // Pick A New Random X Position
my=int(rand()%(height/2))*2; // Pick A New Random Y Position
}
BOOL Initialize (GL_Window* window, Keys* keys) // Any GL Init Code & User Initialiazation Goes Here
{
tex_data=new BYTE[width*height*3]; // Allocate Space For Our Texture
g_window = window; // Window Values
g_keys = keys; // Key Values
Reset(); // Call Reset To Build Our Initial Texture, Etc.
// Start Of User Initialization
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, tex_data);
glClearColor (0.0f, 0.0f, 0.0f, 0.0f); // Black Background
glClearDepth (1.0f); // Depth Buffer Setup
glDepthFunc (GL_LEQUAL); // The Type Of Depth Testing
glEnable (GL_DEPTH_TEST); // Enable Depth Testing
glEnable(GL_COLOR_MATERIAL); // Enable Color Material (Allows Us To Tint Textures)
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
quadric=gluNewQuadric(); // Create A Pointer To The Quadric Object
gluQuadricNormals(quadric, GLU_SMOOTH); // Create Smooth Normals
gluQuadricTexture(quadric, GL_TRUE); // Create Texture Coords
glEnable(GL_LIGHT0); // Enable Light0 (Default GL Light)
return TRUE; // Return TRUE (Initialization Successful)
}
void Deinitialize (void) // Any User DeInitialization Goes Here
{
delete [] tex_data; // Delete Our Texture Data (Freeing Up Memory)
}
void Update (float milliseconds) // Perform Motion Updates Here
{
int dir; // Will Hold Current Direction
if (g_keys->keyDown [VK_ESCAPE]) // Is ESC Being Pressed?
TerminateApplication (g_window); // Terminate The Program
if (g_keys->keyDown [VK_F1]) // Is F1 Being Pressed?
ToggleFullscreen (g_window); // Toggle Fullscreen Mode
if (g_keys->keyDown [' '] && !sp) // Check To See If Spacebar Is Pressed
{
sp=TRUE; // If So, Set sp To TRUE (Spacebar Pressed)
Reset(); // If So, Call Reset And Start A New Maze
}
if (!g_keys->keyDown [' ']) // Check To See If Spacebar Has Been Released
sp=FALSE; // If So, Set sp To FALSE (Spacebar Released)
xrot+=(float)(milliseconds)*0.02f; // Increase Rotation On The X-Axis
yrot+=(float)(milliseconds)*0.03f; // Increase Rotation On The Y-Axis
zrot+=(float)(milliseconds)*0.015f; // Increase Rotation On The Z-Axis
done=TRUE; // Set done To True
for (int x=0; x<width; x+=2) // Loop Through All The Rooms
{
for (int y=0; y<height; y+=2) // On X And Y Axis
{
if (tex_data[((x+(width*y))*3)]==0) // If Current Texture Pixel (Room) Is Blank
done=FALSE; // We Have To Set done To False (Not Finished Yet)
}
}
if (done) // If done Is True Then There Were No Unvisited Rooms
{
// Display A Message At The Top Of The Window, Pause For A Bit And Then Start Building A New Maze!
SetWindowText(g_window->hWnd,"Lesson 42: Multiple Viewports... 2003 NeHe Productions... Maze Complete!");
Sleep(5000);
SetWindowText(g_window->hWnd,"Lesson 42: Multiple Viewports... 2003 NeHe Productions... Building Maze!");
Reset();
}
// Check To Make Sure We Are Not Trapped (Nowhere Else To Move)
if (((mx>(width-4) || tex_data[(((mx+2)+(width*my))*3)]==255)) && ((mx<2 || tex_data[(((mx-2)+(width*my))*3)]==255)) &&
((my>(height-4) || tex_data[((mx+(width*(my+2)))*3)]==255)) && ((my<2 || tex_data[((mx+(width*(my-2)))*3)]==255)))
{
do // If We Are Trapped
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -