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;
}

我的问题是,除了清除的颜色之外,什么都看不到。它可能有什么问题?(并且当我初始化时没有错误或其他东西)我使用 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...在更新代码中执行此操作时,每次更新都会调用一次,这通常是人们可能会假设的,导致您的几何体在每次调用时在 x 轴上移动 3 个单位(即可能不是您的意思)。相反,我建议推动矩阵进行此转换,并在您完成翻译时弹出它。OpenGL提供了一个矩阵堆栈,所以基本上你在推送矩阵时所做的就是“将其存储起来以备后用”,然后进行变换和渲染,然后弹出矩阵,导致你回到第一方。这样,您不会在每次更新时将矩阵转换新的距离,而是将矩阵从 origo 移动到每次更新时要绘制的位置。

所以我的建议是将您的代码更改为主循环的代码:

//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 回答