2012-12-21 149 views
0

嘗試紋理天空盒並使用「ImageLoader :: createJPG」加載紋理,加載正確格式的紋理文件,我的代碼紋理加載到多邊形就好了在每一個顯示循環(這顯然是一個可怕的性能打擊)。在Init中加載紋理()

但是,當我嘗試相同的代碼在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的,是定義..

+0

這似乎並不是實際失敗的代碼 - 沒有init()函數。沒有看到實際代碼的最佳猜測是,GL上下文在init()期間可能無效。其他GL初始化是否發生在該功能中? TexParameter設置大概在ImageLoader()中,或者它可能不會以給定的形式運行。 – JasonD

+0

更新與init ..不知道爲什麼我沒有擺在首位。其他GL初始化發生在函數中,是的,TexParameter設置在ImageLoader中.. –

+0

是的。是否有任何理由在init()中單獨調用它們不會設置全局變量? –

回答

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變量,並使用全局替代。

+0

謝謝!這是一個愚蠢的錯誤。 –

+0

現在它起作用了(在我的init()函數在正確的地方回調>。<)。謝謝! –