How to infer translate, shear, etc from manual matrix operations? - c++

While reading some code from UCMerced's TriPath Toolkit, I came across these
float xmin, xmax, ymin, ymax;
float mat[16] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
TheLct->get_bounds ( xmin, xmax, ymin, ymax );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ();
float width = xmax-xmin;
float height = ymax-ymin;
mat[0]=mat[5]=mat[10]= 1.8f * (1 / (width > height ? width : height));
glMultMatrixf ( mat );
mat[0]=mat[5]=mat[10]= 1;
mat[12]=-(xmin+w/2);
mat[13]=-(ymin+h/2);
glMultMatrixf ( mat );
In the first transformation, the first three diagonal 1's in the matrix are multiplied by a factor. From my limited knowledge of the identity matrix, this appears to be scaling by a factor.
The second transformation, however, I don't really understand:
mat[12]=-(xmin+w/2);
mat[13]=-(ymin+h/2);
glMultMatrixf ( mat );
First of all, I don't know what it even means to change indices 12 and 13 in such a matrix. I'm trying to figure it out by reading the wikipedia page on transformations, but I guess I don't have enough math-related domain knowledge to make sense of it.
Whereas the OpenGL resources I can find don't really seem to modify matrices in this manner, rather they use functions like glScaleF.
How can I relate manual matrix transformations such as the above to scaling, shearing, translating, and rotating?

The first matrix, as you correctly guessed, is a uniform scale matrix. The second matrix is just a translation (along x and y axis). Note that the (fixed function matrix stack of the) GL
uses a column major memory layout, where the translation part is always in m[12], m[13], m[14] (see also answer 9.005 in the old GL FAQ). The combined transformation is not a perspective projection (that would require that (m[3], m[7], m[11]) is not the null vector), but an orthogonal one.
For an easy explanation of how all these numbers can be geometrically interpreted, you might find this article useful.

The last is a perspective projection. See http://en.wikipedia.org/wiki/Transformation_matrix

Related

glLineWidth alternative? [duplicate]

This question already has answers here:
OpenGL Line Width
(4 answers)
Closed 2 years ago.
glLineWidth guarantees to support only width 1. On Windows, it's limited to width 10. To overcome this limitation, the common suggestion is to "simply" render a rectangle instead.
Since this seems like a basic requirement (render 2D/3D lines of arbitrary width, mesh wireframe, etc.), I was wondering if anyone has a code snippet for it.
It would work similar to what the legacy OpenGL offers.
Input: two 3D points and width.
Output: It would render a 3D line that faces the camera with width in pixels.
Emphasis:
It needs to face the camera.
The width is in screen pixels.
Since it's a 3D (flat) line, these properties aren't defined properly. So, I guess it would be something like "as much as possible" and "on average" (whatever that means). This is probably why glLineWidth is limited.
Something basic that doesn't answer the nuances, which is enough for me at the moment (for now, only 2D lines, for given world thickness):
GLUquadricObj *pQuadric = gluNewQuadric();
glPushMatrix();
// flatten y to make a rectangle
glm::dmat4 S = glm::scale( glm::dvec3(1., 0.001 / radius, 1.) );
// translate
glm::dmat4 T = glm::translate( toPoint<glm::dvec3>(p0) );
// rotate
glm::dvec3 xaxis(1, 0, 0);
glm::dmat4 R1 = glm::rotate( -M_PI / 2, xaxis );
glm::dvec3 u( toPoint<glm::dvec3>(p1 - p0) );
u = glm::normalize( u );
glm::dvec3 yaxis(0, 1, 0);
glm::dmat4 R2 = glm::orientation(u, yaxis);
// combine transforms
glm::dmat4 A = T * R2 * R1 * S;
glMultMatrixd( (double*)&A[0] );
glGetDoublev(GL_MODELVIEW_MATRIX, (double*)&A[0]);
gluCylinder(pQuadric, radius, radius, height, 4, 1);
glPopMatrix();
gluDeleteQuadric(pQuadric);

Apply multiple transformations to view

