无法使用C ++在OpenGL ES中显示纹理

时间:2011-10-03 06:22:23

标签: c++ ios opengl-es textures

我正在尝试为iOS编写一个小型便携式2D引擎来学习C ++和OpenGL。现在我正在尝试显示我已加载的纹理。在使用CoreGraphic库加载时,我已成功显示纹理,但现在我正在尝试使用fread和libpng在C ++中加载文件我只看到一个白色的盒子。

我的纹理是64x64所以它不是2问题的力量。我也启用了GL_TEXTURE_2D。

第一个代码块用于加载png,将png转换为图像数据,并将数据加载到OpenGL中。

void AssetManager::loadImage(const std::string &filename)
{
Texture texture(filename);
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_bytep *row_pointers = NULL;
int bitDepth, colourType;

FILE *pngFile = fopen(std::string(Game::environmentData.basePath + "/" + filename).c_str(), "rb");

if(!pngFile)
     return;

png_byte sig[8];

fread(&sig, 8, sizeof(png_byte), pngFile);
rewind(pngFile);//so when we init io it won't bitch
if(!png_check_sig(sig, 8))
    return;

png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,NULL,NULL);

if(!png_ptr)
    return;

if(setjmp(png_jmpbuf(png_ptr)))
    return;

info_ptr = png_create_info_struct(png_ptr);

if(!info_ptr)
    return;

png_init_io(png_ptr, pngFile);

png_read_info(png_ptr, info_ptr);

bitDepth = png_get_bit_depth(png_ptr, info_ptr);

colourType = png_get_color_type(png_ptr, info_ptr);

if(colourType == PNG_COLOR_TYPE_PALETTE)
    png_set_palette_to_rgb(png_ptr);

/*if(colourType == PNG_COLOR_TYPE_GRAY && bitDepth < 8)
    png_set_gray_1_2_4_to_8(png_ptr);*/

if(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
    png_set_tRNS_to_alpha(png_ptr);

if(bitDepth == 16)
    png_set_strip_16(png_ptr);
else if(bitDepth < 8)
    png_set_packing(png_ptr);

png_read_update_info(png_ptr, info_ptr);


png_get_IHDR(png_ptr, info_ptr, &texture.width, &texture.height,
             &bitDepth, &colourType, NULL, NULL, NULL);

int components = GetTextureInfo(colourType);

if(components == -1)
{
    if(png_ptr)
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
    return;
}

GLubyte *pixels = (GLubyte *)malloc(sizeof(GLubyte) * (texture.width * texture.height * components));

row_pointers = (png_bytep *)malloc(sizeof(png_bytep) * texture.height);

for(int i = 0; i < texture.height; ++i)
    row_pointers[i] = (png_bytep)(pixels + (i * texture.width * components));

png_read_image(png_ptr, row_pointers);
png_read_end(png_ptr, NULL);


// make it
glGenTextures(1, &texture.name);
// bind it
glBindTexture(GL_TEXTURE_2D, texture.name);

GLuint glcolours;

switch (components) {
    case 1: 
        glcolours = GL_LUMINANCE;
        break;
    case 2: 
        glcolours = GL_LUMINANCE_ALPHA;
        break;
    case 3: 
        glcolours = GL_RGB;
        break;
    case 4: 
        glcolours = GL_RGBA;
        break;
}

glTexImage2D(GL_TEXTURE_2D, 0, components, texture.width, texture.height, 0, glcolours, GL_UNSIGNED_BYTE, pixels);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

png_destroy_read_struct(&png_ptr, &info_ptr, NULL);

fclose(pngFile);
free(row_pointers);
free(pixels);

textures.push_back(texture);
}

以下是我的Texture类的代码:

class Texture
{
public:

Texture(const std::string &filename) { this->filename = filename; }

unsigned int name;
unsigned int size;

unsigned int width;
unsigned int height;

std::string filename;
};

以下是我设置视图的方式:

