OpenGL: Set position instead of translating? - java

Can I set the current render position to be an arbitrary value, instead of just giving it an offset from the current location?
This is what I'm doing currently:
gl.glTranslatef(3.0f, 0.0f, 2.0f);
It allows me to say "I want to move left" but not "I want to move to point (2, 1, 2)". Is there a way to do the latter?
I'm using OpenGL with JOGL.
Update:
#Bahbar suggests the following:
gl.glLoadIdentity();
gl.glTranslatef(...);
When I do this, everything except six lines disappears. I'm not sure why. I'm having a problem with the far clipping plane being too close, so perhaps they're too far away to be rendered.

Yes. Just start from the identity matrix.
gl.glLoadIdentity();
gl.glTranslatef(...);

Yes, you can set your view position using the gluLookAt command. If you look at the OpenGL FAQ, there is a question that has the line most relevant to your problem: 8.060 How do I make the camera "orbit" around a point in my scene?
You can simulate an orbit by
translating/rotating the scene/object
and leaving your camera in the same
place. For example, to orbit an object
placed somewhere on the Y axis, while
continuously looking at the origin,
you might do this:
gluLookAt(camera[0], camera[1], camera[2], /* look from camera XYZ */
0, 0, 0, /* look at the origin */
0, 1, 0); /* positive Y up vector */
glRotatef(orbitDegrees, 0.f, 1.f, 0.f); /* orbit the Y axis */
/* ...where orbitDegrees is derived from mouse motion */
glCallList(SCENE); /* draw the scene */
If you insist on physically orbiting the camera position, you'll
need to transform the current camera
position vector before using it in
your viewing transformations.
In either event, I recommend you
investigate gluLookAt() (if you aren't
using this routine already).
Note that gluLookAt call: the author is storing the camera position in a 3 value array. If you do the same, you will be able to specify your viewpoint in absolute coordinates exactly as you wanted.
NOTE: if you move the eye position, it's likely that you're going to want to specify the view direction as well. In this example, the author has decided to keep the eye focused on the point (0, 0, 0).
There's a good chance that this question is related to what you're trying to do as well: GLU.gluLookAt in Java OpenGL bindings seems to do nothing.

Related

Rotate a GLUT bitmap/stroke string around its center?

As you can see on the pictures the string rotates around its origin.
No rotation:
Rotated:
Changing the RasterPos or translate it does not change this at all. I tried glutStrokeString and glutBitmapString. The code for the example:
gl.glColor4f((float) 1, (float) 0, (float) 0, 1.0f);
gl.glScalef(0.0015f, 0.0015f, 0.0015f);
gl.glRotatef(-angleHorizontal, 0, 1, 0);
glut.glutStrokeString(GLUT.STROKE_ROMAN, "ABCDEF");
glutBitmapCharacter:
You can't. It makes use of the (outdated, deprecated, legacy) OpenGL bitmap operations, which are always aligned to the pixel grid.
glutStrokeCharacter:
These are just regular line segments that transform through the fixed function pipeline; or if you're in a compatibility profile through an early GLSL version shader program that uses the set of built-in variables to access the fixed function pipeline state. In one of my codesamples programs (which I wrote to explain how the projection frustum works) I have some helper function to draw arrows with annotations. You can find the full code here https://github.com/datenwolf/codesamples/blob/master/samples/OpenGL/frustum/frustum.c the relevant function starts in line 114.

How to move my camera in OpenGL

