# ChobitsTheZero

Member

6

117 Neutral

• Rank
Newbie

• Interests
Programming
1. ## RayCasting - sphere, plane and OBB

I have a problem understanding how excatly the intersection test when testing ray against plane, sphere and OBB. I have a presentation of an assignment and I need to be able to explain how the code I have written works. But I dont really understand it, even though I have searched over the internet and read through a lot of sites telling how it works. I still dont really understand it, im hoping that someone can give me a dummy explaination of it that is easy to understand. I'll provide the code in question down below, all help is appreciated. I just really wanna understand how this stuff works fully. void GiantSphere::test(Ray & ray, HitData & hit) { float b = ray.d.Dot(ray.o - this->center); float j = (ray.o - this->center).Dot(ray.o - this->center) - pow(this->radius,2); float intersectCalc = b*b - j; if (intersectCalc >= 0) { float t = -b - sqrt(intersectCalc); if (hit.t == -1 || hit.t > t) { hit.t = t; hit.lastShape = this; hit.color = this->c; hit.lastNormal = this->normal(ray.o+ray.d*t); } } } Vec GiantSphere::normal(Vec & point) { Vec temp = point - this->center; temp.Normalize(); return temp; } GiantSphere::GiantSphere(Vec _center, float _radius, Color _color) { this->center = _center; this->radius = _radius; this->c = _color; } void GiantPlane::test(Ray & ray, HitData & hit) { float d = this->n.Dot(this->point); float t = (d - this->n.Dot(ray.o)) / this->n.Dot(ray.d); if (t > 0.0001f) { if (hit.t == -1 || hit.t > t) { hit.t = t; hit.lastShape = this; hit.color = this->c; hit.lastNormal = this->n; } } } Vec GiantPlane::normal(Vec & point) { point = n; return point; } GiantPlane::GiantPlane(Vec normal, float _d, Color color) { this->n = normal; //this->d = _d; this->point = this->n*_d; this->c = color; } void GiantOBB::test(Ray & ray, HitData & hit) { float tMin = -INFINITY; float tMax = +INFINITY; Vec p = Bcenter - ray.o; // Bcenter is the center of the OBB. Ray.o is the origin of the ray. Vec arr[3] = { Bu,Bv,Bw }; // Direction/base vectors. (1,0,0), (0,1,0), (0,0,1) for (auto& i : arr) { float e = i.Dot(p); float f = i.Dot(ray.d); float q = -e - halfBu; // The distance between the center and each side. 100. float w = -e + halfBu; if (abs(f) > 1e-20f) { float t1 = ((e + halfBu) / f); float t2 = ((e - halfBu) / f); if (t1 > t2) { std::swap(t1, t2); } if (t1 > tMin) { tMin = t1; } if (t2 < tMax) { tMax = t2; } if (tMin > tMax) { return; } if (tMax < 0) { return; } } else if (q > 0 || w < 0) { return; } } if (tMin > 0) { if (hit.t == -1) { hit.t = tMin; hit.lastShape = this; hit.color = c; } else if (tMin < hit.t) { hit.t = tMin; hit.lastShape = this; hit.color = c; } } else if (tMax <= 0) { if (hit.t == -1) { hit.t = tMax; hit.lastShape = this; hit.color = c; } else if (tMax < hit.t) { hit.t = tMax; hit.lastShape = this; hit.color = c; } } } Vec GiantOBB::normal(Vec & point) { Vec arr[3] = { this->Bu,this->Bv,this->Bw }; float halfArr[3] = { this->halfBu,this->halfBv,this->halfBw }; Vec returnValue = Vec(); for (int i = 0; i < 3; i++) { Vec sPlus = this->Bcenter + (arr[i] * halfArr[i]); Vec sMinus = this->Bcenter - (arr[i] * halfArr[i]); if ((point - sPlus).Dot(arr[i]) < 0.0001f && (point - sPlus).Dot(arr[i]) > -0.0001f) { float dot = (point - sPlus).Dot(arr[i]); returnValue = arr[i]; } else if ((point - sMinus).Dot(arr[i] * -1) < 0.0001f && (point - sMinus).Dot(arr[i] * -1) > -0.0001f) { float dot2 = (point - sMinus).Dot(arr[i]); returnValue = arr[i] * -1; } } return returnValue; } GiantOBB::GiantOBB(Vec b, Vec b1, Vec b2, Vec b3, float Hu, float Hv, float Hw, Color _color) { this->Bcenter = b; this->Bu = b1; this->Bv = b2; this->Bw = b3; this->c = _color; this->halfBu = Hu; this->halfBv = Hv; this->halfBw = Hw; } GiantOBB::GiantOBB(Vec b, float Hu, float Hv, float Hw, Color _color) { this->Bcenter = b; this->halfBu = Hu; this->halfBv = Hv; this->halfBw = Hw; this->c = _color; }

