2017-12-15 15 views
3

私は、ドットパターンを描き、ガスケットを生成する関数を使ってSierpinskiガスケットを生成しようとしています。トラブルシューティングSierpinski with OpenGL

しかし、コンパイルしてプログラムを実行すると、黒い画面だけが表示されます。問題の原因は何ですか?ここで

は私のコードです:

#include <Windows.h> 
#include <gl/GL.h> 
#include <glut.h> 

void myInit(void) { 
    glClearColor(1.0, 1.0, 1.0, 0.0); 
    glColor3f(0.0f, 0.0f, 0.0f); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluOrtho2D(0.0, 640.0, 0.0, 480.0); 
} 

class GLintPoint { 
    public: 
     GLint x, y; 
    }; 

    int random(int m) { 
     return rand() % m; 
    } 

    void drawDot(GLint x, GLint y) 
    { 
     glBegin(GL_POINTS); 
     glVertex2i(x, y); 
     glEnd(); 
    } 

    void Sierpinski(void) { 
    GLintPoint T[3] = {{ 10, 10 }, {300, 30}, {200, 300}}; 

    int index = random(3); 
    GLintPoint point = T[index]; 
    for (int i = 0; i < 1000; i++) 
    { 
     index = random(3); 
     point.x = (point.x + T[index].x)/2; 
     point.y = (point.y + T[index].y)/2; 
     drawDot(point.x, point.y); 
    } 
    glFlush(); 
} 

void main(int argc, char** argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(640, 480); 
    glutInitWindowPosition(100, 150); 
    glutCreateWindow("Sierpinski Gasket"); 
    glutDisplayFunc(drawDot); 
    myInit(); 
    glutMainLoop(); 
} 
+1

あなたが白ドットと黒ドットのような単純なものを、描くことに成功していますか? – Yunnosch

+0

はい、完全に描画されますが、これでは何も描画されません。 –

+0

参照のために何かをうまく描くための最小限のコードも示してください。この問題は、作業中の類似コードと非作業コードの違いにある可能性があります。 – Yunnosch

答えて

0

私はあなたが白地に黒のドットを描画する場合は、あなたがして、代わりに

glutDisplayFunc(drawDot); 
+0

これはSierpinski関数を呼び出して動作しました。ありがとう。 –

1

glutDisplayFunc(Sierpinski); 

を呼ぶべきだと思いますglClear背景をクリアする必要があります:

glClear(GL_COLOR_BUFFER_BIT); 

注:glClearColorは、ビューポートをクリアするために使用される色を設定しますが、それ自体はクリアされません。

あなたのコードは次のように何とかなります。

void drawDot(GLint x, GLint y) 
{ 
    glVertex2i(x, y); 
} 

void Sierpinski(void) { 
    GLintPoint T[3] = {{ 10, 10 }, {300, 30}, {200, 300}}; 

    int index = random(3); 
    GLintPoint point = T[index]; 

    glClearColor(1.0, 1.0, 1.0, 1.0); // set up white clear color 
    glClear(GL_COLOR_BUFFER_BIT); // clear the back ground (white) 

    glMatrixMode(GL_MODELVIEW); 
    glPushMatrix();     // setup model matrix 
    glScalef(1.5f, 1.5f, 1.0f);  // scale the point distribution 

    glColor3f(0.0f, 0.0f, 0.0f); // set black draw color 
    glPointSize(5.0f);    // set the size of the points 
    glBegin(GL_POINTS); 
    for (int i = 0; i < 1000; i++) 
    { 
     index = random(3); 
     point.x = (point.x + T[index].x)/2; 
     point.y = (point.y + T[index].y)/2; 
     drawDot(point.x, point.y); 
    } 
    glEnd(); 
    glPopMatrix();     // reset model matrix 

    glFlush(); 
} 
+0

これが解決しました。私は描画関数の色を設定するのを忘れていました。ありがとう。 –

+0

@AbdulBasitMehtab回答の変更を参照してください。あなたは 'glClear'を呼び出さなければなりません – Rabbid76

+0

さて、それを得ました。 しかし、出力は非常に小さい、それを少し大きくする方法は? –

関連する問題