我无法在立方体的两侧渲染纹理。我已成功在立方体的顶部和底部渲染纹理,但无法在侧面渲染。
我有一个充满48个元素的纹理缓冲区(每个面部4 * 2个元素,6个面部是48个),它们是完整的将是良好的坐标。
正在绘制的立方体形状是绘图,但是没有渲染边。
我正在绘制的图像只是一个数字为1-9的图像,您可以从立方体的顶部看到。 textureBuffer一遍又一遍地模仿......
texture[0] = 0;
texture[1] = 0;
texture[2] = 1;
texture[3] = 0;
texture[4] = 1;
texture[5] = 1;
texture[6] = 0;
texture[7] = 1;
texture[8] = 0;
texture[9] = 0;
texture[10] = 1;
texture[11] = 0;
texture[12] = 1;
texture[13] = 1;
texture[14] = 0;
texture[15] = 1;
texture[16] = 0f;
texture[17] = 0f;
texture[18] = 1f;
texture[19] = 0f;
texture[20] = 1f;
texture[21] = 1f;
texture[22] = 0f;
texture[23] = 1f;
它只是加载纹理Buffer来渲染完整的纹理。
看起来只有前16个纹理坐标被绘制和使用,因为只有矩形的顶部和底部表面被纹理化。我调试了它,当我填充TextureBuffer时,大小是48。
@Override
public void draw(GL10 gl)
{
super.draw(gl);
//gl.glColor4f(255, 0, 0, 150);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnable(GL10.GL_ALPHA_TEST);
gl.glAlphaFunc(GL10.GL_GREATER, 0.0f);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2,GL10.GL_FLOAT,0,textureBuffer);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID);
gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_BLEND);
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3,GL10.GL_FLOAT,0,vertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES,indexBuffer.capacity(),GL10.GL_UNSIGNED_SHORT,indexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
gl.glDisable(GL10.GL_ALPHA_TEST);
gl.glDisable(GL10.GL_TEXTURE_2D);
gl.glColor4f(255, 255, 255, 255);
}
进来的参数变量纹理包含48个元素
public void constructTextureBuffer(float[] texture)
{
ByteBuffer vbb = ByteBuffer.allocateDirect(texture.length*4);
vbb.order(ByteOrder.nativeOrder());
textureBuffer = vbb.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);
}
正确设置vertexBuffer并使用索引缓冲区渲染多维数据集。你知道为什么没有渲染立方体的边?
所以我尝试手工创建一个形状,我遇到了与纹理缓冲区相同的情况。我可以渲染两张脸而不是第三张脸!似乎任何过去8个纹理顶点都不起作用。
这张照片显示了我的新形状。注意水平扩展。无论我对那些纹理坐标做什么,该纹理都不会改变。这也是我随机对象的第三面。
答案 0 :(得分:0)
我还没有让索引缓冲区和纹理协同工作。我几乎尝试了一切!所以相反(不幸的是)我正在构建许多三角形。为了解析blender生成的.obj文件,我编写了这个函数,为我创建了顶点缓冲区和纹理缓冲区。
public static Mesh createMesh(int resourceID)
{
Mesh m = new Mesh();
Scanner s = null;
BufferedReader inputStream = null;
ArrayList<Float> readInVerticies = new ArrayList<Float>();
ArrayList<Float> readInTextures = new ArrayList<Float>();
ArrayList<Short> readInVertexIndicies = new ArrayList<Short>();
ArrayList<Short> readInTextureIndicies = new ArrayList<Short>();
int numberFaces = 0;
try
{
inputStream = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(resourceID)));
s = new Scanner(inputStream);
String line = null;
/*
* Read the header part of the file
*/
line = inputStream.readLine();
line = inputStream.readLine();
line = inputStream.readLine();
line = inputStream.readLine();
line = inputStream.readLine();
while(line.charAt(0) == 'v' && line.charAt(1) != 't')
{
s = new Scanner(line);
s.next();
readInVerticies.add(s.nextFloat());
readInVerticies.add(s.nextFloat());
readInVerticies.add(s.nextFloat());
line = inputStream.readLine();
}
while(line.charAt(0)=='v' && line.charAt(1)=='t')
{
s = new Scanner(line);
s.next(); //read in "vt"
readInTextures.add(s.nextFloat());
readInTextures.add(s.nextFloat());
line = inputStream.readLine();
}
line = inputStream.readLine();
line = inputStream.readLine();
while(line != null && line.charAt(0) == 'f')
{
s = new Scanner(line);
s.useDelimiter("[ /\n]");
String buffer = s.next();
short vi1,vi2,vi3,vi4;
short ti1,ti2,ti3,ti4;
vi1 = s.nextShort();
ti1 = s.nextShort();
vi2 = s.nextShort();
ti2 = s.nextShort();
vi3 = s.nextShort();
ti3 = s.nextShort();
vi4 = s.nextShort();
ti4 = s.nextShort();
readInVertexIndicies.add(vi1);
readInVertexIndicies.add(vi2);
readInVertexIndicies.add(vi3);
readInVertexIndicies.add(vi4);
readInTextureIndicies.add(ti1);
readInTextureIndicies.add(ti2);
readInTextureIndicies.add(ti3);
readInTextureIndicies.add(ti4);
numberFaces = numberFaces + 1;
line = inputStream.readLine();
}
/*
* constructing our verticies. Use the number of faces * 6 because
* there are 2 triangles per face and 3 verticies on a triangle but there are
* also 3 coordinates per vertex.
*
* For the texture, the same number but there are only 2 coordinates per texture
*/
float verticies[] = new float[numberFaces * 6 * 3];
float textures[] = new float[numberFaces * 6 * 2];
for(int i=0;i<numberFaces;i++)
{
verticies[i*18+0] = readInVerticies.get((readInVertexIndicies.get(i*4+0)-1)*3+0);
verticies[i*18+1] = readInVerticies.get((readInVertexIndicies.get(i*4+0)-1)*3+1);
verticies[i*18+2] = readInVerticies.get((readInVertexIndicies.get(i*4+0)-1)*3+2);
textures[i*12+0] = readInTextures.get((readInTextureIndicies.get(i*4+0)-1)*2+0);
textures[i*12+1] = readInTextures.get((readInTextureIndicies.get(i*4+0)-1)*2+1);
verticies[i*18+3] = readInVerticies.get((readInVertexIndicies.get(i*4+1)-1)*3+0);
verticies[i*18+4] = readInVerticies.get((readInVertexIndicies.get(i*4+1)-1)*3+1);
verticies[i*18+5] = readInVerticies.get((readInVertexIndicies.get(i*4+1)-1)*3+2);
textures[i*12+2] = readInTextures.get((readInTextureIndicies.get(i*4+1)-1)*2+0);
textures[i*12+3] = readInTextures.get((readInTextureIndicies.get(i*4+1)-1)*2+1);
verticies[i*18+6] = readInVerticies.get((readInVertexIndicies.get(i*4+2)-1)*3+0);
verticies[i*18+7] = readInVerticies.get((readInVertexIndicies.get(i*4+2)-1)*3+1);
verticies[i*18+8] = readInVerticies.get((readInVertexIndicies.get(i*4+2)-1)*3+2);
textures[i*12+4] = readInTextures.get((readInTextureIndicies.get(i*4+2)-1)*2+0);
textures[i*12+5] = readInTextures.get((readInTextureIndicies.get(i*4+2)-1)*2+1);
verticies[i*18+9] = readInVerticies.get((readInVertexIndicies.get(i*4+0)-1)*3+0);
verticies[i*18+10] = readInVerticies.get((readInVertexIndicies.get(i*4+0)-1)*3+1);
verticies[i*18+11] = readInVerticies.get((readInVertexIndicies.get(i*4+0)-1)*3+2);
textures[i*12+6] = readInTextures.get((readInTextureIndicies.get(i*4+0)-1)*2+0);
textures[i*12+7] = readInTextures.get((readInTextureIndicies.get(i*4+0)-1)*2+1);
verticies[i*18+12] = readInVerticies.get((readInVertexIndicies.get(i*4+2)-1)*3+0);
verticies[i*18+13] = readInVerticies.get((readInVertexIndicies.get(i*4+2)-1)*3+1);
verticies[i*18+14] = readInVerticies.get((readInVertexIndicies.get(i*4+2)-1)*3+2);
textures[i*12+8] = readInTextures.get((readInTextureIndicies.get(i*4+2)-1)*2+0);
textures[i*12+9] = readInTextures.get((readInTextureIndicies.get(i*4+2)-1)*2+1);
verticies[i*18+15] = readInVerticies.get((readInVertexIndicies.get(i*4+3)-1)*3+0);
verticies[i*18+16] = readInVerticies.get((readInVertexIndicies.get(i*4+3)-1)*3+1);
verticies[i*18+17] = readInVerticies.get((readInVertexIndicies.get(i*4+3)-1)*3+2);
textures[i*12+10] = readInTextures.get((readInTextureIndicies.get(i*4+3)-1)*2+0);
textures[i*12+11] = readInTextures.get((readInTextureIndicies.get(i*4+3)-1)*2+1);
}
m.constructVertexBuffer(verticies);
m.constructTextureBuffer(textures);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return m;
}
因此,如果您在解析.obj文件时遇到问题,请随意将其用作参考或指南! Blender为您提供4个顶点的所有面孔,这会将所有内容变为3s,每个面构造2个三角形。