我有一个多维数据集定义为:
float vertices[] = { -width, -height, -depth, // 0
width, -height, -depth, // 1
width, height, -depth, // 2
-width, height, -depth, // 3
-width, -height, depth, // 4
width, -height, depth, // 5
width, height, depth, // 6
-width, height, depth // 7
}; 我有一个128x128的图像,我只是想把它画在立方体的6个面中的每一个上,而不是其他的东西。那么纹理坐标是什么呢?我需要实际的值。
这是绘图代码:
// Counter-clockwise winding.
gl.glFrontFace(GL10.GL_CCW);
// Enable face culling.
gl.glEnable(GL10.GL_CULL_FACE);
// What faces to remove with the face culling.
gl.glCullFace(GL10.GL_BACK);
// Enabled the vertices buffer for writing and to be used during
// rendering.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// Specifies the location and data format of an array of vertex
// coordinates to use when rendering.
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVerticesBuffer);
// Bind the texture according to the set texture filter
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[filter]);
gl.glEnable(GL10.GL_TEXTURE_2D);
// Enable the texture state
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Point to our buffers
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
// Set flat color
gl.glColor4f(red, green, blue, alpha);
gl.glDrawElements(GL10.GL_TRIANGLES, mNumOfIndices,
GL10.GL_UNSIGNED_SHORT, mIndicesBuffer);
// ALL the DRAWING IS DONE NOW
// Disable the vertices buffer.
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// Disable face culling.
gl.glDisable(GL10.GL_CULL_FACE);这是索引数组:
short indices[] = { 0, 2, 1,
0, 3, 2,
1,2,6,
6,5,1,
4,5,6,
6,7,4,
2,3,6,
6,3,7,
0,7,3,
0,4,7,
0,1,5,
0,5,4
}; 我不确定是否需要索引数组来查找tex坐标。请注意,我给出的立方体顶点数组是使用索引数组的立方体的最有效表示。立方体可以完美地绘制,但不能绘制纹理。只有一面显示正确的图片,但其他面是混乱的。我使用了各种关于纹理的在线教程中描述的方法,但它不起作用。
发布于 2011-02-14 00:19:53
你要找的是一个cube map。在OpenGL中,可以一次定义六个纹理(表示立方体的大小边),并使用3D纹理坐标(而不是常用的2D纹理坐标)映射它们。对于简单的立方体,纹理坐标将与顶点各自的法线相同。(如果仅以这种方式对平面立方体进行纹理处理,也可以在顶点着色器中合并法线和纹理坐标!)立方体贴图比尝试将相同的纹理应用于重复的四边形(额外的不必要的绘制步骤)要简单得多。
GLuint mHandle;
glGenTextures(1, &mHandle); // create your texture normally
// Note the target being used instead of GL_TEXTURE_2D!
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_CUBE_MAP, mHandle);
// Now, load in your six distinct images. They need to be the same dimensions!
// Notice the targets being specified: the six sides of the cube map.
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, width, height, 0,
format, GL_UNSIGNED_BYTE, data1);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, width, height, 0,
format, GL_UNSIGNED_BYTE, data2);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, width, height, 0,
format, GL_UNSIGNED_BYTE, data3);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, width, height, 0,
format, GL_UNSIGNED_BYTE, data4);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, width, height, 0,
format, GL_UNSIGNED_BYTE, data5);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, width, height, 0,
format, GL_UNSIGNED_BYTE, data6);
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// And of course, after you are all done using the textures...
glDeleteTextures(1, &mHandle);在指定纹理坐标时,您将使用3个坐标的集合,而不是2个的集合。在一个简单的立方体中,您使用归一化向量指向8个角。如果N= 1.0 / sqrt(3.0),那么一个角将是N,N,N;另一个角将是N,N,-N;依此类推。
发布于 2011-02-13 18:30:30
发布于 2011-02-13 18:31:45
对我来说,更容易考虑的是宽度= x,高度=y和深度= z,然后就是得到6个面的简单问题。
float vertices[] = { -x, -y, -z, // 0
x, -y, -z, // 1
x, y, -z, // 2
-x, y, -z, // 3
-x, -y, z, // 4
x, -y, z, // 5
x, y, z, // 6
-x, y, z// 7
}; 例如,你的立方体的正面将有一个正的深度(这个立方体的中心从你给出的顶点是0,0,0 ),现在由于有8个点和4个正的深度,你的正面是4,5,6,7,这是从-x,-y逆时针到-x,y。
好的,你的背面都是负的深度或-z,所以它就是0,1,2,3。
看到照片了吗?你的左脸都是负的宽度或-x,所以0,3,4,7,你的右脸是正x,所以1,2,5,6。
我会让你计算出立方体的顶部和底部。
https://stackoverflow.com/questions/4983532
复制相似问题