get xyz from vector - c++

Below is a function which helps control the car, it creates a matrix then creates a vector using that matrix and now I want to be able to get the x,y and z from that vector.
I've tried i32 CarX = vecVel.GetX; but im getting these three errors
error C3867: 'Vec2::GetX': function call missing argument list; use '&Vec2::GetX' to create a pointer to member
error C2440: '=' : cannot convert from 'const float (__thiscall Vec2::* )(void) const' to 'i32'
IntelliSense: a pointer to a bound function may only be used to call the function
I thought i32 carX = vecVel[0]; might work but no. i32 carX = vecVel.FX; doesnt work because it says FX is protected
void APIENTRY Car_Update(Object *pObject)
{
Car *pCar=(Car *)pObject;
Matrix mat;
Matrix *pmat;
Object_GetMatrix(pObject,&mat);
Object_GetMatrixPtr(pObject,&pmat);
Vec3 vecVel(pCar->vecBounce + mat.GetColumn(2) * pCar->fSpeed);
pCar->vecBounce = pCar->vecBounce *0.5f;
mat.RotY(pCar->fRot);
mat.SetColumn(3, vecVel + mat.GetColumn(3));
//mat.GetColumn;
pCar->fSpeed *= 0.8f;// friction
pCar->fRotWheelLast = pCar->fRotWheel;
pCar->fRotWheel += (pCar->fSpeed*30.f);
Level_GenerateDraw( &mat.GetColumn(3) );
Level_GenerateAlphas( &mat.GetColumn(3) );
Car_Light(pCar);
Collision_UpdateMat(pCar->pBox, &mat);
float fCol=1.f;
while(Level_TestBoxCollide( pCar->pBox ))
{
ColData Data;
float fDot;
Collision_GetColData(&Data);
fDot = -1.8f * Data.normal.Dot(vecVel);
pCar->vecBounce = (vecVel + Data.normal * fDot)*fCol;
pCar->fSpeed = 0.f;
mat.SetColumn(3, pmat->GetColumn(3) + pCar->vecBounce);
Collision_UpdateMat(pCar->pBox, &mat);
vecVel = pCar->vecBounce;
fCol-=0.1f;
if(fCol<0.f)
{
pCar->vecBounce.Set(0.f,0.f,0.f);
mat.SetColumn(3, pmat->GetColumn(3));
Collision_UpdateMat(pCar->pBox, &mat);
ASSERT(!Level_TestBoxCollide( pCar->pBox ), "still colliding");
}
}
Object_SetMatrix(pObject, &mat);
Vec3 vecWidth(mat.GetColumn(0)*0.2f);
Vec3 vecWheel1(mat.GetColumn(3) - mat.GetColumn(0)*0.6f);
Vec3 vecWheel2(mat.GetColumn(3) + mat.GetColumn(0)*0.6f);
vecWheel1.SetY( vecWheel1.GetY() - 0.7f );
vecWheel2.SetY( vecWheel2.GetY() - 0.7f );
Trail_AddPoint((Object*)pCar->pTrail[0], vecWheel1, vecWidth);
Trail_AddPoint((Object*)pCar->pTrail[1], vecWheel2, vecWidth);
}

The compiler is telling you that in order to call a function on a class you must follow the call with parentheses: i32 carX = vecVel.FX(). That is assuming that FX is a function that returns an i32 value on Vec2.

Related

GLSL + Scenekit: Function definition not allowed

