From: SkyFlash <sky...@ch...> - 2003-02-10 13:42:32
|
Hm.... what I really need is a formula to calculate distortion. What I have right now is the computation which point is currently drawn under the 2d cursor (x and y axis wise in the 3d space) For this I calculate the vector from the camera in percentage to the field of view, the range and the angle. But what I am missing is the distortion that comes from the perspective in the 2d coordinate system, if you look at it, everything on top of the screen is shown closer to the screen center, which means if you set the cursor at 50% left of center and move upwards, the 3d cursor will not move exactly upward but up and to the right a little. I want a formula to calculate that: _____________ / \ <--- 80% high / \ / \ ------------------- Unluckily, its not linear. I think its an x^2 something, which makes sense as both sides slide in. What I need is: I am at 80% high, which percentage do I need to subtract from my left/right axis to get the point? SkyFlash > those are a few globals i use to store the absolute direction > of the camera local coordinate axes vector gvX = > vector(1,0,0); vector gvY = vector(0,1,0); vector gvZ = vector(0,0,1); > > // get axis each time camera is rotated > glGetFloatv(GL_MODELVIEW_MATRIX,gModelMat); > gvX.x = gModelMat[ 0]; > gvX.y = gModelMat[ 4]; > gvX.z = gModelMat[ 8]; > > gvY.x = gModelMat[ 1]; > gvY.y = gModelMat[ 5]; > gvY.z = gModelMat[ 9]; > > gvZ.x = gModelMat[ 2]; > gvZ.y = gModelMat[ 6]; > gvZ.z = gModelMat[10]; > > > // also we need info about the camera frustum, the near and > the farplane, // this can be received by extracting from the > perspective matrix, // usually set up only at game start > > float gfGLX,gfGLY,gfGLZ; > > glGetFloatv(GL_PROJECTION_MATRIX,gfGLProjectionMatrix); > > gfGLX = 1.0/gfGLProjectionMatrix[0]; > gfGLY = 1.0/gfGLProjectionMatrix[5]; > gfGLZ = gfGLProjectionMatrix[10]; > > > // and the pixel resolution of the window > int giCX = 640; > int giCY = 480; > > > // o is the correct origin of the mouseray, > // you can also take the camera position, > // but a point exactly on that position is not yet drawn > // u is the direction of the mouseray > // norm() is for normalizing the vector, 512 is the length of > the mouseray, // set to a value thats appropriate for your > coordinate system // oh and gvCampos is the position of the > camera // mouse[0] and mouse[1] are the x and y mousecoords > in pixels from the // left top window cornor in my > coordinate system, // maybe you need to give coordinates from > another corner like left bottom in your coordinate system > > vector o,u; > > { > float mx = (((float)mouse[0]/(float)giCX) * 2.0 - 1.0)*gfGLX; > float my = (((float)mouse[1]/(float)giCY) * 2.0 - 1.0)*gfGLY; > > u = - gvZ + gvX*mx - gvY*my; > o = gvCampos + u; > u = norm(u)*512.0; > } |