glm::mat4 * glm::mat4 yielding incorrect results? - c++

For context, I've been learning OpenGL, and got to perspective projections. Spent a solid hour trying to figure out why my triangular pyramid wasn't showing up right, until I noticed one of my matrix multiplications (performed using operator* for glm::mat4) was giving me incorrect results. Tried multiplying two matrices with the same values, outside of that context, and sure enough, the result was still incorrect.
The two matrices I tried to multiply were:
glm::mat4 left, right;
left[0][0] = 2.09928f; left[0][1] = 0.0f; left[0][2] = 0.0f; left[0][3] = 0.0f;
left[1][0] = 0.0f; left[1][1] = 3.73205f; left[1][2] = 0.0f; left[1][3] = 0.0f;
left[2][0] = 0.0f; left[2][1] = 0.0f; left[2][2] = 1.0202f; left[2][3] = -2.0202f;
left[3][0] = 0.0f; left[3][1] = 0.0f; left[3][2] = 1.0f; left[3][3] = 0.0f;
right[0][0] = 0.999998f; right[0][1] = 0.0f; right[0][2] = -0.00174533f; right[0][3] = 0.0f;
right[1][0] = 0.0f; right[1][1] = 1.0f; right[1][2] = 0.0f; right[1][3] = 0.0f;
right[2][0] = 0.00174533f; right[2][1] = 0.0f; right[2][2] = 0.999998f; right[2][3] = 5.0f;
right[3][0] = 0.0f; right[3][1] = 0.0f; right[3][2] = 0.0f; right[3][3] = 1.0f;
Using left * right, I get:
2.09928, 0, -0.00178059, 0.00352592
0, 3.73205, 0, 0
0.00366393, 0, 6.0202, -2.0202
0, 0, 1, 0
which is incorrect. On the other hand, with this multiplication algorithm:
inline glm::mat4 Multiply(const glm::mat4& left, const glm::mat4 right)
{
glm::mat4 ret;
for (unsigned int i = 0; i < 4; i++) {
for (unsigned int j = 0; j < 4; j++) {
ret[i][j] = left[i][0] * right[0][j] +
left[i][1] * right[1][j] +
left[i][2] * right[2][j] +
left[i][3] * right[3][j];
}
}
return ret;
}
I get:
2.09928, 0, -0.00366393, 0
0, 3.73205, 0, 0
0.00178059, 0, 1.0202,3.08081
0.00174533, 0, 0.999998, 5
which is correct.
So, what am I doing wrong here?
Thanks in advance!

ret[i][j] = left[i][0] * right[0][j] +
left[i][1] * right[1][j] +
left[i][2] * right[2][j] +
left[i][3] * right[3][j];
GLM uses column-major matrices. As such, left[0] is the first column. You are doing a dot-product between the column of the lhs matrix and the rows of the rhs matrix. That's backwards; it's row * column.
What you are probably doing wrong is building your matrices incorrectly, in row-major fashion.

Related

Incorrect lookat matrix

