📄 lesson36.cpp
字号:
glNormal3f(normal[0],normal[1],normal[2]); // Set The Normal
// Render The Quad
glVertex3f(vertexes[0][0],vertexes[0][1],vertexes[0][2]);
glVertex3f(vertexes[1][0],vertexes[1][1],vertexes[1][2]);
glVertex3f(vertexes[2][0],vertexes[2][1],vertexes[2][2]);
glVertex3f(vertexes[3][0],vertexes[3][1],vertexes[3][2]);
}
}
glEnd(); // Done Rendering Quads
glPopMatrix(); // Pop The Matrix
}
void ViewOrtho() // Set Up An Ortho View
{
glMatrixMode(GL_PROJECTION); // Select Projection
glPushMatrix(); // Push The Matrix
glLoadIdentity(); // Reset The Matrix
glOrtho( 0, 640 , 480 , 0, -1, 1 ); // Select Ortho Mode (640x480)
glMatrixMode(GL_MODELVIEW); // Select Modelview Matrix
glPushMatrix(); // Push The Matrix
glLoadIdentity(); // Reset The Matrix
}
void ViewPerspective() // Set Up A Perspective View
{
glMatrixMode( GL_PROJECTION ); // Select Projection
glPopMatrix(); // Pop The Matrix
glMatrixMode( GL_MODELVIEW ); // Select Modelview
glPopMatrix(); // Pop The Matrix
}
void RenderToTexture() // Renders To A Texture
{
glViewport(0,0,128,128); // Set Our Viewport (Match Texture Size)
ProcessHelix(); // Render The Helix
glBindTexture(GL_TEXTURE_2D,BlurTexture); // Bind To The Blur Texture
// Copy Our ViewPort To The Blur Texture (From 0,0 To 128,128... No Border)
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 0, 0, 128, 128, 0);
glClearColor(0.0f, 0.0f, 0.5f, 0.5); // Set The Clear Color To Medium Blue
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And Depth Buffer
glViewport(0 , 0,640 ,480); // Set Viewport (0,0 to 640x480)
}
void DrawBlur(int times, float inc) // Draw The Blurred Image
{
float spost = 0.0f; // Starting Texture Coordinate Offset
float alphainc = 0.9f / times; // Fade Speed For Alpha Blending
float alpha = 0.2f; // Starting Alpha Value
// Disable AutoTexture Coordinates
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_2D); // Enable 2D Texture Mapping
glDisable(GL_DEPTH_TEST); // Disable Depth Testing
glBlendFunc(GL_SRC_ALPHA,GL_ONE); // Set Blending Mode
glEnable(GL_BLEND); // Enable Blending
glBindTexture(GL_TEXTURE_2D,BlurTexture); // Bind To The Blur Texture
ViewOrtho(); // Switch To An Ortho View
alphainc = alpha / times; // alphainc=0.2f / Times To Render Blur
glBegin(GL_QUADS); // Begin Drawing Quads
for (int num = 0;num < times;num++) // Number Of Times To Render Blur
{
glColor4f(1.0f, 1.0f, 1.0f, alpha); // Set The Alpha Value (Starts At 0.2)
glTexCoord2f(0+spost,1-spost); // Texture Coordinate ( 0, 1 )
glVertex2f(0,0); // First Vertex ( 0, 0 )
glTexCoord2f(0+spost,0+spost); // Texture Coordinate ( 0, 0 )
glVertex2f(0,480); // Second Vertex ( 0, 480 )
glTexCoord2f(1-spost,0+spost); // Texture Coordinate ( 1, 0 )
glVertex2f(640,480); // Third Vertex ( 640, 480 )
glTexCoord2f(1-spost,1-spost); // Texture Coordinate ( 1, 1 )
glVertex2f(640,0); // Fourth Vertex ( 640, 0 )
spost += inc; // Gradually Increase spost (Zooming Closer To Texture Center)
alpha = alpha - alphainc; // Gradually Decrease alpha (Gradually Fading Image Out)
}
glEnd(); // Done Drawing Quads
ViewPerspective(); // Switch To A Perspective View
glEnable(GL_DEPTH_TEST); // Enable Depth Testing
glDisable(GL_TEXTURE_2D); // Disable 2D Texture Mapping
glDisable(GL_BLEND); // Disable Blending
glBindTexture(GL_TEXTURE_2D,0); // Unbind The Blur Texture
}
BOOL Initialize (GL_Window* window, Keys* keys) // Any GL Init Code & User Initialiazation Goes Here
{
g_window = window;
g_keys = keys;
// Start Of User Initialization
angle = 0.0f; // Set Starting Angle To Zero
BlurTexture = EmptyTexture(); // Create Our Empty Texture
glViewport(0 , 0,window->init.width ,window->init.height); // Set Up A Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(50, (float)window->init.width/(float)window->init.height, 5, 2000); // Set Our Perspective
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
glEnable(GL_DEPTH_TEST); // Enable Depth Testing
GLfloat global_ambient[4]={0.2f, 0.2f, 0.2f, 1.0f}; // Set Ambient Lighting To Fairly Dark Light (No Color)
GLfloat light0pos[4]= {0.0f, 5.0f, 10.0f, 1.0f}; // Set The Light Position
GLfloat light0ambient[4]= {0.2f, 0.2f, 0.2f, 1.0f}; // More Ambient Light
GLfloat light0diffuse[4]= {0.3f, 0.3f, 0.3f, 1.0f}; // Set The Diffuse Light A Bit Brighter
GLfloat light0specular[4]={0.8f, 0.8f, 0.8f, 1.0f}; // Fairly Bright Specular Lighting
GLfloat lmodel_ambient[]= {0.2f,0.2f,0.2f,1.0f}; // And More Ambient Light
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient); // Set The Ambient Light Model
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient); // Set The Global Ambient Light Model
glLightfv(GL_LIGHT0, GL_POSITION, light0pos); // Set The Lights Position
glLightfv(GL_LIGHT0, GL_AMBIENT, light0ambient); // Set The Ambient Light
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0diffuse); // Set The Diffuse Light
glLightfv(GL_LIGHT0, GL_SPECULAR, light0specular); // Set Up Specular Lighting
glEnable(GL_LIGHTING); // Enable Lighting
glEnable(GL_LIGHT0); // Enable Light0
glShadeModel(GL_SMOOTH); // Select Smooth Shading
glMateriali(GL_FRONT, GL_SHININESS, 128);
glClearColor(0.0f, 0.0f, 0.0f, 0.5); // Set The Clear Color To Black
return TRUE; // Return TRUE (Initialization Successful)
}
void Deinitialize (void) // Any User DeInitialization Goes Here
{
glDeleteTextures(1,&BlurTexture); // Delete The Blur Texture
}
void Update (DWORD milliseconds) // Perform Motion Updates Here
{
if (g_keys->keyDown [VK_ESCAPE] == TRUE) // Is ESC Being Pressed?
{
TerminateApplication (g_window); // Terminate The Program
}
if (g_keys->keyDown [VK_F1] == TRUE) // Is F1 Being Pressed?
{
ToggleFullscreen (g_window); // Toggle Fullscreen Mode
}
angle += (float)(milliseconds) / 5.0f; // Update angle Based On The Clock
}
void Draw (void) // Draw The Scene
{
glClearColor(0.0f, 0.0f, 0.0f, 0.5); // Set The Clear Color To Black
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The View
RenderToTexture(); // Render To A Texture
ProcessHelix(); // Draw Our Helix
DrawBlur(25,0.02f); // Draw The Blur Effect
glFlush (); // Flush The GL Rendering Pipeline
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -