From 072888e2d518f5e2c5e1355436a37578aabfc0ae Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Sat, 8 Apr 2023 20:05:45 +0200 Subject: [PATCH] 3d X11 projection example --- tX.cc | 93 +++++++++++++++++++++++++++++++++++------------------- vecmat3.cc | 6 ++-- vecmat3.h | 2 +- 3 files changed, 65 insertions(+), 36 deletions(-) diff --git a/tX.cc b/tX.cc index 258fc03..914722f 100644 --- a/tX.cc +++ b/tX.cc @@ -143,13 +143,13 @@ public: -void plotobject(Display* display, Pixmap pixmap, GC gc, int width,int height, const NRVec &lines, const Mat3 &camera_angle, const Vec3 &camera, const Vec3 &plane_to_camera, double xod,double xdo,double yod,double ydo) +void plotobject(Display* display, Pixmap pixmap, GC gc, int width,int height, const NRVec &lines, const Mat3 &rot_angle, const Vec3 &camera, const Vec3 &plane_to_camera, double xod,double xdo,double yod,double ydo) { for(int i=0; i lines(lines0); -Mat3 camera_angle; +Mat3 rot_angle; double eul[3]={40.*M_PI/180.,40.*M_PI/180.,10.*M_PI/180.}; -euler2rotmat(eul,camera_angle,"xyz"); -Vec3 camera={50,30,40}; +euler2rotmat(eul,rot_angle,"zyz"); +double zoom=1; +rot_angle *= zoom; +Vec3 camera={50,60,40}; Vec3 plane_to_camera={-20,-20,-20}; XSetForeground(display,gc,mycolor.pixel); -plotobject(display, pixmap, gc, width,height, lines,camera_angle,camera,plane_to_camera,xod,xdo,yod,ydo); +plotobject(display, pixmap, gc, width,height, lines,rot_angle,camera,plane_to_camera,xod,xdo,yod,ydo); XClearWindow(display,win); //expose the pixmap XSetForeground(display, gc, BlackPixel(display, DefaultScreen(display)));//vratit cernou @@ -289,6 +291,7 @@ int ispressed3=0; int x,y,x0,y0; while(1) //LOOP .... process events { + bool redraw=0; XNextEvent(display, &event); switch (event.type) { @@ -304,31 +307,47 @@ while(1) //LOOP .... process events if(event.xbutton.button==Button1) ispressed1=1; if(event.xbutton.button==Button2) ispressed2=1; if(event.xbutton.button==Button3) ispressed3=1; + if(event.xbutton.button==Button4) + { + if(ispressed1) {eul[2]+=0.02; redraw=1;} + else if(ispressed2) {camera[2]+=0.2; redraw=1;} + else if (ispressed3) {plane_to_camera[2]+=0.5; redraw=1;} + else {zoom*=1.01; redraw=1;} + } + if(event.xbutton.button==Button5) + { + if(ispressed1) {eul[2]-=0.02; redraw=1;} + else if(ispressed2) {camera[2]-=0.2; redraw=1;} + else if (ispressed3) {plane_to_camera[2]-=0.5; redraw=1;} + else {zoom*=.99; redraw=1;} + } break; case MotionNotify: x = event.xbutton.x; y = event.xbutton.y; if(ispressed1) { - if(x0>x) eul[0]+=0.01; - if(x0y) eul[1]+=0.01; - if(y0x) eul[0]+=0.02; + if(x0y) eul[1]+=0.02; + if(y0x) camera[0]+=0.2; + if(x0y) camera[1]-=0.2; + if(y0x) plane_to_camera[0]-=0.5; + if(x0y) plane_to_camera[1]+=0.5; + if(y0 -void perspective(T *proj_xy, const Vec3 &point, const Mat3 &camera_angle, const Vec3 &camera, const Vec3 &plane_to_camera) +void perspective(T *proj_xy, const Vec3 &point, const Mat3 &rot_angle, const Vec3 &camera, const Vec3 &plane_to_camera) { -Vec3 d=camera_angle*(point-camera); +Vec3 d=rot_angle*point-camera; T scale = plane_to_camera[2]/d[2]; for(int i=0; i<2; ++i) proj_xy[i]= scale*d[i] + plane_to_camera[i]; } @@ -858,7 +858,7 @@ template class Vec3; \ template class Mat3; \ template void euler2rotmat(const T *eul, Mat3 &a, const char *type, bool transpose=0, bool direction=0, bool reverse=0); \ template void rotmat2euler(T *eul, const Mat3 &a, const char *type, bool transpose=0, bool direction=0, bool reverse=0); \ -template void perspective(T *proj_xy, const Vec3 &point, const Mat3 &camera_angle, const Vec3 &camera, const Vec3 &plane_to_camera); \ +template void perspective(T *proj_xy, const Vec3 &point, const Mat3 &rot_angle, const Vec3 &camera, const Vec3 &plane_to_camera); \ #ifndef AVOID_STDSTREAM diff --git a/vecmat3.h b/vecmat3.h index f486022..2d34933 100644 --- a/vecmat3.h +++ b/vecmat3.h @@ -191,7 +191,7 @@ template void rotmat2euler(T *eul, const Mat3 &a, const char *type, bool transpose=0, bool direction=0, bool reverse=0); template -void perspective(T *proj_xy, const Vec3 &point, const Mat3 &camera_angle, const Vec3 &camera, const Vec3 &plane_to_camera); +void perspective(T *proj_xy, const Vec3 &point, const Mat3 &rot_angle, const Vec3 &camera, const Vec3 &plane_to_camera); }//namespace