gpt4 book ai didi

c - OpenGL矩形动画

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:30:32 25 4
gpt4 key购买 nike

我正在尝试根据随机数输入“动画化”矩形的高度。因此,对于每个新的随机数,都会重新绘制矩形。

我该怎么做?

我的代码:

#include <time.h>
#include <GL/freeglut.h>
#include <GL/gl.h>

float height;
int i;

/* display function - code from:
http://fly.cc.fer.hr/~unreal/theredbook/chapter01.html
This is the actual usage of the OpenGL library.
The following code is the same for any platform */
void renderFunction()
{
srand(time(NULL));
height = rand() % 10;

glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5); // bottom left corner
glVertex2f(-0.5, height); // top left corner
glVertex2f(-0.3, height); // top right corner
glVertex2f(-0.3, -0.5); // bottom right corner
glEnd();
glFlush();
}

/* Main method - main entry point of application
the freeglut library does the window creation work for us,
regardless of the platform. */
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(900,600);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL - First window demo");

glutDisplayFunc(renderFunction);
glutIdleFunc(renderFunction);
glutReshapeFunc(renderFunction);

glutMainLoop();

return 0;
}

虽然程序没有崩溃,但它只是简单地绘制了一个矩形。

最佳答案

鉴于您的尺码在 0.0 <= dimension <= 1.0 范围内并且您计算的高度在 0 <= height <= 9 范围内,您需要像这样缩放随机数:

height = (float)rand() / RAND_MAX;

另请移步srand(time(NULL));来自 renderFunction()main()否则你的矩形大小将在每一秒内被限制。

关于c - OpenGL矩形动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28053630/

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