OpenGL 2d纹理渲染问题

时间:2011-11-29 19:41:45

标签: c++ opengl

我正在尝试使用带有GLFW和SOIL的OpenGL加载和绘制2d纹理。我有这个代码,但我只得到一种纯色(似乎来自纹理)。

我已经测试了.png是否加载了SOIL附带的示例,并且它工作正常,因此我的代码中必须存在一些问题。

这是我的代码:

#include <cstdio>

#include "GL/glfw.h"
#include "SOIL.h"

// function declarations
void drawscene();
void idlefunc();
void updatedisplay();

// global data
GLuint texture; // our example texture

int main(int argc, char **argv) {
    if (!glfwInit()) {
        fprintf(stderr, "Failed to initialize GLFW\n");
        return 1;
    }

    if (!glfwOpenWindow(640, 480, 0, 0, 0, 0, 16, 0, GLFW_WINDOW)) {
        fprintf(stderr, "Failed to open GLFW window\n");
        return 1;
    }

    // enable vsync (if available)
    glfwSwapInterval(1);

    // load textures
    texture = SOIL_load_OGL_texture(
        "tex.png",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_DDS_LOAD_DIRECT
    );

    // check for an error during the texture loading
    if (!texture) {
        printf("SOIL loading error: '%s'\n", SOIL_last_result());
    }

    while (glfwGetWindowParam(GLFW_OPENED)) {
        idlefunc();
    }

    // if we get here something went wrong
    return 0;
}

// this function gets called every frame
void idlefunc() {
    updatedisplay();
    drawscene();
}

// set up te display
void updatedisplay() {
    int screen_width, screen_height;
    glfwGetWindowSize(&screen_width, &screen_height);

    if (screen_height <= 0) screen_height = 1;
    if (screen_width <= 0) screen_width = 1;

    glViewport(0, 0, screen_width, screen_height);

    glClearColor(0.02f, 0.02f, 0.02f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glEnable(GL_TEXTURE_2D);
    glDisable(GL_DEPTH_TEST);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, screen_width, screen_height, 0.0, 0.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // displacement trick for exact pixelization
    glTranslatef(0.375f, 0.375f, 0.0f);
}

// draw the scene in this function
void drawscene() {
    glBindTexture(GL_TEXTURE_2D, texture);
    glPushMatrix();
        glTranslatef(10.0f, 10.0f, 0);
        glBegin(GL_QUADS);
            glTexCoord2f(0.0f, 0.0f);
            glVertex2f(0.0f, 0.0f);
            glTexCoord2f(0.0f, 128.0f);
            glVertex2f(0.0f, 128.0f);
            glTexCoord2f(128.0f, 128.0f);
            glVertex2f(128.0f, 128.0f);
            glTexCoord2f(128.0f, 0.0f);
            glVertex2f(128.0f, 0.0f);
        glEnd();
    glPopMatrix();

    glfwSwapBuffers();
}

1 个答案:

答案 0 :(得分:1)

发现问题(感谢user786653)。无论顶点坐标,tex坐标都介于0.0和1.0之间。这是固定代码:

// draw the scene in this function
void drawscene() {
    glBindTexture(GL_TEXTURE_2D, texture);
    glPushMatrix();
        glTranslatef(10.0f, 10.0f, 0);
        glBegin(GL_QUADS);
            glTexCoord2f(0.0f, 0.0f);
            glVertex2f(0.0f, 0.0f);
            glTexCoord2f(0.0f, 1.0f);
            glVertex2f(0.0f, 128.0f);
            glTexCoord2f(1.0f, 1.0f);
            glVertex2f(128.0f, 128.0f);
            glTexCoord2f(1.0f, 0.0f);
            glVertex2f(128.0f, 0.0f);
        glEnd();
    glPopMatrix();

    glfwSwapBuffers();
}