objective-c - OpenGL ES 2.0 纹理变成黑色

标签 objective-c opengl-es-2.0

我第一次尝试使用 OpenGL ES 2.0 在平面上渲染纹理。我一定是做错了什么,因为它总是黑色的。

纹理来自资源中的图像。

这是我的渲染引擎的代码:

初始化:

...//program creation and linking 


UIImage* image = [UIImage imageNamed:@"marilyn.jpg"];

GLubyte* textureData = (GLubyte *)malloc(image.size.width * image.size.height * 4);
CGContext* textureContext = CGBitmapContextCreate(textureData, image.size.width, image.size.height, 8, image.size.width * 4, CGImageGetColorSpace(image.CGImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(textureContext, CGRectMake(0.0, 0.0, (CGFloat)image.size.width, (CGFloat)image.size.height), image.CGImage);
CGContextRelease(textureContext); 

glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);        

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.size.width, image.size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);

 ...//frame and render buffers...

渲染

glViewport(0, 0, backingWidth, backingHeight);

glClearColor(0.3f, 0.3f, 0.4f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(program);

glUniformMatrix4fv(u_modelView, 1, GL_FALSE, modelView.Pointer());
glUniformMatrix4fv(u_projection, 1, GL_FALSE, proj.Pointer()); 
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(u_sampler, 0);

const GLfloat squareVertices[] = {
    -200.0f, -200.0f,
    200.0f,-200.0f,
    -200.0f, 200.0f,        
    200.0f,200.0f,
};
const GLfloat squareTextureCoords[] = {
    1.0, 1.0,
    1.0, 0.0,
    0.0, 1.0,
    0.0, 0.0,
};

glVertexAttribPointer(p_texture.a_position, 2, GL_FLOAT, GL_FALSE, 0, squareVertices);
glEnableVertexAttribArray(p_texture.a_position);
glVertexAttribPointer(p_texture.a_textureCoord, 2, GL_FLOAT, GL_FALSE, 0, squareTextureCoords);
glEnableVertexAttribArray(p_texture.a_textureCoord);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

我很抱歉把所有这些代码都放在一起,但我已经四处寻找了几个小时,但我找不到问题所在。

渲染时我看到的只是一个黑色方 block ,而不是 Marilyn 的图片。

编辑:我忘了显示着色器 顶点着色器:

attribute vec4 position;
attribute vec2 texture_coord;

varying vec2 textureCoord;

uniform mat4 Projection;
uniform mat4 ModelView;

void main(void)
{
    gl_Position = Projection * ModelView * position;
    textureCoord =  texture_coord;
}

片段着色器:

varying mediump vec2 textureCoord;

uniform sampler2D tex;

void main(void)
{
    gl_FragColor = texture2D(tex, textureCoord);
}

最佳答案

很抱歉,正如我在对 cplusogl 回答的评论中所说的那样,问题是我没有使用边长为 2 的幂的正方形纹理。

关于objective-c - OpenGL ES 2.0 纹理变成黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7847961/

相关文章:

objective-c - 可分配 - 是否支持 -objc-arc?

ios - 从顶部(如下拉菜单)隐藏的动画 TableView

ios - 我如何确定 NSData 的内容何时指示 Web 服务请求失败?

Android OpenGL ES 2.0 黑色纹理

ios - 在 Vuforia 中绘图 - 姿势矩阵

objective-c - Xcode : Accessing method instance from other class file

objective-c - 为了支持小数字,我的变量应该是什么样的?

c - yuv420p 到 rgb 图像转换

graphics - 纹理内存使用

opengl-es-2.0 - OpenGL ES 2.0:是否支持GL_COMPRESSED_RGBA_S3TC_DXT5_EXT?