Okey so, I did what you said but it seems that there is also another problem which I can't really figure out what it is. So now there are shadows, but they dont seem to be at the correct position. Instead of being behind the models they are instead infront of them and slightly above them. I was thinking that maybe I had to multiple the vec4(vertexPos, 1) with model, but that also gave a weird result. #Edit: Nevermind I was able to solve it, appearantly the lightspacematrix had some issues with it.

I see! That really helps! I have kinda been short on time and really stressed due to exams so I basically has to rush through it all. But I shouldn't try to make excuses, I guess i'm just bad at understanding how excatly 3D programming works.

5. ## Heightmap finding closest vertices

How would I determine which triangle of the quad the camera is in?
6. ## Heightmap finding closest vertices

So I have rendered a terrain based on a heightmap. It's based on this tutorial that I followed - http://www.mbsoftworks.sk/index.php?page=tutorials&series=1&tutorial=8   Now I want to have my camera to be able to "walk" on the terrain. The getY() function gets the x and z coordinates of the camera, and I understand that with those I have to find the closest vertices of the terrain to decide which triangle the camera is over. The thing is i'm unsure of how to actually do that, I have looked for a lot of examples on google but haven't been able to find any concrete answers for it. Currently my code looks like this: HM_SIZE_X and HM_SIZE_Y are both 6       float Terrain::getY(int x, int z)     {          //return     }          void Terrain::createTerrain()     {     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);          // Setup heightmap          glGenVertexArrays(1, &uiVAOHeightmap); // Create one VAO     glGenBuffers(1, &uiVBOHeightmapData); // One VBO for data     glGenBuffers(1, &uiVBOIndices); // And finally one VBO for indices          glBindVertexArray(uiVAOHeightmap);     glBindBuffer(GL_ARRAY_BUFFER, uiVBOHeightmapData);          float fHeights[HM_SIZE_X*HM_SIZE_Y] =     {     10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f,     5.0f, 5.0f,5.0f, 10.0f, 10.0f, 10.0f,     5.0f, 5.0f, 5.0f, 10.0f, 10.0f, 10.0f,     5.0f, 5.0f, 10.0f, 10.0f, 10.0f, 10.0f,     10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f,     10.0f, 5.0f, 5.0f, 5.0f, 5.0f, 10.0f,     };          float fSizeX = 100.0f, fSizeZ = 100.0f;          for (int i = 0; i<HM_SIZE_X*HM_SIZE_Y;i++)     {     for (int j = 0; j < HM_SIZE_X*HM_SIZE_Y; j++)     {     float column = float(i%HM_SIZE_X), row = float(i / HM_SIZE_X);     vHeightmapData = glm::vec3(     -fSizeX / 2 + fSizeX*column / float(HM_SIZE_X - 1), // X Coordinate     fHeights, // Y Coordinate (it's height)     -fSizeZ / 2 + fSizeZ*row / float(HM_SIZE_Y - 1) // Z Coordinate     );     }     }          glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3)*HM_SIZE_X*HM_SIZE_Y, vHeightmapData, GL_STATIC_DRAW);     glEnableVertexAttribArray(0);     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);          glGenBuffers(1, &uiVBOIndices);     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, uiVBOIndices);     int iIndices[] =     {     0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5, 11, 36,     6, 12, 7, 13, 8, 14, 9, 15, 10, 16, 11, 17, 36,     12, 18, 13, 19, 14, 20, 15, 21, 16, 22, 17, 23, 36,     18, 24, 19, 25, 20, 26, 21, 27, 22, 28, 23, 29, 36,     24, 30, 25, 31, 26, 32, 27, 33, 28, 34, 29, 35     };     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(iIndices), iIndices, GL_STATIC_DRAW);     glEnable(GL_PRIMITIVE_RESTART);     glPrimitiveRestartIndex(HM_SIZE_X*HM_SIZE_Y);     }