PDA

View Full Version : Zoom with OpenGL



disa89
04-12-2013, 11:56 AM
Hi, I'm a new user of OpenGL.
This is what I have to do: I have got a variable x that can increase or decrease.
I would like to know how to increase/decrease the dimensions of each polygons I have drawn by increasing/decreasing the value of the variable x.
Thanks in advance to anyone how will reply.

The Little Body
04-12-2013, 03:49 PM
You can use glScalef(x, x, x) or the modelview matrix for to do this

disa89
04-13-2013, 03:17 AM
I followed your advice and I used glScaled(1.0003, 1.0003, 1.0003). The problem is that the dimensions of the polygons increase indefinitely and the polygons become so big that I cannot see them in window. What I want to do is to increase the size of polygons at a specified rate (for example to double the size) such as a common "zoom". I would be very grateful if you can tell me what is wrong with that.

Timppa
04-13-2013, 08:21 AM
What are you exactly trying to achieve? If you want a binoculars-like zoom that affects everything, you can do this by decreasing the field of view in projection matrix. For example, if you are using glu, you could use gluPerspective(fieldOfView * zoomFactor, aspectRatio, zNear);

If you want to use glScale to scale each of the objects individually, you should do it like this:



glPushMatrix(); // save the current matrix
glScalef(scaleX, scaleY, scaleZ); // scale the matrix
drawObject(); // draw the object(s) that need to be scaled
glPopMatrix(); // load the unscaled matrix

disa89
04-13-2013, 09:21 AM
OK, I try to explane better: I drew six polygons that form a parallelepiped.
I have got the variable x, initially the value of x is 0, but as I said before, the variable can increase or decrease.
Now image that x increases (for example the new value of x is 2), it means that I have to move two units along the z axis and see the object grow. Instead if x decreases (for example it become -3) I have to move three units along the z-axis (in the opposite direction respect to the previous) and see the object shrink.
I hope you've understood all, Google translate is not perfect XD.

Timppa
04-13-2013, 10:19 AM
Ah, so you don't actually want to change the size of the object, you only want to move closer/further so that the object looks smaller/larger? In that case my previous example still works, just replace glScalef with glTranslatef(0, 0, x). In OpenGL the camera never moves, only the objects can be moved around. If you want to move forward on the z-axis, you will have to move all the objects backwards on the z-axis instead.

disa89
04-13-2013, 01:44 PM
Yes yes, I have understood my problem. But I don't know where I am wronging, I have written the function glTranslatef(0, 0, x) but it doesn't work very well. I see the object for a moment and after it sparish from the camera. Have I write something else before/after I call the fuction glTranslatef(0, 0, x)?

tonyo_au
04-13-2013, 06:19 PM
Once an object gets less that near plane (or greater than the far plane) it will disappear. Set you near value to something like 0.1 to view the object for as long as possible.

disa89
04-14-2013, 12:46 AM
What is the command to do that? And have I to write it before or after the command glTranslatef?

Timppa
04-14-2013, 02:36 AM
I see the object for a moment and after it sparish from the camera. Have I write something else before/after I call the fuction glTranslatef(0, 0, x)?

Like I mentioned, you should use glPushMatrix() and glPopMatrix(), see the code example I posted above (replace glScalef with glTranslatef).
For setting the near / far plane, you can use gluPerspective(fieldOfView, aspectRatio, closePlane, farPlane) in case you are using glut.

If you did both things mentioned above and are still having trouble please post your source so we don't have to guess what is wrong. I would also recommend you to find a good tutorial that goes through all the basics or perhaps even a book, that would help you a lot with the basics.

disa89
04-14-2013, 05:24 AM
It's better to post the code now, It is write in java but the functions are totally identical:

private void render(GLAutoDrawable drawable){
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFF ER_BIT);
gl.glEnable(GL.GL_DEPTH_TEST|GL2.GL_NORMALIZE);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glDepthFunc(GL.GL_LEQUAL);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);

gl.glPushMatrix(); //I push the matrix before draw
gl.glTranslatef(0, 0, nord) //I Translate the object

//Green Side - FIRST RECTANGLE
gl.glBegin(GL2.GL_POLYGON);
gl.glColor3f( 0.0f, 1.0f, 0.0f );
gl.glVertex3f( -0.25f+est, -0.125f+altitudine, 0.35f+nord );
gl.glVertex3f( 0.25f+est, -0.125f+altitudine, 0.35f+nord );
gl.glVertex3f( 0.25f+est, 0.125f+altitudine, 0.35f+nord );
gl.glVertex3f( -0.25f+est, 0.125f+altitudine, 0.35f+nord );
gl.glEnd();

