android opengl texture overlapping - android

i am following nehe's tutorials.
i intent to make a menu or at least buttons with opengl, yet object overlap on the menu
my code on the drawFrame function in the renderer
gl.glLoadIdentity();
gl.glScalef(0.05f, 0.05f, 0.05f);
gl.glTranslatef(0.0f, 0.0f, z-zKonum);
gl.glRotatef(xAcisi, 1.0f, 0.0f, 0.0f);
gl.glRotatef(yAcisi, 0.0f, 1.0f, 0.0f);
dokukup.ciz(gl);
gl.glLoadIdentity();
gl.glTranslatef(3.6f, -1.5f, z);
tusYukari.ciz(gl);
gl.glLoadIdentity();
gl.glTranslatef(2.5f, -1.5f, z);
tusAsagi.ciz(gl);
how do i get my menu buttons dominant(always on the top) on the overlapping?

You can get the buttons to appear always on top by drawing the buttons last and disabling depth testing when drawing the buttons. Then make sure to enable depth testing again before drawing the next frame so that your 3D geometry renders properly.
In your drawFrame function you would do the following steps:
Enable depth testing
Draw the main scene geometry
Disable depth testing
Draw the buttons

Related

Move pixels around in OpenGL-ES (Android)

I have a texture that I can render in OpenGL-ES with an orthogonal identity matrix:
gst_gl_shader_set_uniform_matrix_4fv(shader, "u_transformation", 1, FALSE, identity_matrix);
I want to move "the pixels around": half of the top is going to the left, half of the bottom is going to the right as shown on the image below. Is there an "easy" way to do that? I'm on Android.
On this related answered question How to crop/clip in OpenGL using u_transformation, I was able to keep the top part 'a' or the bottom part 'e'. Would there be a way to do a "double gst_gl_shader_set_uniform_matrix_4fv" after "cutting" the scene in two?
The transformation that you want here cannot be represented by a transformation matrix. Matrices can only represent certain classes of transformations. In 3D space:
A 3x3 matrix represents a linear transformation. Typical examples include rotation, scaling, mirroring, shearing.
A 4x3 matrix represents an affine transformation. On top of the above, this includes translations.
If you extend the 3D space to homogenous coordinates with 4 components, a 4x4 matrix can represent additional classes of transformations, like projections.
The transformation in your sketch is none of the above. So applying a matrix to your vertices will not be able to do this.
So what can you do? Two options come to mind:
If you can easily draw the two parts (top/bottom, left/right) separately, you can obviously do that, and simply change the transformation between rendering the two parts.
Apply the logic in your shader code.
For option 2, you could do this either in the vertex or fragment shader. If you have no primitives that cross the boundary between the two parts, handling it in the vertex shader would be more efficient. Otherwise, similar logic can be used in the fragment shader.
Sketching the critical parts for the vertex shader case, let's say you currently have the following that gives you the arrangement in the left side of your sketch:
// Calculate output position and store it in variable "pos".
gl_Position = pos;
To get the second arrangement, the logic could look like this (completely untested...):
if (pos.y > 0.0) {
gl_Position = vec4(0.5 * pos.x - 0.5, 2.0 * pos.y - 1.0, pos.zw)
} else {
gl_Position = vec4(0.5 * pos.x + 0.5, 2.0 * pos.y + 1.0, pos.zw);
}
The idea is that you check whether the vertex is in the top or bottom half, and scale/shift it accordingly to map the top half of the coordinate space into the left half, and the bottom half of the coordinate space into the right half.
This could be streamlined some more by replacing the conditional with a sign operation:
float s = sign(pos.y);
gl_Position = vec4(0.5 * pos.x - sign * 0.5, 2.0 * pos.y - sign, pos.zw);
Some more care will be needed if pos.w is not 1.0, which happens if you e.g. applied a perspective projection to your vertices. In that case, you'll have to incorporate the division by w in the calculations above.
The formula described in Reto answers 'semi' work as they only produce the "a" on the left or the "e" on the right but not both at the same time.
The solution I found is to double the number of vertices and indices and play around with the vertices coordinates like this:
static const GLfloat vertices[] = {
1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f, 0.0f, 0.5f,
1.0f, -1.0f, 0.0f, 1.0f, 0.5f,
0.0f, 1.0f, 0.0f, 1.0f, 0.5f,
-1.0f, 1.0f, 0.0f, 0.0f, 0.5f,
-1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
0.0f, -1.0f, 0.0f, 1.0f, 1.0f
};
static const GLushort indices[] = { 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7 };

Textures and colours not showing properly in OpenGL ES

