2013-02-11 97 views
2

我試圖繪製一個橢圓,並在其中放置一個圓圈,當然使用不同的顏色。我可以自己畫出形狀,但不能讓它們同時出現在屏幕上。使用GL_TRIANGLE_FAN OpenGL把橢圓內的圓?

這裏是我到目前爲止的代碼:

#ifdef __APPLE__ 
#include <GLUT/glut.h> 
#else 
#include <GL/glut.h> 
#endif 

#include <stdlib.h> 

#include <math.h> 


#define PI 3.14159 

const int ScreenWidth = 640; 
const int ScreenHeight = 480; 


void myInit(void) 
{ 
    glPointSize(1.0); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(0.0, (GLdouble)ScreenWidth, 0.0, (GLdouble)ScreenHeight); 
} 


void drawFilledElipse(GLfloat x, GLfloat y, GLfloat radius) 
{ 
    int i; 
    int triangleAmount = 20; //# of triangles used to draw circle 
    glColor3f(0.0f, 0.0f, 0.0f); 

    GLfloat twicePi = 2.0f * PI; 

    GLint x1 =320, y1 = 240, r1 = 75; 

    glBegin(GL_TRIANGLE_FAN); 
    glVertex2f(x, y); // center of circle 
    for (i = 0; i <= triangleAmount; i++) { 
     glVertex2f(
      1.4 * (x + (radius * cos(i * twicePi/triangleAmount))), 
      1.111 * (y + (radius * sin(i * twicePi/triangleAmount))) 
     ); 
    } 

    glutSwapBuffers(); 
} 

void drawFilledCircle(GLfloat x, GLfloat y, GLfloat radius) 
{ 
    int i; 
    int triangleAmount = 20; //# of triangles used to draw circle 
    glColor3f(1.0f, 0.0f, 0.0f); 

    GLfloat twicePi = 2.0f * PI; 

    glBegin(GL_TRIANGLE_FAN); 
    glVertex2f(x, y); // center of circle 
    for(i = 0; i <= triangleAmount;i++) 
    { 
     glVertex2f(
      (x + (radius * cos(i * twicePi/triangleAmount))), 
      (y + (radius * sin(i * twicePi/triangleAmount))) 
     ); 
    } 

    glutSwapBuffers(); 
} 

void myDisplay(void) 
{ 
    glClearColor(1.0, 1.0, 1.0, 0.0); 
    glClear(GL_COLOR_BUFFER_BIT); 

    drawFilledCircle(320,240,75); 
    drawFilledElipse(225,240,100); 

    glEnd(); 
    glFlush(); 
} 

int main(int argc, char ** argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 
    glutInitWindowSize(ScreenWidth, ScreenHeight); 
    glutInitWindowPosition(100, 150); 
    glutCreateWindow("Random dots..."); 
    glutDisplayFunc(myDisplay); 
    myInit(); 
    glutMainLoop(); 
    return 0; 
} 

回答

3

你,當你打破了你抽出成兩個獨立的功能製作的拷貝和粘貼錯誤。您在之後撥打glutSwapBuffers。在完成繪製的所有內容之後,您只想交換緩衝區,而不只是一個對象。

+0

哦,哇,謝謝它現在的作品!非常感謝你,我不知道在每個人之後我不應該那樣做,並且知道我知道。 :) – user2060164 2013-02-11 20:50:46

3

除了Nicol的評論,您還沒有將您的glBegin電話與您的glEnd電話相匹配。您的drawFilledCircledrawFilledElipse例程應以每個調用glEnd結束。在OpenGL中,在glBegin之後調用glBegin時出錯,而沒有介入glEnd