gl.glFlush();

//Yellow Side - SECOND RECTANGLE
gl.glBegin(GL2.GL_POLYGON);
gl.glColor3f( 1.0f, 1.0f, 0.0f );
gl.glVertex3f( 0.25f+est, -0.125f+altitudine, 0.35f+nord );
gl.glVertex3f( 0.25f+est, 0.125f+altitudine, 0.35f+nord );
gl.glVertex3f( 0.25f+est, 0.125f+altitudine, -0.35f+nord );
gl.glVertex3f( 0.25f+est, -0.125f+altitudine, -0.35f+nord );
gl.glEnd();

gl.glFlush();

//Red Side - THIRD RECTANGLE
gl.glBegin(GL2.GL_POLYGON);
gl.glColor3f( 1.0f, 0.0f, 0.0f );
gl.glVertex3f( 0.25f+est, 0.125f+altitudine, 0.35f+nord );
gl.glVertex3f( -0.25f+est, 0.125f+altitudine, 0.35f+nord );
gl.glVertex3f( -0.25f+est, 0.125f+altitudine, -0.35f+nord );
gl.glVertex3f( 0.25f+est, 0.125f+altitudine, -0.35f+nord );
gl.glEnd();

gl.glFlush();

//Blue Side - FORTH RECTANGLE
gl.glBegin(GL2.GL_POLYGON);
gl.glColor3f( 0.0f, 0.0f, 1.0f );
gl.glVertex3f( -0.25f+est, 0.125f+altitudine, -0.35f+nord );
gl.glVertex3f( 0.25f+est, 0.125f+altitudine, -0.35f+nord );
gl.glVertex3f( 0.25f+est, -0.125f+altitudine, -0.35f+nord );
gl.glVertex3f( -0.25f+est, -0.125f+altitudine, -0.35f+nord );
gl.glEnd();

gl.glFlush();

//White Side - FIFTH RECTANGLE
gl.glBegin(GL2.GL_POLYGON);
gl.glColor3f( 1.0f, 1.0f, 1.0f );
gl.glVertex3f( -0.25f+est, -0.125f+altitudine, 0.35f+nord );
gl.glVertex3f( 0.25f+est, -0.125f+altitudine, 0.35f+nord );
gl.glVertex3f( 0.25f+est, -0.125f+altitudine, -0.35f+nord );
gl.glVertex3f( -0.25f+est, -0.125f+altitudine, -0.35f+nord );
gl.glEnd();

gl.glFlush();

//Grey Side - SIXTH RECTANGLE
gl.glBegin(GL2.GL_POLYGON);
gl.glColor3f( 0.5f, 0.5f, 0.5f );
gl.glVertex3f( -0.25f+est, -0.125f+altitudine, 0.35f+nord );
gl.glVertex3f( -0.25f+est, 0.125f+altitudine, 0.35f+nord );
gl.glVertex3f( -0.25f+est, 0.125f+altitudine, -0.35f+nord );
gl.glVertex3f( -0.25f+est, -0.125f+altitudine, -0.35f+nord );
gl.glEnd();

gl.glFlush();

gl.glRotatef(gyrox, 1.0f, 0.0f, 0.0f);
gl.glRotatef(gyroy, 0.0f, 1.0f, 0.0f);
gl.glRotatef(gyroz, 0.0f, 0.0f, 1.0f);

gl.glFlush();

gl.glPopMatrix(); //I pop the matrix after draw
gl.glFlush();
}

If I do that and nord = 1.5 I can't see anything. If instead nord = 1.1 I see the object but it seems not bigger.

Timppa
04-14-2013, 06:35 AM
The reason that happens is because you do not have a projection matrix set up. By default it is using orthographic projection and does not have perspective, all non-normalized coordinates are clipped (this is why 1.1 - 0.35 works but 1.5 - 0.35 does not). There are also a several other things that should be fixed.

I would recommend you to read a tutorial (http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/) about OpenGL matrices. Understanding them is essential. Once you understand how matrices work you should be able to fix this program easily. :)

disa89
04-14-2013, 09:40 AM
YEAH! thank you for the tutorial I'm fixing the program using both gluPerspective() and glTranslated(). Now I do some tests. Thank you very much!!