简体   繁体   English

如何在OpenGL ES 2.0中为每个网格加载不同的纹理?

[英]How to load different textures for each mesh in OpenGL ES 2.0?

I am new in OpenGL ES and I am trying to do some basic stuff as part of my learning curve. 我是OpenGL ES的新手,我正在尝试做一些基本的事情作为学习曲线的一部分。 I was able to render two meshes, a sphere and a cube with two glDrawArrays calls. 我能够使用两个glDrawArrays调用来渲染两个网格,一个球体和一个立方体。 I am trying to load a different texture to wrap around the cube and sphere but what is happening is that the last loaded texture is wrapped to both meshes. 我正在尝试加载不同的纹理以包裹立方体和球体,但是发生的是最后加载的纹理被包裹到两个网格中。 Does anyone know why this might be happening? 有谁知道为什么会这样?

Here is the code for the mesh: 这是网格的代码:

glEnable(GL_DEPTH_TEST);

glGenVertexArraysOES(1, &vertexArray);
glBindVertexArrayOES(vertexArray);

glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeMeshVerts, meshVerts, GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 4*8, BUFFER_OFFSET(0));

glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 4*8, BUFFER_OFFSET(4*3));

glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 4*8, BUFFER_OFFSET(4*6));

// Texture
// Create image from file
NSString *path = [[NSBundle mainBundle] pathForResource:textureImage ofType:@"jpg"];
NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:texData];

// Generates a new OpenGL ES texture buffer and // initializes the buffer contents using pixel data from the // specified Core Graphics image, cgImage. //生成新的OpenGL ES纹理缓冲区,并//使用//指定的Core Graphics图像cgImage中的像素数据初始化缓冲区内容。

[TextureLoader textureWithCGImage:image.CGImage options:nil error:NULL];

glBindVertexArrayOES(0);

Here is the draw code: 这是绘制代码:

glBindVertexArrayOES(_vertexArray);

// Render the object with ES2
glUseProgram(_program);

// Set the sampler texture unit to 0
glUniform1i (uniforms[UNIFORM_TEXTURE_MATRIX], 0);

glDrawArrays(GL_TRIANGLES, 0, numVertices);

You are not binding the textures to the sampler before rendering. 在渲染之前,您没有将纹理绑定到采样器。 First you need to keep a reference to the GLKTextureInfo object returned by textureWithCGImage: , then you can use glBindTexture with the texture name to bind it to the current samper. 首先,您需要保留对textureWithCGImage:返回的GLKTextureInfo对象的引用,然后可以使用带有纹理名称的glBindTexture将其绑定到当前的Samper。 eg 例如

GLKTextureInfo *tex = [TextureLoader textureWithCGImage:image.CGImage options:nil error:NULL];
...
glBindTexture(GL_TEXTURE_2D, tex.name);

You will need to do this before each draw call to set the correct texture to the sampler. 您需要在每次绘制调用之前执行此操作,以为采样器设置正确的纹理。 You can bind each texture you need to different samplers and then just pass in the correct sampler uniform also. 您可以将所需的每个纹理绑定到不同的采样器,然后也只需传递正确的采样器制服即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM