0

これが私のコードです:

int main(int argc, char* argv[]) {

    if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
        return false;
    }
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
    SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 ) ;
    SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 ) ;
     if((window = SDL_CreateWindow("SDL opengl",100,100,640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN)) == NULL) {
        return false;
    }
    maincontext = SDL_GL_CreateContext(window);
    SDL_GL_SetSwapInterval(0);
    glewExperimental=true;
    GLenum err=glewInit();

    Chapter* chapter1 = new Chapter();
    chapter1->init();

    glClearColor(0.0,0.0,0.0,1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0,640.0/480.0,1.0,500.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


    SDL_Event Event;

    while(Running) {

        while(SDL_PollEvent(&Event)) {
            event_update(&Event);
        }
        //main loop

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glColor3f(1.0f, 1.0f, 1.0f);

        glBegin(GL_TRIANGLES);
            glVertex3f(0.0f, 1.0f, 0.0f);
            glVertex3f(-1.0f, -1.0f, 0.0f);
            glVertex3f(1.0f, -1.0f, 0.0f);
        glEnd();

        glTranslatef(3.0f, 0.0f, 0.0f);

        glBegin(GL_QUADS);
            glVertex3f(-1.0f, 1.0f, 0.0f);
            glVertex3f(1.0f, 1.0f, 0.0f);
            glVertex3f(1.0f, -1.0f, 0.0f);
            glVertex3f(-1.0f, -1.0f, 0.0f);
        glEnd();
        SDL_GL_SwapWindow(window);

    }
    return 0;
}

そして私の問題は、クリアされた色だけで何も見えないことです。何が問題になる可能性がありますか?(そして、Im init.ingのときにエラーなどはありません)重要な場合は、Visual Stuido 2010を使用しています。

4

1 に答える 1

1

見た目から、ニア クリッピング プレーンは 1.0 に、ファー クリッピング プレーンは 500.0 に設定されています。

ただし、Z 距離 0.0 で描画します

最初のステップとして、ジオメトリを表示できるかどうかを確認するために、図面を錐台 ([1.0, 500.0] の範囲内) に移動しようとします。

glVertex3f (x, y, z); // here your z value is 0, I suggest this could be the problem.

時間があまりなかったので、ここでもう少し詳しく説明できればと思いました。

更新コードでを実行するglTranslate...と、これは更新ごとに 1 回呼び出されますが、これはかなり頻繁に想定されるため、呼び出されるたびにジオメトリが x 軸で 3 単位移動します (つまり、おそらく意図したものではありません)。代わりに、マトリックスをプッシュしてこの変換を行い、翻訳が完了したらポップすることをお勧めします。OpenGL はマトリックス スタックを提供するため、基本的にマトリックスをプッシュするときに行うことは、「後で使用するために保存」し、変換とレンダリングを行ってからマトリックスをポップすることで、結果として正方形に戻ります。このようにして、マトリックスを更新ごとに新しい距離に変換するのではなく、マトリックスをオリゴから、更新ごとに描画したい位置に移動します。

したがって、私の提案は、メイン ループのコードを次のように変更することです。

//main loop
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);

/* push the current state of the matrix (i.e. store it so we don't make the mentioned change per update */
glPushMatrix();
/* think of it as moving your camera, you positioned your camera (frustum) at origo looking at everything inside a frustum shape one unit in front of the camera to 500 units in front of the camera, at an angle and so on.. so we want to draw our triangle to the left (from your view) 3 units and 10 units into the screen */
glTranslate3f(-3.0f, 0.0f, -10.0f);
glBegin(GL_TRIANGLES);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();

/* translate again, i.e. "move your camera" this time 6 units to the right, 3 units to compensate for the move you did to draw your triangle, and 3 units to place it in the right end. this time we do not move it in the z-axis, because we are already 10 pixels into the screen. */
glTranslatef(6.0f, 0.0f, 0.0f);

glBegin(GL_QUADS);
    glVertex3f(-1.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 0.0f);
glEnd();
/* pop the matrix changes we made with glTranslatef, so that we are back at the origo looking down negative z-axis so that the next draw-call we will again go 3 units left, 10 units down the z-axis and draw a triangle and so on.. hope it makes sense */
glPopMatrix();
SDL_GL_SwapWindow(window);

少なくとも、目に見える方法で何かをレンダリングするのに役立つことを願っています! すべてが不明確に思われる場合は、もう少し説明を試みることができますが、うまくいけば、これがレンダリングされて、少し遊んでみて、どのように組み合わされるかの感触をつかむことができます.

于 2013-12-06T11:28:24.640 に答える