gpt4 book ai didi

c++ - 我如何计算 glFrustum 参数?

转载 作者:行者123 更新时间:2023-12-04 07:55:48 24 4
gpt4 key购买 nike

我有这个代码:

/*
* This is a simple program that computes FPS
* by means of a circular buffer
*/
#include <GL/glut.h>
//#include <numeric>
#include <unistd.h>
#include <time.h>
#include <stdio.h>

// Number of elements in the circular buffer
#define NELS 10

// Number of lines
#define NLINES 10000

// circular buffer used to compute frame rate
float circularBuffer[NELS];
int firstInd = 0, nEls = 0;

// function to get the number of elapsed ticks
uint32_t getTick()
{
struct timespec ts;
unsigned theTick = 0U;
clock_gettime( CLOCK_REALTIME, &ts );
theTick = ts.tv_nsec / 1000000;
theTick += ts.tv_sec * 1000;
return theTick;
}

// Function to compute real modulus and NOT remained as % does
inline int modulo(int a, int b) {
const int result = a % b;
return result >= 0 ? result : result + b;
}

// Compute sum of the elements in the circular buffer
float sumCircularBuffer()
{
int ind;
float sum = 0;

if (nEls > 0) {
for (ind=1; ind<=nEls; ind++) {
sum = sum + circularBuffer[modulo(firstInd-ind, NELS)];
}
}

return sum;
}

// accumulate buffer and update window title
void computeAndShowFrameRate(void)
{
static float lastTime = 0.0f;
static unsigned int frameCount = 0;
char windowTitle[100];
float sumFPS;

float currentTime = (float)getTick() * 0.001f;
// Initialize lastTime to the current time
if (lastTime == 0) {
lastTime = currentTime;
}

// increase frame count
frameCount++;
if (currentTime - lastTime > 1.0f) {
// insert the current fps in the circular buffer
circularBuffer[firstInd] = ((float)frameCount) / (currentTime - lastTime);

// update variable lastTime
lastTime = currentTime;

//circularBuffer[firstInd] = (float)frameCount;
firstInd = ((firstInd+1)%NELS);
if (nEls < NELS) {
nEls++;
}
frameCount = 0;

// sum elements in circular buffer
sumFPS = sumCircularBuffer();
snprintf(windowTitle, 100, "FPS = %6.2f", sumFPS/nEls);
// update window title
glutSetWindowTitle(windowTitle);
}
}

// display function
void display(void)
{
int currLineInd;

// get current frame rate
computeAndShowFrameRate();

// clear buffer
glClear (GL_COLOR_BUFFER_BIT);

for (currLineInd = 0; currLineInd<NLINES; currLineInd++) {
// draw line
glBegin(GL_LINES);
// random color
glColor3f((float)rand()/RAND_MAX, (float)rand()/RAND_MAX, (float)rand()/RAND_MAX);
// random first point
glVertex2f((float)rand()/RAND_MAX, (float)rand()/RAND_MAX);
// random color
glColor3f((float)rand()/RAND_MAX, (float)rand()/RAND_MAX, (float)rand()/RAND_MAX);
// random second point
glVertex2f((float)rand()/RAND_MAX, (float)rand()/RAND_MAX);
glEnd();
}

glFinish();
glutPostRedisplay();
}

// initialization function
void init (void)
{
// Use current time as seed for random generator
srand(time(0));

// select clearing color
glClearColor (0.0, 0.0, 0.0, 0.0);

// Orthographic projection
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

// Window size and mode
int main(int argc, char** argv)
{
// pass potential input arguments to glutInit
glutInit(&argc, argv);

// set display mode
// GLUT_SINGLE = single buffer window
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize (400, 400);
glutInitWindowPosition (100, 100);
glutCreateWindow ("OpenGL Window");

// Call initialization routinesx
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

我必须用 glFrustum 替换 glOrtho 函数并得到相同的结果。

我阅读了关于 khronos 的 opengl 指南并了解 glOrtho 和 glFrustum 之间的区别,但我不知道如何计算参数。

如何计算传递给 glFrustum 函数的参数?

最佳答案

glFrustum() 需要 6 个参数来指定 6 个裁剪平面:leftrightbottom、< strong>top、nearfar 平面。视觉表示是这样的: enter image description here

将得出的值取决于您的实现和您使用的模型的规模。如上所述,如果投影几何体位于近平面前面或远平面后面,它将被裁剪,因此它不会可见。

要解决此问题,您必须重新计算 glFrustum() 函数的参数(坏主意)或沿 z 轴移动相机/场景。

引用资料:

  1. http://www.songho.ca/opengl/gl_transform.html
  2. https://learnopengl.com/Getting-started/Coordinate-Systems

关于c++ - 我如何计算 glFrustum 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66709363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com