我有一個簡單的skybox
。天空盒紋理不顯示在opengl
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <vector>
#include <iostream>
#include "texture.h"
#include "deviLoader.h"
using namespace std;
static std::vector<GLuint> textures;
class Skybox
{
private:
float x, y, z;
float width, height, length;
bool isBottomRequired;
void rearrange();
void loadTextures();
public:
Skybox() {};
Skybox(float x,
float y,
float z,
float width,
float height,
float length);
void renderSkybox();
void setBottom(bool required);
~Skybox() {};
};
予加載使用魔鬼LIB在方法loadTextures
紋理。
void Skybox::loadTextures()
{
cout<<"SKYBOX: texture loading...";
Texture t;
LoadTexture(IL_BMP, "Back.bmp", &t);
textures.push_back(t.texID);
LoadTexture(IL_BMP, "Front.bmp", &t);
textures.push_back(t.texID);
LoadTexture(IL_BMP, "Bottom.bmp", &t);
textures.push_back(t.texID);
LoadTexture(IL_BMP, "Top.bmp", &t);
textures.push_back(t.texID);
LoadTexture(IL_BMP, "Left.bmp", &t);
textures.push_back(t.texID);
LoadTexture(IL_BMP, "Right.bmp", &t);
textures.push_back(t.texID);
/*
LoadTexture(IL_BMP, "Back.bmp", &t);
textures[0] = t.texID;
*/
cout<<"SUCCESS"<<endl;
}
其中功能loadTexture
void LoadTexture(ILenum FileType, char *filename, Texture *texture)
{
ilInit();
iluInit();
ilLoad(FileType, filename);
int err = ilGetError();
if (err != IL_NO_ERROR)
{
const char* strError = iluErrorString(err);
cout<<strError<<" - error during loading!"<<endl;
return;
}
texture->width = ilGetInteger(IL_IMAGE_WIDTH);
texture->height = ilGetInteger(IL_IMAGE_HEIGHT);
texture->bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
texture->imageData = ilGetData();
ilEnable(IL_CONV_PAL);
unsigned int type = ilGetInteger(IL_IMAGE_FORMAT);
glGenTextures(1, &texture->texID);
glBindTexture(GL_TEXTURE_2D, texture->texID);
gluBuild2DMipmaps(GL_TEXTURE_2D, texture->bpp, texture->width, texture->height,
type, GL_UNSIGNED_BYTE, texture->imageData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
在我main.cpp
我宣佈skybox
對象作爲一個全局變量,呈現在方法draw
Skybox skyBox;
.....
//part of method draw
if (selected[7] == 1) {
glDisable(GL_FOG);
glEnable(GL_TEXTURE_2D);
glPushMatrix();
glTranslatef(-0.2, 4, 2.3);
glScalef(0.15, 0.2, 0.19);
skyBox.renderSkybox();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
if (selected[6] == 1)
glEnable(GL_FOG);
}
renderSkybox
方法
void Skybox::renderSkybox()
{
glBindTexture(GL_TEXTURE_2D, textures[0]);
glBegin(GL_QUADS);
glTexCoord2f(1.0, 0.0); glVertex3f(x + width, y, z);
glTexCoord2f(1.0, 1.0); glVertex3f(x + width, y + height, z);
glTexCoord2f(0.0, 1.0); glVertex3f(x, y + height, z);
glTexCoord2f(0.0, 0.0); glVertex3f(x, y, z);
glEnd();
glBindTexture(GL_TEXTURE_2D, textures[1]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z + length);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height, z + length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y + height, z + length);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y,z + length);
glEnd();
if (isBottomRequired)
{
glBindTexture(GL_TEXTURE_2D, textures[2]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y,z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y,z + length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y,z + length);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y,z);
glEnd();
}
glBindTexture(GL_TEXTURE_2D, textures[3]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y + height, z);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y + height, z + length);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y + height,z + length);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height,z);
glEnd();
glBindTexture(GL_TEXTURE_2D, textures[4]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height,z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height,z + length);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y,z + length);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y,z);
glEnd();
glBindTexture(GL_TEXTURE_2D, textures[5]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y,z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y,z + length);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height,z + length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y + height,z);
glEnd();
};
當我畫的其他對象 - 可以看出,天空盒看起來是黑色的 問題是什麼?爲什麼它是黑色的?
編輯我的情況下,如果有人決定要考慮的問題更多的細節補充項目。有項目和glm,assimp,魔鬼,freeglut libs。項目沒有完成,但它應該工作。
http://www.filedropper.com/task
嘗試啓用GL_TEXTURE_2D,然後再加載紋理。 –
它也不適用於此。 – lapots
它實際上加載紋理嗎? – Vallentin