使用OpenGL 实现一个实心小球沿矩形均速运动代码
时间: 2024-05-11 16:14:43 浏览: 16
以下是使用OpenGL实现一个实心小球沿矩形均速运动的示例代码:
```c++
#include <GL/glut.h>
GLfloat ballPosX = 0.0f; // 小球的初始x坐标
GLfloat ballPosY = 0.0f; // 小球的初始y坐标
GLfloat ballRadius = 0.1f; // 小球的半径
GLfloat ballSpeedX = 0.03f; // 小球在x轴上的速度
GLfloat ballSpeedY = 0.02f; // 小球在y轴上的速度
GLfloat rectLeft = -0.8f; // 矩形的左边界
GLfloat rectRight = 0.8f; // 矩形的右边界
GLfloat rectBottom = -0.8f; // 矩形的下边界
GLfloat rectTop = 0.8f; // 矩形的上边界
void init() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // 设置背景颜色为黑色
glEnable(GL_DEPTH_TEST); // 启用深度测试
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清空颜色缓冲区和深度缓冲区
// 绘制矩形
glColor3f(1.0f, 1.0f, 1.0f); // 设置矩形颜色为白色
glBegin(GL_QUADS);
glVertex2f(rectLeft, rectBottom);
glVertex2f(rectRight, rectBottom);
glVertex2f(rectRight, rectTop);
glVertex2f(rectLeft, rectTop);
glEnd();
// 绘制小球
glColor3f(1.0f, 0.0f, 0.0f); // 设置小球颜色为红色
glTranslatef(ballPosX, ballPosY, 0.0f); // 将小球移到正确的位置
glutSolidSphere(ballRadius, 30, 30); // 绘制实心小球
glTranslatef(-ballPosX, -ballPosY, 0.0f); // 将小球移回原来的位置
// 更新小球位置
ballPosX += ballSpeedX;
ballPosY += ballSpeedY;
// 判断小球是否触碰到矩形边界
if (ballPosX - ballRadius < rectLeft) {
ballPosX = rectLeft + ballRadius;
ballSpeedX = -ballSpeedX; // 反弹
}
if (ballPosX + ballRadius > rectRight) {
ballPosX = rectRight - ballRadius;
ballSpeedX = -ballSpeedX; // 反弹
}
if (ballPosY - ballRadius < rectBottom) {
ballPosY = rectBottom + ballRadius;
ballSpeedY = -ballSpeedY; // 反弹
}
if (ballPosY + ballRadius > rectTop) {
ballPosY = rectTop - ballRadius;
ballSpeedY = -ballSpeedY; // 反弹
}
glutSwapBuffers(); // 切换前后缓冲区
}
void reshape(int width, int height) {
glViewport(0, 0, width, height); // 设置视口大小
glMatrixMode(GL_PROJECTION); // 切换到投影矩阵模式
glLoadIdentity(); // 重置投影矩阵
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); // 设置正交投影矩阵
glMatrixMode(GL_MODELVIEW); // 切换回模型视图矩阵模式
}
void timer(int value) {
glutPostRedisplay(); // 标记窗口需要重新绘制
glutTimerFunc(16, timer, 0); // 16毫秒后再次调用timer函数
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); // 双缓冲和深度缓冲
glutInitWindowSize(640, 480);
glutCreateWindow("OpenGL Demo");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
}
```
运行上述代码后,应该可以看到一个白色的矩形和一个红色的实心小球在矩形内均速运动,并在触碰到矩形边界时反弹。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)