简体   繁体   English

OpenGL 纹理加载与土壤帮助需要

[英]OpenGL texture loading with soil help need

Here is the code.这是代码。 Please help guys请帮助伙计们

#include <GL/glut.h>
#include <iostream>
#include <math.h>
#include "SOIL.h"
using namespace std;

GLuint texture[1];

int LoadGLTextures()
{
    texture[0] = SOIL_load_OGL_texture
        (
        "NeHe.bmp",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_INVERT_Y
        );

    if(texture[0] == 0)
        return false;


    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

    return true;
}

void ChangeSize(int w, int h)
{
    if(h == 0)
    {
        h = 1;
    }
    float ratio = 1.0f * w/h;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0,0,w,h);
    gluPerspective(45,ratio,1,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0,0.0,6.0,
              0.0,0.0,0.0,
              0.0f,1.0f,0.0f);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture[0]);

    glBegin(GL_QUADS);

    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);

    glEnd();

    glFlush();
}

void idle(void)
{
    glutPostRedisplay();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
    glutCreateWindow("ZC");

    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(display);
    glutIdleFunc(idle);

    glEnable(GL_DEPTH_TEST);

    glutMainLoop();
}

I dont get errors, but not loading the texture.我没有收到错误,但没有加载纹理。

You forgot to call LoadGLTextures in your main program.您忘记在主程序中调用LoadGLTextures I ran your program with this change:我用这个更改运行了你的程序:

  glEnable(GL_DEPTH_TEST);

  LoadGLTextures(); //<-- added this

  glutMainLoop();
}

and things worked fine.一切顺利。

I provided my own NeHe.bmp file, since you didn't link to one.我提供了我自己的 NeHe.bmp 文件,因为你没有链接到一个。 It could also fail if your .bmp file is bad in some way.如果您的 .bmp 文件在某些​​方面不好,它也可能会失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM