HeightFIeld divided into parts - PhysX - c++

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.

Related

Raytracing program fails to detect intersections in c++

I am working on a simple raytracer in c++. I am currently implementing an intersection function but have encountered some issues.
For some reason, the collision detection only works for a tiny rectangle in my image. In the image below you can see that it draws the room quite fine for a small part of the screen but fails to do so for the rest of the scene. Only a small section gets drawn correctly.
Why does my intersection detection not work? I have included the code for the intersection and draw function below.
LoadTestModel(m_Model);
m_Light.position = glm::vec3(0.0f, -1.0f, 0.0);
m_Light.color = glm::vec3(0.f, 0.f, 0.f);
m_Light.ambient = glm::vec3(0.5f, 0.5f, 0.5f);
m_Camera.position = glm::vec3(0.0, 0.0, -2.0);
m_Camera.yaw = 0.0f;
}
void Lab2Scene::Draw(Window& window)
{
if (!m_RenderNext) return;
m_RenderNext = false;
for (uint32_t y = 0; y < window.GetHeight(); ++y)
{
for (uint32_t x = 0; x < window.GetWidth(); ++x)
{
Ray ray = {};
glm::vec3 d(x - (window.GetWidth() / 2), y - (window.GetHeight() / 2), (window.GetHeight() / 2));
d = glm::normalize(d);
ray.direction = d * m_Camera.GetRotationY();
ray.start = m_Camera.position;
// Find the closest intersection of the casted ray.
Intersection nearest_intersection = {};
if (ClosestIntersection(ray, m_Model, nearest_intersection))
{
//window.PutPixel(x, y, glm::vec3(1.f, 0.f, 0.f));
window.PutPixel(x, y, DirectLight(m_Light, nearest_intersection, m_Model) + m_Model[nearest_intersection.triangleIndex].color * m_Light.ambient); // DirectLight(m_Light, intersection, m_Model)
}
else
{
window.PutPixel(x, y, m_Light.color);
}
}
}
}
bool Lab2Scene::ClosestIntersection(const Ray& ray, const std::vector<Triangle>& triangles, Intersection& intersection)
{
float m = std::numeric_limits<float>::max();
intersection.distance = m;
bool inters = false;
for (int i = 0; i < triangles.size(); ++i) {
float dot = glm::dot(ray.direction, triangles[i].normal);
if (dot != 0) {
using glm::vec3;
using glm::mat3;
vec3 v0 = triangles[i].v0;
vec3 v1 = triangles[i].v1;
vec3 v2 = triangles[i].v2;
vec3 e1 = v1 - v0;
vec3 e2 = v2 - v0;
vec3 b = ray.start - v0;
mat3 A(-ray.direction, e1, e2);
vec3 x = glm::inverse(A) * b;
if (x[1] >= 0 && x[2] >= 0 && x[1] + x[2] <= 1 && x[0] >= 0) {
vec3 intersect = ray.start + (x[0] * ray.direction);
if (glm::distance(ray.start, intersect) <= intersection.distance) {
intersection.position = intersect;
intersection.distance = glm::distance(ray.start, intersect);
intersection.triangleIndex = i;
inters = true;
}
}
}
}
return inters;
}

OpenGL + Cloth simulation physics isn't working

