# Screen Position Calculations - Help Needed

Hello guys,

I’ve got three values (angles) - x and y that camera is facing (yaw and pitch, and z if needed), these are:

x - heading from true north in OGL coordinates
y - pitch from flat in OGL coordinates

I am wondering if it is possible by using these values to get Screen Position as a vector of xy?

Also, if we have a pitch value from flat Surface, would it be possible to recalculate pitch if camera moves up? If yes, how?

Sorry for asking for help but I am new to OpenGL and desperatly in need of advice.

Best Regards,
Michal

so everyone just left this guy for 12 days, I should start a school.
I’m going to teach you everything in one minute !!!

if you dont want everything, then dont get my example files and dont learn everything,
-== WORLD-3dpnt to SCREEN ==-

void w2s(int xy,int fol,double v,int x,int y) {
xy = (int)((fol/v)*v+x);
xy = (int)((fol/v)*v+y);
}

orbital(cameye,3); // .pos has no obliquity. remember
NORMALIZE(cameye); /// required for dotproduct ANGLE(v,v2)
/// crossproduct tho, is always a right angle vector,
/// normal input not needed, normalize crossproduct result.
set16mat(cmatrix,cameye); /// normal input not required
INITVECTOR(camup,cmatrix,cmatrix,cmatrix);///a normal in the matrix
/// ////////////////// LookAt transform
if(invertmatrix(cmatrix,wmatrix))
/// now begin loop on every point
SUBV(anyPoint,anyPoint,campos);
popmatrix(v,wmatrix,anyPoint);
if Z is positive infront of camera, draw it, clip out xy limits
draw at w2s(f,v) <x y

one of these days I might change fol from int to double for more precise adjustments
now needed in OGL @ 200 fps, awesome !!!

first examples in main, is the trig - focal length to field of view.

know the trig laws and right-angle rules

3D 101, heh

and here’s the working prototype almost as described -

without more testing, I just figure its reversed for leaving ortho2D with 0 at bottom

http://trueinnerbeing.x10host.com/code/scrcap2.png

uses inverted gluLookAt, and gluPerspective with input of fov/2

Update, after sizing the window I realize, ooops

gluPerspective(CLAMP(fov/Aspect,0.001,170.0)

I knew 2 was only close but what? it was aspect

ok then - beginners will likely begin wrong if they dont have this…

BG stars lights camera scene text = the math that IS the Camera!
http://trueinnerbeing.x10host.com/code/code.html

Worth taking an hour to Google and make high def tga textures and running as eyeCandy.
Improvements very welcome, appreciated and worthwhile - chk it out -

ITS BIGGER
ITS BETTER

how much does anti Aliasing COST? lol

Latest Features:
Gravity Simulation: c
Focal len and user defined star size: S,s
Random StarGate. Warps to the stars: 5
StarGate: 6
http://trueinnerbeing.x10host.com/code/code.html
this code answers many questions - got know at least this much about the matrix.
not exactly a newbie - misnomer, hah

took a while, but finally got around to matching perspective screen xy !!
… simple, use the same perspective matrix you gave to the projection matrix.
found the deprecated glulookAt and gluPerspective code, especially lookat was ugly - rewritten.

working example **********************************
('cause no one else knew how, someone had to do it.)
functions not included, are in the file.

``````
#define NORMALIZE4(V) {  \
V/=V;  \
V/=V;   \
V/=V;   \
}

#define popmatrix4( U, M, V ) { \
U = V*M+V*M+V*M+V*M; \
U = V*M+V*M+V*M+V*M;  \
U = V*M+V*M+V*M+V*M;  \
U = V*M+V*M+V*M+V*M;  \
if (U) NORMALIZE4(U); \
} /// != 0.0)&& (U != 1) 1 changes nothing, NOT 0

double pmat = { 0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0 } ;

void setpmat() {
double fl; // = tan(dtor(90-fovx/aspect/2)); /// UNIT focal len
fl = 1/tan(dtor(fov/Aspect/2)); ///  same number  /// fl * cx = actual focal length
pmat  = fl/Aspect;
pmat  = fl;
pmat = (farclip + nearclip) / (nearclip - farclip);
pmat = 2*farclip*nearclip / (nearclip - farclip);
}

void fovmat(double v[],double p[]) {
int cx = (int)(_Width/2),cy = (int)(_Height/2);
double pnt2, pnt = { 0,0,0,1 } ;
COPYVECTOR(pnt,p);NORMALIZE(pnt); /// matrix in unit form, normalize
popmatrix4(pnt2,pmat,pnt);
COPYVECTOR(v,pnt2);
v *= -cx; v *= -cy;
v += cx; v += cy;
}

``````

In the gl scheme,

``````
glMatrixMode(GL_PROJECTION);
glMultMatrixd(pmat); //perspective
glMatrixMode(GL_MODELVIEW);
Camera(); // invertmatrix()

``````

And for 2D - a 3D radius to 2D screen points.
first invert cam matrix to world wmatrix -
apply perspective to points in front.

``````
SUBV(v,v,campos);
if (VLEN(v) < farclip) {
popmatrix(v2,wmatrix,v);
if (v2 < 0.0) { /// reversed
fovmat(v,v2); // FIN

fovmat(v2,w);
SUBV2(v2,v2,v);
sub = 10 + (int)VLEN2(v2);
printstring(v,v-sub,0.0,elem[i].name,3);
// not some 3d point bouncing around dependent on z roll
// sticks like glue at any fov angle.

//or return a perspective size for an image -- fsize(1,12);
double fsize(double x, double z) { // Z focal len
double v, p = { x,0,z } ;
fovmat(v,p);
return v - _Width / 2;
}

``````