Hi,
I am trying to texture a cube but the texture isnt shown correctly. So I played around with opengl for android and was able to load objects and texture them. To test my code, I want to create a testing environment for Windows.
It seems to work, except the texture. For some reason my cube looks like htt p://s23.postimg.org/3wxi3axa3/Cube.jpg
(cant post urls). But it should be completly diffrent, but since it works on my phone I am guessing that I did something wrong and thats where I need your help :D. Since my code is not very clear, I will only post the important parts.
I am using a loader to read the file:
public static void draw(GL2 gl) {
if(mShouldLoadTexture)
gl.glBindTexture(GL2.GL_TEXTURE_2D, mTextureId);
.
gl.glFrontFace(GL2.GL_CW);
// Enable face culling.
gl.glEnable(GL2.GL_CULL_FACE);
// Enabled the vertices buffer for writing and to be used during
// rendering.
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
if(mShouldLoadTexture)
gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL2.GL_FLOAT, 0, vertexBuffer);
if(mShouldLoadTexture)
gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, textureBuffer);
gl.glDrawArrays(GL2.GL_TRIANGLES, 0, vertices.length/3);
gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
if(mShouldLoadTexture)
gl.glDisableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
}
gl.glGenTextures(1, textures, 0);
mTextureId = textures[0];
gl.glBindTexture(GL2.GL_TEXTURE_2D, mTextureId);
gl.glPixelStorei(GL2.GL_UNPACK_ALIGNMENT, 1);
//"Bild" is the class I created to maintain the image I am loading. The code how the buffer is created is below
gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, Bild.getWidth(), Bild.getHeight(), 0, GL2.GL_RGBA, GL2.GL_UNSIGNED_BYTE, Bild.getBuffer());
gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
gl.glTexParameterf(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
}
Image create buffer(testBuffer is the buffer which is returned in bild.getbuffer()) :
public image(String filename){
FileInputStream fileInputStream=null;
URL path=null;
BufferedImage bufferedImage=null;
try
{
File Datei=null;
Datei=new File(filename);
path=Datei.toURI().toURL();
bufferedImage = ImageIO.read(path);
}catch(Exception e)
{
//TODO clean up
System.out.println("Error 39 bufferedImage:"+e);
System.out.println("Error 40 bufferedImage:"+path.getFile());
System.exit(0);
}
int width = bufferedImage.getWidth();
zW=width;
int height = bufferedImage.getHeight();
zH=height;
// store pixels in TYPE_INT_ARGB format in packedPixels array
int[] packedPixels = new int[width * height];
bufferedImage.getRGB(0, 0, width, height, packedPixels, 0, width);
// OpenGL expects textures to be flipped upside down
int[] temp = new int[width * height];
for(int x=0; x<width; x++)
{
for(int y=0; y < height; y++)
temp[x + (y* width)] = packedPixels[x + ((height - 1 - x) * width)];
}
ByteBuffer buffer = ByteBuffer.allocateDirect(temp.length * 4);
buffer.order(ByteOrder.nativeOrder());
buffer.position(0);
testBuffer=buffer.asIntBuffer();
// convert the image data to the specified format
testBuffer.put(temp);
testBuffer.rewind();
//testBuffer.position(0);
}
Hope someone can help me
-Barry
PS: if the code is to messy to understand just tell me and I will try to clean it up