I am attempting to implement a cloth simulation using a spring-particle system but something isn't quite right with my physics. When I run the simulation the cloth draws as expected but over time the force of gravity pulls the cloth downward indefinitely. In other words the forces caused by the springs are not accumulating properly to overcome the downward pull of gravity and I end up with this...:
It continues to droop downwards indefinitely. From all of the debugging I have done what I have seen is that the accumulation of forces on a particle caused by all of the springs attached to it is not properly summing when the force of gravity causes increased stretch. I can not figure out what I have overlooked in my physics.
My cloth updates every time step using this function
void Cloth::updateGeometry(atlas::utils::Time const& t) {
for (int i = 0; i < mSprings.size(); ++i) {
mSprings[i].calculateForces();
}
for (int i = 0; i < mParticles.size(); ++i) {
mParticles[i].updateGeometry(t);
}
}
My springs update using the below function where p1 and p2 are pointers to each particle that this spring is attached to.
void Spring::calculateForces() {
glm::vec3 springVector = normalize((p2->getCurrentPosition() - p1->getCurrentPosition()));
GLfloat stretchLength = length(p2->getCurrentPosition(), p1->getCurrentPosition());
GLfloat displacementFromRest = restLength - stretchLength;
glm::vec3 springForce = -k * displacementFromRest * normalize(springVector);
//Multiply the displacements by the spring constant to get
//A vector which represents the force on each spring
p1->addToSumOfSpringForces(springForce);
p2->addToSumOfSpringForces(-springForce);
}
Finally my particles update using.
void Particle::updateGeometry(atlas::utils::Time const& t) {
if (!stationary) {
previousPosition = currentPosition;
glm::vec3 forceOfGravity = mass * gravity;
glm::vec3 totalForce = forceOfGravity + (mass * totalSpringForces) - velocity*Damping;
acceleration = totalForce / mass;
//Perform Euler Integration
currentPosition += t.deltaTime * velocity;
velocity += t.deltaTime * acceleration;
//============== End Euler==============//
//Reset the forces acting on the particle from all of the springs
//So that a new accumulated total can be calculated.
totalSpringForces = glm::vec3{ 0.0f, 0.0f, 0.0f };
}
}
The totalSpringForces variable is updated by the call to addToSumOfSpringForces(springForce); in the spring update function. The idea is that each spring is evaluated first according to the current position of each particle then each particle's totalSpringForcevariable is accumulated each iteration using
void Particle::addToSumOfSpringForces(glm::vec3 force) {
totalSpringForces += force;
}
Just to add clarification the cloth is constructed using structural, bend and shear springs in accordance with this description. This may be unneccesary but I've included my cloth constructor below.
Cloth::Cloth(GLfloat width_, GLfloat height_, GLuint numParticlesWide_, GLuint numParticlesHigh_) :
width(width_),
height(height_),
numParticlesHigh(numParticlesHigh_),
numParticlesWide(numParticlesWide_),
clothRotationVector{0.0f, 0.0f, 0.0f},
clothPosition{ 0.0f, 5.0f, 0.0f },
clothRotationAngle(0.0f)
{
USING_ATLAS_MATH_NS;
USING_ATLAS_GL_NS;
glm::vec3 clothColour{1.0f, 0.5f, 0.2f};
//Create Particles
GLuint count = 0;
restLength = (width * (1 / (float)numParticlesWide));
for (GLuint y = 0; y < numParticlesHigh; ++y) {
for (GLuint x = 0; x < numParticlesWide; ++x) {
glm::vec3 pos = {(width * (x / (float)numParticlesWide)), (-height * (y / (float)numParticlesHigh)), 0.0f};
mParticles.push_back(Particle(pos, count, clothColour));
++count;
}
}
//Create Springs
for (GLuint x = 0; x < numParticlesWide; ++x) {
for (GLuint y = 0; y < numParticlesHigh; ++y) {
//============ Structural springs ==========//
//Connect to the particle to the immediate right of the current particle
if (x < numParticlesWide - 1) mSprings.push_back(Spring(getParticle(x,y), getParticle(x+1,y)));
//Connect to the particle that is immediately below the current particle
if (y < numParticlesHigh - 1) mSprings.push_back(Spring(getParticle(x,y), getParticle(x,y+1)));
//============ Shear Springs ================//
//Connect the shear springs to make the X pattern
if (x < numParticlesWide - 1 && y < numParticlesHigh - 1) {
mSprings.push_back(Spring(getParticle(x, y), getParticle(x + 1, y + 1)));
mSprings.push_back(Spring(getParticle(x+1, y), getParticle(x, y+1)));
}
//============ Bend Springs ===============//
//Connect the current particle to the second particle over to the right
if (x < numParticlesWide - 2) mSprings.push_back(Spring(getParticle(x,y), getParticle(x+2,y)));
//Connect the current particle to the particle two below
if (y < numParticlesHigh - 2) mSprings.push_back(Spring(getParticle(x,y), getParticle(x, y+2)));
////Create the X pattern
//if (x < numParticlesWide - 2 && y < numParticlesHigh - 2) {
// mSprings.push_back(Spring(getParticle(x, y), getParticle(x+2,y+2)));
// mSprings.push_back(Spring(getParticle(x+2,y), getParticle(x,y+2)));
//};
}
}
//Set the top left and right as stationary
getParticle(0, 0)->makeStationary();
getParticle(numParticlesWide - 1, 0)->makeStationary();
//Make Indices for Particles
for (GLuint row = 0; row < numParticlesWide - 1; ++row) {
for (GLuint col = 0; col < numParticlesHigh - 1; ++col) {
//Triangle one
mParticleIndices.push_back(getParticle(row,col)->getIndex());
mParticleIndices.push_back(getParticle(row,col+1)->getIndex());
mParticleIndices.push_back(getParticle(row+1, col)->getIndex());
//Triangle two
mParticleIndices.push_back(getParticle(row, col+1)->getIndex());
mParticleIndices.push_back(getParticle(row+1, col+1)->getIndex());
mParticleIndices.push_back(getParticle(row+1, col)->getIndex());
}
}
glGenBuffers(1, &clothVertexBufferID);
glGenBuffers(1, &clothIndexBufferID);
sendDataToGPU();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, clothIndexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mParticleIndices.size() * sizeof(GLushort), &mParticleIndices[0], GL_STATIC_DRAW);
defineVAO();
std::string shaderDir = generated::ShaderPaths::getShaderDirectory();
std::vector<ShaderInfo> shaders
{
{ GL_VERTEX_SHADER, shaderDir + "Cloth.vs.glsl" },
{ GL_FRAGMENT_SHADER, shaderDir + "Cloth.fs.glsl" }
};
mModel = glm::translate(Matrix4(1.0f), clothPosition);
mShaders.push_back(ShaderPointer(new Shader));
mShaders[0]->compileShaders(shaders);
mShaders[0]->linkShaders();
mUniforms.insert(UniformKey("mvpMat",mShaders[0]->getUniformVariable("mvpMat")));
mShaders[0]->disableShaders();
}
EDIT
I have verified that the totalSpringForcesvariable is indeed changing. I added a print statement in my Particle update function which you can see in the image below. The selected the totalSpringForces for only particle #55 which in this case is the particle immediately under particle 0 which is stationary and not allowed to move. Also the printout is after about 20-25 iterations. As you can see the totalSpringForcesin the y direction has positive value of 0.7037 (i.e. is counteracting gravity). I let it run for half an hour and it only got to 5.
Currently my constants are
k = 2.0f
mass = 0.1f
damping = 0.55f
gravity{ 0.0f, -9.81f, 0.0f },

