如何绘制具有随机 no.of 个顶点的多边形并旋转它们?
How to draw polygons with random no.of vertices and rotate them?
对于 2D 小行星游戏,我试图绘制以不同速度旋转的随机多边形。我想到了使用 rand() 函数生成变量 "vertex" 、 "xaxis"、"yaxis" 和 radius 。但问题是当我绘制它们并旋转它们时,它们似乎一直在发生。就像它每次旋转时都会绘制不同的多边形。
这就是我通过选择围绕假想圆的圆周的顶点来绘制多边形的方法。
void spinAsteroid(bool zaxis, GLfloat rotation, GLfloat radius, GLfloat xpos, GLfloat ypos, bool multiplyMatrix, int speed)
{
GLfloat z;
z = 0.0;
int vertexRand = (rand() % 9) + 1;
int vertex = vertexRand;
if (zaxis)z = 1.0;
if (!multiplyMatrix) {
glLoadIdentity();
}
glTranslatef(xpos, ypos, 0);
glRotatef(rotation, 0, 0, z);
glTranslatef(-xpos, -ypos, 0);
drawAsteroid(radius, vertex, xpos, ypos);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
//glPushMatrix();
if(value!=7){ translatePattern(); }
//glPopMatrix();
//glPushMatrix();
int count = 0;
while(count<11){
int randIndex = rand() % 10;
int vertexRand = (rand() % 9) + 1;
spinAsteroid(true, angle, radius[randIndex], xaxis[randIndex], yaxis[randIndex], false, 2);
count++;
}
我只想在会旋转的不同位置绘制随机多边形。
像glTranslate
and glRotate
这样的操作定义了一个变换矩阵并将当前矩阵乘以新矩阵。
如果您在一行中进行乘法转换,则这些转换会与之前的转换连接在一起。
对于每颗小行星的单独变换,您必须在应用小行星变换之前存储当前矩阵,并在绘制小行星后恢复当前矩阵。
自 Legacy OpenGL matrices are organized on a stack, this can be achieved by push and pop the current matrix (see glPushMatrix
/ glPopMatrix
).
此外,您还必须设置一组随机点和半径,但您必须在每一帧中以相同的顺序绘制它们。
例如:
void spinAsteroid(GLfloat rotation, GLfloat radius, GLfloat xpos, GLfloat ypos, int vertex, int speed)
{
glPushMatrix();
glTranslatef(xpos, ypos, 0);
glRotatef(rotation, 0, 0, 1.0f);
glTranslatef(-xpos, -ypos, 0);
drawAsteroid(radius, vertex, xpos, ypos);
glPopMatrix();
}
for(int i = 0; i < 10; ++i)
{
spinAsteroid(angle, radius[i], xaxis[i], yaxis[i], i+1, 2);
}
对于 2D 小行星游戏,我试图绘制以不同速度旋转的随机多边形。我想到了使用 rand() 函数生成变量 "vertex" 、 "xaxis"、"yaxis" 和 radius 。但问题是当我绘制它们并旋转它们时,它们似乎一直在发生。就像它每次旋转时都会绘制不同的多边形。
这就是我通过选择围绕假想圆的圆周的顶点来绘制多边形的方法。
void spinAsteroid(bool zaxis, GLfloat rotation, GLfloat radius, GLfloat xpos, GLfloat ypos, bool multiplyMatrix, int speed)
{
GLfloat z;
z = 0.0;
int vertexRand = (rand() % 9) + 1;
int vertex = vertexRand;
if (zaxis)z = 1.0;
if (!multiplyMatrix) {
glLoadIdentity();
}
glTranslatef(xpos, ypos, 0);
glRotatef(rotation, 0, 0, z);
glTranslatef(-xpos, -ypos, 0);
drawAsteroid(radius, vertex, xpos, ypos);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
//glPushMatrix();
if(value!=7){ translatePattern(); }
//glPopMatrix();
//glPushMatrix();
int count = 0;
while(count<11){
int randIndex = rand() % 10;
int vertexRand = (rand() % 9) + 1;
spinAsteroid(true, angle, radius[randIndex], xaxis[randIndex], yaxis[randIndex], false, 2);
count++;
}
我只想在会旋转的不同位置绘制随机多边形。
像glTranslate
and glRotate
这样的操作定义了一个变换矩阵并将当前矩阵乘以新矩阵。
如果您在一行中进行乘法转换,则这些转换会与之前的转换连接在一起。
对于每颗小行星的单独变换,您必须在应用小行星变换之前存储当前矩阵,并在绘制小行星后恢复当前矩阵。
自 Legacy OpenGL matrices are organized on a stack, this can be achieved by push and pop the current matrix (see glPushMatrix
/ glPopMatrix
).
此外,您还必须设置一组随机点和半径,但您必须在每一帧中以相同的顺序绘制它们。
例如:
void spinAsteroid(GLfloat rotation, GLfloat radius, GLfloat xpos, GLfloat ypos, int vertex, int speed)
{
glPushMatrix();
glTranslatef(xpos, ypos, 0);
glRotatef(rotation, 0, 0, 1.0f);
glTranslatef(-xpos, -ypos, 0);
drawAsteroid(radius, vertex, xpos, ypos);
glPopMatrix();
}
for(int i = 0; i < 10; ++i)
{
spinAsteroid(angle, radius[i], xaxis[i], yaxis[i], i+1, 2);
}