I'm trying to have my camera move in the way games do in first person. I have shapes drawn and oriented to look like a hallway, so I need the camera to move forward as if your moving through the hallway. What lines of code should I use, and where should I put them?
You should see the things from the opposite side, you don't move the camera. Instead you move the world so that its projection changes according to the camera (which doesn't really exist) position.
This is usually done by having a projection matrix which embeds the current camera position and orientation and this matrix is used inside your shaders, after having applied the model matrix (remember it's not symmetric).
Take a look a this good tutorial to get the necessary knowledge. Basically everything reduces to:
gl_Position = camera * model * vec4(vertex, 1);

Why are Polygons misshaped when using Vector coordinates in a Java Applet?

I have a completely graphed out blueprint of X, Y coordinates of 8 different multi-pointed shapes on paper. I put these coordinates into an array such as..
Polygon shape1;
int[] shapeOneX = {1,2,3,4,5,6,7,8,9};
int[] shapeOneY = {1,2,3,4,5,6,7,8,9};
shape1 = new Polygon {shapeOneX, shapeOneY, shapeOneX.length};
These coordinates are fake, and not my actual ones but on paper, these coordinates would follow the rules completely on how you would expect vector graphing to look like. When I load this into a Java Applet, the shape does not follow these exact coordinates. They're sometimes close, but not exact, and I need precision for my project.
Does anyone know why, or if there is a different formula you need to use on the coordinates to have it look the same in a java applet? If need more info, let me know.
I understand that starting coordinates for java applet start at the top left 0,0 then expand from there. I guess my questioning is,I have the understanding that "vector" cords start at 0,0 as a Mid point. I don't know much about graphing. So... my shapes are being created from a vector style, but being "placed" into an applet which has a 0,0 top left origin. Which is fine, I have the tools to adjust them where I need to put them. I just can't get them to create the shape I actually graph on paper. Do I need to graph on paper from a 0,0 top left origin and only create positive X, Y variables?
Another Edit-- I've noticed that when it draws onto the applet, it draws it almost mirrored as well. In other words, (x) goes right, (-x) goes left. That's normal. But (y) goes DOWN, and (-y) goes UP.. That doesn't seem normal HMM.. Confused.
Final Edit(probably) -- Well I was right about the Y axis being mirrored. Why? I don't know. But it has allowed for me to redesign some coordinates. I am currently under the impression that line borders were so thick that connected each vertex, that they reformed the shapes into a blob of junk. Because of the overlapping borders. It was hard to see where each vertex actually truly was. I also had to increase the values of my (x,y) coordinates in order to compensate for the size difference. Which I have probably near 100 or so different (x,y) combinations that I will need to re-do because of this... I really wish there was an easier answer. I am open to any and all suggestions, meanwhile I will plug away at remapping this. Thanks everyone who has, or continues to contribute.
For Example.. This first was the orignal coordinates:
int[] wallX = { -2,-2,-1,-1, 2, 2 };
int[] wallY = { -1, 3, 3, 0, 0,-1 };
And then the new WORKING coordinates I found to work are:
int[] wallOneX = { -2,-2, 1, 1, 10, 10 };
int[] wallOneY = { 4,-8,-8, 1, 1, 4 };
So thats the difference of numbers needed to create the same shape from paper, into the java applet. I don't really see a pattern or anything to recreate it for all my other ones. So I don't know.
You need to scale your coordinates based on the height and width of your jpanel or canvas object on which you are painting the polygon. use getHeight() and getWidth() to get the dimensions. Also, the origin is in the upper right corner of the jpanel or canvass, so you either need to use addition/subtraction to shift the scaled coordinates, or you need to use the affine transform to get the polygon where you want it to go.
Sometimes it helps to start with working examples. You might try this approach or this approach. Here is a third approach already in an applet.

How does one rotate relative to the camera in openGL or more specifically LWJGL?

I am making a 3d game with LWJGL. In this game, whenever I press an arrow key, I want the screen to rotate in that direction, regardless of the current orientation. I am struggling to implement this in code. Using three glRotatef functions based off of a rotation vector3f, does not accomplish this. Rotating up and down work because glRotatef(rotation.x, 1, 0, 0) is called first, but left and right only work when your not looking up or down. If you are, you rotate around a universal y axis, and camera spins. I saw that another implementation could use gluLookAt(), but I imagine I would encounter the same problem.
EDIT
I thought I solved my issue by changing the order by which glRotatef()'s where called depending on the direction I want to rotate. I thought this would work because in my game, I will only be rotating one axis at a time. It worked somewhat but in some orientations it doesn't.
if(updown){
glRotatef(rotation.x, 1, 0, 0);
glRotatef(rotation.y, 0, 1, 0);
} else if(leftright){
glRotatef(rotation.y, 0, 1, 0);
glRotatef(rotation.x, 1, 0, 0);
}
glTranslatef(position.x, position.y, position.z);
gluLookAt would probably get you there much quicker, but you would need to manually rotate the eye coordinate about the origin coordinate. With gluLookAt you also need to calculate an up vector if you plan to rotate around all 3 axes.
I have a hunch you just need to add a glTranslate before you do your glRotate so that the camera has something to orbit around.
If you show some code you might be able to get more help.
I am over this issue and have moved on. Because I am only going to rotate one axis 90 degrees at a time, I went through all 64 possible orientations and applied the necesary transforations manualy.

Rotation in OpenGl ES to place objects then rotate the world

I am developing an augmented reality application for android and trying to use openGl to place cubes at locations in the world. My current method can be seen in the code below:
for(Marker ma: ARData.getMarkerlist().values()) {
Log.d("populating", "");
gl.glPushMatrix();
Location maLoc = new Location("loc");
maLoc.setLatitude(ma.lat);
maLoc.setLongitude(ma.lng);
maLoc.setAltitude(ma.alt);
float distance = currentLoc.distanceTo(maLoc);
float bearing = currentLoc.bearingTo(maLoc);
Log.d("distance", String.valueOf(distance));
Log.d("bearing", String.valueOf(bearing));
gl.glRotatef(bearing,0,0,1);
gl.glTranslatef(0,0,-distance);
ma.cube.draw(gl);
gl.glPopMatrix();
}
gl.glRotatef(y, 0, 1, 0);
gl.glRotatef(x, 1, 0, 0);`
Where y is yaw and x is the pitch. currently I am getting a single cube on the screen at a 45 degree angle someway in the distance. It looks like I am getting sensible bearing and distance values. Could it have something to do with the phones orientation? If you need more code let me know.
EDIT: I updated bearing rotation to gl.glRotatef(bearing,0,1,0); I am now getting my cubes mapped horizontally along the screen at different depths. Still no movement using heading and pitch but #Mirkules has identified some reasons why that might be.
EDIT 2: I am now attempting to place the cubes by rotating the matrix by the difference in angle between heading and bearing to a marker. However, all I get is a sort of jittering where the cubes appear to be rendered in a new position and then jump back to there old position. Code as above except for the following:
float angleDiff = bearing - y;
gl.glRotatef((angleDiff),0,1,0);
gl.glTranslatef(0,0,-distance);
bearing and y are both normalised to a 0 - 360 scale. Also, I moveed my "camera rotation" to above the code where I set the markers.
EDIT 3: I have heading working now using, float angleDiff = (bearing + y)/2;. However, I cant seem to get pitch working. I have attempted to use gl.glRotatef(-x,1,0,0); but that doesn't seem to work.
It's tricky to tell exactly what you're trying to do here, but there are a few things that stick out as potential problems.
Firstly, your final two rotations don't seem to actually apply to anything. If these are supposed to represent a movement of the world or camera (which mostly amounts to much the same thing) then they need to happen before drawing anything.
Then your rotations themselves perhaps won't entirely do what you intend.
Your cube is rotated around the Z axis. The usual convention in GL is for the camera to look down the Z axis, with the Y axis being considered 'up'. You can naturally interpret axes however you like, but a rotation around 'Z' would not typically be 'bearing', but 'roll'. 'Bearing' to me would be analogous to 'yaw'.
As you translate along the Z axis, I assume you are trying to position the object by rotating and translating, but obviously if the rotation is around the same axis as you translate along, it won't actually alter the position of the cube - it will always just be directly in front of the camera, spinning on its axis.
I'm not really clear on why you're trying to position the cube like that when it seems like you start off with a more specific location. You could probably directly construct a more appropriate matrix.
Finally, your camera/world rotation is two concatenated rotations around Y and X. You call these pitch and roll, but typically using euler angles for a camera rotation does not result in an intuitive result where terms like pitch and roll make complete sense. It is common to maintain an orientation and apply individual rotations to that in order to update it, rather than attempting to update several dependent rotations.
So yes, I would expect that this code, in the absence of other matrix operations, would likely result in drawing one or more cubes straight ahead which are simply rotated by some angle around the view direction.

Categories

Resources