Can i get rotation from this matrix algorithm - c++

Can i get rotation(yaw , pitch,roll) from this matrix algorithm ?
thanks
matrix[0][0] = cos(pitch)*cos(yaw);
matrix[0][2] = cos(pitch)*sin(yaw);
matrix[0][1] = sin(pitch);
matrix[0][3] = 0;
matrix[1][0] = -cos(roll)*sin(pitch)*cos(yaw)+sin(roll)*sin(yaw);
matrix[1][1] = cos(roll)*cos(pitch);
matrix[1][2] = -cos(roll)*sin(pitch)*sin(yaw)-sin(roll)*cos(yaw);
matrix[1][3] = 0;
matrix[2][0] = -sin(roll)*sin(pitch)*cos(yaw)-cos(roll)*sin(yaw);
matrix[2][1] = sin(roll)*cos(pitch);
matrix[2][2] = cos(roll)*cos(yaw)-sin(roll)*sin(pitch)*sin(yaw);
matrix[2][3] = 0;
matrix[3][0] = 0;
matrix[3][1] = 0;
matrix[3][2] = 0;
matrix[3][3] = 1;
edit : i used this code but it not working :
D3DXMATRIX matrix2 = D3DXMATRIX(matrix);
yaw = atan2(matrix2._13, matrix2._11);
pitch = asin(matrix2._12);
roll = atan2(matrix2._32, matrix2._22);
Edit 3:
this is my old function , it works but not 100%
VECTOR getAnglefromMatrix(float* m)
{
float pitch, yaw, roll;
D3DXMATRIX matrix = D3DXMATRIX(m);
if (matrix._11 == 1.0f)
{
yaw = todegree(atan2f(matrix._13, matrix._34));
pitch = 0;
roll = 0;
}
else if (matrix._11 == -1.0f)
{
yaw = todegree(atan2f(matrix._13, matrix._34));
pitch = 0;
roll = 0;
}
else
{
yaw = todegree(atan2(-matrix._31,matrix._11));
pitch = todegree(asin(matrix._21));
roll = todegree(atan2(-matrix._23,matrix._22));
}
return vector3d(yaw,pitch,roll);
}
i did something wrong ?

We can go through the entries and find some that are nice for back-calculations:
matrix[0][0] = cos(pitch)*cos(yaw);
matrix[0][2] = cos(pitch)*sin(yaw);
This gives us:
yaw = atan2(matrix[0][2], matrix[0][0])
For the pitch:
matrix[0][1] = sin(pitch);
This gives us:
pitch = asin(matrix[0][1]) //this assumes pitch is between -pi/2 and +pi/2
Then
matrix[1][1] = cos(roll)*cos(pitch);
matrix[2][1] = sin(roll)*cos(pitch);
This gives us:
roll = atan2(matrix[2][1], matrix[1][1])
I omitted the cases where the arguments to atan2 become zero. I am confident you can figure them out. In these cases, the Euler angles are not uniquely defined.

Related

When I use += to add a number to an array, it always ends up being 0, no matter what the number is

