You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.4 KiB
99 lines
2.4 KiB
#include "frustum.h"
|
|
#include "misc.h"
|
|
|
|
Frustum::Frustum()
|
|
{
|
|
|
|
}
|
|
|
|
void Frustum::Init()
|
|
{
|
|
/*´´½¨program*/
|
|
mProgram = CreateGPUProgram("res/shader/WhiteColor.vs", "res/shader/WhiteColor.fs");
|
|
|
|
mPosLocation = glGetAttribLocation(mProgram, "pos");
|
|
|
|
mMLocation = glGetUniformLocation(mProgram, "M");
|
|
mVLocation = glGetUniformLocation(mProgram, "V");
|
|
mPLocation = glGetUniformLocation(mProgram, "P");
|
|
}
|
|
|
|
void Frustum::InitPerspective(float fov, float aspect, float zNear, float zFar)
|
|
{
|
|
//¼ÆËã½ü¼ô²ÃÃæ
|
|
float halfFOV = 22.5f;
|
|
float randianHalfFOV = 3.14*halfFOV / 180.0f;
|
|
float tanHalfFOV = sinf(randianHalfFOV) / cosf(randianHalfFOV);
|
|
float yNear = tanHalfFOV * zNear;
|
|
float xNear = yNear*aspect;
|
|
|
|
//¼ÆËãÔ¶¼ô²ÃÃæ
|
|
float yFar = tanHalfFOV * zFar;
|
|
float xFar = yFar*aspect;
|
|
|
|
float vertexes[24] = {
|
|
-xNear,-yNear,-zNear,
|
|
xNear,-yNear,-zNear,
|
|
xNear,yNear,-zNear,
|
|
-xNear,yNear,-zNear,
|
|
|
|
-xFar,-yFar,-zFar,
|
|
xFar,-yFar,-zFar,
|
|
xFar,yFar,-zFar,
|
|
-xFar,yFar,-zFar,
|
|
};
|
|
|
|
mVBO = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(float)*24, GL_STATIC_DRAW, vertexes);
|
|
|
|
unsigned int indexes[] = {
|
|
0,1,1,2,2,3,3,0, //½ü¼ô²ÃÃæ
|
|
4,5,5,6,6,7,7,4, //Ô¶¼ô²ÃÃæ
|
|
0,4,3,7,2,6,1,5
|
|
};
|
|
mIBO = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*24, GL_STATIC_DRAW, indexes);
|
|
|
|
}
|
|
|
|
void Frustum::InitOrtho(float left, float right, float bottom, float top, float zNear, float zFar)
|
|
{
|
|
|
|
|
|
float vertexes[24] = {
|
|
left,bottom,-zNear,
|
|
right,bottom,-zNear,
|
|
right,top,-zNear,
|
|
left,top,-zNear,
|
|
|
|
left,bottom,-zFar,
|
|
right,bottom,-zFar,
|
|
right,top,-zFar,
|
|
left,top,-zFar,
|
|
};
|
|
|
|
mVBO = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(float) * 24, GL_STATIC_DRAW, vertexes);
|
|
|
|
unsigned int indexes[] = {
|
|
0,1,1,2,2,3,3,0, //½ü¼ô²ÃÃæ
|
|
4,5,5,6,6,7,7,4, //Ô¶¼ô²ÃÃæ
|
|
0,4,3,7,2,6,1,5
|
|
};
|
|
mIBO = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * 24, GL_STATIC_DRAW, indexes);
|
|
}
|
|
|
|
void Frustum::Draw(float* M, float* V, float* P)
|
|
{
|
|
glUseProgram(mProgram);
|
|
glUniformMatrix4fv(mMLocation, 1, GL_FALSE, M);
|
|
glUniformMatrix4fv(mVLocation, 1, GL_FALSE, V);
|
|
glUniformMatrix4fv(mPLocation, 1, GL_FALSE, P);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, mVBO);
|
|
glEnableVertexAttribArray(mPosLocation);
|
|
glVertexAttribPointer(mPosLocation, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIBO);
|
|
glDrawElements(GL_LINES, 24, GL_UNSIGNED_INT, 0);
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
glUseProgram(0);
|
|
}
|