1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| void SetPerspectiveProj(float FOV, float Width, float Height, float zNear, float zFar)
{
m_persProj.FOV = FOV;
m_persProj.Width = Width;
m_persProj.Height = Height;
m_persProj.zNear = zNear;
m_persProj.zFar = zFar;
}
pipeline.cpp :
void Pipeline::InitPerspectiveProj(mat4& m) const
{
const float ar = m_persProj.Width / m_persProj.Height;
const float zNear = m_persProj.zNear;
const float zFar = m_persProj.zFar;
// const float zRange = zNear - zFar;
const float tanHalfFOV = m_persProj.FOV;
m = perspective(tanHalfFOV, ar, zNear, zFar);
}
const mat4 Pipeline::GetTrans(){
mat4 ScaleTrans, RotateTrans, TranslationTrans, PersProjTrans;
InitScaleTransform(ScaleTrans);
InitRotateTransform(RotateTrans);
InitTranslationTransform(TranslationTrans);
InitPerspectiveProj(PersProjTrans);
m_transformation = TranslationTrans * RotateTrans * ScaleTrans;
return m_transformation;
}
main.cpp :
static void RenderSceneCB(){
glClear(GL_COLOR_BUFFER_BIT);
static float Scale =0.0f;
Scale+=0.1f;
Pipeline p;
p.Scale(1.0f, 1.0f, 1.0f);
p.WorldPos(0, 0.0f, 10.0f);
p.Rotate(0.0f, Scale, 0.0f);
p.SetPerspectiveProj(70.0f, WINDOW_WIDTH, WINDOW_HEIGHT, 1.0f, 1000.0f);
glUniformMatrix4fv(gWorldLocation, 1, GL_FALSE, value_ptr(p.GetTrans()));
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0);
glutSwapBuffers();
} |
Partager