有人可以帮我解释这段代码的逻辑吗?它不是随机画一个圆圈。
#include <math.h>
...
void drawCircle(){
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(programObject);
const float DEG2RAD = 3.14159f / 180.0f;
int totalVertices = 360;
const float radsPerLine = (360.f / totalVertices) * DEG2RAD;
float lineVertices[(totalVertices * 4)];
int i;
int ii = 0;
float rad = 0.8f;
for (i = 0; i < (totalVertices); i++) {
float degInRad = radsPerLine * i;
float cosine = cos(degInRad);
float sine = sin(degInRad);
lineVertices[ii++] = 0.0f;
lineVertices[ii++] = 0.0f;
lineVertices[ii++] = cosine * rad;
lineVertices[ii++] = sine * rad;
}
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, lineVertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_LINES, 0, totalVertices * 4);
}
分形图案出现在一个很高的数字(如360)。另一个奇怪的事情是有些线条似乎比其他线条更高,这也很奇怪。
答案 0 :(得分:1)
能够指定有多少段必须接近圆圈可能更有用:
void fillWithCircleData(GLfloat *lineVertices, GLint numLines, GLfloat radius){
const GLfloat DEG2RAD = 3.14159/180;
const GLfloat radsPerLine = (360.f/(numLines)*DEG2RAD;
GLint i;
for (i=0; i < numLines; ++i)
{
GLfloat rads = radsPerLine*i;
lineVertices[i*2] = cos(rads)*radius;
lineVertices[i*2+1] = sin(rads)*radius;
}
}
请注意,这会产生numLines
个顶点,但它不会重复最后一个顶点,因为最后一个顶点与第一个顶点相同(2PI == 0
)。这意味着您的元素数组必须提供numLines+1
索引,这是最后一个与第一个索引相同的索引。这样可以避免两次处理相同的顶点。您需要将基元从GL_LINE
更改为GL_LINES_STRIP
。
编辑:如果您不想指定一个元素数组,只需生成最后一个创建数组的点来保存numLines + 1并将上面的循环更改为{{1 }}。
注意:代码未经过测试。
答案 1 :(得分:0)
这最终起作用......
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(programObject);
const float DEG2RAD = 3.14159f / 180.0f;
int totalVertices = 360*12;
const float radsPerLine = (180.f / totalVertices) * DEG2RAD;
float lineVertices[(totalVertices * 4)];
int i;
int ii = 0;
float rad = 0.5f;
for (i = 0; i < (totalVertices); i++) {
float degInRad = radsPerLine * i;
float cosine = cos(degInRad);
float sine = sin(degInRad);
GLfloat x1 = -1 * (cosine * rad);
GLfloat y1 = -1 * (sine * rad);
GLfloat x2 = (cosine * rad);
GLfloat y2 = (sine * rad);
lineVertices[ii++] = x1;
lineVertices[ii++] = y1;
lineVertices[ii++] = x2;
lineVertices[ii++] = y2;
}
int size = sizeof(lineVertices)/sizeof(lineVertices[0]);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, lineVertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_LINES, 0, totalVertices * 2);
有两件事可以解决这个问题...