I'm trying to create a vertex shader in Scenekit. The shader is successfully added to the geometry of the SCNNode, however it displays purple because I get an error. I added the shaders to the geometry using SCNShaderModifiers and know that they are successfully found and attached.
The exact error messages I recieved is:
2020-08-31 21:26:53.977977-0700 Azure[69834:3664712] [SceneKit] Error: FATAL ERROR : failed compiling shader:
Error Domain=MTLLibraryErrorDomain Code=3 "Compilation failed:
program_source:2901:97: error: function definition is not allowed here
float2 wavedx(float2 position, float2 direction, float speed, float frequency, float timeshift) {
^
program_source:2908:49: error: function definition is not allowed here
float getwaves(float2 position, int iterations) {
^
" UserInfo={NSLocalizedDescription=Compilation failed:
program_source:2901:97: error: function definition is not allowed here
float2 wavedx(float2 position, float2 direction, float speed, float frequency, float timeshift) {
^
program_source:2908:49: error: function definition is not allowed here
float getwaves(float2 position, int iterations) {
^
}
However, when I am not using methods, the shader works perfectly. To test this I used a regular sinusoidal shader. The following example worked:
uniform float Amplitude = 0.1;
_geometry.position.z += sin(u_time + _geometry.position.x);
The code I was trying to get to work was this, which has functions:
float Time = u_time;
float DRAG_MULT = 0.048;
vec2 wavedx(vec2 position, vec2 direction, float speed, float frequency, float timeshift) {
float x = dot(direction, position) * frequency + timeshift * speed;
float wave = exp(sin(x) - 1.0);
float dx = wave * cos(x);
return vec2(wave, -dx);
}
float getwaves(vec2 position, int iterations) {
float iter = 0.0;
float phase = 6.0;
float speed = 2.0;
float weight = 1.0;
float w = 0.0;
float ws = 0.0;
for(int i = 0; i < iterations; i++){
vec2 p = vec2(sin(iter), cos(iter));
vec2 res = wavedx(position, p, speed, phase, Time);
position += normalize(p) * res.y * weight * DRAG_MULT;
w += res.x * weight;
iter += 12.0;
ws += weight;
weight = mix(weight, 0.0, 0.2);
phase *= 1.18;
speed *= 1.07;
}
return w / ws;
}
I have no idea what's going wrong here. These are all standalone GLSL snippets. Thanks!
I added the shaders to the geometry using SCNShaderModifiers and know that they are successfully found and attached.
If you use SCNShaderModifiers, you also have to adhere to their documentation:
Custom global functions. If your shader modifier benefits from factoring common code into functions, place their definitions here. If
you include custom functions in your snippet, you must place the
#pragma body directive between your function definitions and the main body of the snippet.

Eigen passing Quaternionf, discards qualifiers

i am trying to use Eigen::Quaternionf. But i am getting when i just want to asign one Quaternion an error.
oldQuat = pos;
both are Eigen::Quaternionf, the following error is given. Is must be because the Methods are declared as constant (see: http://eigen.tuxfamily.org/dox/classEigen_1_1Quaternion.html)
Fehler: passing 'const Quaternionf {aka const Eigen::Quaternion<float>}' as 'this' argument of 'Eigen::Quaternion<Scalar, Options>& Eigen::Quaternion<Scalar, Options>::operator=(const Eigen::Quaternion<Scalar, Options>&) [with _Scalar = float, int _Options = 0, Eigen::Quaternion<Scalar, Options> = Eigen::Quaternion<float>]' discards qualifiers [-fpermissive]
I have to idea on how to get past this error. Thanks in advance
edit:
for (itCanon = canonicalValues.begin(), itTraj = exampleTraj.begin(); itCanon != canonicalValues.end(); ++itCanon, itTraj++)
{
const SampledTrajectoryV2::TrajData& state = *itTraj;
Eigen::Vector3f axis;
axis << itTraj->getPosition(1), itTraj->getPosition(2), itTraj->getPosition(3);
Eigen::AngleAxisf angleAxis(itTraj->getPosition(0), axis);
Eigen::Quaternionf pos(angleAxis);
pos.normalize();
//Error in both these lines! Same error both times.
Eigen::Vector3f vecVel = calcAngularVelocity(oldQuat, pos);
oldQuat = pos;
// D0 element R3x3 nicht kompatibel mit quat
result[*itCanon] = - A_Z*(B_Z*2*log(pos) - TAU * vecVel);
}
oldQuat is declared in the header. as are A_z, B_Z and TAU.
Eigen::Quaternionf<S, O>::operator= is declared as non const, but oldQuat is a const object (I suspect). Change it to non-const.

error X8000 : D3D11 Internal Compiler error : Invalid Bytecode: Invalid operand type for operand #1 of opcode #86 (counts are 1-based)

I'm absolutely stumped as well as my instructors/lab-assistants.
For some reason, the following HLSL code is returning this in the output window:
error X8000 : D3D11 Internal Compiler error : Invalid Bytecode: Invalid operand type for operand #1 of opcode #86 (counts are 1-based).
Here's the function in the HLSL causing the issue:
// Projects a sphere diameter large in screen space to calculate desired tesselation factor
float SphereToScreenSpaceTessellation(float3 p0, float3 p1, float diameter)
{
float3 centerPoint = (p0 + p1) * 0.5f;
float4 point0 = mul( float4(centerPoint,1.0f) , gTileWorldView);
float4 point1 = point0;
point1.x += diameter;
float4 point0ClipSpace = mul(point0, gTileProj);
float4 point1ClipSpace = mul(point1, gTileProj);
point0ClipSpace /= point0ClipSpace.w;
point1ClipSpace /= point1ClipSpace.w;
point0ClipSpace.xy *= gScreenSize;
point1ClipSpace.xy *= gScreenSize;
float projSizeOfEdge = distance(point0ClipSpace, point1ClipSpace);
float result = projSizeOfEdge / gTessellatedTriWidth;
return clamp(result, 0, 64);
}
I've narrowed it down to the point where it may be the "mul" intrinsic. We've taken everything out of the code and tried to return out a temporary variable like this, and it works fine:
float SphereToScreenSpaceTessellation(float3 p0, float3 p1, float diameter)
{
float temp = 0;
float3 centerPoint = (p0 + p1) * 0.5f;
float4 point0 = mul( float4(centerPoint,1.0f) , gTileWorldView);
float4 point1 = point0;
point1.x += diameter;
float4 point0ClipSpace = mul(point0, gTileProj);
float4 point1ClipSpace = mul(point1, gTileProj);
point0ClipSpace /= point0ClipSpace.w;
point1ClipSpace /= point1ClipSpace.w;
point0ClipSpace.xy *= gScreenSize;
point1ClipSpace.xy *= gScreenSize;
float projSizeOfEdge = distance(point0ClipSpace, point1ClipSpace);
float result = projSizeOfEdge / gTessellatedTriWidth;
return temp;
//return clamp(result, 0, 64);
}
If anyone is wondering:
gTileWorldView, gTileProj are float4x4's in a .hlsli file
gScreenSize is a float2 in a .hlsli file.
gTessellatedTriWidth is a float in a .hlsli file.
The following function is as states in a 2011 NVidia shader at : http://dx11-xpr.googlecode.com/svn/trunk/XPR/Media/Effects/TerrainTessellation.fx
I tried to copy and paste their solution replacing their variables with the one above, and the same error listed happens.
I'm absolutely stumped and I need assistance in order to do this assignment, please help.
Check out this line:
point0ClipSpace.xy *= gScreenSize;
Is gScreenSize a float2? I do not believe you can scalar multiply a vec by any vec type.

"Cannot initialize a parameter of type 'float *' with an Ivalue of type 'float'" compiler error

I am making a game in xcode with c++, opengl and GLUT. I have a model which has its own .h and .cpp file and I have drawn it but I now want to translate it. I can't use gltranslate because I want to move only the gun, not my camera. The model file goes as follows:
//M4.h
extern unsigned int M4NumVerts;
extern float M4Verts [151248];
extern float M4Normals [140064];
extern float M4TexCoords [153680];
//M4.cpp
unsigned int M4NumVerts = 37812;
float M4Verts [151248] = {
// f 1/1/1 1582/2/1 4733/3/1
-0.00205801177070031, 0.0252329378141267, -0.266482197565778,
-0.00205347560809555, 0.015738643990207, -0.265580239652506,
-0.00908273427886488, 0.018200092410135, -0.264843587943923,...};
float M4Normals [140064] = {
// f 1/1/1 1582/2/1 4733/3/1
-0.1361849642872, -0.0937589754128839, -0.986236741371776,
-0.1361849642872, -0.0937589754128839, -0.986236741371776,
-0.1361849642872, -0.0937589754128839, -0.986236741371776,...};
float M4TexCoords [153680] = {
// f 1/1/1 1582/2/1 4733/3/1
0.110088, 0.229552,
0.108891, 0.243519,
0.119508, 0.240861,..};
I have a function in my math class that translates the points like this:
void Math::translatePoint(float P[3], float x, float y, float z){
P[0] += x;
P[1] += y;
P[2] += z;
}
The function works for one point if I do the following:
Math::translatePoint(M4Verts[x], 0, 0, -0.5);
I want it to work for all the points so I made this for loop:
for (int x = 0; x < M4NumVerts; x++) {
Math::translatePoint(M4Verts[x], 0, 0, -0.5);
}
I do this but it wont work and I am met with the error:
Cannot initialize a parameter of type 'float *' with an Ivalue of type 'float'
I have searched around and tried to find alternatives like defining x as a float instead but nothing I try works. Can anybody help.
translatePoints expects an array of float of length 3. But you are passing a single float. Hence the compiler error. I'm afraid that I cannot discern what you are trying to do and so suggest the code that would be correct.

Is my compiler confused with what it thinks to be overloaded functions?

I have the following header functions:
float computeDistance3(Vector3f& vec_a, Vector3f& vec_b);
float computeDotProduct3(Vector3f& vecta, Vector3f& vectb);
float computeGeoDotProd3(Vector3f& vecta, Vector3f& vectb);
With the following definitions
float computeDistance3(Vector3f& vec_a, Vector3f& vec_b) {
float x = vec_a.x - vec_b.x;
float y = vec_a.y - vec_b.y;
float z = vec_a.z - vec_b.z;
return sqrt((x * x) + (y * y) + (z * z));
}
float computeDotProduct3(Vector3f& vec_a, Vector3f vec_b) {
return (vec_a.x * vec_b.x)
+ (vec_a.y * vec_b.y)
+ (vec_a.z * vec_b.z);
}
float computeGeoDotProd3(Vector3f& vecta, Vector3f& vectb) {
float amag, bmag, dotProd;
amag = vecta.computeMagnitude();
bmag = vectb.computeMagnitude();
dotProd = computeDotProduct3(vecta, vectb);
bool notZero = (amag != 0.0f && bmag != 0.0f) && dotProd != 0.0f;
if (notZero) {
return cosf(dotProd / (amag * bmag));
} else {
return -1.0f;
}
}
I know that their signatures are the same. Is this confusing the compiler? I'm guessing so, because when I compile the code, I get this:
vector3f.cpp: In function ‘float computeGeoDotProd(Vector3f&, Vector3f&)’:
vector3f.cpp:139:43: error: call of overloaded ‘computeDotProduct3(Vector3f&, Vector3f&)’ is ambiguous
vector3f.cpp:139:43: note: candidates are:
vector3f.h:31:7: note: float computeDotProduct3(Vector3f&, Vector3f&)
vector3f.cpp:127:7: note: float computeDotProduct3(Vector3f&, Vector3f)
Question
What is the solution to unconfusing the compiler?
You're missing an & in the definition:
float computeDotProduct3(Vector3f& vec_a, Vector3f vec_b) {
should be:
float computeDotProduct3(Vector3f& vec_a, Vector3f& vec_b) {
So you end up with two different (overloaded) function prototypes that differ only by the reference & - hence ambiguous.