Jump to content
  • Advertisement
Sign in to follow this  
Bengy

Texture coordinates ignored?

This topic is 2654 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[syntax="java"][/syntax]Hello everybody,
I've got some problems with the Rotation of a Texture that I am mapping to a Quad.

I'll post the Code of my TexuredQuad class below.
I've got a derived class which should show a rectangle with a 2*2 Map on it looking like this:
4f99977c23.jpg

My problem is that the rotation of the map is wrong ( it should be blue, red, white, green)

I thought the Problem were the texture coordinates and so i tried a couple different configurations always with the same result.

Is my thinking wrong or is there some fault in the Code:

/**
* The TexuredQuad constructor.
* Initiate the buffers.
*/
public TexuredQuad() {
float texture[] = {
// The initial texture mapping coordinates (u, v) for the vertices.
//PROBLEM: always looks the same regardless on which configuration I use
0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
//1.0f,1.0f,0.0f,1.0f,1.0f,0.0f,1.0f,1.0f};
//0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f};
float vertices[] = { -0.5f, -0.5f, // Bottom Left
0.5f, -0.5f, // Bottom Right
-0.5f, 0.5f, // Top Left
0.5f, 0.5f // Top Right
};
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
textureBuffer = byteBuf.asFloatBuffer();
textureBuffer.put(texture);
textureBuffer.position(0);

indexBuffer = ByteBuffer.allocateDirect(indices.length);
indexBuffer.put(indices);
indexBuffer.position(0);

vertexBuffer = byteBuf.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
}

/**
* Called by the renderer every frame. This is where the Object is being drawn.
* @param gl GL10 interface
*/
public void Render(GL10 gl) {
if (!mRender) return; // If Object should not be rendered ->return
gl.glPushMatrix(); // Save the Camera Matrix

gl.glBindTexture(GL10.GL_TEXTURE_2D, // Bind the previously generated texture
mTextureID[0]);
gl.glTranslatef(mTranslateX, mTranslateY, mTranslateZ); //Translate the Object according to its position
gl.glScalef(mScaleX, mScaleY, 0.0f); // Scale the Object according to its size
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); // Load the textureBuffer
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer); // Load the vertexBuffer
gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,
GL10.GL_UNSIGNED_BYTE, indexBuffer);

gl.glPopMatrix(); // Restore Camera Matrix
}


Thank you for your helps and thoughts,
Benni

Share this post


Link to post
Share on other sites
Advertisement
First, the texture coordinates should be like this:

float texture[] = {
0.0, 1.0,
1.0, 1.0,
1.0, 0.0,
0.0, 0.0
};


Remember that in OpenGL texture space, the v (y coordinate) increases in the positive direction as it goes down the texture from top to bottom.

What does your setup code look like?

Share this post


Link to post
Share on other sites
Thanks for the replies,

I've tried all different kinds for the texture buffer also the one you suggested the problem is that the rendering doesn't seem to be influenced at all (even when I use just 0.0,0.0 for every vertex the Texture is mapped completely but wrongly rotated, although it should have only one color then).

My setup code is:


public void SetUpOGL(GL10 gl, int width, int height){
gl.glEnable(GL10.GL_TEXTURE_2D); // Enable the Texturing in the Renderer .
gl.glClearDepthf(1.0f); // Depth buffer setup.
gl.glShadeModel(GL10.GL_SMOOTH); // Enable smooth shading.
gl.glEnable(GL10.GL_DEPTH_TEST); // Enables depth testing.
gl.glDepthFunc(GL10.GL_LEQUAL); // Set the type of depth testing to do.
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // Really nice perspective calculations. TODO Disable for 2D?
GL10.GL_NICEST);
gl.glViewport(0, 0, width, height); // Sets the current view port to the new size.

gl.glMatrixMode(GL10.GL_PROJECTION); // Load the Projection Matrix.
gl.glLoadIdentity(); // Reset the Projection Matrix.
GLU.gluPerspective(gl, 45.0f, // Set the Projection Matrix to a Perspective Matrix.
(float) width / (float) height,
0.1f, 101.0f);
}