I've been following the tutorials over at http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_3D.html and I've encountered a problem.
I've managed to get both of the following examples to work:
2.7 Example 5: 3D Shapes - Rotating Color Cube and Pyramid (Nehe Lesson 5: 3D Shapes)
Example 2: Cube2.java
2.8 Example 6: Texture (Nehe Lesson 6: Texture)
But when I try to draw both a coloured cube and a textured cube I get the following:
http://i.imgur.com/Smbsa.png
(First part of the image)
The coloured cube is invisible but clips the texture cube and the texture cube's textures are coloured by the last colour (yellow) of the coloured cube.
I basically just draw both of the cubes with:
// ----- Render the Color Cube -----
gl.glLoadIdentity(); // Reset the model-view matrix
gl.glTranslatef(0.0f, 0.0f, -6.0f); // Translate right and into the screen
gl.glScalef(0.5f, 0.5f, 0.5f); // Scale down (NEW)
gl.glRotatef(angleCube, 1.0f, 1.0f, 0.0f); // rotate about the axis (1,1,1) (NEW)
cube.draw(gl); // Draw the cube (NEW)
// Update the rotational angle after each refresh (NEW)
angleCube += speedCube; // (NEW)
// ----- Render the Texture Cube -----
gl.glLoadIdentity(); // Reset the model-view matrix
gl.glTranslatef(-1.0f, 0.0f, -6.0f); // Translate right and into the screen
gl.glScalef(0.5f, 0.5f, 0.5f); // Scale down (NEW)
gl.glRotatef(angleCube, 1.0f, 1.0f, 0.0f); // rotate about the axis (1,1,1) (NEW)
texturecube.draw(gl); // Draw the cube (NEW)
I tried adding a third so called "Photo cube" from the part "2.9 Example 6a: Photo-Cube".
(Can only post 2 links check the imgur link above)
I added:
gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
to the texture cube and noticed that the blue colour "spills" over to the photo cube.
When I add:
gl.glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
the following textures are no longer coloured but this feels like a crude way to remove past colours..
When I remove:
texturecube.loadTexture(gl, context); // Load image into Texture (NEW)
photocube.loadTexture(gl); // Load image into Texture (NEW)
gl.glEnable(GL10.GL_TEXTURE_2D); // Enable texture (NEW)
this happens:
(Can only post 2 links check the imgur link above)
The colour cube is back and all other surfaces are coloured.
My questions:
What am I doing wrong when the coloured cube disappears? (I guess that it should be possible to both use textures and colours at the same time)
Is there a better way to "clear" the colour from previous cubes/objects other than "gl.glColor4f(1.0f, 1.0f, 1.0f, 0.0f);"?
Your question is very detailed which is good, though it would help if you posted the full code sample of your drawing section. It's hard for me to guess what you might be doing that would cause the problem.
Anyway I'll try your two questions:
First: My guess is that you don't disable texturing before drawing your colored cube. But you don't have the full code so I can't say with certainty. Call glDisable(GL_TEXTURE_2D) before rendering your second cube, and reenable it before drawing the next textured cube.
Second: No, that's the correct way to do it (Though you probably want 1,1,1,1, not 1,1,1,0 in case you ever want to do anything with transparency). Alternatively there's a way to do it via pushing the color state onto an OpenGL stack and popping it later, though that's deprecated and shouldn't be used.

OpenGL Android: X axis is mysteriously flipped

I am displaying a quad in a pseudo-2D canvas via OpenGL.
To do so, I use orthographic projection via:
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(-ratio, ratio, -1, 1, 0, 10000);
The coordinates of the displayed quad are:
float[] quadCoords = {-10.0f, -10.0f, 5.0f,
10.0f, -10.0f, 5.0f,
10.0f, 10.0f, 5.0f,
-10.0f, 10.0f, 5.0f};
This quad is rendered as 2 triangles (I spare you the code). I am also applying a texture, which is working nicely.
The "camera" is defined before rendering the quad like so:
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, -10.0f, -10.0f, -5, -10.0f, -10.0f, 0f, 0f, 1.0f, 0.0f);
As you can see, the viewport centers at [-10, -10, 0], which should be centered at the bottom left corner of the quad. However, when rendering the scene, it looks like this:
This appears to be the RIGHT bottom corner - but it is not. I checked it, and it turns out the X axis is flipped. Am I doing something wrong with gluLookAt? Or have I missed something?
Ok that's a little silly, but I found the answer minutes after writing this question (hours after it occurred):
The "camera" is looking at the backside of the quad. Assigning "0" for all z-coordinates of the quad and "+1" for the z-coordinate of the eyepoint in gluLookAt fixed it.

draw opengl texture at full screen

I want to draw opengl texture at full screen.
(texture : 128x128 ===> device screen : 320x480)
Below code works good, but texture is small.
I have to use only glFrustumf function(not glOrthof function).
How can I draw texture in full screen size?
// this is android source code
float ratio = (float) screenWidth / screenHeight;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
GLU.gluLookAt(gl, 0.0f, 0.0f, -2.5f, // eye
0.0f, 0.0f, 0.0f, // center
0.0f, 1.0f, 0.0f); // up
// draw blah blah
Why do you have to use glFrustum only? Switching to glOrtho for drawing the background, then switching to glFrustum for regular drawing would be the canonical solution.
BTW: gluLookAt must happen in the modelview matrix, not in the projection matrix like you do right now. As it stands your code is broken and if you were a student in one of my OpenGL classes I'd give you negative points for this cardinal error.

Poor shading problem in Android OpenGL ES

I set up diffuse lighting:
private float[] lightAmbient = { 0.5f, 0.5f, 0.5f, 1.0f };
private float[] lightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
private float[] lightPosition = { 0.0f, 0.0f, 2.0f, 1.0f };
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbientBuffer);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuseBuffer);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPositionBuffer);
gl.glEnable(GL10.GL_LIGHT0);
gl.glShadeModel(GL10.GL_SMOOTH);
But I get triangulated shading or flat color on the cube located at origin ( center) and rotated 45 deg around x and y. So the cube is directly in front of the light. Any reasons why I am getting such poor results? Attached is the cube image.
OpenGL ES calculates colors at the vertices of each triangle. The color is then interpolated across the triangle, Ideally the vertices should calculate the same colors between the two triangles but a variety of situations could cause it not to.
It appears are though your cube edges are modeled with two triangles. You could decompose the cube side into more triangles, but that adds more memory storage and could slow down drawing.
You could also move to OpenGL ES 2.0 and write a shader, which can properly interpolate the colors across the surface, but that will require rewriting the entire pipeline. OGL ES doesn't let you mix old style and shader based implementations.

Categories

Resources