我在opengl C ++中绘制了一个饼图,并且在初始化时,颜色变得很好。但是当我按下一个按钮来rand()颜色时,饼图变得不可见。
见下我的饼图代码
void PieChart1(int x, int y, int radius)
{
//float i;
int triangleAmount = 100; //# of triangles used to draw circle
int startPos = 0;
int Pie1AnglePercentage[7];
GLfloat twicePi = 2.0f * 3.14159;
float TotalPie1Banana = 0;
//TotalZoo1Banana = 0;
//int zoo1Data[7] = { 200, 300, 250, 275, 150, 310, 380 };
//Zoo1PieData
GLfloat colorTable[7][3] = { { Pie12014R, Pie12014G, Pie12014B },
{ Pie12013R, Pie12013G, Pie12013B },
{ Pie12012R, Pie12012G, Pie12012B },
{ Pie12011R, Pie12011G, Pie12011B },
{ Pie12010R, Pie12010G, Pie12010B },
{ Pie12009R, Pie12009G, Pie12009B },
{ Pie12008R, Pie12008G, Pie12008B } };
float Zoo1PieData[7] = { BananaDisplay2014,BananaDisplay2013,BananaDisplay2012,
BananaDisplay2011,BananaDisplay2010,BananaDisplay2009,BananaDisplay2008 };
//PIE 1 ZOO 1 SUM BANANAS TOTAL
for(int i = 0; i < 7; ++i)
TotalPie1Banana += Zoo1PieData[i];
//PIE 1 PERCENTAGE FOR EACH YEAR
for (int i = 0; i < 7; ++i)
Pie1AnglePercentage[i] = (Zoo1PieData[i] / TotalPie1Banana) * 100;
triangleAmount = 0;
for (int i = 0; i < 7; ++i)
triangleAmount += Pie1AnglePercentage[i];
for (int i = 0; i < 7; ++i)
{
glBegin(GL_TRIANGLE_FAN);
glColor3f(colorTable[i][0], colorTable[i][1], colorTable[i][2]);
glVertex3f(x, y, 0);
for (int j = startPos; j <= startPos + Pie1AnglePercentage[i]; ++j)
{
float const t = (twicePi*(float)j / (float)triangleAmount) + 3.141592;
glVertex3f(x - sin(t) * radius, y + cos(t) * radius, 0);
}
startPos += Pie1AnglePercentage[i];
glEnd();
}
以上是我的饼图代码。
这是我的案例密钥代码
case 'c':
Pie12014R = rand() % 255;
Pie12014G = rand() % 255;
Pie12014B = rand() % 255;
Pie12013R = rand() % 255;
Pie12013G = rand() % 255;
Pie12013B = rand() % 255;
Pie12012R = rand() % 255;
Pie12012G = rand() % 255;
Pie12012B = rand() % 255;
Pie12011R = rand() % 255;
Pie12011G = rand() % 255;
Pie12011B = rand() % 255;
Pie12010R = rand() % 255;
Pie12010G = rand() % 255;
Pie12010B = rand() % 255;
Pie12009R = rand() % 255;
Pie12009G = rand() % 255;
Pie12009B = rand() % 255;
Pie12008R = rand() % 255;
Pie12008G = rand() % 255;
Pie12008B = rand() % 255;
我不知道为什么当我按下C键时,馅饼会立即消失。
答案 0 :(得分:1)
饼图颜色设置为glColor3f
而非glColor3ub
,但我的颜色值已声明为无符号字节。