I am a little amazed. I have been debugging my code for hours now, and GLM seems to be giving up on me. I am struggling with the following 2 instances:
....
cout << "multiplying A:" << endl;
displayMatrix(node->wMatrix);
cout << "and B:" << endl;
displayMatrix((node->children)[i]->wMatrix);
//switch order!
mat4 temp = (node->children)[i]->wMatrix * node->wMatrix;
cout << "Get result as:" << endl;
displayMatrix(temp);
...
The displayMatrix method is as follows:
void displayMatrix(mat4 &m)
{
cout << m[0][0] << " " << m[0][1] << " " << m[0][2] << " " << m[0][3] << endl;
cout << m[1][0] << " " << m[1][1] << " " << m[1][2] << " " << m[1][3] << endl;
cout << m[2][0] << " " << m[2][1] << " " << m[2][2] << " " << m[2][3] << endl;
cout << m[3][0] << " " << m[3][1] << " " << m[3][2] << " " << m[3][3] << endl;
}
Here is the output I get:
multiplying A:
1 0 0 0
0 1 0 0.5
0 0 1 0
0 0 0 1
and B:
0.540302 -0.841471 0 0
0.841471 0.540302 0 -0.5
0 0 1 0
0 0 0 1
Get result as:
0.540302 -0.841471 0 0
0.841471 0.540302 0 0
0 0 1 0
0 0 0 1
NOTICE that in the code above, the matrix multiplication order is the reverse of what you would write on paper. In other words, the code says B * A. I was very thrown off by this.
The second instance:
cout << "temp:" << endl;
cout << temp.x << " " << temp.y << " " << temp.z << " " << temp.w << endl;
cout << "binding matrix inverse: " << endl;
displayMatrix(bindingInvs.at(jIndex));
temp = bindingInvs.at(jIndex) * temp;
cout << "now temp:" << endl;
cout << temp.x << " " << temp.y << " " << temp.z << " " << temp.w << endl;
cout << "joint world matrix: " << endl;
displayMatrix(joints.at(jIndex)->wMatrix);
temp = (joints.at(jIndex)->wMatrix) * temp;
cout << "now temp:" << endl;
cout << temp.x << " " << temp.y << " " << temp.z << " " << temp.w << endl;
cout << "weight: " << jWeight << endl;
temp = jWeight * temp;
cout << "now temp:" << endl;
cout << temp.x << " " << temp.y << " " << temp.z << " " << temp.w << endl;
The output that I get now is:
temp:
0.087 0 -0.05 1
binding matrix inverse:
1 -0 0 -0
-0 1 -0 0
0 -0 1 -0
-0 0 -0 1
now temp:
0.087 0 -0.05 1
joint world matrix:
1 0 0 0
0 1 0 0.5
0 0 1 0
0 0 0 1
now temp:
0.087 0 -0.05 1
weight: 1
now temp:
0.087 0 -0.05 1
Temp is never getting changed for some reason. I don't know what to do, or why this is happening. My programs compiles and runs (I am pasting from output above). Of course, this is not the entire program. This is only the steps for debugging. But I feel confident that this much should be enough to tell what's going on.
Your displayMatrix function is confusing you, since you print the matrices transposed to what you would expect on paper. GLM uses column major ordering, so the addressing is m[col][row].
Now with that in mind, the operation A*B is actually what you should expect.
For the temp vector, the same problem arises: the first matrix you multiply it by is identity, so it is unchanged. The second matrix is identity, except the last row is 0 0.5 0 1, so x, y and z will be unchanged and the new w' will be 0.5 * y + w. Since y is 0 to begin with, nothing is changed here,too.
Related
Hello I need help with this tracing problem, here here is a copy of the problem:
What is the output of the following program segment?
int u = 4, v = 3;
one(u, v);
cout << u << " " << v << endl;
cout << two(u, v) << " " ;
cout << u << " " << v << endl;
void one(int &x, int& y){
int a;
a = ++x ;
x += y++;
y = ++a;
}
int two(int s, int &t){
int b;
b = s – t;
s += t + b ;
t += 4 * b;
cout << s << " " << t << " " << b << endl;
return ( b ) ;
}
I managed to find the first output of function One, then I plugged it into function Two to find its output. But function Two returned the cout instead of the return (b), can anyone show me what I am doing wrong, any help would be greatly appreciated! Here is a copy of the output after plugging it into visual studios:
Output:
8 6
16 14 2
2 8 14
The relevant fragment is equivalent to the following, with the step-by-step breakdown in comments.
cout << u << " " // prints 8
<< v // prints 6
<< endl; // ends first line "8 6"
int n = two(u, v); // calls 'two' which prints the second line "16 4 2" and returns 2
cout << n // prints 2 which is the value returned by 'two' at the previous step
<< " " ; // prints a space but does not end the line
cout << u << " " // prints 8 next on the third line
<< v // prints 14
<< endl; // ends third line and prints "2 8 14"
clang-format formats my code as follow:
std::cout << 1 << 1 << 1 << 1 << 1 << 1
<< 1 << 1 << 1 << 1 << 1 << 1;
but my style-guide requires:
std::cout << 1 << 1 << 1 << 1 << 1 << 1 <<
1 << 1 << 1 << 1 << 1 << 1;
I tried setting BreakBeforeBinaryOperators:None and AlignOperands:false, but it didn't help me. Is there any other options?
I have a program that takes numbers that a person enters and sums it.
This happens 3 times, so I have 3 totals. The problem I am having is that I need to order them from greatest to least no matter what the sums come out to be.(this isnt the full code assume the sums are calculated and are declared)
#include <iostream>
#include <string>
using namespace std;
string firstName1, lastName1; // input and output for the users names
string firstName2, lastName2;
string firstName3, lastName3;
// from greatest to least
if ( sum > sum_2 > sum_3 )
{
cout << "Total for" << " " << firstName1 << " " << lastName1 << " " << "$" << sum << ".00" << endl;
cout << "Total for" << " " << firstName2 << " " << lastName2 << " " << "$" << sum_2 << ".00" << endl;
cout << "Total for" << " " << firstName3 << " " << lastName3 << " " << "$" << sum_3 << ".00" << endl;
}
In c++, the syntax sum > sum_2 > sum_3 won't evaluate as you're assuming. It's equivalent to (sum > sum_2) > sum_3.
In the case where sum is greater than sum_2, sum > sum_2 will evaluate to true. Then, this boolean value will be converted to an integer, 1 and compared with sum_3.
To do what you're trying to accomplish try:
if (sum > sum_2 && sum_2 > sum_3)
Use a helper swap function:
void swap( int *a, int *b )
{
int temp = *a;
*a = *b;
*b = temp;
}
And bubble sort it:
int sums[3] = { sum, sum_2, sum_3 };
for ( int i = 0; i < 3; ++i )
for ( int j = 0; j < i; ++j )
if ( sums[j] < sums[i] )
swap( &sums[j], &sums[i] );
cout << "Total for" << " " << firstName1 << " " << lastName1 << " " << "$" << sums[0] << ".00" << endl;
cout << "Total for" << " " << firstName2 << " " << lastName2 << " " << "$" << sums[1] << ".00" << endl;
cout << "Total for" << " " << firstName3 << " " << lastName3 << " " << "$" << sums[2] << ".00" << endl;
I have a code that uses Gram-Schmidt inside a loop. I want to reduce the number of calls to this algorithm as much as possible, but the thing is that despite of getting the same result before and after the call, when I print the results of some operations using these values they are different. For example, on the code below the result of abs(muGS[k][0]) - abs(before2) should be 0 or very close to 0, since the printed values of this variable (before and after the call) are the same. However, this not what happens. muGS is a double matrix and its values are usually between 0 and 1.
int k = 1;
double before2;
while(k < end) {
before2 = muGS[k][0];
gramSchmidt(b, muGS, cGS, k);
//prints for debug
if (abs(muGS[k][0]) - abs(before2) > 0.1) {
if (abs(muGS[k][0]) - abs(before2) > 0.1) {
cout << "1 muGS[k] diff:" << abs(muGS[k][0]) - abs(before2) << endl;
cout << "1 muGS[k] before:" << muGS[k][0] << endl;
cout << "1 muGS[k] after:" << muGS[k][0] << endl;
cout << "1 muGS[k] mult before:" << before2 * before2 << endl;
cout << "1 muGS[k] mult after:" << muGS[k][0] * muGS[k][0] << endl;
cout << "1 muGS[k] abs before:" << abs(before2) << endl;
cout << "1 muGS[k] abs after:" << abs(muGS[k][0]) << endl;
}
getchar();
}
for (i = k-1; i >= 0; i--) {
for (j = 0; j < i; j++) {
muGS[k][j] -= round(muGS[k][i]) * muGS[i][j];
}
}
//some other operations that don't change the value of muGS
k++;
}
Output:
1 muGS[k] diff:0.157396
1 muGS[k] before:0.288172
1 muGS[k] after:0.288172
1 muGS[k] mult before:0.0171023
1 muGS[k] mult after:0.083043
1 muGS[k] abs before:0.130776
1 muGS[k] abs after:0.288172
Another thing that happens is that the absolute value of before2 is very different from the value of before2.
Is it possible that I'm having some precision loss or why is this happening?
Thanks
There is no precision loss. You just have a mistake in your code:
cout << "1 muGS[k] before:" << muGS[k][0] << endl;
cout << "1 muGS[k] after:" << muGS[k][0] << endl;
You print same value for both before and after.
But shoulde be:
cout << "1 muGS[k] before:" << before2 << endl;
cout << "1 muGS[k] after:" << muGS[k][0] << endl;
void printAst(int x)
{
for( int i = 0; i < x; i++)
{
cout << "*";
}
cout << " (" << x << ")" << endl;
}
void printHisto(int histo[])
{
//cout.precision(3);
int count = 0;
for(double i = -3.00; i < 3.00; i += 0.25)
{
cout << setprecision(3) << i << " to " << i + 0.25 << ": " << printAst(histo[count]) << endl;
// cout << setw(3) << setfill('0') << i << " to " << i + 0.25 << ": " << histo[count] << endl;
count ++;
}
}
I want my output to be formatted like this, so I used setprecision(3), which also does not work.
-3.00 to -2.75: (0)
-2.75 to -2.50: * (1)
-2.50 to -2.25: * (1)
-2.25 to -2.00: * (6)
-2.00 to -1.75: ***** (12)
So instead it is formatted like this
-3 to -2.75: 3
-2.75 to -2.5: 4
-2.5 to -2.25: 5
-2.25 to -2: 0
-2 to -1.75: 0
The main problem however, is that when I try to call printAst on to histo[count]. This is what is causing this error. PrintAst is used to print the asteriks, histo[count] provides the amount of asteriks to print.
cout << setprecision(3) << i << " to " << i + 0.25 << ": " << printAst(histo[count]) << endl;
You seem to have a misunderstanding about how chaining << works in streams.
cout << 42 looks like an operator expression with two operands, but it's really a call to a function with two parameters (the name of the function is operator<<). This function returns a reference to the stream, which enables the chaining.
An expression like this:
cout << 1 << 2;
is equivalent to this:
operator<<( operator<<(cout, 1), 2);
Now, the problem is that a parameter to a function can't be void but that's what printAst returns. Instead, you need to return something that can be streamed - in other words, something that operator<< is already overloaded for. I'd suggest std::string:
std::string printAst(int x);
{
std::string s = " (" + std::string(x,'*') + ")";
return s;
}
You can read more about operator overloading.