SDL_Renderer:OpenGL纹理刚刚赢了

时间:2016-07-28 13:15:48

标签: opengl sdl sdl-2

我使用SDL_Renderer API绘制(大部分)2D场景,但很难用它绑定并将纹理应用到3D几何体。我已尝试直接使用OpenGL调用绑定纹理,并且我还尝试使用SDL_CreateTextureFromSurface创建纹理,并使用SDL_GL_BindTexture绑定。在这两种情况下,我都留下了平坦的白色几何形状。这是相关代码:

/**
 * @see View::render(View *, SDL_Renderer *)
 */
static void render(View *self, SDL_Renderer *renderer) {

    super(View, self, render, renderer);

    MeshModelView *this = (MeshModelView *) self;

    if (this->model) {

        const SDL_Rect frame = $(self, renderFrame);
        glViewport(frame.x, frame.y, frame.w, frame.h);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        glOrtho(-2.0, 2.0, -2.0, 2.0, -3.0, 3.0);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        // Quake is goofy: rotate so that Z is up
        glRotatef(90.0, 1.0, 0.0, 0.0);
        glRotatef(90.0, 0.0, 0.0, 1.0);
        glRotatef(15.0, 0.0, 1.0, 0.0);
        glTranslatef(0.0, 0.0, -1.0);

        glPushMatrix();

        glRotatef(quetoo.time * 0.08, 0.0, 0.0, 1.0);
        glScalef(this->scale, this->scale, this->scale);

        glColor4f(1.0, 1.0, 1.0, 1.0);

        const r_model_t *model = R_LoadModel(this->model);
        if (model) {

            assert(model->type == MOD_MD3);

            static SDL_Texture *texture;
            if (texture == NULL) {
                const char *image = model->mesh->material->diffuse->media.name;

                SDL_Surface *surface;
                if (Img_LoadImage(image, &surface)) {
                    texture = SDL_CreateTextureFromSurface(renderer, surface);
                    SDL_FreeSurface(surface);
                }
            }

            assert(texture);
            SDL_GL_BindTexture(texture, NULL, NULL);

            const r_md3_t *md3 = (r_md3_t *) model->mesh->data;
            const d_md3_frame_t *frame = md3->frames;

            vec3_t verts[model->num_verts];

            glEnableClientState(GL_VERTEX_ARRAY);
            glVertexPointer(3, GL_FLOAT, 0, verts);

            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
            glTexCoordPointer(2, GL_FLOAT, 0, model->texcoords);

            const r_md3_mesh_t *mesh = md3->meshes;
            for (uint16_t i = 0; i < md3->num_meshes; i++, mesh++) {

                const r_md3_vertex_t *v = mesh->verts;

                for (uint16_t j = 0; j < mesh->num_verts; j++, v++) {
                    VectorAdd(v->point, frame->translate, verts[j]);
                }

                glDrawElements(GL_TRIANGLES, mesh->num_tris * 3, GL_UNSIGNED_INT, mesh->tris);
            }

            glDisableClientState(GL_VERTEX_ARRAY);
            glDisableClientState(GL_TEXTURE_COORD_ARRAY);

            SDL_GL_UnbindTexture(texture);
        }

        glPopMatrix();
    }

    glViewport(0, 0, r_context.window_width, r_context.window_height);
}

我已通过代码调试并验证SDL_Texture对象是有效的OpenGL纹理。我已经检查了glGetError,但没有。正如我所说,我也尝试将纹理绑定到:

glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(texnum);

达到同样的效果:

The geometry is flat white

我已经从这个问题中读到SDL_Texture将无法用于加载它的第一帧:

SDL_Renderer won't show textures after passing through the function

那很好,但就我而言,它实际上从未出现过。我的预感是SDL_Renderer有一个着色器绑定,不知道对纹理进行采样。但这只是一种理论,而且我被困住了。也许我错过了很简单的事情......

0 个答案:

没有答案