calculating vertex normals in opengl with c++

could anyone please help me calculating vertex normals in OpenGL?
I am loading an obj file and adding Gouraud shading by calculating vertex normals without using glNormal3f or glLight functions..
I have declared functions like operators, crossproduct, innerproduct,and etc..
I have understood that in order to get vertex normals, I first need to calculate surface normal aka normal vector with crossproduct.. and also
since I am loading an obj file.. and I am placing the three points of Faces of the obj file in id1,id2,id3 something like that
I would be grateful if anyone can help me writing codes or give me a guideline how to start the codes. please ...
thanks..
its to draw
FACE cur_face = cube.face[i];
glColor3f(cube.vertex_color[cur_face.id1].x,cube.vertex_color[cur_face.id1].y,cube.vertex_color[cur_face.id1].z);
glVertex3f(cube.vertex[cur_face.id1].x,cube.vertex[cur_face.id1].y,cube.vertex[cur_face.id1].z);
glColor3f(cube.vertex_color[cur_face.id2].x,cube.vertex_color[cur_face.id2].y,cube.vertex_color[cur_face.id2].z);
glVertex3f(cube.vertex[cur_face.id2].x,cube.vertex[cur_face.id2].y,cube.vertex[cur_face.id2].z);
glColor3f(cube.vertex_color[cur_face.id3].x,cube.vertex_color[cur_face.id3].y,cube.vertex_color[cur_face.id3].z);
glVertex3f(cube.vertex[cur_face.id3].x,cube.vertex[cur_face.id3].y,cube.vertex[cur_face.id3].z);
}
This is the equation for color calculation
VECTOR kd;
VECTOR ks;
kd=VECTOR(0.8, 0.8, 0.8);
ks=VECTOR(1.0, 0.0, 0.0);
double inner = kd.InnerProduct(ks);
int i, j;
for(i=0;i<cube.vertex.size();i++)
{
VECTOR n = cube.vertex_normal[i];
VECTOR l = VECTOR(100,100,0) - cube.vertex[i];
VECTOR v = VECTOR(0,0,1) - cube.vertex[i];
float xl = n.InnerProduct(l)/n.Magnitude();
VECTOR x = (n * (1.0/ n.Magnitude())) * xl;
VECTOR r = x - (l-x);
VECTOR color = kd * (n.InnerProduct(l)) + ks * pow((v.InnerProduct(r)),10);
cube.vertex_color[i] = color;
*This answer is for triangular mesh and can be extended to poly mesh as well.
tempVertices stores list of all vertices.
vertexIndices stores details of faces(triangles) of the mesh in a vector (in a flat manner).
std::vector<glm::vec3> v_normal;
// initialize vertex normals to 0
for (int i = 0; i != tempVertices.size(); i++)
{
v_normal.push_back(glm::vec3(0.0f, 0.0f, 0.0f));
}
// For each face calculate normals and append to the corresponding vertices of the face
for (unsigned int i = 0; i < vertexIndices.size(); i += 3)
{
//vi v(i+1) v(i+2) are the three faces of a triangle
glm::vec3 A = tempVertices[vertexIndices[i] - 1];
glm::vec3 B = tempVertices[vertexIndices[i + 1] - 1];
glm::vec3 C = tempVertices[vertexIndices[i + 2] - 1];
glm::vec3 AB = B - A;
glm::vec3 AC = C - A;
glm::vec3 ABxAC = glm::cross(AB, AC);
v_normal[vertexIndices[i] - 1] += ABxAC;
v_normal[vertexIndices[i + 1] - 1] += ABxAC;
v_normal[vertexIndices[i + 2] - 1] += ABxAC;
}
Now normalize each v_normal and use.
Note that the number of vertex normals is equal to the number of vertices of the mesh.
This code works fine on my machine
glm::vec3 computeFaceNormal(glm::vec3 p1, glm::vec3 p2, glm::vec3 p3) {
// Uses p2 as a new origin for p1,p3
auto a = p3 - p2;
auto b = p1 - p2;
// Compute the cross product a X b to get the face normal
return glm::normalize(glm::cross(a, b));
}
void Mesh::calculateNormals() {
this->normals = std::vector<glm::vec3>(this->vertices.size());
// For each face calculate normals and append it
// to the corresponding vertices of the face
for (unsigned int i = 0; i < this->indices.size(); i += 3) {
glm::vec3 A = this->vertices[this->indices[i]];
glm::vec3 B = this->vertices[this->indices[i + 1LL]];
glm::vec3 C = this->vertices[this->indices[i + 2LL]];
glm::vec3 normal = computeFaceNormal(A, B, C);
this->normals[this->indices[i]] += normal;
this->normals[this->indices[i + 1LL]] += normal;
this->normals[this->indices[i + 2LL]] += normal;
}
// Normalize each normal
for (unsigned int i = 0; i < this->normals.size(); i++)
this->normals[i] = glm::normalize(this->normals[i]);
}
It seems all you need to implement is the function to get the average vector from N vectors. This is one of the ways to do it:
struct Vector3f {
float x, y, z;
};
typedef struct Vector3f Vector3f;
Vector3f averageVector(Vector3f *vectors, int count) {
Vector3f toReturn;
toReturn.x = .0f;
toReturn.y = .0f;
toReturn.z = .0f;
// sum all the vectors
for(int i=0; i<count; i++) {
Vector3f toAdd = vectors[i];
toReturn.x += toAdd.x;
toReturn.y += toAdd.y;
toReturn.z += toAdd.z;
}
// divide with number of vectors
// TODO: check (count == 0)
float scale = 1.0f/count;
toReturn.x *= scale;
toReturn.y *= scale;
toReturn.z *= scale;
return toReturn;
}
I am sure you can port that to your C++ class. The result should then be normalized unless the length iz zero.
Find all surface normals for every vertex you have. Then use the averageVector and normalize the result to get the smooth normals you are looking for.
Still as already mentioned you should know that this is not appropriate for edged parts of the shape. In those cases you should use the surface vectors directly. You would probably be able to solve most of such cases by simply ignoring a surface normal(s) that are too different from the others. Extremely edgy shapes like cube for instance will be impossible with this procedure. What you would get for instance is:
{
1.0f, .0f, .0f,
.0f, 1.0f, .0f,
.0f, .0f, 1.0f
}
With the normalized average of {.58f, .58f, .58f}. The result would pretty much be an extremely low resolution sphere rather then a cube.

How to draw a tapered arc (curve with decreasing thickness) in OpenGL?

I have the following code to draw an arbitrary arc:
void CenteredArc::drawPolygonArc(float radius, float thickness, float startAngle, float arcAngle) {
float num_segments = 360.0;
float radiusOuter = radius + thickness / 2;
float radiusInner = radius - thickness / 2;
float theta = arcAngle / num_segments;
float tangetial_factor = tanf(theta);//calculate the tangential factor
float radial_factor = cosf(theta);//calculate the radial factor
float xxOuter = radiusOuter * cosf(startAngle);
float yyOuter = radiusOuter * sinf(startAngle);
float xxInner = radiusInner * cosf(startAngle);
float yyInner = radiusInner * sinf(startAngle);
float prevXXOuter = -1;
float prevYYOuter = -1;
float prevXXInner = -1;
float prevYYInner = -1;
glPolygonMode(GL_FRONT, GL_FILL);
for(int ii = 0; ii < num_segments; ii++)
{
if (prevXXOuter != -1) {
glBegin(GL_POLYGON);
glVertex2f(prevXXOuter, prevYYOuter);
glVertex2f(xxOuter, yyOuter);
glVertex2f(xxInner, yyInner);
glVertex2f(prevXXInner, prevYYInner);
glEnd();
}
//calculate the tangential vector
//remember, the radial vector is (x, y)
//to get the tangential vector we flip those coordinates and negate one of them
float txOuter = -yyOuter;
float tyOuter = xxOuter;
float txInner = -yyInner;
float tyInner = xxInner;
//add the tangential vector
prevXXOuter = xxOuter;
prevYYOuter = yyOuter;
prevXXInner = xxInner;
prevYYInner = yyInner;
xxOuter += txOuter * tangetial_factor;
yyOuter += tyOuter * tangetial_factor;
xxInner += txInner * tangetial_factor;
yyInner += tyInner * tangetial_factor;
//correct using the radial factor
xxOuter *= radial_factor;
yyOuter *= radial_factor;
xxInner *= radial_factor;
yyInner *= radial_factor;
}
}
However, I would like for the arc to start off with the specified thickness on one end and gradually decrease to a thickness of zero on the other end. Any suggestions?
Edit: I am not using GL_LINE_STRIP because I am trying to avoid having overlapping lines and gaps like so:
I would use a line strip with decreasing glLineWidth.
This is my implementation, it doesn't gradially reduce the lineWidth but it could be modified to do so. Sorry for the extra stuff, it's from my game engine.
for(int i=0;i<arcs().size();i++)
{
Entities::Arc temp = arcs().at(i);
glLineWidth(temp.LW.value); // change LWidth
glColor3f( temp.CL.R, temp.CL.G, temp.CL.B );
// theta is now calculated from the arc angle instead, the
// - 1 part comes from the fact that the arc is open
float theta = temp.A.value*DEG2RAD / float(WW_SPLINE_ACCURACY - 1);
float tan = tanf(theta);
float cos = cosf(theta);
// we are now at the starting angle
double x = temp.R.value * cosf(temp.A.value*DEG2RAD);
double y = temp.R.value * sinf(temp.A.value*DEG2RAD);
// since the arc is not a closed curve, this is a strip now
glBegin(GL_LINE_STRIP);
for(int ii = 0; ii < WW_SPLINE_ACCURACY; ii++)
{
glVertex2d(x + temp.C.X, y + temp.C.Y);
double tx = -y;
double ty = x;
x += tx * tan;
y += ty * tan;
x *= cos;
y *= cos; //y = ( y + (ty*tan) )*cos;
}
glEnd();
glLineWidth(WW_DEFAULT_LWIDTH); // reset LWidth
}
I also used these values
#define WW_SPLINE_ACCURACY 72 // 72 for extra smooth arcs/circles, 32 minimum
#define WW_BEZIER_ACCURACY 20
/* Math stuff */
#define DEG2RAD 3.14159/180
#define PI 3.1415926535897932384626433832795;
...
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glEnable (GL_LINE_SMOOTH);
glEnable (GL_BLEND);
//glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
glEnable(GL_POLYGON_SMOOTH);
glClearColor(0.188f, 0.169f, 0.329f, 1.0f); //#302b54
I'm not allowed to release the full source since I wrote it for a company but sharing a part or two wont hurt anybody :D

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;
}