I am trying to create a rotation matrix around the X-axis using glm::gtc::matrix_transform::rotate:
glm::rotate(glm::mat4f(1.0f), glm::radians(90.f), glm::vec3f(1.f, 0.f, 0.f));
I expected the resulting matrix to be (translational offsets removed):
1, 0, 0
0, cos(90), -sin(90)
0, sin(90), cos(90)
0, 0, 0
(See e.g. https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations)
However, the result is slightly off, i.e.:
1, 0, 0
0, 0.9996240, -0.0274121
0, 0.0274121, 0.9996240
0, 0, 0
I looked at https://github.com/g-truc/glm/blob/master/glm/gtc/matrix_transform.inl and surely enough, the implementation uses a weird factor c + (1 - c) that would explain the results.
My question is now, why? Why is the definition of glm's rotation matrix different? What is the theory behind it?
glm implementation uses this formula from Wikipedia.
The following lines of code are identical to the formula:
Result[0][0] = c + (1 - c) * axis.x * axis.x;
Result[0][1] = (1 - c) * axis.x * axis.y + s * axis.z;
Result[0][2] = (1 - c) * axis.x * axis.z - s * axis.y;
Result[0][3] = 0;
Result[1][0] = (1 - c) * axis.y * axis.x - s * axis.z;
Result[1][1] = c + (1 - c) * axis.y * axis.y;
Result[1][2] = (1 - c) * axis.y * axis.z + s * axis.x;
Result[1][3] = 0;
Result[2][0] = (1 - c) * axis.z * axis.x + s * axis.y;
Result[2][1] = (1 - c) * axis.z * axis.y - s * axis.x;
Result[2][2] = c + (1 - c) * axis.z * axis.z;
Result[2][3] = 0;
There is nothing weird in c + (1 - c) because c + (1 - c) * axis.x * axis.x is the same as c + ((1 - c) * axis.x * axis.x). Do not forget about operator precedence.
Most likely you are having issues with floating-point precision loss.
Related
My function getHeightOfTerrain() is calling a barycentric formula function that is not returning the correct height for the one set test height in : heightMapFromArray[][].
I've tried watching OpenGL JAVA Game tutorials 14,21, 22, by "thin matrix" and I am confused on how to use my array: heightMapforBaryCentric in both of the supplied functions, and how to set the arguments that are passed to the baryCentic() function in some sort of manner so that I can solve the problem.
int creaateTerrain(int height, int width)
{
float holderY[6] = { 0.f ,0.f,0.f,0.f,0.f,0.f };
float scaleit = 1.5f;
float holder[6] = { 0.f,0.f,0.f,0.f,0.f,0.f };
for (int z = 0, z2 =0; z < iterationofHeightMap;z2++)
{
//each loop is two iterations and creates one quad (two triangles)
//however because each iteration is by two (i.e. : x=x+2) om bottom
//the amount of triangles is half the x value
//
//number of vertices : 80 x 80 x 6.
//column
for (int x = 0, x2 = 0; x < iterationofHeightMap;x2++)
{
//relevant - A : first triangle - on left triangle
//[row] [colum[]
holder[0] = heightMapFromArray[z][x];
//holder[0] = (float)imageData[(z / 2 * MAP_Z + (x / 2)) * 3];
//holder[0] = holder[0] / 255;// *scaleit;
vertices.push_back(glm::vec3(x, holder[0], z));
//match height map with online barycentric use
heightMapforBaryCentric[x2][z2] = holder[0];
holder[1] = heightMapFromArray[z+2][x];
//holder[1] = (float)imageData[(((z + 2) / 2 * MAP_Z + ((x) / 2))) * 3];
//holder[1] = holder[1] / 255;// 6 * scaleit;
vertices.push_back(glm::vec3(x, holder[1], z + 2));
//match height map with online barycentric use
heightMapforBaryCentric[x2][z2+1] = holder[1];
holder[2] = heightMapFromArray[z+2][x+2];
//holder[2] = (float)imageData[(((z + 2) / 2 * MAP_Z + ((x + 2) / 2))) * 3];
//holder[2] = holder[2] / 255;// *scaleit;
vertices.push_back(glm::vec3(x + 2, holder[2], z + 2));
////match height map with online barycentric use
heightMapforBaryCentric[x2+1][z2+1] = holder[2];
//relevant - B - second triangle (on right side)
holder[3] = heightMapFromArray[z][x];
//holder[3] = (float)imageData[((z / 2)*MAP_Z + (x / 2)) * 3];
//holder[3] = holder[3] / 255;// 256 * scaleit;
vertices.push_back(glm::vec3(x, holder[3], z));
holder[4] = heightMapFromArray[x+2][z+2];
//holder[4] = (float)imageData[(((z + 2) / 2 * MAP_Z + ((x + 2) / 2))) * 3];
//holder[4] = holder[4] / 255;// *scaleit;
vertices.push_back(glm::vec3(x + 2, holder[4], z + 2));
holder[5] = heightMapFromArray[x+2][z];
//holder[5] = (float)imageData[((z / 2)*MAP_Z + ((x + 2) / 2)) * 3];
//holder[5] = holder[5] / 255;// *scaleit;
vertices.push_back(glm::vec3(x + 2, holder[5], z));
x = x + 2;
}
z = z + 2;
}
return(1);
}
float getHeightOfTerrain(float worldX, float worldZ) {
float terrainX = worldX;
float terrainZ = worldZ;
int gridSquareSize = 2.0f;
gridX = (int)floor(terrainX / gridSquareSize);
gridZ = (int)floor(terrainZ / gridSquareSize);
xCoord = ((float)(fmod(terrainX, gridSquareSize)) / (float)gridSquareSize);
zCoord = ((float)(fmod(terrainZ, gridSquareSize)) / (float)gridSquareSize);
if (xCoord <= (1 - zCoord))
{
answer = baryCentric(
//left triangle
glm::vec3(0.0f, heightMapforBaryCentric[gridX][gridZ], 0.0f),
glm::vec3(0.0f, heightMapforBaryCentric[gridX][gridZ+1], 1.0f),
glm::vec3(1.0f, heightMapforBaryCentric[gridX+1][gridZ+1], 1.0f),
glm::vec2(xCoord, zCoord));
// if (answer != 1)
// {
// fprintf(stderr, "Z:gridx: %d gridz: %d answer: %f\n", gridX, gridZ,answer);
//
// }
}
else
{
//right triangle
answer = baryCentric(glm::vec3(0, heightMapforBaryCentric[gridX][gridZ], 0),
glm::vec3(1,heightMapforBaryCentric[gridX+1][gridZ+1], 1),
glm::vec3(1,heightMapforBaryCentric[gridX+1][gridZ], 0),
glm::vec2(xCoord, zCoord));
}
if (answer == 1)
{
answer = 0;
}
//answer = abs(answer - 1);
return(answer);
}
float baryCentric(glm::vec3 p1, glm::vec3 p2, glm::vec3 p3 , glm::vec2 pos) {
float det = (p2.z - p3.z) * (p1.x - p3.x) + (p3.x - p2.x) * (p1.z - p3.z);
float l1 = ((p2.z - p3.z) * (pos.x - p3.x) + (p3.x - p2.x) * (pos.y - p3.z)) / det;
float l2 = ((p3.z - p1.z) * (pos.x - p3.x) + (p1.x - p3.x) * (pos.y - p3.z)) / det;
float l3 = 1.0f - l1 - l2;
return (l1 * p1.y + l2 * p2.y + l3 * p3.y);
}
My expected results were that the center of the test grid's height to be the set value .5 and gradually less as the heights declined. My results were the heights being all the same, varied, or increasing. Usually these heights were under the value of one.
In implementing Blinn/Loop's algorithm on curve rendering, I realize there is a special case on Loop Curve Type. As described in their paper (subsection 4.4, page 6-7), they said the curve should be divided into two but I'm really confused how to obtain the intersection point.
Here's my rendering result:
As stated in the paper, this artifact occurs when either td/sd or te/se lie in between value [0, 1].
My source code:
...
case CURVE_TYPE_LOOP:
td = d2 + sqrt(4.0 * d1 * d3 - 3.0 * d2 *d2);
sd = 2.0 * d1;
te = d2 - sqrt(4.0 * d1 * d3 - 3.0 * d2 * d2);
se = 2.0 * d1;
if((td / sd > 0.0 && td/ sd < 1.0) || (te / se > 0.0 && te/ se < 1.0))
std::cout << "error\n";
// F matrix will be multiplied with inverse M3 to obtain tex coords (I use Eigen library btw...)
F << td * te, td * td * te, td * te * te, 1,
(-se * td) - (se * te), (-se * td * td) - (2.0 * sd * te * td), (-sd * te * te) - (2.0 * se * td * te), 0,
sd * se, te * sd * sd + 2.0 * se * td* sd, td * se * se + 2 * sd * te * se, 0,
0, -sd * sd * se, -sd * se * se, 0;
break;
...
Solved it,
I should get the splitting value t,
here's my code:
// get t
double splitLoop = -1.0;
switch (curve_type)
{
case CURVE_TYPE_UNKNOWN:
break;
case CURVE_TYPE_SERPENTINE:
tl = d2 + ((1.0 / sqrt(3.0)) * sqrt(3.0 * d2 * d2 - 4.0 * d1 * d3));
sl = 2.0 * d1;
tm = d2 - ((1.0 / sqrt(3.0)) * sqrt(3.0 * d2 * d2 - 4.0 * d1 * d3));
sm = 2.0 * d1;
F << tl * tm, tl * tl * tl, tm * tm * tm, 1,
-(sm * tl) -(sl * tm), -(3.0 * sl * tl * tl), -(3.0 * sm * tm * tm), 0,
sl * sm, 3.0 * sl * sl * tl, 3.0 * sm * sm * tm, 0,
0, -(sl * sl * sl), -(sm * sm * sm), 0;
break;
case CURVE_TYPE_LOOP:
td = d2 + sqrt(4.0 * d1 * d3 - 3.0 * d2 *d2);
sd = 2.0 * d1;
te = d2 - sqrt(4.0 * d1 * d3 - 3.0 * d2 * d2);
se = 2.0 * d1;
// Get splitting t
if((td / sd) > 0.0 && (td / sd) < 1.0)
{
splitLoop = td / sd;
}
else if((te / se) > 0.0 && (te/ se) < 1.0)
{
splitLoop = te / se;
}
F << td * te, td * td * te, td * te * te, 1,
(-se * td) - (se * te), (-se * td * td) - (2.0 * sd * te * td), (-sd * te * te) - (2.0 * se * td * te), 0,
sd * se, te * sd * sd + 2.0 * se * td* sd, td * se * se + 2 * sd * te * se, 0,
0, -sd * sd * se, -sd * se * se, 0;
break;
case CURVE_TYPE_QUADRATIC:
break;
case CURVE_TYPE_LINE:
break;
}
if(splitLoop > 0.0 && splitLoop < 1.0)
{
// SPLIT
double x01 = (x1 - x0) * splitLoop + x0;
double y01 = (y1 - y0) * splitLoop + y0;
double x12 = (x2 - x1) * splitLoop + x1;
double y12 = (y2 - y1) * splitLoop + y1;
double x23 = (x3 - x2) * splitLoop + x2;
double y23 = (y3 - y2) * splitLoop + y2;
double x012 = (x12 - x01) * splitLoop + x01;
double y012 = (y12 - y01) * splitLoop + y01;
double x123 = (x23 - x12) * splitLoop + x12;
double y123 = (y23 - y12) * splitLoop + y12;
double x0123 = (x123 - x012) * splitLoop + x012;
double y0123 = (y123 - y012) * splitLoop + y012;
// CURVE A (recursive)
DrawCubic(x0, y0, x01, y01, x012, y012, x0123, y0123);
// CURVE B (recursive)
DrawCubic(x0123, y0123, x123, y123, x23, y23, x3, y3);
}
else
{
// Draw as usual...
}
== EDIT ==
After i experimented again for a while, There's a numerical error on my program when the values of td/sd or te/se on subcurves lie again in between [0, 1], since my program use recursive by calling DrawCubic(), it causes recursive heap error.
In the meantime, I use 'hack' solution where I will not call DrawCurve() inside the recursive call (making sure the recursive is called only once). So far the result is quite satisfying and I don't see any artifact.
Any feedback is really welcomed since I'm not really good in numerical calculation :)
I have a code on C++ it's b-spline curve that has 4 points if I want to change it to 6 point what shall I change in the code?
You can check the code:
#include "graphics.h"
#include <math.h>
int main(void) {
int gd, gm, page = 0;
gd = VGA;
gm = VGAMED;
initgraph(&gd, &gm, "");
point2d pontok[4] = { 100, 100, 150, 200, 170, 130, 240, 270 }; //pontok means points
int ap;
for (;;) {
setactivepage(page);
cleardevice();
for (int i = 0; i < 4; i++)
circle(integer(pontok[i].x), integer(pontok[i].y), 3);
double t = 0;
moveto((1.0 / 6) * (pontok[0].x * pow(1 - t, 3) +
pontok[1].x * (3 * t * t * t - 6 * t * t + 4) +
pontok[2].x * (-3 * t * t * t + 3 * t * t + 3 * t + 1) +
pontok[3].x * t * t * t),
(1.0 / 6) * (pontok[0].y * pow(1 - t, 3) +
pontok[1].y * (3 * t * t * t - 6 * t * t + 4) +
pontok[2].y * (-3 * t * t * t + 3 * t * t + 3 * t + 1) +
pontok[3].y * t * t * t));
for (t = 0; t <= 1; t += 0.01)
lineto(
(1.0 / 6) * (pontok[0].x * pow(1 - t, 3) +
pontok[1].x * (3 * t * t * t - 6 * t * t + 4) +
pontok[2].x * (-3 * t * t * t + 3 * t * t + 3 * t + 1) +
pontok[3].x * t * t * t),
(1.0 / 6) * (pontok[0].y * pow(1 - t, 3) +
pontok[1].y * (3 * t * t * t - 6 * t * t + 4) +
pontok[2].y * (-3 * t * t * t + 3 * t * t + 3 * t + 1) +
pontok[3].y * t * t * t));
/* Egerkezeles */ //Egerkezeles means mouse event handling
if (!balgomb)
ap = getactivepoint((point2d *)pontok, 4, 5);
if (ap >= 0 && balgomb) { //balgomb means left mouse button
pontok[ap].x = egerx; //eger means mouse
pontok[ap].y = egery;
}
/* Egerkezeles vege */
setvisualpage(page);
page = 1 - page;
if (kbhit())
break;
}
getch();
closegraph();
return 0;
}
From your formula, it looks like you are trying to draw a cubic Bezier curve. But the formula does not seem entirely correct. You can google "cubic Bezier curve" to find the correct formula. The Wikipedia page contains the formula for any degree of Bezier curve. You can find the "6-points" formula from there by using degree = 5.
I am getting an unexpected VARSYM error for my ZIMPL program, I have no idea what the problem is, here is a portion of the code
Here are the variables
var FWPlus1 integer >= 0 <= 4;
var FWPlus2 integer >= 0 <= 4;
var FWPlus3 integer >= 0 <= 4;
goes up to 28, with the upper bound at 3, 2, and 1 for some of the points
here is the equation that is getting the error
subto R3: FCOMx ==
((FWPlus1 * (FWPlus1 * 0 + 0 )) +(FWPlus2 * (FWPlus2 * .105 + 5.47008 )) +
(FWPlus3 * (FWPlus3 * .2054 + 10.70110)) +(FWPlus4 * (FWPlus4 * .29683 + 15.46443)) +
(FWPlus6 * (FWPlus6 * .48028 + 25.02197)) +(FWPlus7 * (FWPlus7 * .50223 + 26.16553)) +
(FWPlus8 * (FWPlus8 * .50223 + 26.16553)) +(FWPlus9 * (FWPlus9 * .48028 + 25.02197)) +
(FWPlus10 * (FWPlus10 * .43734 + 22.78483)) +(FWPlus11 * (FWPlus11 * .37529 + 19.55188)) +
(FWPlus12 * (FWPlus12 * .29683 + 15.46443)) +(FWPlus13 * (FWPlus13 * .20540 + 10.70110)) +
(FWPlus14 * (FWPlus14 * .105 + 5.47008)) +(FWPlus15 * (FWPlus15 * 0 + 0)) +
(FWPlus16 * (FWPlus16 * -.105 + -5.47008)) +(FWPlus17 * (FWPlus17 * -.2054 + -10.70110)) +
(FWPlus18 * (FWPlus18 * -.29683 + -15.46443)) +(FWPlus19 * (FWPlus19 * -.37529 + -19.55188)) +
(FWPlus20 * (FWPlus20 * -.43734 + -22.78483)) +(FWPlus21 * (FWPlus21 * -.48028 + -25.02197)) +
(FWPlus22 * (FWPlus22 * -.50223 + -26.16553)) +(FWPlus23 * (FWPlus23 * -.50223 + -26.16553)) +
(FWPlus24 * (FWPlus24 * -.48028 + -25.02197)) +(FWPlus25 * (FWPlus25 * -.37529 + -19.55188)) +
(FWPlus26 * (FWPlus26 * -.29683 + -15.44827)) +(FWPlus27 * (FWPlus27 * -.20540 + -10.68992)) +
(FWPlus28 * (FWPlus28 * -.10499 + -5.46437)))
/(FWPlus1 +FWPlus2 +FWPlus3 +FWPlus4 +FWPlus6 +FWPlus7 +FWPlus8 +FWPlus9 +FWPlus10 +FWPlus11 +FWPlus12 +
FWPlus13 +FWPlus14 +FWPlus15 +FWPlus16 +FWPlus17 +FWPlus18 +FWPlus19 +FWPlus20 +FWPlus21 +FWPlus22 +FWPlus23 +
FWPlus24 +FWPlus25 +FWPlus26 +FWPlus27 + FWPlus28);
the error says it is at the end at the semicolon
Sorry but I think I figured it out, it didn't like that I was multiplying by zero in 2 of the terms
I'm making a vector drawing application and noticed that Anti Grain Geometry have an example that does exactly what I want. http://www.antigrain.com/demo/index.html then below there is an example on perspective for Win32. I don't understand their cpp file. Based on this example. If I have a bunch of verticies to form an object, like their lion, and then I have 4 verticies as control points, how could I achieve their effect? Ex, what transformation do I apply to each point?
Thanks
From that very page you posted, there's a link to the source
code. I'll explain the bilinear transformation in
http://www.antigrain.com/__code/include/agg_trans_bilinear.h.html
The idea here is to find a transformation of the form:
output_x = a * input_x + b * input_x * input_y + c * input_y + d
output_y = e * input_x + f * input_x * input_y + g * input_y + h
The term "bilinear" comes from each of those equations being linear in
either of the input coordinates by themselves. We want to solve for
the right values of a, b, c, and d. Say you have the reference
rectangle r1, r2, r3, r4 which you want to map to (0,0), (1,0), (0,1),
(1,1) (or some image coordinate system).
For a,b,c,d:
0 = a * r1_x + b * r1_x * r1_y + c * r1_y + d
1 = a * r2_x + b * r2_x * r2_y + c * r2_y + d
0 = a * r3_x + b * r3_x * r3_y + c * r3_y + d
1 = a * r4_x + b * r4_x * r4_y + c * r4_y + d
For e,f,g,h:
0 = e * r1_x + f * r1_x * r1_y + g * r1_y + h
0 = e * r2_x + f * r2_x * r2_y + g * r2_y + h
1 = e * r3_x + f * r3_x * r3_y + g * r3_y + h
1 = e * r4_x + f * r4_x * r4_y + g * r4_y + h
You can solve this however you like best. (If you're familiar with
matrix notation, these are two matrix equations for which the matrix
is the same, and then you simply need to find the LU decomposition
once, and solve the two unknown vectors). The coefficients are then
applied to map the interior of the rectangle to the position in the
rectangle.
If by any chance you're looking for the inverse transform, that is,
if you want to know where a given pixel will land, you simply switch
inputs and outputs:
For a,b,c,d:
r1_x = a * 0 + b * 0 * 0 + c * 0 + d
r2_x = a * 1 + b * 1 * 0 + c * 0 + d
r3_x = a * 0 + b * 0 * 1 + c * 1 + d
r4_x = a * 1 + b * 1 * 1 + c * 1 + d
For e,f,g,h:
r1_y = e * 0 + f * 0 * 0 + g * 0 + h
r2_y = e * 1 + f * 1 * 0 + g * 0 + h
r3_y = e * 0 + f * 0 * 1 + g * 1 + h
r4_y = e * 0 + f * 0 * 1 + g * 1 + h
You're talking about perspective transformation from 2D planar onto a square 'in space' I think.
Well - This one is not that difficult. The mathematics are explained in the paper:
Heckbert, Paul, Fundamentals of
Texture Mapping and Image Warping,
Master’s thesis, UCB/CSD 89/516, CS
Division, U.C. Berkeley, June 1989.
(I don't link to the paper due to copyright reasons. It's available on the net and you shouldn't have any problems finding it though)
This gives you the math and some ready to use equations to do it.
If you are looking for some "easy to rip" code I suggest to download the OpenVG reference implementation and take a close look at the functions "vguComputeWarpQuadToSquare", "vguComputeWarpSquareToQuad" and "vguComputeWarpQuadToQuad" :-) They cover all you need.
Download here: http://www.khronos.org/registry/vg/ri/openvg-1.1-ri.zip
These functions will calculate a 3x3 matrix that does the transformation. To use this matrix you have to extend your 2D coordinates into 2D homogenous coordinates. That's not that difficult but beyond the scope of the question. If you need help to work with them I suggest that you ask that in a different question.