2

私の目標は、テクスチャが読み込まれるときにテクスチャをスケーリングできるようにすることです。そのため、スプライトがレンダリングされるすべてのフレームで行う必要はありません。最適な方法は、スケーリングされたテクスチャを別のテクスチャにレンダリングし、基本的にキャッシュすることだと考えました。ただし、次のコードでは、(glClearColor により) 赤いクワッドしか得られないため、新しいテクスチャをレンダリングする方法ではなく、FBO が機能していることがわかります。

Texture *Graphics::loadTexture(const std::string& filename, int scale = 0) {
SDL_Surface *surface;
GLuint texture;

if((surface = IMG_Load(filename.c_str()))) {
    // Get the number of colors
    GLint numberOfColors = surface->format->BytesPerPixel;
    GLenum format;

    // Set the format of the texture based on the number of channels
    if(numberOfColors == 4) {
            if(surface->format->Rmask == 0x000000ff) {
                format = GL_RGBA;
            } else {
                format = GL_BGRA;
            }
    } else if(numberOfColors == 3) {
        if(surface->format->Rmask == 0x000000FF) {
            format = GL_RGB;
        } else {
            format = GL_BGR;
        }
    } else {
        throw Exception("Invalid image type for image  " + filename); 
    }

    // Generate texture id
    glGenTextures(1, &texture);

    // Bind the texture
    glBindTexture(GL_TEXTURE_2D, texture);

    // Texture stretching properties
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    // Create the image
    glTexImage2D(GL_TEXTURE_2D, 0, 4, surface->w, surface->h, 
                0, format, GL_UNSIGNED_BYTE, surface->pixels);

    glBindTexture(GL_TEXTURE_2D, 0);

} else {
    return NULL;
}
Texture *result;

if(scale > 1) {
    GLuint scaledTexture;
    GLuint fbo;
    GLuint fbod;

    // First we setup the depth buffer //
    // Create the framebuffer
    glGenRenderbuffersEXT(1, &fbod);

    // Bind the render buffer
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbod);

    // Set the render buffer storage to be a depth component
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, surface->w*scale, surface->h*scale);

    // Set the render buffer of this buffer to the depth buffer
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fbod);

    // Unbind the render buffer
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);

    // Next we setup the texture //
    glGenTextures(1, &scaledTexture);
    glBindTexture(GL_TEXTURE_2D, scaledTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w*scale, surface->h*scale, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glBindTexture(GL_TEXTURE_2D, 0);

    // Setup the frame buffer //
    glGenFramebuffersEXT(1, &fbo);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);

    // Attach the texture and render buffer to the frame buffer
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D,  scaledTexture, 0);

    // Attach the depth buffer
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fbod);

    glPushAttrib(GL_VIEWPORT_BIT | GL_ENABLE_BIT);
    glViewport(0, 0, surface->w*scale, surface->h*scale);

    glLoadIdentity();

    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBindTexture(GL_TEXTURE_2D, texture);

    glPushMatrix();

    glBegin(GL_QUADS);
        glTexCoord2i( 0, 0 );
        glVertex3f( 0.f, 0.f, 0.0f );

        glTexCoord2i( 1, 0 );
        glVertex3f( (GLfloat)surface->w*scale, 0.0f, 0.0f );

        glTexCoord2i( 1, 1 );
        glVertex3f( (GLfloat)surface->w*scale, (GLfloat)surface->h*scale, 0.f );

        glTexCoord2i( 0, 1 );
        glVertex3f( 0.0f, (GLfloat)surface->h*scale, 0.f );
    glEnd();

    glPopMatrix();


    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    glPopAttrib();
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

    result = new Texture(scaledTexture, surface->w, surface->h);
} else {
    result = new Texture(texture, surface->w, surface->h);
}

//Texture *result = new Texture(texture, surface->w, surface->h);

if(surface) {
    SDL_FreeSurface(surface);
}

return result;
}
4

3 に答える 3

1

いくつかのコメント:

  • glLoadIdentity() を実行するとき、どのマトリックスをリセットするか知っていますか? glMatrixMode を使用します。
  • glPushMatrix()/glPopMatrix() は、間にマトリックスを変更しないため、役に立ちません。
  • 2D テクスチャが有効になっていますか? glEnable(GL_TEXTURE_2D); を使用します。

とにかく、両方のマトリックスをクリアすると、画面の右上部分 (または生成されたテクスチャを使用しているポリゴン) にテクスチャが表示されます。UVs et Graphics::loadTexture を修正するだけです:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

これは機能するはずですが、ソフトウェアでスケーリングを行う必要があると思います。

于 2011-02-09T13:30:02.987 に答える
1

何をすべきかを教えようとしているのではなく、インデントを行う(テクスチャをスケーリングする)最も簡単な方法を教えてくれる場合、答えは次のとおりです。

glBlitFramebuffer(sx0,sy0,sx1,sy1,dx0,dy0,dx1,dy1, GL_COLOR_BUFFER_BIT, GL_LINEAR)

サイズが一致しない場合、読み取った画像を自動的にスケーリングします。これを機能させるには、GL_READ_FRAMEBUFFER、GL_DRAW_FRAMEBUFFER、および glDrawBuffer() をそれぞれに設定する必要があります。

于 2011-02-09T21:43:12.373 に答える
1

さて、テクスチャのロード時にスケーリングを実装する代わりに、スプライト オブジェクトに実装することにしました。そのため、頂点データを GPU に送信すると、実際のテクスチャをスケーリングするのではなく、頂点によってスプライトをスケーリングします。

于 2011-02-09T20:58:36.497 に答える