Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411579 Posts in 69386 Topics- by 58445 Members - Latest Member: Mansreign

May 05, 2024, 04:13:25 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Finding visible area of OpenGL "world" using OpenGL and glut
Pages: 1 [2]
Print
Author Topic: Finding visible area of OpenGL "world" using OpenGL and glut  (Read 6647 times)
Tycho Brahe
Level 10
*****

λx.x


View Profile
« Reply #20 on: August 04, 2009, 12:56:50 PM »

or... at least, thats the problem. It still doesnt work... OH WELL

[edit]

tbh, I don't think this is ever going to work.

whatever I do, the difference between the values is MINUSCULE regardless of values fed in, I may give up on this.
« Last Edit: August 04, 2009, 01:05:49 PM by MrHackenbacker » Logged
Zaphos
Guest
« Reply #21 on: August 04, 2009, 01:07:51 PM »

What value are you using for winZ now?  Not sure what it should actually be; might want to gluProject() a point on your plane (eg 0,0,0) just to see what the winZ should be to be on the plane.
Logged
Ivan
Owl Country
Level 10
*


alright, let's see what we can see


View Profile
« Reply #22 on: August 04, 2009, 01:16:25 PM »

This is really ugly, since my matrix class is kind of messed up, but hope it helps.

Here's my 2D coordinate test method (tests a 2d coordinate against a polygon).

The matrix passed to it is the full transform matrix of the polygon's object multiplied by the inverse of the current camera matrix.

Code:

bool OpenGLRenderer::test2DCoordinate(float x, float y, Tau::Polygon *poly, const Matrix4 &matrix) {
GLdouble nearPlane[3],farPlane[3];

GLdouble mv[16];
Matrix4 camInverse = cameraMatrix.inverse();
Matrix4 cmv;
cmv.identity();
cmv = cmv * camInverse;

for(int i=0; i < 16; i++) {
mv[i] = cmv.ml[i];
}

GLint vp[4];
glGetIntegerv( GL_VIEWPORT, vp );

gluUnProject(x, yRes - y, 0.0, mv, sceneProjectionMatrix, vp,  &nearPlane[0], &nearPlane[1], &nearPlane[2]);
gluUnProject(x, yRes - y, 1.0, mv, sceneProjectionMatrix, vp,  &farPlane[0], &farPlane[1], &farPlane[2]);

Vector3 nearVec(nearPlane[0], nearPlane[1], nearPlane[2]);
Vector3 farVec(farPlane[0], farPlane[1], farPlane[2]);

Vector3 dirVec = farVec - nearVec;
dirVec.Normalize();

Vector3 hitPoint;

Matrix4 fullMatrix = matrix;

if(poly->getVertexCount() == 3) {
return rayTriangleIntersect(Vector3(0,0,0), dirVec, fullMatrix * (*poly->getVertex(0)), fullMatrix  * (*poly->getVertex(1)), fullMatrix *  (*poly->getVertex(2)), &hitPoint);
} else if(poly->getVertexCount() == 4) {
return (rayTriangleIntersect(Vector3(0,0,0), dirVec, fullMatrix * (*poly->getVertex(2)), fullMatrix  * (*poly->getVertex(1)), fullMatrix *  (*poly->getVertex(0)), &hitPoint) ||
rayTriangleIntersect(Vector3(0,0,0), dirVec, fullMatrix * (*poly->getVertex(0)), fullMatrix  * (*poly->getVertex(3)), fullMatrix *  (*poly->getVertex(2)), &hitPoint));
} else {
return false;
}
}

Here's the ray triangle intersect method for good measure:

Code:
bool Renderer::rayTriangleIntersect(Vector3 ray_origin, Vector3 ray_direction, Vector3 vert0, Vector3 vert1, Vector3 vert2, Vector3 *hitPoint)
{
float t,u,v;
t = 0; u = 0; v = 0;

Vector3 edge1 = vert1 - vert0;
Vector3 edge2 = vert2 - vert0;

Vector3 tvec, pvec, qvec;
float det, inv_det;


pvec = ray_direction.crossProduct(edge2);
det = edge1.dot(pvec);

if (det > -0.00001f)
return false;

inv_det = 1.0f / det;

tvec = ray_origin - vert0;

u = tvec.dot(pvec) * inv_det;
if (u < -0.001f || u > 1.001f)
return false;

qvec = tvec.crossProduct(edge1);

v = ray_direction.dot(qvec) * inv_det;
if (v < -0.001f || u + v > 1.001f)
return false;

t = edge2.dot(qvec) * inv_det;

if (t <= 0)
return false;

hitPoint->x = ray_origin.x+t*ray_direction.x;
hitPoint->y = ray_origin.y+t*ray_direction.y;
hitPoint->z = ray_origin.z+t*ray_direction.z;

return true;
}
« Last Edit: August 04, 2009, 01:22:27 PM by Ivan » Logged

http://polycode.org/ - Free, cross-platform, open-source engine.
Tycho Brahe
Level 10
*****

λx.x


View Profile
« Reply #23 on: August 04, 2009, 01:34:09 PM »

@Zaphos

you, sir, are a god.

you've managed to solve all my problems, and in less than a day.

Thanks!

I used your idea of using gluProject() to get the correct z value, then fed that into gluUnProject() to get the right coordinates.

Again, Many thanks.
Logged
Zaphos
Guest
« Reply #24 on: August 04, 2009, 01:44:21 PM »

Haha, no worries -- glad it works Smiley
Logged
Tycho Brahe
Level 10
*****

λx.x


View Profile
« Reply #25 on: August 08, 2009, 03:36:54 AM »

Just realised that, looking back over the thread that anyone else looking for the answer may not be able to get it, so here is the code I finally settled on for anyone else with the same problem.

so here it is:
Code:
b2Vec2 CALCULATEPOS(int x, int y)
{
	
   
    int viewport[4];
    double modelview[16], projection[16];

    glGetIntegerv(GL_VIEWPORT, viewport);
    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);

    int mx = x;
    int my = viewport[3]-y;

    GLdouble WinX,WinY,WinZ;

    gluProject(0,0,0,modelview, projection, viewport, &WinX,&WinY,&WinZ);

    GLdouble X,Y,Z;
    gluUnProject((double)mx, (double)my,WinZ, modelview, projection, viewport, &X, &Y, &Z);
    //printf("x:%f   y:%f   z:%f\n",(float)WinX,(float)WinY,(float)WinZ);

    float Xf,Yf,Zf;

    Xf = (float)X;

    Yf = (float)Y;

    Zf = (float)Z;

    //printf("X:%f, Y:%f, Z:%f\n",Xf,Yf,Zf);


    b2Vec2 Return(Xf,Yf);

    return Return;

}

(btw this uses some data types from box2d (like b2vec2);
Logged
Pages: 1 [2]
Print
Jump to:  

Theme orange-lt created by panic