使用SOIL进行OpenGL纹理处理

时间:2016-05-22 06:26:04

标签: c++ c opengl textures soil

我正在用openGL打造篮球比赛,不过我正在采取措施并将我的工作分成几部分。我目前正专注于纹理,试图使用SOIL库建造房屋。

我对这一切都非常新,所以我可能设置错了,但我的主要问题是纹理就好像它们从我能看到的边缘被夹住了。

目前这个项目是一个奇异的墙,意在用砖砌成纹理,这里是我得到的输出图像: Texture Fail

以下是源代码:

#include <stdlib.h>
#include <GL/glut.h>
#include <SOIL.h>
#include <stdio.h>

float _angle = 0.0;
GLuint _textureBrick;

static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void renderScene(void)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);

    // Front side brick wall
    glPushMatrix();
        glBindTexture(GL_TEXTURE_2D, _textureBrick);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTranslatef(0,0,-6);
        glRotatef(_angle, 0.0, 1.0, 0.0);
        glBegin(GL_QUADS);  // Wall
            glTexCoord3f(-50.0,2.0,0.1);  glVertex3f(-50,0,1);
            glTexCoord3f(50.0,2.0,0.1);  glVertex3f(50,0,1);
            glTexCoord3f(50.0,0.0,0.1);  glVertex3f(50,-1.5,1);
            glTexCoord3f(-50.0,0.0,0.1);  glVertex3f(-50,-1.5,1);
        glEnd();
    glPopMatrix();

    glutSwapBuffers();
}

void mySpecialFunc(int key, int x, int y){
    switch (key) {
    case GLUT_KEY_RIGHT:
        _angle += 1;
        if (_angle > 360) _angle = 0.0;
        break;
    case GLUT_KEY_LEFT:
        _angle -= 1;
        if (_angle > 360) _angle = 0.0;
        break;
    }
    glutPostRedisplay();
}

GLuint loadTex(const char* texname)
{
    GLuint texture = SOIL_load_OGL_texture
                    (
                        texname,
                        SOIL_LOAD_AUTO,
                        SOIL_CREATE_NEW_ID,
                        SOIL_FLAG_INVERT_Y

                    );

    if( 0 == texture )
    {
        printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
    }

    //glBindTexture(GL_TEXTURE_2D, texture);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    return texture;
}

void Initialize() {
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-100.0, 100.0, -100.0, 100.0, -100.0, 100.0);

    //_textureFloor = loadTex("C:\\Users\\Mikle\\OneDrive\\Uni work\\Second Year\\Projects\\3D-House-using-OpenGL-and-C--master\\court.png");

    _textureBrick = loadTex("C:\\Users\\Mikle\\OneDrive\\Uni work\\Second Year\\Projects\\3D-House-using-OpenGL-and-C--master\\bricks.bmp");
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowPosition(0,0);
    glutInitWindowSize(600,600);
    glutCreateWindow("Textured House");
    glEnable(GL_DEPTH_TEST);

    glutReshapeFunc(resize);
    glutSpecialFunc(mySpecialFunc);
    glutDisplayFunc(renderScene);
    Initialize();

    glutMainLoop();

    return 0;
}

任何帮助都会受到赞赏,谢谢你!

1 个答案:

答案 0 :(得分:1)

迈克尔。为了生成mipmap并可能修复纹理,您应该将土壤加载函数的参数更改为

GLuint texture = SOIL_load_OGL_texture
                (
                    texname,
                    SOIL_LOAD_AUTO,
                    SOIL_CREATE_NEW_ID,
                    SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT

                );

这将自动从OpenGL库生成mipmap,因为之后没有调用glGenerateMipmaps()。它还节省了存储纹理所需的内存。纹理夹紧和包裹的另一个建议是使用

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

使用加载功能后。祝你的OpenGL学习体验顺利。我希望这能以某种方式帮助你。

编辑:

你也不应该每一帧重复glTexParameteri。这会给您(或用户的)CPU带来过多的不必要的压力。