OpenGL: Nothing printed for GL_TRIANGLE_FAN

  • westwicked
    Likes 0

    Problem Description

    I am trying to draw sections of triangle fans to form a circle. Each circle sector will have 36 triangles (PIE_SECTION) to make a sector for a pie chart. The colors are randomized in another function (randomPieColor()) but a default color of (0, 0, 0) wouldn’t work as well. Basically, nothing has appeared in the screen with this code. I seem to be able to individually draw a sector by keying in each vertex of a triangle_fan, rather than using a ‘for loop’. However, as stated, its 36 triangles per sector, and the values (zoo1[i].bananas) are randomized as well.. So, its tough manually keying the vertices without a ‘for loop’.

    I am not sure what is the problem here, honestly. Here are the codes : 

    void plotPie1(){
    	GLfloat centerX = 100;
    	GLfloat centerY = 150;
    	GLfloat startingAngle = 0;
    	GLfloat endAngle;
    	GLfloat angleIncrement;
    	randomPieColor();
    
    	GLfloat radius = 50;
    	GLint totalBan = 0;
    	for(int i = 0; i < YEARSIZE; i++)
    		totalBan += zoo1[i].bananas;
    
    	for(int i = 0; i < YEARSIZE; i++){
    		endAngle = zoo1[i].bananas / totalBan * 360.0f;
    		drawCircle(zoo1[i].pieColors, centerX, centerY, radius, startingAngle, endAngle);
    		startingAngle += endAngle;
    	}
    }
    
    void drawCircle(Color color, GLfloat x, GLfloat y, GLfloat radius, GLfloat startingAngle, GLfloat endingAngle){
    	GLfloat pi = 22 / 7;
    	glColor3ub(color.red, color.green, color.blue);
    	GLfloat angle = startingAngle * (pi / 180);
    	GLfloat angle_increment = endingAngle * (pi / (180 * PIE_SECTION));
    
    	glBegin(GL_TRIANGLE_FAN);
    	glVertex2f(x, y);
    	
    	for(int i = 0; i <= PIE_SECTION; i++){
    		glVertex2f(x + radius * cos(angle), y + radius * sin(angle));
    		angle += angle_increment;
    	}
    	glEnd();
    }

    As you can see, what I am trying to do is to get an angle for a sector, and split it into 36 sectors which is the const PIE_SECTION. With the starting angle, and the ending angle defined to draw the circle in a clockwise fashion starting from the 2nd quadrant at 0 degrees, illustrated below:

    At each loop, the angle will be incremented to its total angle, 36 sectors combined. Please help me take a look at the logic, as well as why nothing is shown on my screen with this code..

    Thanks!

     

    Update: It looks like my logic to getting the angle is wrong. I have changed it from 

    endAngle = zoo1[i].bananas / totalBan * 360.0f;

    to

    endAngle = 360.0f / totalBan * zoo1[i].bananas;

    and it worked!

    But I am getting an extra white sector in my chart.. I will continue to scan through the code.


  • Sonar Systems admin
    Likes 0

    Can you show us a screenshot of the new problem.

  • westwicked
    Likes 0

    I have got 7 datas, therefore I need 7 sectors. However, I’ve got a new sector in white. As shown below:

    I checked and it turns out that the white part is not an extra data sector. It is there because my circle isn’t joined together indecision

    Any ideas? By the way, thank you for replying!

  • Sonar Systems admin
    Likes 0

    I did have a similar issue, try splitting it into 8, see what happens.


    This reply has been verified.
  • westwicked
    Likes 0

    I got it! The error seems to be in my data type.

    GLfloat pi = 22 / 7;

    should be

    GLfloat pi = 22.0f / 7;

    cool

    Sorry for your time wasted on trying it out. Thanks a lot for your help, nevertheless!

  • Sonar Systems admin
    Likes 1

    Something so simple fixes it, great to know for the future.

     

    Thanks for sharing this question as I have learnt something new.

Login to reply