OpenGL Android: X axis is mysteriously flipped - android

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.

Related

Finding/Remapping bounds of OpenGL ES coordinate plane

I'm trying to make 2D graphics for my Android app that consists of six thin rectangles that each take up about 1/6th of the screen in width and equal the screen's height. I'm not sure the right way to determine the bounds of the x and y OpenGL coordinate plane on screen. Eventually I will need to write logic that tests which of the 6 rectangles a touch event occurs in, so I have been trying to solve this problem by remapping OpenGL's coordinate plane into the device's screen coordinate plane (where the origin (0,0) is at the top left of the screen instead of the middle.
I declare one of my six rectangles like so:
private float vertices1[] = {
2.0f, 10.0f, 0.0f, // 0, Top Left
2.0f, -1.0f, 0.0f, // 1, Bottom Left
4.0f, -1.0f, 0.0f, // 2, Bottom Right
4.0f, 10.0f, 0.0f, // 3, Top Right
};
but since i'm not sure what the visible limits are on the x and y planes (in the OpenGL coordinate system) I have no concrete way of knowing what vertices my rectangle needs to be instantiated with to occupy 1/6th of the display. Whats the ideal way to do this?
I've tried approaches such as using glOrthoof() to remap OpenGL's coordinates into easy to work with device screen coordinates:
gl.glViewport(0, 0, width, height);
// Select the projection matrix
gl.glMatrixMode(GL10.GL_PROJECTION);
// Reset the projection matrix
gl.glLoadIdentity();
// Calculate the aspect ratio of the window
GLU.gluPerspective(gl, 45.0f,(float) width / (float) height,0.1f, 100.0f);
gl.glOrthof(0.0f,width,height, 0.0f, -1.0f, 5.0f);
// Select the modelview matrix
gl.glMatrixMode(GL10.GL_MODELVIEW);
// Reset the modelview matrix
gl.glLoadIdentity();
but when I do my rectangle dissapears completely.
You certainly don't want to use a perspective projection for 2D graphics. That just doesn't make much sense. A perspective projection is for... well, creating a perspective projection, which is only useful if your objects are actually placed in 3D space.
Even worse, you have two calls to set up a perspective matrix:
GLU.gluPerspective(gl, 45.0f,(float) width / (float) height,0.1f, 100.0f);
gl.glOrthof(0.0f,width,height, 0.0f, -1.0f, 5.0f);
While that's legal, it rarely makes sense. What essentially happens if you do this is that both projections are applied in succession. So the first thing to do is get rid of the gluPerspective() call.
To place your 6 rectangles, you have a few options. Almost the easiest one is to not apply any transformations at all. This means that you will specify your input coordinates in normalized device coordinates (aka NDC), which is a range of [-1.0, 1.0] in both the x- and y-direction. So for 6 rectangles rendered side by side, you would use a y-range of [-1.0, 1.0] for all the rectangles, and an x-range of [-1.0, -2.0/3.0] for the first, [-2.0/3.0, -1.0/3.0] for the second, etc.
Another option is that you use an orthographic projection that makes specifying the rectangles even more convenient. For example, a range of [0.0, 6.0] for x and [0.0, 1.0] for y would make it particularly easy:
gl.glOrthof(0.0f, 6.0f, 0.0f, 1.0f, -1.0f, 1.0f);
Then all rectangles have a y-range of [0.0, 1.0], the first rectangle has a x-range of [0.0, 1.0], the second rectangle [1.0, 2.0], etc.
BTW, if you're just starting with OpenGL, I would pass on ES 1.x, and directly learn ES 2.0. ES 1.x is a legacy API at this point, and I wouldn't use it for any new development.

android opengl texture overlapping

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

Camera movement in OpenGL ES android

I am very confused about the use on GLU.gLookAt(eyeX,eyeY,eyeZ,Xpos,Ypos,Zpos,upX,upY,upZ) method. All I want is to zoom the 3d cube.
When I increase/decrease value of eyeZ, the camera moves forward/backward to the cube. Its all fine up to a certain limit of eyeZ, but when I increase the eyeZ value beyond that limit, it starts reverting the effect i.e. instead of zooming in it starts zooming out.
I might not be good in openGL to understand above method but could anyone tell me whats the basic reason behind this.
I referred to this link
http://jerome.jouvie.free.fr/opengl-tutorials/Tutorial8.php
If you want I can post my code over here..
public void onDrawFrame(GL10 gl)
{
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
//gl.glTranslatef(xPos, yPos, -zoomFactor);
GLU.gluLookAt(gl, eyeX, eyeZ, eyeZ, 0f, 0f, 0f , 0f, 1f, 0f);
gl.glRotatef(mAngleX, 0, 1, 0);
gl.glRotatef(mAngleY, -1, 0, 0);
// Draw the model
cube.draw(gl);
}
this is the method where i am using gLookAt method..
GLU.gluLookAt(gl, eyeX, eyeZ, eyeZ, 0f, 0f, 0f , 0f, 1f, 0f); is a function that puts your camera looking at a particular spot, in this case its (0,0,0) (i think, cant remember which way round the parameters are, but im assuming the last 3 are your up vector). So if you move your camera towards what you are looking at, eventually it will go through it and out the other side, and since you are using GLU.gluLookAt it will turn to face the object behind it, thus giving you the impression that you are zooming out when you carry on moving in the same direction.

OpenGL ES: glOrtho() shows object, but gluPerspective() doesn't

I am showing a textured sqad, centered around [0,0,-10], width and height = 10000. The camera is positioned at [0,0,0] and looks down the negative z-axis (eyepoint=[0,0,0], center=[0,0,-1]):
GLU.gluLookAt(gl, 0f, 0f, 0f, 0f, 0f, -1f, 0f, 1f, 0f);
Lighting and Depth-Test are disabled.
In orthographic mode, the squad is displayed perfectly, with texture and all - I can even zoom and pan around.
However, when switching to perspective mode, via:
GLU.gluPerspective(gl, 60.0f, w / h, 1.0f, 1000.0f);
then the view is just blank. Has anybody got any idea what could cause this?
UPDATE:
Using glFrustum instead of gluPerspective, it works:
gl.glFrustumf(-scaledHalfW, scaledHalfW, -scaledhalfH, scaledhalfH, 1.0f, 100.0f);
But why does gluPerspective not show anything?
Is w / h an integer division maybe?

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.

Categories

Resources