I am working on a OpenGL ES application using C++. I run the code on iOS with a few wrapper Obj-c classes that enable me to use gestures (like pan, pinch, rotation).
I use orthographic projection. The code only draws a simple quad for now, and I want to be able to apply any transformations to it using user gesture. That means move it, zoom in and out, rotate (some other derived gestures, like zoom using double-tap etc.).
I thought it will be fine if I simply store origin, scale and angle floats and construct Matrix using each of these, then multiply. That works for simple translation and scale, however, I can't rotate around any point. Now I can't figure out what do I need to be able to rotate around some point, translate, rotate some more around different point.
Here is example how I initialize my matrix
Mat4 Mat4::translation(float tx, float ty)
{
float a[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1 };
return Mat4(a);
}
Mat4 Mat4::rotation(float angle)
{
float a[16] = { cosf(angle), sinf(angle), 0, 0, -sinf(angle), cosf(angle), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
return Mat4(a);
}
How can I apply any number of transformations to 2D view in OpenGL(ES), using matrices? These transformations are caused by gestures (pan, pinch, rotate). For pinch and rotation, I know amount and center point in screen coordinates.
I believe your problem boils down to finding out with respect to which reference frame you define your transformations.
In practice, you can specify any trasformation (i.e. translation and rotation in 3D) as a 4x4 matrix, like you've done in your example. If they are defined with respect to the current pose of the object, then you can apply a new one by multiplying the new matrix on the right. If they are specified with respect to a fixed reference frame (e.g. your screen origin which does not move) then you pre-multiply them.
Say you apply two rotations, defined by T_rot_1 and T_rot_2
If transformations are defined wrt reference frame attached to the object:
T_final_wrt_fixed = T_rot_1_wrt_obj * T_rot_2_wrt_obj
If transformations are defined wrt reference to fixed frame:
T_final_wrt_fixed = T_rot_2_wrt_fixed * T_rot_1_wrt_fixed
For more info, check out these slides.
Rotation around arbitrary point is done by translation of the point to origin, rotation around origin and then translating back.

How do I rotate a 3D object about an axis without changing it's location? (LWJGL)

I'm currently trying to get a composite 3D object that consists of two 3D pyramids to rotate about a given axis by an angle in radians. However, one of the pyramids of the composite object is not staying flush to the other.
In the image below, I am attempting to rotate the left object PI radians about the X axis. However, one of the pyramids doesn't stay in the correct position.
http://i.stack.imgur.com/MekFZ.png
Below is the code I am using to to the scale, rotate, and translate.
float[] model = { xSize, 0, 0, 0,
0, ySize, 0, 0,
0, 0, zSize, 0,
xLoc, yLoc, zLoc, 1 };
FloatBuffer modelBuf = BufferUtils.createFloatBuffer(model.length);
matrix = new Matrix4f();
matrix = matrix.scale(xSize, ySize, zSize);
matrix = matrix.rotateXYZ(dxRot, dyRot, dzRot);
matrix = matrix.setTranslation(xLoc, yLoc, zLoc);
modelBuf = matrix.get(modelBuf);
glUniformMatrix4fv( unif_model, false, modelBuf );
Does anybody know why this is could be happening?
It seems as if you want to group objects. You should use a transform tree for that. I.e. introduce a transformation matrix for the entire group and separate transformation matrices for each object within the group (relative to the group's coordinate system).
In your concrete example, the objects' matrices would consist only of a constant translation and rotation part. They do not change. When rendering the objects, you should set the current transformation matrix as:
transform = groupTransform * objectTransform
If you want to move, rotate, scale an entire group, apply these transformations only to groupTransform.

dynamically render a 2d board in 3d view

I am a beginner in openGL. I am currently working on a program which take in inputs the width and the length of a board. Given those inputs i want to dynamically position my camera so that i can have a view on the whole board. Let' s say that my window size is 1024x768.
Are there any mathematical formula to compute the different parameters of the opengl function glookat to make it possible ?
the view i want to have on the board should look like this.
It doesn't matter if a board too big will make things look tiny. What matters the most here is to position the camera in a way that the view on the whole board is made possible
So far i am hopelessly randomly changing the parameters of my glookat function till i ran into something decent for a X size width and and Y size Height.
my gluperpective function :
gluPerspective(70 ,1024 / 768,1,1000)
my glooatfunction for a 40 * 40 board
gluLookAt(20, 20, 60, 20, -4, -20, 0, 1, 0);
how i draw my board (plane):
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt(20, 20, 60, 20, -4, -20, 0, 1, 0);
glBindTexture(GL_TEXTURE_2D, texture_sol);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex3i(width, 0, height);
glTexCoord2i(10, 0); glVertex3i(0, 0, height)
glTexCoord2i(10, 10); glVertex3i(0, 0, 0);
glTexCoord2i(0, 10); glVertex3i(width, 0, 0);
glEnd();
the output looks as follow :
gluLookAt takes 2 points and a vector; the eye and centre positions and the up vector. There's no issue with the last parameter. The first two are relevant to your question.
I see that your board in the world space is extending on the positive X and Y axes with some arbitrary width and height values. Lets take width = height = 1.0 for instance. So the board spans from (0, 0), (1, 0), (1, 1), (0, 1); the Y value is ignored here since the board lies on the Y = 0 plane and have the same value for all vertices; these are just (X, Z) values.
Now coming to gluLookAt, eye is where the camera is in world space and centre is the point where you want the camera to be looking at (in world space)
Say you want the camera to look at centre of the board I presume, so
eye = (width / 2.0f, 0, height/2.0f);
Now you've to position the camera at its vantage point. Say somewhere above the board but towards the positive Z direction since there's where the user is (assuming your world space is right handed and positive Z direction is towards the viewer), so
centre = (width / 2.0f, 5.0f, 1.0f);
Since the farthest point on Z is 0, I just added one more to be slightly father than that. Y is how much above you want to see the board from, I just chose 5.0 as an example. These are just arbitrary values I can come up with, you'll still have to experiment with these values. But I hope you got the essence of how gluLookAt works.
Though this is written as an XNA tutorial, the basic technique and math behind it should carry over to OpenGL and your project:
Positioning the Camera to View All Scene Objects
Also see
OpenGL FAQ
8.070 How can I automatically calculate a view that displays my entire model? (I know the bounding sphere and up vector.)
Edit in response to the comment question
A bounding sphere is simply a sphere that completely encloses your model. It can be described as:
A bounding sphere, S, of a point set P with n points is described by
a center point, c, and a radius, r.
So,
P = the vertices of your model (the board in this case)
c = origin of your model
r = distance from origin of the vertex, in P, farthest from the origin
So the Bounding Sphere for your board would be composed of the origin location (c) and the distance from one corner to the origin (r) assuming the board is a square and all points are equidistant.
For more complicated models, you may employ pre-created solutions [1] or implement your own calculations [2] [3]

How to tell the size of font in pixels when rendered with openGL

I'm working on the editor for Bitfighter, where we use the default OpenGL stroked font. We generally render the text with a linewidth of 2, but this makes smaller fonts less readable. What I'd like to do is detect when the fontsize will fall below some threshold, and drop the linewidth to 1. The problem is, after all the transforms and such are applied, I don't know how to tell how tall (in pixels) a font of size <fontsize> will be rendered.
This is the actual inner rendering function:
if(---something--- < thresholdSizeInPixels)
glLineWidth(1);
float scalefactor = fontsize / 120;
glPushMatrix();
glTranslatef(x, y + (fix ? 0 : size), 0);
glRotatef(angle * radiansToDegreesConversion, 0, 0, 1);
glScalef(scaleFactor, -scaleFactor, 1);
for(S32 i = 0; string[i]; i++)
OpenglUtils::drawCharacter(string[i]);
glPopMatrix();
Just before calling this, I want to check the height of the font, then drop the linewidth if necessary. What goes in the ---something--- spot?
Bitfighter is a pure old-school 2D game, so there are no fancy 3D transforms going on. All code is in C++.
My solution was to combine the first part Christian Rau's solution with a fragment of the second. Basically, I can get the current scaling factor with this:
static float modelview[16];
glGetFloatv(GL_MODELVIEW_MATRIX, modelview); // Fills modelview[]
float scalefact = modelview[0];
Then, I multiply scalefact by the fontsize in pixels, and multiply that by the ratio of windowHeight / canvasHeight to get the height in pixels that my text will be rendered.
That is...
textheight = scalefact * fontsize * widndowHeight / canvasHeight
And I liked also the idea of scaling the line thickness rather than stepping from 2 to 1 when a threshold is crossed. It all works very nicely now.
where we use the default OpenGL stroked font
OpenGL doesn't do fonts. There is no default OpenGL stroked font.
Maybe you are referring to GLUT and its glutStrokeCharacter function. Then please take note that GLUT is not part of OpenGL. It's an independent library, focused on providing a simplicistic framework for small OpenGL demos and tutorials.
To answer your question: GLUT Stroke Fonts are defined in terms of vertices, so the usual transformations apply. Since usually all transformations are linear, you can simply transform the vector (0, base_height, 0) through modelview and projection finally doing the perspective divide (gluProject does all this for you – GLU is not part OpenGL, too), the resulting vector is what you're looking for; take the vector length for scaling the width.
This should be determinable rather easily. The font's size in pixels just depends on the modelview transformation (actually only the scaling part), the projection transformation (which is a simple orthographic projection, I suppose) and the viewport settings, and of course on the size of an individual character of the font in untransformed form (what goes into the glVertex calls).
So you just take the font's basic size (lets consider the height only and call it height) and first do the modelview transformation (assuming the scaling shown in the code is the only one):
height *= scaleFactor;
Next we do the projection transformation:
height /= (top-bottom);
with top and bottom being the values you used when specifying the orthographic transformation (e.g. using glOrtho). And last but not least we do the viewport transformation:
height *= viewportHeight;
with viewportHeight being, you guessed it, the height of the viewport specified in the glViewport call. The resulting height should be the height of your font in pixels. You can use this to somehow scale the line width (without an if), as the line width parameter is in floats anyway, let OpenGL do the discretization.
If your transformation pipeline is more complicated, you could use a more general approach using the complete transformation matrices, perhaps with the help of gluProject to transform an object-space point to a screen-space point:
double x0, x1, y0, y1, z;
double modelview[16], projection[16];
int viewport[4];
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);
gluProject(0.0, 0.0, 0.0, modelview, projection, viewport, &x0, &y0, &z);
gluProject(fontWidth, fontHeight, 0.0, modelview, projection, viewport, &x1, &y1, &z);
x1 -= x0;
y1 -= y0;
fontScreenSize = sqrt(x1*x1 + y1*y1);
Here I took the diagonal of the character and not only the height, to better ignore rotations and we used the origin as reference value to ignore translations.
You might also find the answers to this question interesting, which give some more insight into OpenGL's transformation pipeline.