I've started working on shadow maps for directional lights and for that I need a lookAt matrix, but when I tried constructing from example of an online tutorial it looked something like this:
It currently looks like this:
https://media.giphy.com/media/QrMnqBBJZuATu/giphy.gif
I've tried multiple ways of constructing it but with no success, I checked if the normalization, cross and translation functions weren't correct but that wasn't the case. I've also tried changing from column-major matrices to row-major matrices but with no luck. Would someone be able to point out what I did wrong?
Lookat matrix constructing:
Center vector = (0, 0, 0),
Up vector = (0, 1, 0)
Matrix4f Matrix4f::lookAt(const Vector3f& position, const Vector3f& center, const Vector3f& up) {
Matrix4f out(1.0f);
Vector3f z = position.substract(center).normalize();
Vector3f y = up;
Vector3f x = y.cross(z).normalize();
y = z.cross(x);
out.mElements[0 * 4 + 0] = x.x;
out.mElements[0 * 4 + 1] = x.y;
out.mElements[0 * 4 + 2] = x.z;
out.mElements[1 * 4 + 0] = y.x;
out.mElements[1 * 4 + 1] = y.y;
out.mElements[1 * 4 + 2] = y.z;
out.mElements[2 * 4 + 0] = z.x;
out.mElements[2 * 4 + 1] = z.y;
out.mElements[2 * 4 + 2] = z.z;
return (out * Matrix4f::translation(Vector3f(-position.x, -position.y, -position.z)));
}
}
Credit for code: https://stackoverflow.com/users/5577765/rabbid76
This is how I pass a matrix to the shader:
void Shader::setMat4(const char* name, const math::Matrix4f& matrix){
glUniformMatrix4fv(getUniformLocation(name), 1, GL_TRUE, matrix.mElements);
}
After I've calculated the lookAt matrix I directly pass it to the vertex shader to the uniform: view and calculate a point like this:
gl_Position = projection * view * model * vec4(vertexPosition, 1.0);
And this is how my matrix multiplication works:
Matrix4f Matrix4f::multiply(const Matrix4f& other) const {
Matrix4f out;
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
fl32 o = 0;
for (int c = 0; c < 4; c++) {
o += this->mElements[c + y * 4] * other.mElements[x + c * 4]; }
out.mElements[x + y * 4] = o;
}
}
return out;
}
Edit: updated picture
Edit: Added a more detailed description
You need to normalize s before u is calculated. I'm not sure if it is the only problem
If your positions (position, center) and the up vector are in viewport space, then the Z-Axis of the view matrix is the inverse line of sight and the Y-Axis is the up vector. See the following code:
Matrix4f Matrix4f::lookAt(const Vector3f& position, const Vector3f& center, const Vector3f& up)
{
Matrix4f out(1.0f); // I suppose this initilizes a 4*4 identity matrix
// Z-Axis is the line of sight
Vector3f z = position.substract(center).normalize(); // inverse line of sight
// Y-Axis is the up vector
Vector3f y = up;
// X-Axis is the cross product of Y-Axis and Z-Axis
Vector3f x = y.cross(z).normalize();
// orthonormalize the Y-Axis
y = z.cross( x );
out.mElements[0*4 + 0] = x.x;
out.mElements[0*4 + 1] = x.y;
out.mElements[0*4 + 2] = x.z;
out.mElements[1*4 + 0] = y.x;
out.mElements[1*4 + 1] = y.y;
out.mElements[1*4 + 2] = y.z;
out.mElements[2*4 + 0] = z.x;
out.mElements[2*4 + 1] = z.y;
out.mElements[2*4 + 2] = z.z;
return (out * Matrix4f::translation(Vector3f(-position.x, -position.y, -position.z)));
}
After hours of trying to make the lookat matrix working I had given up on way of constructing the lookat matrix, instead I constructed a lookat matrix based on the position of the camera and position the camera should look at, using trigonometry functions I was able to create the result which I was seeking.
My current way of constructing a lookat matrix:
Matrix4f Matrix4f::lookAt(const Vector3f& position, const Vector3f& center) {
Vector3f deltaVector = (position - center).normalize();
fl32 yaw = (fl32)radToDeg(atan(deltaVector.x / deltaVector.z));
fl32 pitch = (fl32)radToDeg(acos(Vector2f(deltaVector.x, deltaVector.z).magnitude()));
if (deltaVector.z > 0)
yaw = yaw - 180.0f;
Matrix4f yRotation = Matrix4f::rotation(Vector3f(0.0f, 1.0f, 0.0f), -yaw);
Matrix4f xRotation = Matrix4f::rotation(Vector3f(1.0f, 0.0f, 0.0f), pitch);
Matrix4f translation = Matrix4f::translation(position);
return (translation * (yRotation * xRotation));
}

HeightFIeld divided into parts - PhysX