And this is the Rendering method that calls the Render method of my TexturedQuad:
public void Render(GL10 gl) {
gl.glMatrixMode(GL10.GL_MODELVIEW); // Load the model matrix
gl.glLoadIdentity(); // And reset it.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); // Point to the vertex array.
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Point to the texture coordinate array.
//gl.glFrontFace(GL10.GL_CCW); // Set the face rotation (counterclockwise order of vertexes to build a quad) TODO check clockwise (GL_CW) and without
// TODO: translate matrix move to camera class
gl.glTranslatef(0.0f, 0.0f, 0.0f);

for(Object3D o:Objects){
o.Render(gl);} // For every object in the array call o.Render(gl).

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // Undo pointing to vertex array. TODO check whether that can be done in Enable_2D()
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); // Undo pointing to texture coordinate array. TODO same
}


Hope this helps.

PS: I'm using opengl es 1.1 for an android application.

Share this post


Link to post
Share on other sites
Well, if the texture seems rotated, why don't you just switch around the texture coordinates such as
float texture[] = {
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0
};or
float texture[] = {
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0
};

etc until it looks like you want.

Share this post


Link to post
Share on other sites

Well, if the texture seems rotated, why don't you just switch around the texture coordinates such as
float texture[] = {
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0
};or
float texture[] = {
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0
};

etc until it looks like you want.



Okey, I think I didn't express myself clearly:
my Problem is not the wrong rotation of the texture itself I know how I should theoretically correct that.
The Problem is that it doesn't matter whether I change it or not because the output is always the same.
Even if I don't initialize texure:
float texture[] = float[8];
I still get the same output shown in the picture above though this should be impossible...
I have the feeling that openGL doesn't care about my texture coordinates, but if I leave them completely out then of course I don't get any output at all.
And if anybody thinks I must change them somewhere else: the code posted in the first comment is the only time I use texture or the TextureBuffer.
I'm somewhat desperate now so I'd be glad about any Ideas where I could find my problem.

Thanks,
Benni

Share this post


Link to post
Share on other sites
edit: nevermind :) you can have 2 coordinates per vertex

so, its not a problem with indices vs. texture coordinates?
its simply not using them, or the ones you think it will?

Share this post


Link to post
Share on other sites

edit: nevermind :) you can have 2 coordinates per vertex

so, its not a problem with indices vs. texture coordinates?
its simply not using them, or the ones you think it will?

Exactly that is my problem... (not using the the texture coordinates)

Share this post


Link to post
Share on other sites
Finally I figured it out smile.gif

I had to use two different ByteBuffers to create the Vertex and the Texure Buffer:

ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);// Allocate a memory block and create a direct buffer
byteBuf.order(ByteOrder.nativeOrder()); // Set the order in the buffer
textureBuffer = byteBuf.asFloatBuffer(); // Create the textureBuffer out of the ByteBuffer
textureBuffer.put(texture); // Put the texture mapping coordinates into the textureBuffer
textureBuffer.position(0); // Set the position of the Buffer

ByteBuffer byteBuf2 = ByteBuffer.allocateDirect(vertices.length * 4);// Allocate a memory block and create a direct buffer
byteBuf2.order(ByteOrder.nativeOrder()); // Set the order in the buffer
vertexBuffer = byteBuf2.asFloatBuffer(); // Allocate a memory block and create a direct buffer
vertexBuffer.put(vertices); // Put the vertex coordinates to the indexBuffervertexBuffer.position(0);


instead of:
ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);// Allocate a memory block and create a direct buffer
byteBuf.order(ByteOrder.nativeOrder()); // Set the order in the buffer
textureBuffer = byteBuf.asFloatBuffer(); // Create the textureBuffer out of the ByteBuffer
textureBuffer.put(texture); // Put the texture mapping coordinates into the textureBuffer
textureBuffer.position(0); // Set the position of the Buffer

vertexBuffer = byteBuf.asFloatBuffer(); // Allocate a memory block and create a direct buffer
vertexBuffer.put(vertices); // Put the vertex coordinates to the indexBuffervertexBuffer.position(0);


Seems like otherwise the vertex information is also used for the coordinates.
Should have read the article about ByteBuffers:
The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark values will be independent.[/quote]

Share this post


Link to post
Share on other sites
Remember that in OpenGL texture space, the v (y coordinate) increases in the positive direction as it goes down the texture from top to bottom.[/quote]
Actually it is bottom left = 0,0, top right = 1,1

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!