void Renderer::Setup(Rectangle rect, CameraType cameraType)
{
_viewRect = rect;

glViewport(0,0,_viewRect.width,_viewRect.height);

if(cameraType == Renderer::Orthographic)
{

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(_viewRect.x,_viewRect.width,_viewRect.y,_viewRect.height,kZNear,kZFar);
    glMatrixMode(GL_MODELVIEW);
}
else
{
    GLfloat size = kZNear * tanf(DegreesToRadians(kFieldOfView) / 2.0); 

    glEnable(GL_DEPTH_TEST);

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity();
    glFrustumf(-size, size, -size / (_viewRect.width / _viewRect.height), size / (_viewRect.width / _viewRect.height), kZNear, kZFar); 
    glMatrixMode(GL_MODELVIEW);
}

glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_ONE, GL_SRC_COLOR);
glEnable(GL_BLEND); 
}

现在我在这里绘制纹理:

void Renderer::DrawTexture(int x, int y, Texture &texture)
{   
GLfloat vertices[] = {
    x,  _viewRect.height - y, 0.0,
    x,  _viewRect.height - (y + texture.height), 0.0,
    x + texture.width, _viewRect.height - y, 0.0,
    x + texture.width, _viewRect.height - (y + texture.height), 0.0
};


static const GLfloat normals[] = {
    0.0, 0.0, 1.0,
    0.0, 0.0, 1.0,
    0.0, 0.0, 1.0,
    0.0, 0.0, 1.0
};

GLfloat texCoords[] = {
 0.0, 1.0,
 0.0, 0.0,
 1.0, 1.0,      
 1.0, 0.0
 };


glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glLoadIdentity();
glTranslatef(0.0, 0.0, -3.0);

glBindTexture(GL_TEXTURE_2D, texture.name);

glVertexPointer(3, GL_FLOAT, 0, vertices);
glNormalPointer(GL_FLOAT, 0, normals);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

更新

我改变了用于生成纹理的功能。它现在创建一个随机测试纹理。无论如何,我仍然会看到白色的纹理。我会继续深入研究我的渲染方式。

这是新功能:

void AssetManager::CreateNoisyTexture(const char * key)
{    
Texture texture(key, 64, 64);
const unsigned int components = 4;


GLubyte *pixels = (GLubyte *)malloc(sizeof(GLubyte) * (texture.width * texture.height * components));
GLubyte *pitr1 = pixels;    
GLubyte *pitr2 = pixels + (texture.width * texture.height * components);

while (pitr1 != pitr2) {

    *pitr1 = rand() * 0xFF;
    *(pitr1 + 1) = rand() * 0xFF;
    *(pitr1 + 2) = rand() * 0xFF;
    *(pitr1 + 3)  = 0xFF;

    pitr1 += 4;
}

glGenTextures(1, &texture.name);    
glBindTexture(GL_TEXTURE_2D, texture.name); 
glTexImage2D(GL_TEXTURE_2D, 0, components, texture.width, texture.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

free(pixels);

printf("Created texture with key: %s  name: %d", texture.key, texture.name);

textures.push_back(texture);
}

1 个答案:

答案 0 :(得分:3)

好的我明白了。问题是我使用了错误的内部像素格式。

glTexImage2D(GL_TEXTURE_2D, 0, 4, texture.width, texture.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

应该是:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.width, texture.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

我需要传入GL_RGBA而不是4.在文档中我读到了这个:

  

internalFormat - 指定纹理中的颜色分量数。必须为1,2,3或4,或以下符号常量之一:

我认为这没关系,但我猜它确实如此。还有一点需要注意,我通过使用glGetError()来确定错误发生的位置。当我调用glTexImage2D()时,错误是GL_INVALID_OPERATION。

感谢您对IronMensan的帮助!

更新:

所以我无法发送4的原因是因为它不被允许,而且在OpenGL ES 1.1规范中,internalFormat和format需要相同。您只能发送GL_ALPHA,GL_RGB,GL_RGBA,GL_LUMINANCE或GL_LUMINANCE_ALPHA。经验教训。