I have a terrain with a specific length and width, which take the information from he file bmp. With Shade of grey i can modify my terrain. Fine, it works great.
Here comes the problem.
I need my terrain divided into physically smaller parts, joined together create the original terrain.
How can i do this? I know that its possible. Any sugestions? Where, why there?
Mys file:
HeightField.cpp
#include "HeightField.h"
HeightField::HeightField(Terrain terrain, PxPhysics& sdk, PxMaterial &material, int width, int height)
{
this->nrVertices = terrain.NumVertices;
this->terrain = terrain;
this->width = width;
this->height = height;
this->fillSamples();
this->fillDesc();
this->aHeightField = sdk.createHeightField(hfDesc);
this->hfGeom = new PxHeightFieldGeometry(aHeightField, PxMeshGeometryFlags(), this->terrain.dy / 255.0, this->terrain.dx, this->terrain.dz);
this->terrainPos = new PxTransform(PxVec3(-this->terrain.dx*(this->width - 1) / 2, 0.0f, this->terrain.dz*(this->height - 1) / 2), PxQuat(3.1415 / 2.0, PxVec3(0, 1, 0)));
this->g_pxHeightField = sdk.createRigidDynamic(*this->terrainPos);
this->g_pxHeightField->setRigidDynamicFlag(PxRigidDynamicFlag::eKINEMATIC, true);
PxShape* aHeightFieldShape = this->g_pxHeightField->createShape(*(this->hfGeom), material);
}
HeightField::~HeightField()
{
}
void HeightField::fillSamples()
{
this->samples = (PxHeightFieldSample*)malloc(sizeof(PxHeightFieldSample)*(this->nrVertices));
for (int i = 0; i < this->nrVertices; i++)
{
samples[i].height = this->terrain.hminfo.heightMap[i].y;
samples[i].clearTessFlag();
}
}
void HeightField::fillDesc()
{
this->hfDesc.format = PxHeightFieldFormat::eS16_TM;
this->hfDesc.nbColumns = this->width;
this->hfDesc.nbRows = this->height;
this->hfDesc.samples.data = this->samples;
this->hfDesc.samples.stride = sizeof(PxHeightFieldSample);
}
Terrain.cpp
#include "Terrain.h"
Terrain::Terrain(void)
{
v = NULL;
indices = NULL;
dx = dz = 1000; //odleg³oœæ miêdzy punktami grid'a
dy = 1000; //maksymalna wysokoϾ terenu
}
Terrain::~Terrain(void)
{
if (v != NULL) delete [] v;
if (indices != NULL) delete indices;
if (hminfo.heightMap != NULL) delete [] hminfo.heightMap;
}
bool Terrain::HeightMapLoad(char* filename, float sx, float sz, float maxy)
{
FILE *filePtr; // Point to the current position in the file
BITMAPFILEHEADER bitmapFileHeader; // Structure which stores information about file
BITMAPINFOHEADER bitmapInfoHeader; // Structure which stores information about image
int imageSize, index;
unsigned char height;
// Open the file
filePtr = fopen(filename,"rb");
if (filePtr == NULL)
return 0;
dx = sz;
dz = sz;
dy = maxy;
// Get the width and height (width and length) of the image
hminfo.terrainWidth = bitmapInfoHeader.biWidth;
hminfo.terrainHeight = bitmapInfoHeader.biHeight;
// Initialize the heightMap array (stores the vertices of our terrain)
hminfo.heightMap = new IntV3[hminfo.terrainWidth * hminfo.terrainHeight];
// We use a greyscale image, so all 3 rgb values are the same, but we only need one for the height
// So we use this counter to skip the next two components in the image data (we read R, then skip BG)
int k=0;
// Read the image data into our heightMap array
for(int j=0; j< hminfo.terrainHeight; j++)
{
for(int i=0; i< hminfo.terrainWidth; i++)
{
height = bitmapImage[k];
index = ( hminfo.terrainWidth * (hminfo.terrainHeight - 1 - j)) + i;
hminfo.heightMap[index].x = i - (hminfo.terrainWidth - 1)/2;
hminfo.heightMap[index].y = height;
hminfo.heightMap[index].z = j - (hminfo.terrainHeight - 1)/2;
k+=3;
}
k++;
}
int cols = hminfo.terrainWidth;
int rows = hminfo.terrainHeight;
//Create the grid
NumVertices = 2 * rows * cols;
NumFaces = (rows-1)*(cols-1)*2;
v = new struct HeightFieldVertex[NumVertices];
for(DWORD i = 0; i < rows; ++i)
{
for(DWORD j = 0; j < cols; ++j)
{
v[i*cols+j].pos.x = hminfo.heightMap[i*cols+j].x * dx;
v[i*cols+j].pos.y = (float(hminfo.heightMap[i*cols+j].y)/255.0) * dy;
v[i*cols+j].pos.z = hminfo.heightMap[i*cols+j].z * dz;
v[i*cols+j].texCoord = D3DXVECTOR2(j, i);
}
}
indices = new DWORD[NumFaces * 3];
k = 0;
for(DWORD i = 0; i < rows-1; i++)
{
for(DWORD j = 0; j < cols-1; j++)
{
indices[k] = i*cols+j; // Bottom left of quad
indices[k+1] = i*cols+j+1; // Bottom right of quad
indices[k+2] = (i+1)*cols+j; // Top left of quad
indices[k+3] = (i+1)*cols+j; // Top left of quad
indices[k+4] = i*cols+j+1; // Bottom right of quad
indices[k+5] = (i+1)*cols+j+1; // Top right of quad
k += 6; // next quad
}
}
//normals & tangents
std::vector<D3DXVECTOR3> tempNormal;
//normalized and unnormalized normals
D3DXVECTOR3 unnormalized(0.0f, 0.0f, 0.0f);
//tangent stuff
std::vector<D3DXVECTOR3> tempTangent;
D3DXVECTOR3 tangent(0.0f, 0.0f, 0.0f);
float tcU1, tcV1, tcU2, tcV2;
//Used to get vectors (sides) from the position of the verts
float vecX, vecY, vecZ;
//Two edges of our triangle
D3DXVECTOR3 edge1(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 edge2(0.0f, 0.0f, 0.0f);
//Compute face normals
//And Tangents
for(int i = 0; i < NumFaces; ++i)
{
//Get the vector describing one edge of our triangle (edge 0,2)
vecX = v[indices[(i*3)+1]].pos.x - v[indices[(i*3)]].pos.x;
vecY = v[indices[(i*3)+1]].pos.y - v[indices[(i*3)]].pos.y;
vecZ = v[indices[(i*3)+1]].pos.z - v[indices[(i*3)]].pos.z;
edge1 = D3DXVECTOR3(vecX, vecY, vecZ); //Create our first edge
//Get the vector describing another edge of our triangle (edge 2,1)
vecX = v[indices[(i*3)+2]].pos.x - v[indices[(i*3)]].pos.x;
vecY = v[indices[(i*3)+2]].pos.y - v[indices[(i*3)]].pos.y;
vecZ = v[indices[(i*3)+2]].pos.z - v[indices[(i*3)]].pos.z;
edge2 = D3DXVECTOR3(vecX, vecY, vecZ); //Create our second edge
//Cross multiply the two edge vectors to get the un-normalized face normal
D3DXVec3Cross(&unnormalized, &edge1, &edge2);
tempNormal.push_back(unnormalized);
//Find first texture coordinate edge 2d vector
tcU1 = v[indices[(i*3)+1]].texCoord.x - v[indices[(i*3)]].texCoord.x;
tcV1 = v[indices[(i*3)+1]].texCoord.y - v[indices[(i*3)]].texCoord.y;
//Find second texture coordinate edge 2d vector
tcU2 = v[indices[(i*3)+2]].texCoord.x - v[indices[(i*3)]].texCoord.x;
tcV2 = v[indices[(i*3)+2]].texCoord.y - v[indices[(i*3)]].texCoord.y;
//Find tangent using both tex coord edges and position edges
tangent.x = (tcV2 * edge1.x - tcV1 * edge2.x) / (tcU1 * tcV2 - tcU2 * tcV1);
tangent.y = (tcV2 * edge1.y - tcV1 * edge2.y) / (tcU1 * tcV2 - tcU2 * tcV1);
tangent.z = (tcV2 * edge1.z - tcV1 * edge2.z) / (tcU1 * tcV2 - tcU2 * tcV1);
tempTangent.push_back(tangent);
}
//Compute vertex normals (normal Averaging)
D3DXVECTOR4 normalSum(0.0f, 0.0f, 0.0f, 0.0f);
D3DXVECTOR4 tangentSum(0.0f, 0.0f, 0.0f, 0.0f);
int facesUsing = 0;
float tX, tY, tZ; //temp axis variables
//Go through each vertex
for(int i = 0; i < NumVertices; ++i)
{
//Check which triangles use this vertex
for(int j = 0; j < NumFaces; ++j)
{
if(indices[j*3] == i ||
indices[(j*3)+1] == i ||
indices[(j*3)+2] == i)
{
tX = normalSum.x + tempNormal[j].x;
tY = normalSum.y + tempNormal[j].y;
tZ = normalSum.z + tempNormal[j].z;
normalSum = D3DXVECTOR4(tX, tY, tZ, 0.0f); //If a face is using the vertex, add the unormalized face normal to the normalSum
facesUsing++;
}
}
//Get the actual normal by dividing the normalSum by the number of faces sharing the vertex
normalSum = normalSum / facesUsing;
facesUsing = 0;
//Check which triangles use this vertex
for(int j = 0; j < NumFaces; ++j)
{
if(indices[j*3] == i ||
indices[(j*3)+1] == i ||
indices[(j*3)+2] == i)
{
//We can reuse tX, tY, tZ to sum up tangents
tX = tangentSum.x + tempTangent[j].x;
tY = tangentSum.y + tempTangent[j].y;
tZ = tangentSum.z + tempTangent[j].z;
tangentSum = D3DXVECTOR4(tX, tY, tZ, 0.0f); //sum up face tangents using this vertex
facesUsing++;
}
}
//Get the actual normal by dividing the normalSum by the number of faces sharing the vertex
tangentSum = tangentSum / facesUsing;
//Normalize the normalSum vector and tangent
D3DXVec4Normalize(&normalSum, &normalSum);
D3DXVec4Normalize(&tangentSum, &tangentSum);
//Store the normal and tangent in our current vertex
v[i].normal.x = normalSum.x;
v[i].normal.y = normalSum.y;
v[i].normal.z = normalSum.z;
v[i].tangent.x = tangentSum.x;
v[i].tangent.y = tangentSum.y;
v[i].tangent.z = tangentSum.z;
D3DXVECTOR3 bit;
D3DXVec3Cross(&bit, &v[i].normal, &v[i].tangent);
v[i].bitangent = -1.0 * bit;
//Clear normalSum, tangentSum and facesUsing for next vertex
normalSum = D3DXVECTOR4(0.0f, 0.0f, 0.0f, 0.0f);
tangentSum = D3DXVECTOR4(0.0f, 0.0f, 0.0f, 0.0f);
facesUsing = 0;
}
////terrain AABB
//MinX = -1.0 * dx * (hminfo.terrainWidth - 1)/2;
//MinY = 0.0;
//MinZ = -1.0 * dz * (hminfo.terrainHeight - 1)/2;
//MaxX = dx * (hminfo.terrainWidth - 1)/2;
//MaxY = dy;
//MaxZ = dz * (hminfo.terrainHeight - 1)/2;
return true;
}
Fragment of Base.cpp
HeightField *hf = new HeightField(g_Terrain, *g_PhysicsSDK, *material, g_Terrain.hminfo.terrainWidth, g_Terrain.hminfo.terrainHeight);
g_PxScene->addActor(*(hf->g_pxHeightField));
I want to get something similar, but in order to work correctly
PVD - simulation Terrain
If you are familiar with the website rastertek for working with DirectX 10 or 11 in c++ within his updated DirectX 11 Terrain Series or version 2 a similar construct was created in tutorial 9 in which he calls Terrain Cells. The direct link to that tutorial can be found here: DirectX 11: Terrain Series2: Tutorial 9 - Terrain Cells. This should provide an excellent reference that I think is related toward the topic at hand and your initial question.

Transformed w value is 0

I'm making software renderer.
Sometimes, transformed vertex's w coordinate is 0.
So, if i divide other coordinate by w, it occures error.
if camera position's z coordinate equal with vertex position's z coordinate, transformed vertex's w coordinate is 0.
is there wrong on my code?
below code is my transform code.
void Camera::ComputeWorldToCameraMat()
{
Vector3 zaxis;
zaxis.Set( _dir.x - _pos.x, _dir.y - _pos.y, _dir.z - _pos.z );
zaxis.Normalize();
// xaxis = zaxis X up
Vector3 xaxis;
xaxis = cross(zaxis, _up);
xaxis.Normalize();
Vector3 yaxis;
yaxis = cross(zaxis, xaxis);
_worldToCamera.Identity();
_worldToCamera.a11 = xaxis.x;
_worldToCamera.a12 = yaxis.x;
_worldToCamera.a13 = zaxis.x;
_worldToCamera.a14 = 0;
_worldToCamera.a21 = xaxis.y;
_worldToCamera.a22 = yaxis.y;
_worldToCamera.a23 = zaxis.y;
_worldToCamera.a24 = 0;
_worldToCamera.a31 = xaxis.z;
_worldToCamera.a32 = yaxis.z;
_worldToCamera.a33 = zaxis.z;
_worldToCamera.a34 = 0;
_worldToCamera.a41 = -dot(xaxis, _pos);
_worldToCamera.a42 = -dot(yaxis, _pos);
_worldToCamera.a43 = -dot(zaxis, _pos);
_worldToCamera.a44 = 1;
}
void Camera::ComputeProjectionMat( Viewport* viewport)
{
int width = viewport->_width;
int height = viewport->_height;
//float tanfov = (float)tan(_fov * 0.5f);
float cot = 1.0f / tan( _fov * 0.5f );
float aspect = (float)(width / height);
_projection.Identity();
_projection.a11 = cot / aspect;
_projection.a12 = 0;
_projection.a13 = 0;
_projection.a14 = 0;
_projection.a21 = 0;
_projection.a22 = cot;
_projection.a23 = 0;
_projection.a24 = 0;
_projection.a31 = 0;
_projection.a32 = 0;
_projection.a33 = -1 * (_farZ + _nearZ) / ( _farZ - _nearZ );
_projection.a34 = 2 * ( _nearZ * _farZ ) / ( _farZ - _nearZ );
_projection.a41 = 0;
_projection.a42 = 0;
_projection.a43 = 1;
_projection.a44 = 0;
}

Batching in DirectX

I have done a framework so far in DirectX 9 and It uses a Single VertexBuffer which draws every texture I load, I have used one Vertex Array which draws every single Texture Quad inside of a Lock and Unlock call. I have heard there is a thing called Batching where I can Lock it once and Unlock when I draw everything. I know I can draw every texture quad in one call to lock and unlock but I have no idea how i can set different textures to each of these. Can Anyone here Guide me ?
vertexBufferRHW->Lock(0, 0, (void**)&VerticesRHW, NULL);
VerticesRHW[0].Color = color;
VerticesRHW[0].X = (float) Position.X;
VerticesRHW[0].Y = (float) Position.Y;
VerticesRHW[0].Z = 0.0f;
VerticesRHW[0].RHW = 1.0f;
VerticesRHW[0].U = 0.0f;
VerticesRHW[0].V = 0.0f;
VerticesRHW[1].Color = color;
VerticesRHW[1].X = (float) (Position.X+TextureFile.Width);
VerticesRHW[1].Y = (float) Position.Y;
VerticesRHW[1].Z = 0.0f;
VerticesRHW[1].RHW = 1.0f;
VerticesRHW[1].U = 1.0f;
VerticesRHW[1].V = 0.0f;
VerticesRHW[2].Color = color;
VerticesRHW[2].X = (float) (Position.X+TextureFile.Width);
VerticesRHW[2].Y = (float) (Position.Y+TextureFile.Height);
VerticesRHW[2].Z = 0.0f;
VerticesRHW[2].RHW = 1.0f;
VerticesRHW[2].U = 1.0f;
VerticesRHW[2].V = 1.0f;
VerticesRHW[3].Color = color;
VerticesRHW[3].X = (float) Position.X ;
VerticesRHW[3].Y = (float) (Position.Y+TextureFile.Height);
VerticesRHW[3].Z = 0.0f;
VerticesRHW[3].RHW = 1.0f;
VerticesRHW[3].U = 0.0f;
VerticesRHW[3].V = 1.0f;
vertexBufferRHW->Unlock();
spriteDevice->SetTexture(0,TextureFile.TextureFile);
spriteDevice->DrawPrimitive(D3DPT_TRIANGLEFAN,0,2);
You can make all your quads a height/width of 1 and then do a for loop like so:
for( ... loop through all the textures )
{
// create a matrix that scales your quad to make it the correct width/height and in the correct location
D3DXMATRIX matrix, scale, translation;
D3DXMatrixScaling( &scale, texture.width, texture.height, 0 );
D3DXMatrixTranslation( &translation, position.x, position.y, 0 );
matrix = scale * translation;
spriteDevice->SetTransform( D3DTS_WORLDMATRIX, &matrix );
spriteDevice->SetTexture(0,texture);
spriteDevice->DrawPrimitive(D3DPT_TRIANGLEFAN,0,2);
}

Projecting a 3D point to 2D screen coordinate OpenTK

Using Monotouch and OpenTK I am trying to get the screen coordinate of one 3D point. I have my world view projection matrix set up, and OpenGL makes sense of it and projects my 3D model perfectly, but how to use the same matrix to project just one point from 2D to 3D?
I thought I could simply use:
Vector3.Transform(ref input3Dpos, ref matWorldViewProjection, out projected2Dpos);
Then have the projected screen coordinate in projected2DPos. But the resulting Vector4 does not seem to represent the proper projected screen coordinate. And I do not know how to calculate it from there on.
I found I need to divide by Vector4.w, however I am still getting the wrong values. Using this method now:
private static bool GluProject(OpenTK.Vector3 objPos, OpenTK.Matrix4 matWorldViewProjection, int[] viewport, out OpenTK.Vector3 screenPos)
{
OpenTK.Vector4 _in;
_in.X = objPos.X;
_in.Y = objPos.Y;
_in.Z = objPos.Z;
_in.W = 1f;
Vector4 _out = OpenTK.Vector4.Transform(_in, matWorldViewProjection);
if (_out.W <= 0.0)
{
screenPos = OpenTK.Vector3.Zero;
return false;
}
_out.X /= _out.W;
_out.Y /= _out.W;
_out.Z /= _out.W;
/* Map x, y and z to range 0-1 */
_out.X = _out.X * 0.5f + 0.5f;
_out.Y = -_out.Y * 0.5f + 0.5f;
_out.Z = _out.Z * 0.5f + 0.5f;
/* Map x,y to viewport */
_out.X = _out.X * viewport[2] + viewport[0];
_out.Y = _out.Y * viewport[3] + viewport[1];
screenPos.X = _out.X;
screenPos.Y = _out.Y;
screenPos.Z = _out.Z;
return true;
}
I cannot see any errors though... :S
In the first question you're missing the last step: Mapping from NDC (Normalized Device Coordinates) to viewport coordinates. That's what the lines
/* Map x,y to viewport */
_out.X = _out.X * viewport[2] + viewport[0];
_out.Y = _out.Y * viewport[3] + viewport[1];
in your GluProject do,
You have two options. You can calculate it yourself, or use the glProject function. I prefer the first.
Number 1:
private Vector2 Convert(
Vector3 pos,
Matrix4 viewMatrix,
Matrix4 projectionMatrix,
int screenWidth,
int screenHeight)
{
pos = Vector3.Transform(pos, viewMatrix);
pos = Vector3.Transform(pos, projectionMatrix);
pos.X /= pos.Z;
pos.Y /= pos.Z;
pos.X = (pos.X + 1) * screenWidth / 2;
pos.Y = (pos.Y + 1) * screenHeight / 2;
return new Vector2(pos.X, pos.Y);
}
Number 2:
public Vector2 form3Dto2D(Vector3 our3DPoint)
{
Vector3 our2DPoint;
float[] modelviewMatrix = new float[16];
float[] projectionMatrix = new float[16];
int[] viewport = new int[4];
GL.GetFloat(GetPName.ModelviewMatrix, modelviewMatrix);
GL.GetFloat(GetPName.ProjectionMatrix, projectionMatrix);
GL.GetInteger(GetPName.Viewport, viewport);
OpenTK.Graphics.Glu.Project(our3DPoint, convertFloatsToDoubles(modelviewMatrix),
convertFloatsToDoubles(projectionMatrix), viewport, out our2DPoint);
return new Vector2(our2DPoint.X, our2DPoint.Y)
}
public static double[] convertFloatsToDoubles(float[] input)
{
if (input == null)
{
return null; // Or throw an exception - your choice
}
double[] output = new double[input.Length];
for (int i = 0; i < input.Length; i++)
{
output[i] = input[i];
}
return output;
}