gpt4 book ai didi

c - 游戏项目: Explanation Required

转载 作者:行者123 更新时间:2023-11-30 21:37:34 24 4
gpt4 key购买 nike

我正在开发一个 OpenGL 项目,我需要对该主题的核心组件进行一些简短的解释,因为我需要向有需要的人解释。

以下是程序部分

下面是程序中用到的全局变量和头文件

#include<GL/glut.h>
#include<math.h>
#include<stdbool.h>
#define PI 3.14159265f
#include<stdio.h>

GLfloat ballRadius = 0.2,xradius=0.2,xxradius=1.0;
GLfloat ballX = 0.0f;
GLfloat ballY = 0.0f;
GLfloat ballXMax,ballXMin,ballYMax,ballYMin;
GLfloat xSpeed = 0.02f;
GLfloat ySpeed = 0.007f;
int refreshMills = 30;
GLfloat angle=0.0;
int xa,ya;
int flag=0,flag1=0;
int score = 0;
void *currentfont;
GLfloat xo=0, yo=0, x, y;
GLdouble clipAreaXLeft,clipAreaXRight,clipAreaYBottom,clipAreaYTop;
void balldisp() ;
void scoredisp();

这是 reshape 函数。我需要做它正在做什么、正在计算和存储什么。这里很困惑

void reshape(GLsizei width,GLsizei height)
{
GLfloat aspect = (GLfloat)width / (GLfloat)height;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(width >=height)
{
clipAreaXLeft = -1.0 * aspect;
clipAreaXRight = 1.0 * aspect;
clipAreaYBottom = -1.0;
clipAreaYTop = 1.0;
}
else
{
clipAreaXLeft = -1.0;
clipAreaXRight = 1.0 ;
clipAreaYBottom = -1.0 / aspect;
clipAreaYTop = 1.0/ aspect;
}
gluOrtho2D(clipAreaXLeft,clipAreaXRight,clipAreaYBottom,clipAreaYTop+0.10);
ballXMin = clipAreaXLeft + ballRadius;
ballXMax = clipAreaXRight - ballRadius;
ballYMin = clipAreaYBottom + ballRadius;
ballYMax = clipAreaYTop - ballRadius;
}

下面是显示球的代码。它正在计算什么以及如何设置速度和方向。这里很困惑

void balldisp() 
{
glTranslatef(ballX,ballY,0.0f);
glBegin(GL_TRIANGLE_FAN);
color();
glVertex2f(0.0f,0.0f);
int numSegments = 100;
int i;
for(i=0;i<=numSegments;i++)
{
angle = i*2.0f*PI/numSegments;
glVertex2f(cos(angle)*ballRadius,sin(angle)*ballRadius);
}
glEnd();

ballX += xSpeed;
ballY += ySpeed;

if(ballX > ballXMax)
{ xa=ballX;
ballX = ballXMax;
xSpeed = -xSpeed;

}
else if(ballX < ballXMin)
{ xa=ballX;
ballX = ballXMin;
xSpeed = -xSpeed;

}
if(ballY > ballYMax)
{ ya=ballY;
ballY = ballYMax;
ySpeed = -ySpeed;

}
else if(ballY < ballYMin)
{ ya=ballY;
ballY = ballYMin;
ySpeed = -ySpeed;

}

我想了解 reshape 功能和小球显示。他们在做什么以及那里的事情是如何完成的。

附注该项目是关于球的随机运动,它撞击窗口的边界并向其他方向移动

最佳答案

reshape函数已向 GLUT 注册(使用 glutReshapeFunc ),以便每当窗口大小发生变化时,它就会被 GLUT 调用。 请注意,在 reshape 函数中放置用于设置视口(viewport)和/或投影矩阵的 OpenGL 函数是不好的风格,应该避免。所有与OpenGL绘图相关的函数(glViewport和矩阵设置)都属于显示函数。

类似地,显示函数是用 GLUT 注册的(使用 glutDisplayFunc ),以便每当窗口需要重绘时(因为它变得可见,内容需要刷新或重绘),它就会被 GLUT 调用。已通过 glutPostRedisplay 请求。

关于c - 游戏项目: Explanation Required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29769205/

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