0

I've started with JOGL lately, I know how to create and draw objects on the canvas, but I couldn't find tutorial or explanations on how to set and rotate the camera. I only found source code, but since I'm quite new with this, it doesn't help too much.

Does anyone know of a good tutorial or place to start? I googled but couldn't find anything (only for JOGL 1.5, and I'm using 2.0).

4
  • this might be helpful land-of-kain.de/docs/jogl Commented May 7, 2012 at 14:32
  • thanks, I saw this site, but I think it uses JOGL 1.5, as most of the method are different in the API I got.. Commented May 7, 2012 at 15:12
  • Possible duplicate: Check jogl's pmvMatrix stackoverflow.com/a/13485670/398672 Commented Dec 28, 2012 at 23:12
  • possible duplicate of something that was asked 6 months later... Commented Dec 29, 2012 at 0:08

2 Answers 2

1

UPDATE

As datenwolf points out my explanation is tied to the OpenGL 2 pipeline, which has been superseded. This means you have to do your own manipulation from world space into screen space if you want to eschew the deprecated methods. Sadly, this little footnote hasn't gotten around to being attached to every last bit of OpenGL sample code or commentary in the universe yet.

Of course I don't know why it's necessarily a bad thing to use the existing GL2 pipeline before picking a library to do the same or building one yourself.

ORIGINAL


I'm playing around with JOGL myself, though I have some limited prior experience with OpenGL. OpenGL uses two matrices to transform all the 3D points you pass through it from 3D model space into 2D screen space, the Projection matrix and the ModelView matrix.

The projection matrix is designed to compensate for the translation between the 3D world and the 2D screen, projecting a higher dimensional space onto a lower dimensional one. You can get lots more details by Googling gluPerspective, which is a function in the glut toolkit for setting that matrix.

The ModelView1 matrix on the other hand is responsible for translating 3D coordinates items from scene space into view (or camera) space. How exactly this is done depends on how you're representing the camera. Three common ways of representing the camera are

  • A vector for the position, a vector for the target of the camera, and a vector for the 'up' direction
  • A vector for the position plus a quaternion for the orientation (plus perhaps a single floating point value for scale, or leave scale set to 1)
  • A single 4x4 matrix containing position, orientation and scale

Whichever one you use will require you to write code to translate the representation into something you can give to the OpenGL methods to set up the ModelView matrix, as well as writing code than translates user actions into modifications to the Camera data.

There are a number of demos in JOGL-Demos and JOCL-Demos that involve this kind of manipulation. For instance, this class is designed to act as a kind of primitive camera which can zoom in and out and rotate around the origin of the scene, but cannot turn otherwise. It's therefore represented as only 3 floats: and X and Y rotation and a Z distance. It applies its transform to the Modelview something like this2:

gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, z);
gl.glRotatef(rotx, 1f, 0f, 0f);
gl.glRotatef(roty, 0f, 1.0f, 0f);

I'm currently experimenting with a Quaternion+Vector+Float based camera using the Java Vecmath library, and I apply my camera transform like this:

Quat4d orientation;
Vector3d position;
double scale;
...
public void applyMatrix(GL2 gl) {
  Matrix4d matrix = new Matrix4d(orientation, position, scale);
  double[] glmatrix = new double[] {
    matrix.m00, matrix.m10, matrix.m20, matrix.m30,
    matrix.m01, matrix.m11, matrix.m21, matrix.m31,
    matrix.m02, matrix.m12, matrix.m22, matrix.m32,
    matrix.m03, matrix.m13, matrix.m23, matrix.m33,
  };
  gl.glMatrixMode(GL2.GL_MODELVIEW);
  gl.glLoadMatrixd(glmatrix, 0);
}

1: The reason it's called the ModelView and not just the View matrix is because you can actually push and pop matrices on the ModelView stack (this is true of all OpenGL transformation matrices I believe). Typically you either have a full stack of matrices representing various transformations of items relative to one another in the scene graph, with the bottom one representing the camera transform, or you have a single camera transform and keep everything in the scene graph in world space coordinates (which kind of defeats the point of having a scene graph, but whatever).

2: In practice you wouldn't see the calls to gl.glMatrixMode(GL2.GL_MODELVIEW); in the code because the GL state machine is simply left in MODELVIEW mode all the time unless you're actively setting the projection matrix.

Sign up to request clarification or add additional context in comments.

2 Comments

The OpenGL matrix stack has been stripped from modern versions of OpenGL. You should not use the OpenGL builtin matrix stack in newly written code! A lot of people think that the OpenGL matrix operations were GPU accelerated. They never were! Also your suggestion, that the GL state machine is left in MODELVIEW mode is wrong and actually leads to a lot of newbie confusion. In any more advanced program, you need to change the projection several times (things like a HUD, split screen, multiple viewing panes, or if you're rendering a shadow into a shadow buffer all requre this).
The only reason to stick with the OpenGL fixed function pipeline (i.e. the builtin matrix stack) would be, if you wanted to support GPUs predating 2004. These are hard to come by these days.
0

but I couldn't find tutorial or explanations on how to set and rotate the camera

Because there is none. OpenGL is not a scene graph. It's mostly sophisticated canvas and simple point, line and triangle drawing tools. Placing "objects" actually means applying a linear transformations to place a 3 dimensional vector on a 2D framebuffer.

So instead of placing the "camera" you just move around the whole world (transformation) in the opposite way you'd move the camera, yielding the very same outcome.

4 Comments

thanks for your answer, so is there a tutorial on how to do THIS? I'm iterested in moving around the object with the mouse
OpenGL isn't a scene graph, but pointing the person in the direction of figuring out how to take represent a camera (Vector + Quaternion vs Matrix) and how to translate that representation into a call to OpenGL to produce the desired effect (GL_MODELVIEW + glLoadMatrix) is going to be more useful than your fairly impenetrable response. Also, given that the matrix transformation pipeline is BUILT IN to GL, calling it a canvas with drawing primitives is a little disingenuous.
@Jherico: Since OpenGL-3 core matrix manipulation has been stripped away from OpenGL. In fact the plans to remove the matrix stack have been around since OpenGL-2. Also your answer endorses bad practices.
Maybe you should move some of that pertinent information into your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.