在Init()中加载纹理

时间:2012-12-21 21:54:24

标签: visual-c++ opengl

尝试纹理天空盒并使用“ImageLoader :: createJPG”加载纹理,它以正确的格式加载纹理文件,如果我在每个显示循环中加载它们,我的代码会将其纹理化为多边形显然是一个糟糕的表现。)

但是当我在init()中尝试相同的代码只是加载它们一次时,显示器甚至不认为它们存在(我最终得到了一个默认颜色的立方体。)

GLuint skyFront;在#includes之后,在文件顶部的任何其他内容之前声明等。

没有任何启用量或texParameters似乎使init()想要加载纹理。有什么想法吗?

GLuint skyTop;
GLuint skyFront;
GLuint skyBack;
GLuint skyBottom;
GLuint skyLeft;
GLuint skyRight;


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

    glMatrixMode(GL_MODELVIEW);

glLoadIdentity();




glTranslatef(0,0,-0.6);
glTranslatef(0, -0.4, -1);        


glPushMatrix(); 
glDisable(GL_DEPTH_TEST);

glDisable(GL_LIGHTING);
glDisable(GL_BLEND);
//glEnable(GL_LIGHT0);
//glTranslatef(camPos.x, camPos.y,camPos.z);
//glDepthMask(GL_FALSE);
glDisable(GL_CULL_FACE);
//draw skybox
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glColor3f(0.0, 1.0, 0.0);
glTranslatef(0.0,-2.0,-20.0);
        GLuint skyTop = ImageLoader::createJPG("../TestModels/top.jpg");
        GLuint skyBottom = ImageLoader::createJPG("../TestModels/bottom.jpg");
        GLuint skyLeft = ImageLoader::createJPG("../TestModels/left.jpg");
        GLuint skyFront = ImageLoader::createJPG("../TestModels/front.jpg");
        GLuint skyRight = ImageLoader::createJPG("../TestModels/right.jpg");
        GLuint skyBack = ImageLoader::createJPG("../TestModels/back.jpg");

        //FRONT
            glBindTexture(GL_TEXTURE_2D, skyFront);
            glBegin(GL_QUADS);
                glTexCoord2f(0, 0); glVertex3f(-SKYBOXSIZE, SKYBOXSIZE, -SKYBOXSIZE);       //A
                glTexCoord2f(0, 1); glVertex3f(-SKYBOXSIZE, -SKYBOXSIZE, -SKYBOXSIZE);      //B
                glTexCoord2f(1, 1); glVertex3f(SKYBOXSIZE, -SKYBOXSIZE, -SKYBOXSIZE);       //C
                glTexCoord2f(1, 0); glVertex3f(SKYBOXSIZE, SKYBOXSIZE, -SKYBOXSIZE);        //D
            glEnd();

            glBindTexture(GL_TEXTURE_2D, skyLeft);
            //LEFT
            glBegin(GL_QUADS);
                glTexCoord2f(1, 1); glVertex3f(SKYBOXSIZE, SKYBOXSIZE, -SKYBOXSIZE);        //D
                glTexCoord2f(0, 1); glVertex3f(SKYBOXSIZE, -SKYBOXSIZE, -SKYBOXSIZE);       //C
                glTexCoord2f(0, 0); glVertex3f(SKYBOXSIZE, -SKYBOXSIZE, SKYBOXSIZE);        //E
                glTexCoord2f(1, 0); glVertex3f(SKYBOXSIZE, SKYBOXSIZE, SKYBOXSIZE);     //F
            glEnd();

等。等

编辑:

init()代码如下所示:

void init()
{
glewInit()

    GLuint skyFront = ImageLoader::createJPG("../TestModels/front.jpg"); //load the texture, damnit

glClearColor(0.0,0.0,0.0,0.0);                      

glShadeModel(GL_SMOOTH);

//light position and colour
GLfloat light_position[] = { 0.0, 0.0, 20.0,0.0 };
GLfloat white_light[] = {0.8,0.8,0.8,0.0};
GLfloat diff_light[] = {1.0,1.0,1.0,0.0};
GLfloat spec_light[] = {1.0,1.0,1.0,0.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, white_light);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diff_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, spec_light);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);


//ambient light
GLfloat ambient[] = {0.3,0.3,0.3};
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);

//diffuse material component
GLfloat diff[] = {0.6,0.6,0.6};
glMaterialfv(GL_FRONT, GL_DIFFUSE, diff);

//specular material component
GLfloat WhiteSpec[] = {1,1,1};
glMaterialfv(GL_FRONT, GL_SPECULAR, WhiteSpec);

GLfloat shininess = 50;
glMaterialf(GL_FRONT, GL_SHININESS, shininess);

//ENABLE LIGHTING AND DEPTH TEST
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);




glEnable(GL_DEPTH_TEST);

cout << " loading model " << endl;
if(objLoader.loadModel("../TestModels/hummer.obj", model))//returns true if the model is loaded, puts the model in the model parameter
{
    cout << " model loaded " << endl;       

    //if you want to translate the object to the origin of the screen,
    //first calculate the centre of the object, then move all the vertices
    //back so that the centre is on the origin.
    model.calcCentrePoint();
    model.centreOnZero();


    model.calcVertNormalsUsingOctree();  //the method will construct the octree if it hasn't already been created.


    //turn on VBO by setting useVBO to true in 3dmodel.cpp default constructor - only permitted on 8 series cards and higher
    if(!model.useImmediateMode || model.useVBO)
    {
        model.initDrawElements();
    }
    if(model.useVBO)
    {
        model.initVBO();
        model.deleteVertexFaceData();
    }

}
else
{
    cout << " model failed to load " << endl;
}
}

所有剩下的GL命令都会通过,甚至我加载的模型都可以正常运行。 TexPerameters等在ImageLoader中定义,是的..

1 个答案:

答案 0 :(得分:0)

这些是全球性的:

GLuint skyTop;
GLuint skyFront;
GLuint skyBack;
GLuint skyBottom;
GLuint skyLeft;
GLuint skyRight;

但是这设置了一个本地:

void init()
{
    glewInit()

    GLuint skyFront = ImageLoader::createJPG("../TestModels/front.jpg"); //load the texture, damnit

您不需要重新声明skyFront变量,而是使用全局变量。