PDA

View Full Version : Problem with Texture



Barry91
11-17-2013, 04:04 AM
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

tonyo_au
11-17-2013, 05:14 AM
Yourcomments in the image load say the format is ARGB(TYPE_INT_ARGB) but the load says RGBA(GL2.GL_RGBA). You could check this. Also you don't show the uv values for the vertices.

Barry91
11-17-2013, 05:32 AM
Also you don't show the uv values for the vertices
I create the values with blender and read the file with an other loader I wrote. Since it works on my phone, the values should be allright.
vertices are stored in "vertexBuffer" and texturecoordinates are stored in "textureBuffer".


Yourcomments in the image load say the format is ARGB(TYPE_INT_ARGB) but the load says RGBA(GL2.GL_RGBA)
Need to research a bit on this one.

But I noticed that I have not rly understood the diffrence between RGB and RGBA.
Because i the loading works with RGBA but i am working with RGB (bufferedImage.getRGB(0, 0, width, height, packedPixels, 0, width);). So I think I need to understand the diffrence and then convert RGB to RGBA (if that even works).
Is that thought right?

tonyo_au
11-18-2013, 08:46 PM
RGB packs each colour in 3 bytes so 4 pixels will require 12 bytes; RGBA packs each colour in 4 bytes so 4 pixels will require 16 bytes. If your buffer is only RGB then


gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, Bild.getWidth(), Bild.getHeight(), 0,GL2.GL_RGBA, GL2.GL_UNSIGNED_BYTE, Bild.getBuffer());

should be



gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, Bild.getWidth(), Bild.getHeight(), 0, GL2.GL_RGB, GL2.GL_UNSIGNED_BYTE, Bild.getBuffer());