0

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

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

胶合天前;etc. 在文件顶部 #includes 之后的任何其他内容之前声明。

没有多少 enable 或 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中定义的,是的..

4

1 回答 1

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 变量,而是使用全局变量。

于 2012-12-22T09:49:18.860 回答