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.
Related
I'm trying to write an VR application using opengl on Android. I know it's very simple with Google Cardboard SDK but I want to do it entirely in OpenGL to understand clearly. Now, I have something that I am not clearly. I hope someone help me to clarify.
What is off-axis and on-axis projection? Do Google Cardboard use off-axis projection?
I know that in order to create stereo view for VR, camera should be translate d/2 with d is distance between two eyes. I tried something like this
Matrix.setLookAtM(mViewMatrix, 0, 1, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);//translate 1 according x axis for right eye
Matrix.setLookAtM(mViewMatrix, 0, -1, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);//translate -1 according x axis for left eye
Now, suppose real value of d is 5 cm or d/2 = 2.5cm. How I have to translate camera to correctly? I don't know map 5cm in real world into OpenGL coordinate.
I'm looking forward to the help. Sorry because of my bad English. Thank you!
I'm rotating with model in Android's OpenGL.
Why those two examples below don't produce same results? I thought, there is no difference, when I rotate about axis x and then y or y and then x.
gl.glRotatef(_angleY, 0f, 1f, 0f); //ROLL
gl.glRotatef(_angleX, 1f, 0f, 0f); //ELEVATION
gl.glRotatef(_angleZ, 0f, 0f, 1f); //AZIMUTH
gl.glRotatef(_angleX, 1f, 0f, 0f); //ELEVATION
gl.glRotatef(_angleY, 0f, 1f, 0f); //ROLL
gl.glRotatef(_angleZ, 0f, 0f, 1f); //AZIMUTH
Unless those rotations are all applied simultaneously, I would think order definitely would matter.
If I had a cube and I rotated it around the x axis and moved the front face to the top, after rotating around the y axis, the original front face would still be on the top.
If instead I first rotated around the y axis then the original front face would be moved aside so when I then rotated around the x axis the original front face would NOT be rotated to the top.
I believe that order of rotation does matter.
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?
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.
actually I'm drawing a cube, I'm checking rotation problems of the cube, but for this I need to draw a point on the 0,0,-1 opengl coordinate of the screen, I'm using perspective projection, MyGLSurfaceView and android 1.5 opengl es 1.x
How can I draw a black or white point on the 0,0,-1 opengl coordinate of the screen?
If you want to be able to draw directly in window space then the easiest thing would be to load modelview and projection temporarily with the identity matrix and draw a GL_POINT with the location that you need. So that'd be something like:
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
// draw the point here; specifics depending on whether you
// favour VBOs, VBAs, etc
// e.g. (assuming you don't have any client state enabled
// on entry and don't care about leaving the vertex array
// enabled on exit)
GLfloat vertexLocation[] = {0.0f, 0.0f, -1.0f};
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertexLocation);
glDrawArrays(GL_POINTS, 0, 1);
// end of example to plot a GL_POINT
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
// and possibly restore yourself to some other matrix mode
// if, atypically, the rest of your code doesn't assume modelview