I am trying to write code for my robot that keeps track of where it has driven, so that it can know where it is on a Cartesian grid (I am basing it off of this document: http://thepilons.ca/wp-content/uploads/2018/10/Tracking.pdf).
In the beginning, absolute_position[0] (x-coordinate) and absolute_position[1] (y-coordinate) are both set to 2, meaning the robot starts at (2,2) on the grid. In the first loop, before the robot has moved (meaning left, right, and back variables are all set to 0), absolute_position[0] and absolute_position[1] are both set to 0.
As far as I can tell, varCos and varSin should both be 0, so
absolute_position[0] += varCos;
absolute_position[1] += varSin;
should evaluate to
2 += 0;
2 += 0;
but as I said, they both end up at 0.
if I try replacing varCos and varSin with 0, or if I set them both to 0 (rather than offset_global[0] * cos(offset_global[1])), absolute_position ends up being [2,2] like I would expect.
Complete code:
double absolute_position[2] = {2,2};
double theta0;
double left = 0;
double right = 0;
double back = 0;
double prevLeft = 0;
double prevRight = 0;
double prevBack = 0;
double deltaLr = 0;
double deltaRr = 0;
double deltaBr = 0;
double deltaL = 0;
double deltaR = 0;
double deltaB = 0;
double thetar = 0;
double theta1 = 0;
double deltaTheta = 0;
double thetaM = 0;
double offset_local[2];
double offset_global[2];
double varCos = 0;
double varSin = 0;
///////////////////////////////////////////////////
void positionTracking(){
float sL = 10.5;
float sR = 10.5;
float sB = 6.5;
while(true){
//stores current encoder values
left = -leftEncoder;
right = rightEncoder;
back = backEncoder;
//finds the distance traveled for each wheel in inches
deltaL = (left - prevLeft) * 3.25 * M_PI / 360;
deltaR = (right - prevRight) * 3.25 * M_PI / 360;
deltaB = (back - prevBack) * 3.25 * M_PI / 360;
//updates the last values of the encoders to be used next cycle
prevLeft = left;
prevRight = right;
prevBack = back;
//calculates total accumulated encoder values
deltaLr += deltaL;
deltaRr += deltaR;
deltaBr += deltaB;
//calculates new absolute orientation
theta1 = thetar + (deltaLr - deltaRr) / (sL + sR);
if(theta1 < 0){
theta1 += 2 * M_PI;
}
else if(theta1 >= 2 * M_PI){
theta1 -= 2 * M_PI;
}
//find the change in orientation
deltaTheta = theta1 - theta0;
//find local offset vector
if(deltaTheta == 0){
offset_local[0] = deltaB;
offset_local[1] = deltaR;
}
else{
offset_local[0] = 2 * sin(deltaTheta / 2) * (deltaB / deltaTheta + sB);
offset_local[1] = 2 * sin(deltaTheta / 2) * (deltaR / deltaTheta + sR);
}
//calculate the average orientation
thetaM = theta0 + deltaTheta / 2;
//converts cartesian to polar and changes the angle
offset_global[0] = sqrt(pow(offset_local[0], 2) + pow(offset_local[1], 2));
offset_global[1] = atan(offset_local[1] / offset_local[0]) - thetaM;
//converts polar offset back to cartesian and adds it to the absolute_position
varCos = offset_global[0] * cos(offset_global[1]);
varSin = offset_global[0] * sin(offset_global[1]);
absolute_position[0] += varCos;
absolute_position[1] += varSin;
//updates the old orientation to be used next cycle
theta0 = theta1;
}
}
Thanks for any help!
I figured it out: the screen on the robot said varCos and varSin were 0, but when I ran the program on my computer (not the robot) it said they were NaN. I changed it to
varCos = offset_global[0] == 0 ? 0 : offset_global[0] * cos(offset_global[1]);
varSin = offset_global[0] == 0 ? 0 : offset_global[0] * sin(offset_global[1]);
to make sure they are numbers.

2D collisions check between 3 and more objects

i've got a problem on my collision Check Algorithm.
The problem is when i try to resolve collisions between 3 objects, 1 of them is still not colliding and not resolving collisions, here's the code:
void check_collisions(engine_t* engine)
{
for (int i = 0; i < engine->actor_count; i++)
{
actor_t* first = (actor_t*)engine->collision_pairs->data[i];
collider_t* a = (collider_t*)get_component_by_name(first, "collider");
for(int j = 0; j < engine->actor_count; j++)
{
actor_t* second = (actor_t*)engine->collision_pairs->data[j];
if(second == first)
continue;
collider_t* b = (collider_t*)get_component_by_name(second, "collider");
hit_state_t hit = aabb(a, b);
resolve_collisions(a, b, hit.normal);
}
}
}
The problem is that when for example: i have A, B, C
A could collide with B and C at the same frame time, it seems that when more object are colliding the first one (first) will not be calculated anymore.. any idea?
void resolve_collisions(collider_t* a, collider_t* b, vec2_t normal)
{
//Stop rigidbody
vec2_t position = a->owner->transform.position;
vec2_t position2 = b->owner->transform.position;
rigid_body_t* rb = (rigid_body_t*)get_component_by_name(a->owner, "rigid_body");
// if(!rb) { SDL_Log("rigid_body not while resolving collisions"); return; }
//hit from dx
if (normal.x > 0.0f && position.x < b->owner->transform.position.x + b->size.x)
{
rb->velocity.x = 0.0f;
// SDL_Log("collided dx");
position.x = (b->owner->transform.position.x + b->size.x) + 0.7f;
}
//hit from sx
if (normal.x < 0.0f && position.x + a->size.x > b->owner->transform.position.x)
{
rb->velocity.x = 0.0f;
float offset = b->size.x - a->size.x;
float offset2 = a->size.x - b->size.x;
// SDL_Log("collided sx");
position.x = (b->owner->transform.position.x - b->size.x) + offset;
position2.x = (a->owner->transform.position.x - a->size.x) + offset2;
}
//hit from top
if (normal.y < 0.0f && position.y + a->size.y > b->owner->transform.position.y)
{
rb->velocity.y = 0.0f;
float offset = b->size.y - a->size.y;
position.y = (b->owner->transform.position.y - b->size.y) + offset;
}
//hit from bottom
if (normal.y > 0.0f && position.y < b->owner->transform.position.y + b->size.y)
{
rb->velocity.y = 0.0f;
// SDL_Log("collided bottom");
position.y = (b->owner->transform.position.y + b->size.y) + 0.7f;
}
//change pos
a->owner->transform.position = position;
}
any help would be much appreciated!
-Thanks

Why does this not work (glm::make_mat4)?

I have this code:
glm::mat4 aLookAt(const glm::vec3& Eye, const glm::vec3& Center, const glm::vec3& Up)
{
glm::vec3 Z = glm::normalize(Eye - Center);
glm::vec3 Y = Up;
glm::vec3 X = glm::normalize(glm::cross(Y, Z));
Y = glm::normalize(glm::cross(Z, X));
float Matrix[4][4];
Matrix[0][0] = X.x;
Matrix[1][0] = X.y;
Matrix[2][0] = X.z;
Matrix[3][0] = (float)glm::dot(-X, Eye);
Matrix[0][1] = Y.x;
Matrix[1][1] = Y.y;
Matrix[2][1] = Y.z;
Matrix[3][1] = (float)glm::dot(-Y, Eye);
Matrix[0][2] = Z.x;
Matrix[1][2] = Z.y;
Matrix[2][2] = Z.z;
Matrix[3][2] = (float)glm::dot(Z, Eye);
Matrix[0][3] = 0;
Matrix[1][3] = 0;
Matrix[2][3] = 0;
Matrix[3][3] = 1.0f;
glm::mat4 theMatrix = glm::make_mat4(Matrix);
return theMatrix;
}
But whenever I try to compile it, it get the following errors:
Why? This time I actually expected no errors..
The compiler tells you why. You are passing float[4][4] to a method that expects a pointer to float:
template<typename T >
detail::tmat4x4< T > make_mat4 (T const *const ptr)
Those types are not compatible. You need a float[16] instead:
float Matrix[16];
Matrix[0] = X.x;
Matrix[1] = X.y;
Matrix[2] = X.z;
Matrix[3] = (float)glm::dot(-X, Eye);
Matrix[4] = Y.x;
Matrix[5] = Y.y;
Matrix[6] = Y.z;
Matrix[7] = (float)glm::dot(-Y, Eye);
Matrix[8] = Z.x;
Matrix[9] = Z.y;
Matrix[10] = Z.z;
Matrix[11] = (float)glm::dot(Z, Eye);
Matrix[12] = 0;
Matrix[13] = 0;
Matrix[14] = 0;
Matrix[15] = 1.0f;

Opengl View Transformation matrix Rotation

Recently i implemented a simple Opengl program that composes of a scene of objects, i've applied most of the transformation & projection matrices, in such away that i am able to rotate transform & scale objects, move my camera through z & x coordinates and applied perspective projection however when it comes to camera rotation things get weird, my rotation matrix for my camera is simply a rotation matrix that rotates the world uniformly, however when i rotate the world so that i look in the up direction;+y; and when i move forward, the camera doesn't seem to advance in the direction where it is looking at;as it is the case in FPS games my camera moves relative to the world space, i know that i am missing the vectors that specify directions in x,y,z coordinates, but i am unable to incorporate these vectors with my camera (view Transformation) matrix, most of the tutorial on internet either describes it in a block diagram or uses the conventional gluLookAt() function, i really need a brief explanation about view Transformations and specifically camera rotation and how i should implement it in my matrices, my my final matrix is as follows:
resultTransform = perspectiveTrans * cameraTrans * modelTrans;
where:
perspectiveTrans = applies only a perspective projection transformation
cameraTrans = is a combination of rotate,translate matrices that affect all obj.s in the scene
modelTrans =is the transformation that is applied to the models
Matrix4X4.cpp file:
#include "Matrix4X4.h"
using namespace std;
////////////////////////////////// Constructor Declerations ////////////////////////////////
Matrix4X4::Matrix4X4()
{
setIdentity();
}
Matrix4X4::Matrix4X4(float value)
{
for(int i = 0 ; i < 4; i++)
for ( int j = 0; j < 4; j++)
Matrix[i][j] = value;
}
/////////////////////////////////////////////////////////////////////////////////
////////////////////////////// Destructor Decleration //////////////////////////////
Matrix4X4::~Matrix4X4()
{
}
///////////////////////////////////////////////////////////////////////////////////
/////////////////////// Set Identity Matrix /////////////////////////////////////////
void Matrix4X4::setIdentity()
{
Matrix[0][0] =1; Matrix[0][1] = 0; Matrix[0][2] = 0; Matrix[0][3] = 0;
Matrix[1][0] =0; Matrix[1][1] = 1; Matrix[1][2] = 0; Matrix[1][3] = 0;
Matrix[2][0] =0; Matrix[2][1] = 0; Matrix[2][2] = 1; Matrix[2][3] = 0;
Matrix[3][0] =0; Matrix[3][1] = 0; Matrix[3][2] = 0; Matrix[3][3] = 1;
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////// Set Translation Matrix //////////////////////////////////
Matrix4X4 Matrix4X4::setTranslation(float x,float y,float z)
{
Matrix[0][0] =1; Matrix[0][1] = 0; Matrix[0][2] = 0; Matrix[0][3] = x;
Matrix[1][0] =0; Matrix[1][1] = 1; Matrix[1][2] = 0; Matrix[1][3] = y;
Matrix[2][0] =0; Matrix[2][1] = 0; Matrix[2][2] = 1; Matrix[2][3] = z;
Matrix[3][0] =0; Matrix[3][1] = 0; Matrix[3][2] = 0; Matrix[3][3] = 1;
return *this;
}
/////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Set Rotation Matrix ///////////////////////////////////////////
Matrix4X4 Matrix4X4::setRotation(float x,float y,float z)
{
Matrix4X4 xRot;
Matrix4X4 yRot;
Matrix4X4 zRot;
x = (float)x * 3.14/ 180.0;
y = (float)y * 3.14/ 180.0;
z = (float)z * 3.14/ 180.0;
xRot.Matrix[0][0] =1; xRot.Matrix[0][1] = 0; xRot.Matrix[0][2] = 0; xRot.Matrix[0][3] = 0;
xRot.Matrix[1][0] =0; xRot.Matrix[1][1] = cosf(x); xRot.Matrix[1][2] = -sinf(x); xRot.Matrix[1][3] = 0;
xRot.Matrix[2][0] =0; xRot.Matrix[2][1] = sinf(x); xRot.Matrix[2][2] = cosf(x); xRot.Matrix[2][3] = 0;
xRot.Matrix[3][0] =0; xRot.Matrix[3][1] = 0; xRot.Matrix[3][2] = 0; xRot.Matrix[3][3] = 1;
yRot.Matrix[0][0] = cosf(y); yRot.Matrix[0][1] = 0; yRot.Matrix[0][2] = -sinf(y); yRot.Matrix[0][3] = 0;
yRot.Matrix[1][0] =0; yRot.Matrix[1][1] = 1; yRot.Matrix[1][2] = 0; yRot.Matrix[1][3] = 0;
yRot.Matrix[2][0] = sinf(y); yRot.Matrix[2][1] = 0; yRot.Matrix[2][2] = cosf(y); yRot.Matrix[2][3] = 0;
yRot.Matrix[3][0] =0; yRot.Matrix[3][1] = 0; yRot.Matrix[3][2] = 0; yRot.Matrix[3][3] = 1;
zRot.Matrix[0][0] = cosf(z); zRot.Matrix[0][1] = -sinf(z); zRot.Matrix[0][2] = 0; zRot.Matrix[0][3] = 0;
zRot.Matrix[1][0] = sinf(z); zRot.Matrix[1][1] = cosf(z); zRot.Matrix[1][2] = 0; zRot.Matrix[1][3] = 0;
zRot.Matrix[2][0] =0; zRot.Matrix[2][1] = 0; zRot.Matrix[2][2] = 1; zRot.Matrix[2][3] = 0;
zRot.Matrix[3][0] =0; zRot.Matrix[3][1] = 0; zRot.Matrix[3][2] = 0; zRot.Matrix[3][3] = 1;
return (zRot * yRot * xRot) ;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////// Set Scale Matrix //////////////////////////////////////////
Matrix4X4 Matrix4X4::setScale(float x,float y,float z)
{
Matrix[0][0] =x; Matrix[0][1] = 0; Matrix[0][2] = 0; Matrix[0][3] = 0;
Matrix[1][0] =0; Matrix[1][1] = y; Matrix[1][2] = 0; Matrix[1][3] = 0;
Matrix[2][0] =0; Matrix[2][1] = 0; Matrix[2][2] = z; Matrix[2][3] = 0;
Matrix[3][0] =0; Matrix[3][1] = 0; Matrix[3][2] = 0; Matrix[3][3] = 1;
return *this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// Set Perspective Projection ///////////////////////////////////////
void Matrix4X4::setPerspective(float fov,float aRatio,float zNear,float zFar)
{
fov = (fov/2) * 3.14 / 180.0;
float tanHalfFOV = tanf(fov);
float zRange = zNear - zFar;
Matrix[0][0] =1.0f / (tanHalfFOV * aRatio); Matrix[0][1] = 0; Matrix[0][2] = 0; Matrix[0][3] = 0;
Matrix[1][0] =0; Matrix[1][1] = 1.0f / tanHalfFOV; Matrix[1][2] = 0; Matrix[1][3] = 0;
Matrix[2][0] =0; Matrix[2][1] = 0; Matrix[2][2] = (-zNear - zFar)/ zRange; Matrix[2][3] = 2* zFar * zNear / zRange;
Matrix[3][0] =0; Matrix[3][1] = 0; Matrix[3][2] = 1; Matrix[3][3] = 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Getters & Setters ////////////////////////////////////////////
float * Matrix4X4::getMat()
{
return (float *) Matrix;
}
float Matrix4X4::getMember(int x, int y) const
{
return Matrix[x][y];
}
void Matrix4X4::setMat(int row,int col,float value)
{
Matrix[row][col] = value;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// (*) Operator Overload //////////////////////////////////////
Matrix4X4 operator * (const Matrix4X4 & lhs,const Matrix4X4 & rhs)
{
Matrix4X4 result;
for(int i = 0 ; i < 4; i++)
for ( int j = 0; j < 4; j++)
result.setMat(i, j, lhs.getMember(i,0) * rhs.getMember(0, j) +
lhs.getMember(i,1) * rhs.getMember(1, j) +
lhs.getMember(i,2) * rhs.getMember(2, j) +
lhs.getMember(i,3) * rhs.getMember(3, j));
return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
the Transformation code i use in my main block:
SDL_PumpEvents();
for (int x = 0; x< 256; x++)
{
if (state[x] == 1 )
{
if(x == 26)
tranForward -= 0.001;
if (x == 22)
tranForward += 0.001;
if (x == 4)
tranRight += 0.0009;
if (x == 7)
tranRight -= 0.0009;
if (x == 82)
lookUp += 0.02;
if (x == 81)
lookUp -= 0.02;
if (x == 80)
lookRight -= 0.02;
if (x == 79)
lookRight += 0.02;
}
}
modelTrans = Translation.setTranslation(0, 0, 5) * Scale.setScale(0.5, 0.5, 0.5);
camTrans = Rotation.setRotation(lookUp, lookRight, 0) * Translation.setTranslation(tranRight, 0, tranForward);
Projection.setPerspective(70, win.getWidth()/win.getHeight(), 0.1, 1000);
result = Projection * camTrans * modelTrans;
glUniformMatrix4fv(uniformloc, 1, GL_TRUE, result.getMat());
The matrix multiplication does not have the same rules as the scalar multiplication and in your case A*B does NOT equal B*A when multiplying the matrices. If rest of the code is good your solution might simply be turning
result = Projection * camTrans * modelTrans;
into
result = Projection * (modelTrans * camTrans);
Do alway watch out for both, multiplication order and parentheses when dealing with anything but scalar values.
In general when you are combining a translation and rotation matrix you need to think in matrix own space coordinate system, that means like playing a FPS:
Multiplying rotation*translation means the object will rotate first and then translate meaning the object position will depend on the rotation being already applied and a 180 degrees rotation will translate the object backwards from the 3rd view perspective.
Multiplying translation*rotation means the object will translate first and then rotate meaning it will in fact be moved into the same direction no matter the rotation, only the direction of where the object is facing will be changed by rotation matrix.
Just a nice example, if you want to present a movement of earth around sun (the earth is circling the sun while rotating around its own axis being on some radius):
Matrix4X4 orbitRotation; //rotation matrix for where in orbit the object is
Matrix4X4 objectRotation; //object rotation around its own axis
Matrix4X4 orbitRadius; //object orbit radius
Matrix4X4 result = (orbitRotation*orbitRadius)*objectRotation;
my code seemed to ignore the previous matrix calculation and re calculated the transformations with respect to my scene's initial state, the desired world rotation & Translation is achieved by using a fixed value for rotation & Translation, the modified code blocks are as follows:
for (int x = 0; x< 256; x++)
{
if (state[x] == 1 )
{
if(x == 26)
tranForward = -0.001;
if (x == 22)
tranForward = 0.001;
if (x == 4)
tranRight = 0.0009;
if (x == 7)
tranRight = -0.0009;
if (x == 82)
lookUp = 0.02;
if (x == 81)
lookUp = -0.02;
if (x == 80)
lookRight = -0.02;
if (x == 79)
lookRight = 0.02;
}
}
camTrans = Rotation.setRotation(lookUp, lookRight, 0) * Translation.setTranslation(tranRight, 0, tranForward);
result = camTrans * result;
modelTrans = Projection * result;
tranForward = 0.0;
tranRight = 0.0;
lookUp = 0.0;
lookRight = 0.0;
glUniformMatrix4fv(uniformloc, 1, GL_TRUE, modelTrans.getMat());
note that result matrix keeps track of the previous state and the current state transformations are applied with respect to it.

Creating a 3d plane using Frank Luna's technique: what are sinf and cosf?

I am creating a 3d plane that lies on the x and z axis, and has hills that extend on the y axis. The bulk of the code looks like this:
float PeaksAndValleys::getHeight(float x, float z)const
{
return 0.3f*( z*sinf(0.1f*x) + x*cosf(0.1f*z) );
}
void PeaksAndValleys::init(ID3D10Device* device, DWORD m, DWORD n, float dx)
{
md3dDevice = device;
mNumRows = m;
mNumCols = n;
mNumVertices = m*n;
mNumFaces = (m-1)*(n-1)*2;
// Create the geometry and fill the vertex buffer.
std::vector<Vertex> vertices(mNumVertices);
float halfWidth = (n-1)*dx*0.5f;
float halfDepth = (m-1)*dx*0.5f;
for(DWORD i = 0; i < m; ++i)
{
float z = halfDepth - i*dx;
for(DWORD j = 0; j < n; ++j)
{
float x = -halfWidth + j*dx;
// Graph of this function looks like a mountain range.
float y = getHeight(x,z);
vertices[i*n+j].pos = D3DXVECTOR3(x, y, z);
// Color the vertex based on its height.
if( y < -10.0f )
vertices[i*n+j].color = BEACH_SAND;
else if( y < 5.0f )
vertices[i*n+j].color = LIGHT_YELLOW_GREEN;
else if( y < 12.0f )
vertices[i*n+j].color = DARK_YELLOW_GREEN;
else if( y < 20.0f )
vertices[i*n+j].color = DARKBROWN;
else
vertices[i*n+j].color = WHITE;
}
}
D3D10_BUFFER_DESC vbd;
vbd.Usage = D3D10_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof(Vertex) * mNumVertices;
vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = &vertices[0];
HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB));
// Create the index buffer. The index buffer is fixed, so we only
// need to create and set once.
std::vector<DWORD> indices(mNumFaces*3); // 3 indices per face
// Iterate over each quad and compute indices.
int k = 0;
for(DWORD i = 0; i < m-1; ++i)
{
for(DWORD j = 0; j < n-1; ++j)
{
indices[k] = i*n+j;
indices[k+1] = i*n+j+1;
indices[k+2] = (i+1)*n+j;
indices[k+3] = (i+1)*n+j;
indices[k+4] = i*n+j+1;
indices[k+5] = (i+1)*n+j+1;
k += 6; // next quad
}
}
D3D10_BUFFER_DESC ibd;
ibd.Usage = D3D10_USAGE_IMMUTABLE;
ibd.ByteWidth = sizeof(DWORD) * mNumFaces*3;
ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
ibd.CPUAccessFlags = 0;
ibd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA iinitData;
iinitData.pSysMem = &indices[0];
HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mIB));
}
My question pertains to the cosf and sinf. I am familiar with trigonometry and I understand sin, cosine, and tangent, but I am not familiar with cosf and sinf and what they do. From looking at this example, they have a lot to do with finding a y value.
cosf and sinf are simply the float versions of cos and sin. The normal cos and sin functions return double values instead of floats. Note that all these functions work in radians, not degrees.
Combined in the way above, they give a landscape that looks somewhat like a mountain range, as in this plot.