📄 skybox.cpp
字号:
// SkyBox.cpp: Implementierung der Klasse CSkyBox.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SkyBox.h"
//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////
CSkyBox::CSkyBox()
{
}
CSkyBox::~CSkyBox()
{
}
void CSkyBox::DrawSkyBox(float fXRotation, float fYRotation,
unsigned int iFOV, float fSkyBrightness, float fAspect)
{
// Draw skybox
// Convert degrees to texture coordinates
fYRotation /= 150.0f;
fXRotation /= 100.0f;
// Use skybox texture
m_SkyBoxTexture.Use();
// Brightness
glColor3f(fSkyBrightness, fSkyBrightness, fSkyBrightness);
// Save fog, depth buffer, lighting and texture bit
glPushAttrib(GL_FOG_BIT | GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT |
GL_LIGHTING_BIT);
// Disable fog
glDisable(GL_FOG);
// Disable lighting
glDisable(GL_LIGHTING);
// Disable updating and testing the depth buffer
glDisable(GL_DEPTH_TEST);
// Set correct texture parameters for moving skybox
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
// Distance of the skybox to the camera
const float fDistance = -1.0f;
// Project the quad into a 2D plane that covers the whole screen
float fLowerLeft[3];
float fLowerRight[3];
float fUpperLeft[3];
float fUpperRight[3];
Project2D(-1.0f, -1.0f, fDistance, iFOV, fAspect, fLowerLeft);
Project2D(1.0f, -1.0f, fDistance, iFOV, fAspect, fLowerRight);
Project2D(-1.0f, 1.0f, fDistance, iFOV, fAspect, fUpperLeft);
Project2D(1.0f, 1.0f, fDistance, iFOV, fAspect, fUpperRight);
// Draw quad for the skybox
glBegin(GL_QUADS);
glTexCoord2f(0.0f - fYRotation, 0.0f - fXRotation);
glVertex3fv(fLowerLeft);
glTexCoord2f(1.0f - fYRotation, 0.0f - fXRotation);
glVertex3fv(fLowerRight);
glTexCoord2f(1.0f - fYRotation, 1.0f - fXRotation);
glVertex3fv(fUpperRight);
glTexCoord2f(0.0f - fYRotation, 1.0f - fXRotation);
glVertex3fv(fUpperLeft);
glEnd();
glPopAttrib();
}
void CSkyBox::Project2D(const float fU, const float fV, const float fPlane,
const unsigned int iFOV, float fAspect, float fVertex3DOut[3])
{
// Project a point in 2D space. The point in 2D space is normalized.
// Lower-left corner is -1,-1 and upper-right is +1,+1
fVertex3DOut[2]= fPlane;
fVertex3DOut[1] =- tanf(iFOV * 0.5f * 6.283185f / 360.0f ) * fVertex3DOut[2];
fVertex3DOut[0]= fVertex3DOut[1] * fAspect * fU;
fVertex3DOut[1] *= fV;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -