📄 frustummain.cpp
字号:
/* ------------------------------------------------------
View Frustum Culling - Lighthouse3D
-----------------------------------------------------*/
#include "GL/glut.h"
#include "Vec3.h"
#include "FrustumR.h"
#include <stdio.h>
float a = 0;
float nearP = 1.0f, farP = 100.0f;
float angle = 45, ratio=1;
float sphereX,sphereY,sphereZ;
float fps,time;
int frame=0, timebase=0;
int frustumOn = 1;
int spheresDrawn = 0;
int spheresTotal = 0;
FrustumR frustum;
void changeSize(int w, int h) {
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
ratio = w * 1.0/ h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(angle,ratio,nearP,farP);
glMatrixMode(GL_MODELVIEW);
frustum.setCamInternals(angle,ratio,nearP,farP);
}
void RenderSpheres()
{
glColor3f(0,1,0);
spheresTotal=0;
Vec3 a(0,0,0);
for (int i=0; i<3; i++)
{
if (i == 2)
{
a.x = sphereX;
a.y = sphereY;
a.z = sphereZ;
}
else
{
a.x = i*3 - 2;
a.y = 0;
a.z = 0;
}
if (!frustumOn || (frustum.sphereInFrustum(a,0.5) != FrustumR::OUTSIDE))
{
a.x=sphereX;
a.y=sphereY;
a.z=sphereZ;
glPushMatrix();
glTranslatef(a.x,a.y,a.z);
glutSolidSphere(0.5,55,45);
glPopMatrix();
glPushMatrix();
glColor3f(255,0,0);
glTranslatef(1,0,0);
glutSolidSphere(0.4,55,45);
glPopMatrix();
glPushMatrix();
glColor3f(255,0,0);
glTranslatef(-1,0,0);
glutSolidSphere(0.4,55,45);
glPopMatrix();
spheresTotal++;
}
}
}
Vec3 p(0,0,5),l(0,0,0),u(0,1,0);
void renderScene(void)
{
char title[80];
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(p.x,p.y,p.z,
l.x,l.y,l.z,
u.x,u.y,u.z);
frustum.setCamDef(p,l,u);
RenderSpheres();
frame++;
time=glutGet(GLUT_ELAPSED_TIME);
if (time - timebase > 1000)
{
fps = frame*1000.0/(time-timebase);
timebase = time;
frame = 0;
sprintf(title," Total Spheres: %d FPS:%8.2f",spheresTotal,fps);
glutSetWindowTitle(title);
}
glutSwapBuffers();
}
void keyboard(unsigned char a, int x, int y) {
Vec3 v;
switch(a)
{
case 'w':
case 'W':
sphereY++;
break;
case 's':
case 'S':
sphereY--;
break;
case 'd':
case 'D':
sphereX++;
break;
case 'a':
case 'A':
sphereX--;
break;
case 't':
case 'T':
sphereZ++;
break;
case 'g':
case 'G':
sphereZ--;
break;
}
}
void main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,640);
glutCreateWindow("Camera View");
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
glutKeyboardFunc(keyboard);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
//printf("Help Screen\n\nW,S,A,D: Pan vertically and Horizontally\nT,G:Move forward/backward\nR: Reset Position\n\nF: Turn frustum On/Off");
glutMainLoop();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -