The glm::intersect functions do not say how to use them in their documentation and even when i have this I don’t know how to get the point of intersection.
I am only doing walls right now because they are easy to understand;
About the code that I am using:
direction = 0 forward is +Z
direction = 1 forward is +X
direction = 2 forward is -Z
direction = 3 forward is -X
the groups of 6 vertices are the vertices that make up my walls;
RenderInfo.loc has the physical location of the wall. camx and camz are the camera location.
struct NormalVertex {
glm::vec3 position;
glm::vec3 normal;
glm::vec2 texCoord;
float wallType;
glm::vec3 tangent;
glm::vec3 bitangent;
};
struct RenderInfo {
glm::vec3 loc;
NormalVertex verts[6];
};
void RayTrace::trace()
{
depth = 2;
bool renok = false;
for (int i = 0; i < relinfo.size(); i++) {
switch (direction) {
case 0:
if (relinfo.at(i).loc.z - camz >= 0) renok = true;
break;
case 1:
if (relinfo.at(i).loc.x - camx >= 0) renok = true;
break;
case 2:
if (relinfo.at(i).loc.z - camz <= 0) renok = true;
break;
case 3:
if (relinfo.at(i).loc.x - camx <= 0) renok = true;
break;
}
if (renok) {
float lowx = 20000;
float lowy = 20000;
float lowz = 20000;
float higx = -20000;
float higy = -20000;
float higz = -20000;
glm::vec4 location;
for (int j = 0; j < 6; j++) {
location = projection * view * glm::vec4(relinfo.at(i).verts[j].position, 1.0f);
if (location.x < lowx) lowx = location.x;
if (location.y < lowy) lowy = location.y;
if (location.x > higx) higx = location.x;
if (location.y > higy) higy = location.y;
if (location.z / location.w < lowz) lowz = location.z / location.w;
if (location.z / location.w > higz) higz = location.z / location.w;
}
float ztr;
if (cursx > lowx && cursx < higx && cursy > lowy && cursy < higy) {
glm::vec3 anormal = relinfo.at(i).verts[0].normal;
if (abs(anormal.x) > .5) {
float wid = higx - lowx;
ztr = (cursx - lowx) / wid;
normType = 1;
}
else if (abs(anormal.y) > .5) {
float wid = higy - lowy;
ztr = (cursy - lowy) / wid;
normType = 2;
}
else {
ztr = 0;
}
float wid = higz - lowz;
float idep = lowz + wid * ztr;
if (idep < depth) {
selNum = i;
zam = ztr;
wid = higx - lowx;
xam = (cursx - lowx) / wid;
wid = higy - lowy;
yam = (cursy - lowy) / wid;
}
}
}
}
//int dirx[] = {0, 1, 0, -1};
//int dirz[] = { 1, 0, -1, 0 };
if (selNum > -1) {
error = false;
glm::vec3 campos = glm::vec3(camx, 0, camz);
float lowx = 20000;
float lowy = 20000;
float lowz = 20000;
float higx = -20000;
float higy = -20000;
float higz = -20000;
glm::vec3 location;
for (int j = 0; j < 6; j++) {
location = relinfo.at(selNum).verts[j].position - campos;
if (location.x < lowx) lowx = location.x;
if (location.y < lowy) lowy = location.y;
if (location.x > higx) higx = location.x;
if (location.y > higy) higy = location.y;
if (location.z < lowz) lowz = location.z;
if (location.z > higz) higz = location.z;
}
float xloc;
float yloc = lowy + (higy - lowy) * yam;
float zloc;
switch (direction) {
case 0:
xloc = lowx + (higx - lowx) * -xam;
zloc = lowz + (higz - lowz) * zam;
break;
case 1:
xloc = lowx + (higx - lowx) * zam;
zloc = lowz + (higz - lowz) * xam;
break;
case 2:
xloc = lowx + (higx - lowx) * -xam;
zloc = lowz + (higz - lowz) * -zam;
break;
case 3:
xloc = lowx + (higx - lowx) * -zam;
zloc = lowz + (higz - lowz) * -xam;
break;
}
movement = glm::normalize(glm::vec3(xloc, yloc, zloc) - campos);
}
else {
error = true;
}
}