blobt
5 years ago
9 changed files with 124 additions and 3 deletions
-
BIN.vs/shader2/v14/.suo
-
73frustum.cpp
-
14frustum.h
-
12main.cpp
-
BINres/image/camera.dds
-
3res/shader/WhiteColor.fs
-
9res/shader/WhiteColor.vs
-
4shader2.vcxproj
-
12shader2.vcxproj.filters
@ -0,0 +1,73 @@ |
|||
#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::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); |
|||
} |
@ -0,0 +1,14 @@ |
|||
#pragma once |
|||
#include "glew.h" |
|||
#include "Glm/glm.hpp" |
|||
#include "Glm/ext.hpp" |
|||
|
|||
class Frustum { |
|||
public: |
|||
GLuint mVBO, mIBO, mProgram; |
|||
GLint mPLocation, mVLocation, mMLocation, mPosLocation; |
|||
Frustum(); |
|||
void Init(); |
|||
void InitPerspective(float fov, float aspect, float zNear, float zFar); |
|||
void Draw(float* M, float* V, float* P); |
|||
}; |
@ -0,0 +1,3 @@ |
|||
void main() { |
|||
gl_FragColor =vec4(1.0); |
|||
} |
@ -0,0 +1,9 @@ |
|||
attribute vec3 pos; |
|||
|
|||
uniform mat4 M; |
|||
uniform mat4 V; |
|||
uniform mat4 P; |
|||
|
|||
void main() { |
|||
gl_Position = P*V*M*vec4(pos,1.0); |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue