Import Geant4 11.2.0.beta source tree

This commit is contained in:
Gabriele Cosmo
2023-06-30 09:09:57 +02:00
parent aef78ca386
commit dd1f179cda
3780 changed files with 212808 additions and 142780 deletions
+2 -2
View File
@@ -632,7 +632,7 @@ inline void ccontour::set_secondary_grid(int iCol, int iRow)
m_iRowSec=mx<int>(iRow,2);
}
inline void ccontour::set_limits(double pLimits[])
inline void ccontour::set_limits(double pLimits[4])
{
_ASSERT_(pLimits[0]<pLimits[1],"ccontour::set_limits");
_ASSERT_(pLimits[2]<pLimits[3],"ccontour::set_limits");
@@ -642,7 +642,7 @@ inline void ccontour::set_limits(double pLimits[])
}
}
inline void ccontour::get_limits(double pLimits[])
inline void ccontour::get_limits(double pLimits[4])
{
for (int i=0;i<4;i++)
{
+45 -5
View File
@@ -172,15 +172,31 @@ public:
//pr::m_vec[1]= 10;pr::m_vec[5] = 11;pr::m_vec[ 9] = 12;pr::m_vec[13] = 13;
//pr::m_vec[2]= 20;pr::m_vec[6] = 21;pr::m_vec[10] = 22;pr::m_vec[14] = 23;
//pr::m_vec[3]= 30;pr::m_vec[7] = 31;pr::m_vec[11] = 32;pr::m_vec[15] = 33;
T x= pr::m_vec[0]*a_x+pr::m_vec[4]*a_y+pr::m_vec[ 8]*a_z+pr::m_vec[12]*a_p;
T y= pr::m_vec[1]*a_x+pr::m_vec[5]*a_y+pr::m_vec[ 9]*a_z+pr::m_vec[13]*a_p;
T z= pr::m_vec[2]*a_x+pr::m_vec[6]*a_y+pr::m_vec[10]*a_z+pr::m_vec[14]*a_p;
T p= pr::m_vec[3]*a_x+pr::m_vec[7]*a_y+pr::m_vec[11]*a_z+pr::m_vec[15]*a_p;
T x = pr::m_vec[0]*a_x+pr::m_vec[4]*a_y+pr::m_vec[ 8]*a_z+pr::m_vec[12]*a_p;
T y = pr::m_vec[1]*a_x+pr::m_vec[5]*a_y+pr::m_vec[ 9]*a_z+pr::m_vec[13]*a_p;
T z = pr::m_vec[2]*a_x+pr::m_vec[6]*a_y+pr::m_vec[10]*a_z+pr::m_vec[14]*a_p;
T p = pr::m_vec[3]*a_x+pr::m_vec[7]*a_y+pr::m_vec[11]*a_z+pr::m_vec[15]*a_p;
a_x = x;
a_y = y;
a_z = z;
a_p = p;
}
void mul_4_opt(T& a_x,T& a_y,T& a_z,T& a_p,T a_tmp[4]) const {
// a_[x,y,z,p] = this * a_[x,y,z,p]
//pr::m_vec[R + C * 4];
//pr::m_vec[0]= 00;pr::m_vec[4] = 01;pr::m_vec[ 8] = 02;pr::m_vec[12] = 03;
//pr::m_vec[1]= 10;pr::m_vec[5] = 11;pr::m_vec[ 9] = 12;pr::m_vec[13] = 13;
//pr::m_vec[2]= 20;pr::m_vec[6] = 21;pr::m_vec[10] = 22;pr::m_vec[14] = 23;
//pr::m_vec[3]= 30;pr::m_vec[7] = 31;pr::m_vec[11] = 32;pr::m_vec[15] = 33;
a_tmp[0] = pr::m_vec[0]*a_x+pr::m_vec[4]*a_y+pr::m_vec[ 8]*a_z+pr::m_vec[12]*a_p;
a_tmp[1] = pr::m_vec[1]*a_x+pr::m_vec[5]*a_y+pr::m_vec[ 9]*a_z+pr::m_vec[13]*a_p;
a_tmp[2] = pr::m_vec[2]*a_x+pr::m_vec[6]*a_y+pr::m_vec[10]*a_z+pr::m_vec[14]*a_p;
a_tmp[3] = pr::m_vec[3]*a_x+pr::m_vec[7]*a_y+pr::m_vec[11]*a_z+pr::m_vec[15]*a_p;
a_x = a_tmp[0];
a_y = a_tmp[1];
a_z = a_tmp[2];
a_p = a_tmp[3];
}
void mul_3(T& a_x,T& a_y,T& a_z) const {
// a_[x,y,z] = this * a_[x,y,z]
//pr::m_vec[R + C * 4];
@@ -195,6 +211,20 @@ public:
a_y = y;
a_z = z;
}
void mul_3_opt(T& a_x,T& a_y,T& a_z,T a_tmp[3]) const {
// a_[x,y,z] = this * a_[x,y,z]
//pr::m_vec[R + C * 4];
//pr::m_vec[0]= 00;pr::m_vec[4] = 01;pr::m_vec[ 8] = 02;pr::m_vec[12] = 03;
//pr::m_vec[1]= 10;pr::m_vec[5] = 11;pr::m_vec[ 9] = 12;pr::m_vec[13] = 13;
//pr::m_vec[2]= 20;pr::m_vec[6] = 21;pr::m_vec[10] = 22;pr::m_vec[14] = 23;
//pr::m_vec[3]= 30;pr::m_vec[7] = 31;pr::m_vec[11] = 32;pr::m_vec[15] = 33;
a_tmp[0] = pr::m_vec[0]*a_x+pr::m_vec[4]*a_y+pr::m_vec[ 8]*a_z+pr::m_vec[12];
a_tmp[1] = pr::m_vec[1]*a_x+pr::m_vec[5]*a_y+pr::m_vec[ 9]*a_z+pr::m_vec[13];
a_tmp[2] = pr::m_vec[2]*a_x+pr::m_vec[6]*a_y+pr::m_vec[10]*a_z+pr::m_vec[14];
a_x = a_tmp[0];
a_y = a_tmp[1];
a_z = a_tmp[2];
}
void mul_2(T& a_x,T& a_y) const {
// a_[x,y] = this * a_[x,y]
//pr::m_vec[R + C * 4];
@@ -219,6 +249,17 @@ public:
a_y = y;
a_z = z;
}
void mul_dir_3_opt(T& a_x,T& a_y,T& a_z,T a_tmp[3]) const {
// used to multiply normals.
// a_[x,y,z] = rot_part(this) * a_[x,y,z]
a_tmp[0] = pr::m_vec[0]*a_x+pr::m_vec[4]*a_y+pr::m_vec[ 8]*a_z;
a_tmp[1] = pr::m_vec[1]*a_x+pr::m_vec[5]*a_y+pr::m_vec[ 9]*a_z;
a_tmp[2] = pr::m_vec[2]*a_x+pr::m_vec[6]*a_y+pr::m_vec[10]*a_z;
a_x = a_tmp[0];
a_y = a_tmp[1];
a_z = a_tmp[2];
}
void mul_trans_3(T& a_x,T& a_y,T& a_z) const {
// used in sg::healpix.
@@ -465,7 +506,6 @@ public:
pr::m_vec[1],pr::m_vec[5],pr::m_vec[ 9], //2 row
pr::m_vec[2],pr::m_vec[6],pr::m_vec[10]); //3 row
}
private:static void check_instantiation() {mat4<float> dummy;}
};
+3
View File
@@ -56,9 +56,12 @@ public:
public: //backward compatibility
void mul_2f(float& a_x,float& a_y) const {parent::mul_2(a_x,a_y);}
void mul_3f(float& a_x,float& a_y,float& a_z) const {parent::mul_3(a_x,a_y,a_z);}
void mul_3f_opt(float& a_x,float& a_y,float& a_z,float a_tmp[3]) const {parent::mul_3_opt(a_x,a_y,a_z,a_tmp);}
void mul_dir_3f(float& a_x,float& a_y,float& a_z) const {parent::mul_dir_3(a_x,a_y,a_z);}
void mul_dir_3f_opt(float& a_x,float& a_y,float& a_z,float a_tmp[3]) const {parent::mul_dir_3_opt(a_x,a_y,a_z,a_tmp);}
void mul_trans_3f(float& a_x,float& a_y,float& a_z) const {parent::mul_trans_3(a_x,a_y,a_z);}
void mul_4f(float& a_x,float& a_y,float& a_z,float& a_w) const {parent::mul_4(a_x,a_y,a_z,a_w);}
void mul_4f_opt(float& a_x,float& a_y,float& a_z,float& a_w,float a_tmp[4]) const {parent::mul_4_opt(a_x,a_y,a_z,a_w,a_tmp);}
void mul_dir_3d(double& a_x,double& a_y,double& a_z) const { //used in sg::healpix
float x = float(a_x);
float y = float(a_y);
+8 -2
View File
@@ -118,7 +118,10 @@ public:
a_out << "tools::rcsv::histo::read : syntax error in " << sout(line) << std::endl;
return false;
}
to(_words[1],hdata.m_dimension);
if(!to(_words[1],hdata.m_dimension)) {
a_out << "tools::rcsv::histo::read : syntax error in " << sout(line) << std::endl;
return false;
}
} else if(_words[0]=="#annotation") {
if(_words.size()<2) {
a_out << "tools::rcsv::histo::read : syntax error in " << sout(line) << std::endl;
@@ -200,7 +203,10 @@ public:
a_out << "tools::rcsv::histo::read : syntax error in " << sout(line) << std::endl;
return false;
}
to(_words[1],hdata.m_bin_number);
if(!to(_words[1],hdata.m_bin_number)) {
a_out << "tools::rcsv::histo::read : syntax error in " << sout(line) << std::endl;
return false;
}
} else if(_words[0]=="#cut_v") {
if(_words.size()!=2) {
a_out << "tools::rcsv::histo::read : syntax error in " << sout(line) << std::endl;
+6 -12
View File
@@ -125,19 +125,16 @@ inline bool write_paper(std::ostream& a_out,
return false;
}
size_t sz;
unsigned char* buffer = action.get_rgbas(sz,a_top_to_bottom);
if(!buffer) {
std::vector<unsigned char> buffer;
if(!action.get_rgbas(a_top_to_bottom,buffer)) {
a_out << "tools::sg::write_paper : can't get rgba image." << std::endl;
return false;
}
if(!a_png_writer(a_out,a_file,buffer,a_width,a_height,4)) {
if(!a_png_writer(a_out,a_file,vec_data(buffer),a_width,a_height,4)) {
a_out << "tools::sg::write_paper : tools::png::write() failed." << std::endl;
delete [] buffer;
return false;
}
delete [] buffer;
return true;
}
@@ -148,19 +145,16 @@ inline bool write_paper(std::ostream& a_out,
return false;
}
size_t sz;
unsigned char* buffer = action.get_rgbs(sz,a_top_to_bottom);
if(!buffer) {
std::vector<unsigned char> buffer;
if(!action.get_rgbs(a_top_to_bottom,buffer)) {
a_out << "tools::sg::write_paper : can't get rgb image." << std::endl;
return false;
}
if(!a_jpeg_writer(a_out,a_file,buffer,a_width,a_height,3,100)) {
if(!a_jpeg_writer(a_out,a_file,vec_data(buffer),a_width,a_height,3,100)) {
a_out << "tools::sg::write_paper : tools::jpeg::write() failed." << std::endl;
delete [] buffer;
return false;
}
delete [] buffer;
return true;
}
+108 -68
View File
@@ -250,8 +250,6 @@ protected:
return *this;
}
public:
void reset() {}
void clear_color_buffer(float a_r,float a_g,float a_b,float a_a){
clear_color(a_r,a_g,a_b,a_a);
}
@@ -269,40 +267,12 @@ protected:
public:
zb::buffer::ZPixel* get_color_buffer(unsigned int& a_width,unsigned int& a_height) const {return m_zb.get_color_buffer(a_width,a_height);}
unsigned char* get_rgbas(size_t& a_sz,bool a_top_to_bottom = true) {
if(!m_ww || !m_wh) {a_sz = 0;return 0;}
a_sz = 4 * m_ww * m_wh;
uchar* rgbas = new uchar[a_sz];
if(!rgbas) {a_sz = 0;return 0;}
uchar* pos = rgbas;
zb::buffer::ZPixel pix;
uchar* _pix = 0;
for(unsigned int row=0;row<m_wh;row++) {
for(unsigned int col=0;col<m_ww;col++) {
if(!m_zb.get_clipped_pixel(col,a_top_to_bottom?row:m_wh-1-row,pix)){
m_out << "tools::sg::zb_action::get_rgbas : can't get zbuffer pixel" << std::endl;
*pos = 0xFF;pos++;
*pos = 0x00;pos++;
*pos = 0x00;pos++;
*pos = 0xFF;pos++;
} else {
_pix = (uchar*)&pix;
*pos = *_pix;_pix++;pos++;
*pos = *_pix;_pix++;pos++;
*pos = *_pix;_pix++;pos++;
*pos = *_pix;_pix++;pos++;
}
}
}
return rgbas;
}
unsigned char* get_rgbs(size_t& a_sz,bool a_top_to_bottom = true) {
if(!m_ww || !m_wh) {a_sz = 0;return 0;}
a_sz = 3 * m_ww * m_wh;
uchar* rgbs = new uchar[a_sz];
if(!rgbs) {a_sz = 0;return 0;}
uchar* pos = rgbs;
bool get_rgbs(bool a_top_to_bottom,std::vector<uchar>& a_buffer) {
a_buffer.clear();
if(!m_ww || !m_wh) return false;
size_t sz = 3 * m_ww * m_wh;
a_buffer.resize(sz);
uchar* pos = vec_data(a_buffer);
zb::buffer::ZPixel pix;
uchar* _pix = 0;
for(unsigned int row=0;row<m_wh;row++) {
@@ -320,9 +290,91 @@ public:
}
}
}
return rgbs;
return true;
}
bool get_rgbas(bool a_top_to_bottom,std::vector<uchar>& a_buffer) {
a_buffer.clear();
if(!m_ww || !m_wh) return false;
size_t sz = 4 * m_ww * m_wh;
a_buffer.resize(sz);
if(a_top_to_bottom) {
::memcpy(vec_data(a_buffer),m_zb.zimage(),sz);
return true;
}
size_t stride = m_ww*4;
uchar* zpos = ((uchar*)m_zb.zimage())+sz-stride;
uchar* pos = vec_data(a_buffer);
for(unsigned int row=0;row<m_wh;row++,pos+=stride,zpos-=stride) ::memcpy(pos,zpos,stride);
return true;
}
bool get_rgbas_cocoa(unsigned int a_factor,std::vector<uchar>& a_buffer) {
a_buffer.clear();
if(!m_ww || !m_wh) return false;
if(!a_factor) return false;
size_t bpp = 4;
a_buffer.resize(a_factor* m_ww * a_factor * m_wh * bpp);
uchar* zbuffer = (uchar*)m_zb.zimage();
uchar* abuffer = vec_data(a_buffer);
size_t zstride = m_ww*bpp;
size_t astride = a_factor*m_ww*bpp;
size_t i,j,ar,ac,ipix;
uchar* zpos;uchar* apos;
for(j=0;j<m_wh;j++) {
for(i=0;i<m_ww;i++) {
//position in the original image.
zpos = zbuffer + (m_wh-1-j) * zstride + i*bpp; //top_to_bottom.
for(ar=0;ar<a_factor;ar++) {
for(ac=0;ac<a_factor;ac++) {
//position in the new image.
apos = abuffer + (j*a_factor+ar) * astride + (i*a_factor+ac)*bpp;
for(ipix=0;ipix<bpp;ipix++) {
*(apos+ipix) = *(zpos+ipix);
}
}
}
}
}
return true;
}
bool get_bgras(bool a_top_to_bottom,std::vector<uchar>& a_buffer) {
a_buffer.clear();
if(!m_ww || !m_wh) return false;
size_t sz = 4 * m_ww * m_wh;
a_buffer.resize(sz);
if(a_top_to_bottom) {
uchar* pos = vec_data(a_buffer);
uchar* zpos = ((uchar*)m_zb.zimage());
for(size_t count=0;count<sz;count+=4,pos+=4,zpos+=4) {
*(pos+0) = *(zpos+2);
*(pos+1) = *(zpos+1);
*(pos+2) = *(zpos+0);
*(pos+3) = *(zpos+3);
}
} else {
size_t stride = m_ww*4;
uchar* pos_line = vec_data(a_buffer);
uchar* zpos_line = ((uchar*)m_zb.zimage())+sz-stride;
uchar* pos;
uchar* zpos;
size_t count;
for(unsigned int row=0;row<m_wh;row++,pos_line+=stride,zpos_line-=stride) {
pos = pos_line;
zpos = zpos_line;
for(count=0;count<m_ww;count++,pos+=4,zpos+=4) {
*(pos+0) = *(zpos+2);
*(pos+1) = *(zpos+1);
*(pos+2) = *(zpos+0);
*(pos+3) = *(zpos+3);
}
}
}
return true;
}
public:
static bool get_rgb(void* a_tag,unsigned int a_col,unsigned int a_row,float& a_r,float& a_g,float& a_b){
//used with wps.
@@ -339,21 +391,6 @@ public:
zb::buffer::pix2rgba(pix,a_r,a_g,a_b,a);
return true;
}
static bool get_rgba(void* a_tag,unsigned int a_col,unsigned int a_row,float& a_r,float& a_g,float& a_b,float& a_a){
zb_action* rzb = (zb_action*)a_tag;
zb::buffer::ZPixel pix;
if(!rzb->m_zb.get_clipped_pixel(a_col,rzb->wh()-1-a_row,pix)){
rzb->out() << "tools::sg;:zb_action::get_rgba : can't get zbuffer pixel" << std::endl;
a_r = 1;
a_g = 0;
a_b = 0;
a_a = 1;
return false;
}
zb::buffer::pix2rgba(pix,a_r,a_g,a_b,a_a);
return true;
}
protected:
void set_normal_matrix() {
mat4f tmp(m_model);
@@ -366,8 +403,8 @@ protected:
bool project_point(float& a_x,float& a_y,float& a_z,float& a_w) {
a_w = 1;
m_model.mul_4f(a_x,a_y,a_z,a_w);
m_proj.mul_4f(a_x,a_y,a_z,a_w);
m_model.mul_4f_opt(a_x,a_y,a_z,a_w,m_tmp);
m_proj.mul_4f_opt(a_x,a_y,a_z,a_w,m_tmp);
if(a_w==0) return false;
a_x /= a_w;
a_y /= a_w;
@@ -386,19 +423,18 @@ protected:
return m_this.project_point(a_x,a_y,a_z,a_w);
}
virtual bool add_point(float a_x,float a_y,float a_z,float) {
return _add_point(a_x,a_y,a_z,m_this.m_rgba);
return _add_point(a_x,a_y,a_z,m_this.m_rgba.r(),m_this.m_rgba.g(),m_this.m_rgba.b(),m_this.m_rgba.a());
}
virtual bool add_point(float a_x,float a_y,float a_z,float,
float a_r,float a_g,float a_b,float a_a) {
colorf c(a_r,a_g,a_b,a_a);
return _add_point(a_x,a_y,a_z,c);
return _add_point(a_x,a_y,a_z,a_r,a_g,a_b,a_a);
}
virtual bool add_line(float a_bx,float a_by,float a_bz,float,
float a_ex,float a_ey,float a_ez,float) {
m_this.m_vp_mtx.mul_3f(a_bx,a_by,a_bz);
m_this.m_vp_mtx.mul_3f(a_ex,a_ey,a_ez);
m_this.m_vp_mtx.mul_3f_opt(a_bx,a_by,a_bz,m_tmp);
m_this.m_vp_mtx.mul_3f_opt(a_ex,a_ey,a_ez,m_tmp);
a_bz *= -1;
a_ez *= -1;
@@ -422,8 +458,8 @@ protected:
float a_br,float a_bg,float a_bb,float a_ba,
float a_ex,float a_ey,float a_ez,float,
float,float,float,float) {
m_this.m_vp_mtx.mul_3f(a_bx,a_by,a_bz);
m_this.m_vp_mtx.mul_3f(a_ex,a_ey,a_ez);
m_this.m_vp_mtx.mul_3f_opt(a_bx,a_by,a_bz,m_tmp);
m_this.m_vp_mtx.mul_3f_opt(a_ex,a_ey,a_ez,m_tmp);
a_bz *= -1;
a_ez *= -1;
@@ -598,18 +634,18 @@ protected:
return num_2;
}
bool _add_point(float a_x,float a_y,float a_z,const colorf& a_color){
bool _add_point(float a_x,float a_y,float a_z,float a_r,float a_g,float a_b,float a_a){
m_this.m_zb.set_depth_test(m_this.m_DEPTH_TEST);
m_this.m_zb.set_blend(m_this.m_blend);
m_this.m_vp_mtx.mul_3f(a_x,a_y,a_z);
m_this.m_vp_mtx.mul_3f_opt(a_x,a_y,a_z,m_tmp);
a_z *= -1;
zb::point p;
zinit(p,a_x,a_y,a_z);
zb::buffer::ZPixel pix;
color2pix(a_color,pix);
zb::buffer::rgba2pix(a_r,a_g,a_b,a_a,pix);
m_this.m_zb.draw_point(p,pix,npix(m_this.m_point_size));
return true;
@@ -627,9 +663,9 @@ protected:
float p2x = a_p2x;float p2y = a_p2y;float p2z = a_p2z;//float p2w = a_p2w;
float p3x = a_p3x;float p3y = a_p3y;float p3z = a_p3z;//float p3w = a_p3w;
m_this.m_vp_mtx.mul_3f(p1x,p1y,p1z);
m_this.m_vp_mtx.mul_3f(p2x,p2y,p2z);
m_this.m_vp_mtx.mul_3f(p3x,p3y,p3z);
m_this.m_vp_mtx.mul_3f_opt(p1x,p1y,p1z,m_tmp);
m_this.m_vp_mtx.mul_3f_opt(p2x,p2y,p2z,m_tmp);
m_this.m_vp_mtx.mul_3f_opt(p3x,p3y,p3z,m_tmp);
p1z *= -1;
p2z *= -1;
p3z *= -1;
@@ -688,7 +724,7 @@ protected:
float ny = (a_n1y+a_n2y+a_n3y)/3.0f;
float nz = (a_n1z+a_n2z+a_n3z)/3.0f;
m_this.m_normal_matrix.mul_dir_3f(nx,ny,nz);
m_this.m_normal_matrix.mul_dir_3f_opt(nx,ny,nz,m_tmp);
vec3f _normal(nx,ny,nz);_normal.normalize();
@@ -721,6 +757,8 @@ protected:
}
protected:
zb_action& m_this;
private: //optimize:
float m_tmp[3];
};
protected:
@@ -748,6 +786,8 @@ protected:
bool m_light_on;
bool m_DEPTH_TEST;
bool m_blend;
private: //optimize:
float m_tmp[4];
};
}}
+103
View File
@@ -0,0 +1,103 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_sg_zb_viewer
#define tools_sg_zb_viewer
#include "zb_action"
#include "viewer"
namespace tools {
namespace sg {
class zb_viewer : public viewer {
TOOLS_HEADER(zb_viewer,tools::sg::zb_viewer,viewer)
public:
zb_viewer(std::ostream& a_out,unsigned int a_width,unsigned int a_height)
:parent(a_out,a_width,a_height)
,m_mgr_gra()
,m_out_buffer_what(get_rgbs)
{}
virtual ~zb_viewer(){
//WARNING : nodes may refer m_mgr_gra (to handle gstos/texs), then
// we have to delete them first.
m_sg.clear();
}
public:
zb_viewer(const zb_viewer& a_from)
:parent(a_from)
,m_mgr_gra(a_from.m_mgr_gra)
,m_out_buffer_what(get_rgbs)
{}
zb_viewer& operator=(const zb_viewer& a_from){
parent::operator=(a_from);
m_mgr_gra = a_from.m_mgr_gra;
m_out_buffer_what = get_rgbs;
m_out_buffer.clear();
return *this;
}
public:
enum get_what {
get_rgbs = 0,
get_rgbas,
get_bgras
};
bool render(get_what a_what,bool a_top_to_bottom) {
m_out_buffer.clear();
if(!m_ww) return false;
if(!m_wh) return false;
zb_action action(m_mgr_gra,m_out,m_ww,m_wh);
action.clear_color_buffer(m_clear_color.r(),m_clear_color.g(),m_clear_color.b(),m_clear_color.a());
action.clear_depth_buffer();
action.set_do_transparency(false);
action.set_have_to_do_transparency(false);
m_sg.render(action);
if(!action.end()) { //check that matrices stack are ok.
m_out << "tools::sg::zb_viewer: bad zb_action end." << std::endl;
return false;
} else {
if(action.have_to_do_transparency()) {
action.set_do_transparency(true);
m_sg.render(action);
if(!action.end()) { //check that matrices stack are ok.
m_out << "tools::sg::zb_viewer: bad zb_action end." << std::endl;
return false;
}
}
}
m_out_buffer_what = a_what;
bool status = false;
switch(a_what) {
case get_rgbs:
status = action.get_rgbs(a_top_to_bottom,m_out_buffer);
break;
case get_rgbas:
status = action.get_rgbas(a_top_to_bottom,m_out_buffer);
break;
case get_bgras:
status = action.get_bgras(a_top_to_bottom,m_out_buffer);
break;
}\
if(!status) {
m_out << "tools::sg::zb_viewer::render() : can't get rgb image." << std::endl;
m_out_buffer.clear();
return false;
}
/*m_out << "size " << m_out_buffer.size() << std::endl;*/
return true;
}
const std::vector<unsigned char>& out_buffer() const {return m_out_buffer;}
void out_buffer_clear() {m_out_buffer.clear();}
protected:
zb_manager m_mgr_gra;
get_what m_out_buffer_what;
std::vector<unsigned char> m_out_buffer;
};
}}
#endif
+4 -4
View File
@@ -5,13 +5,13 @@
#define tools_version
#define TOOLS_MAJOR_VERSION 6
#define TOOLS_MINOR_VERSION 1
#define TOOLS_MINOR_VERSION 2
#define TOOLS_PATCH_VERSION 0
#define TOOLS_VERSION "6.1.0"
#define TOOLS_VERSION_VRP "v6r1p0"
#define TOOLS_VERSION "6.2.0"
#define TOOLS_VERSION_VRP "v6r2p0"
namespace tools {
inline unsigned int version() {return 60100;}
inline unsigned int version() {return 60200;}
}
#endif
+103 -289
View File
@@ -39,105 +39,35 @@ protected:
ZPixel m_pixel;
};
class point_writer : public virtual writer {
public:
virtual void write(ZPos a_x,ZPos a_y,ZZ a_z) {
if(m_size>=1) { //see zb_action::npix().
ZPos x,y;
for(int i=-int(m_size);i<=int(m_size);i++) {
x = a_x + i;
for(int j=-int(m_size);j<=int(m_size);j++) {
y = a_y + j;
_write(x,y,a_z);
}
void _write_point(ZPos a_x,ZPos a_y,ZZ a_z,ZPixel a_pixel) {
if((a_x<m_begX) || (a_x>m_endX)) return;
if((a_y<m_begY) || (a_y>m_endY)) return;
ZReal zpoint = (ZReal)a_z;
unsigned long offset = a_y * m_zbw + a_x;
ZReal* zbuff = m_zbuffer + offset;
if(m_depth_test) {if(zpoint<*zbuff) return;}
ZPixel* zimage = m_zimage + offset;
*zbuff = zpoint;
blend(*zimage,a_pixel);
}
void write_point(ZPos a_x,ZPos a_y,ZZ a_z,unsigned int a_size,ZPixel a_pixel) {
if(a_size>=1) { //see zb_action::npix().
ZPos x,y;
for(int i=-int(a_size);i<=int(a_size);i++) {
x = a_x + i;
for(int j=-int(a_size);j<=int(a_size);j++) {
y = a_y + j;
_write_point(x,y,a_z,a_pixel);
}
} else {
_write(a_x,a_y,a_z);
}
} else {
_write_point(a_x,a_y,a_z,a_pixel);
}
public:
point_writer(ZPixel a_pixel,buffer& a_buffer,unsigned int a_size)
:writer(a_pixel)
,m_buffer(a_buffer)
,m_size(a_size)
{}
virtual ~point_writer(){}
public:
point_writer(const point_writer& a_from)
:writer(a_from)
,m_buffer(a_from.m_buffer)
,m_size(a_from.m_size)
{}
point_writer& operator=(const point_writer& a_from){
writer::operator=(a_from);
m_size = a_from.m_size;
return *this;
}
protected:
void _write(ZPos a_x,ZPos a_y,ZZ a_z) {
if((a_x<m_buffer.m_begX) || (a_x>m_buffer.m_endX)) return;
if((a_y<m_buffer.m_begY) || (a_y>m_buffer.m_endY)) return;
ZReal zpoint = (ZReal)a_z;
unsigned long offset = a_y * m_buffer.m_zbw + a_x;
ZReal* zbuff = m_buffer.m_zbuffer + offset;
if(m_buffer.m_depth_test) {if(zpoint<*zbuff) return;}
ZPixel* zimage = m_buffer.m_zimage + offset;
*zbuff = zpoint;
m_buffer.blend(*zimage,m_pixel);
}
protected:
buffer& m_buffer;
unsigned int m_size;
};
/*
class edge_point_writer : public virtual writer {
public:
virtual void write(ZPos a_x,ZPos a_y,ZZ) {
if((a_x<m_buffer.m_begX) || (a_x>m_buffer.m_endX)) return;
if((a_y<m_buffer.m_begY) || (a_y>m_buffer.m_endY)) return;
// Computing must be the same as in WriteScanLine routine.
ZReal zpoint =
(ZReal)(- m_buffer.m_planeDC
- m_buffer.m_planeAC * a_x
- m_buffer.m_planeBC * a_y);
// for edge plane quite perpandicular to screen
//if((zpoint<m_buffer.m_zmin)||(zpoint>m_buffer.m_zmax)) return;
unsigned long offset = a_y * m_buffer.m_zbw + a_x;
ZReal* zbuff = m_buffer.m_zbuffer + offset;
if(m_buffer.m_depth_test) {if(zpoint<*zbuff) return;}
ZPixel* zimage = m_buffer.m_zimage + offset;
*zbuff = zpoint;
*zimage = m_pixel;
}
public:
edge_point_writer(ZPixel a_pixel,buffer& a_buffer)
:writer(a_pixel)
,m_buffer(a_buffer)
{}
virtual ~edge_point_writer(){}
public:
edge_point_writer(const edge_point_writer& a_from)
:writer(a_from)
,m_buffer(a_from.m_buffer)
{}
edge_point_writer& operator=(const edge_point_writer& a_from){
writer::operator=(a_from);
return *this;
}
protected:
buffer& m_buffer;
};
*/
}
public:
buffer()
@@ -203,17 +133,6 @@ public:
return false;
}
// Init buffer done by further call to ZBufferErase.
/*
unsigned int size = m_zbw * m_zbh;
ZReal* zbuffer = m_zbuffer;
ZPixel* zimage = m_zimage;
for(unsigned int count=0;count<size;count++,zbuffer++,zimage++){
*zimage = 0;
*zbuffer = - ZREAL_HUGE();
}
*/
set_clip_region(0,0,m_zbw,m_zbh);
m_polygon.clear();
return true;
@@ -247,6 +166,8 @@ public:
}
}
ZPixel* zimage() {return m_zimage;}
bool get_clipped_pixel(ZPos a_x,ZPos a_y,ZPixel& a_pixel) const {
if((a_x<m_begX) || (a_x>m_endX)) {a_pixel = 0;return false;}
if((a_y<m_begY) || (a_y>m_endY)) {a_pixel = 0;return false;}
@@ -270,34 +191,29 @@ public:
}
void draw_point(const point& a_p,ZPixel a_pixel,unsigned int a_size){
point_writer pw(a_pixel,*this,a_size);
pw.write(a_p.x,a_p.y,a_p.z);
write_point(a_p.x,a_p.y,a_p.z,a_size,a_pixel);
}
void draw_line(const point& a_beg,const point& a_end,ZPixel a_pixel,unsigned int a_size){
point_writer pw(a_pixel,*this,a_size);
WriteLine(a_beg,a_end,pw);
WriteLine(a_beg,a_end,a_size,a_pixel);
}
void draw_lines(int a_number,const point* a_list,ZPixel a_pixel,unsigned int a_size){
point_writer pw(a_pixel,*this,a_size);
for(int count=1;count<a_number;count++) {
WriteLine(a_list[count-1],a_list[count],pw);
WriteLine(a_list[count-1],a_list[count],a_size,a_pixel);
}
}
void draw_segments(int a_number,const point* a_list,ZPixel a_pixel,unsigned int a_size){
point_writer pw(a_pixel,*this,a_size);
int segment_number = a_number/2;
for(int count=0;count<segment_number;count++) {
WriteLine(a_list[2*count],a_list[2*count+1],pw);
WriteLine(a_list[2*count],a_list[2*count+1],a_size,a_pixel);
}
}
void draw_markers(int a_number,const point* a_list,ZPixel a_pixel,unsigned int a_size){
point_writer pw(a_pixel,*this,a_size);
for(int count=0;count<a_number;count++){
const point& p = a_list[count];
pw.write(p.x,p.y,p.z);
write_point(p.x,p.y,p.z,a_size,a_pixel);
}
}
@@ -327,40 +243,6 @@ public:
}
/*
void draw_edged_polygon(int a_number,const point* a_list,
ZZ a_A,ZZ a_B,ZZ a_C,ZZ a_D,
//ZZ a_zmin,ZZ a_zmax,
ZPixel a_pixel){
// Assume a_list is closed.
if(a_number<3) return;
if(a_C==0) return; // polygone seen from edge
//if(m_zboundPrec<0) m_zboundPrec = 0;
m_scan_pixel = a_pixel;
m_planeAC = a_A/a_C;
m_planeBC = a_B/a_C;
m_planeDC = a_D/a_C;
//if this polygon A is quite perpandicular to screen and close
//to an other B than |dz| then some pixel of A could overwrite
//pixel of B. Your have then to give a lower m_zboundPrec
//ZZ dz = m_zboundPrec * (a_zmax - a_zmin)/100.;
//m_zmin = (ZReal)(a_zmin - dz);
//m_zmax = (ZReal)(a_zmax + dz);
// draw edge :
edge_point_writer pw(a_pixel,*this);
// some pixel could be out of range [plane.zmin,plane.zmax].
for(int count=1;count<a_number;count++) {
WriteLine(a_list[count-1],a_list[count],pw);
}
}
*/
typedef unsigned char uchar;
static void rgba2pix(float a_r,float a_g,float a_b,float a_a,ZPixel& a_pix) {
uchar* _px = (uchar*)&a_pix;
@@ -377,84 +259,18 @@ public:
a_a = (*_px)/255.0f;_px++;
}
protected:
class scan_writer {
public:
virtual void write(ZPos,ZPos,ZZ,ZPos) = 0;
public:
virtual ~scan_writer(){}
};
class scan_writer_1 : public virtual scan_writer {
public:
virtual void write(ZPos a_x,ZPos a_y,ZZ a_z,ZPos) {
m_writer.write(a_x,a_y,a_z);
}
public:
scan_writer_1(writer& a_writer):m_writer(a_writer){}
virtual ~scan_writer_1(){}
public:
scan_writer_1(const scan_writer_1& a_from)
:scan_writer(a_from)
,m_writer(a_from.m_writer)
{}
scan_writer_1& operator=(const scan_writer_1&){return *this;}
protected:
writer& m_writer;
};
class scan_writer_2 : public virtual scan_writer {
public:
virtual void write(ZPos a_x,ZPos a_y,ZZ a_z,ZPos) {
m_writer.write(a_y,a_x,a_z);
}
public:
scan_writer_2(writer& a_writer):m_writer(a_writer){}
virtual ~scan_writer_2(){}
public:
scan_writer_2(const scan_writer_2& a_from)
:scan_writer(a_from)
,m_writer(a_from.m_writer)
{}
scan_writer_2& operator=(const scan_writer_2&){return *this;}
protected:
writer& m_writer;
};
class scan_writer_3 : public virtual scan_writer {
public:
virtual void write(ZPos a_x,ZPos a_y,ZZ a_z,ZPos a_beg) {
m_writer.write(a_x,2*a_beg-a_y,a_z);
}
public:
scan_writer_3(writer& a_writer):m_writer(a_writer){}
virtual ~scan_writer_3(){}
public:
scan_writer_3(const scan_writer_3& a_from)
:scan_writer(a_from)
,m_writer(a_from.m_writer)
{}
scan_writer_3& operator=(const scan_writer_3&){return *this;}
protected:
writer& m_writer;
};
class scan_writer_4 : public virtual scan_writer {
public:
virtual void write(ZPos a_x,ZPos a_y,ZZ a_z,ZPos a_beg) {
m_writer.write(2*a_beg-a_y,a_x,a_z);
}
public:
scan_writer_4(writer& a_writer):m_writer(a_writer){}
virtual ~scan_writer_4(){}
public:
scan_writer_4(const scan_writer_4& a_from)
:scan_writer(a_from)
,m_writer(a_from.m_writer)
{}
scan_writer_4& operator=(const scan_writer_4&){return *this;}
protected:
writer& m_writer;
};
void scan_write_point_1(ZPos a_x,ZPos a_y,ZZ a_z,ZPos /*a_beg*/,unsigned int a_size,ZPixel a_pixel) {
write_point(a_x,a_y,a_z,a_size,a_pixel);
}
void scan_write_point_2(ZPos a_x,ZPos a_y,ZZ a_z,ZPos /*a_beg*/,unsigned int a_size,ZPixel a_pixel) {
write_point(a_y,a_x,a_z,a_size,a_pixel);
}
void scan_write_point_3(ZPos a_x,ZPos a_y,ZZ a_z,ZPos a_beg,unsigned int a_size,ZPixel a_pixel) {
write_point(a_x,2*a_beg-a_y,a_z,a_size,a_pixel);
}
void scan_write_point_4(ZPos a_x,ZPos a_y,ZZ a_z,ZPos a_beg,unsigned int a_size,ZPixel a_pixel) {
write_point(2*a_beg-a_y,a_x,a_z,a_size,a_pixel);
}
void blend(ZPixel& a_pix,const ZPixel& a_new) {
if(!m_blend) {
@@ -477,7 +293,7 @@ protected:
a_pix = a_new;
}
}
static void WriteScanLine(void* a_tag,int a_beg,int a_end,int a_y){
buffer& a_buffer = *((buffer*)a_tag);
@@ -497,7 +313,6 @@ protected:
ZReal zpoint;
for(int x=beg;x<=end;x++){
// Computing must be the same as in edge_point_writer routine.
zpoint =
(ZReal)(- a_buffer.m_planeDC
- a_buffer.m_planeAC * x
@@ -519,58 +334,11 @@ protected:
}
}
static void WriteLine(const point& a_beg,
const point& a_end,
writer& a_writer){
ZPos dx = a_end.x - a_beg.x;
ZPos dy = a_end.y - a_beg.y;
ZZ dz = a_end.z - a_beg.z;
// 6 2
// 5 1
// 7 3
// 8 4
scan_writer_1 sw1(a_writer);
scan_writer_2 sw2(a_writer);
scan_writer_3 sw3(a_writer);
scan_writer_4 sw4(a_writer);
if( (dx==0) && (dy==0) ) {
a_writer.write(a_beg.x,a_beg.y,a_beg.z);
a_writer.write(a_end.x,a_end.y,a_end.z);
} else if(dx==0) {
if(dy>0)
ScanLine ( a_beg.y, a_beg.x,a_beg.z, dy, dx, dz,sw2);
else
ScanLine ( a_end.y, a_end.x,a_end.z,-dy, dx,-dz,sw2);
} else if(dx>0) {
if((0<=dy) && (dy<=dx)) /*1*/
ScanLine ( a_beg.x, a_beg.y,a_beg.z, dx, dy, dz,sw1);
else if(dx<dy) /*2*/
ScanLine ( a_beg.y, a_beg.x,a_beg.z, dy, dx, dz,sw2);
else if((-dx<=dy) && (dy<0) ) /*3*/
ScanLine ( a_beg.x, a_beg.y,a_beg.z, dx,-dy, dz,sw3);
else if(dy<-dx) /*4*/
ScanLine ( a_end.y, a_end.x,a_end.z,-dy, dx,-dz,sw4);
} else { //dx<0
if((0<=dy) && (dy<=-dx)) /*5*/
ScanLine ( a_end.x, a_end.y,a_end.z,-dx, dy,-dz,sw3);
else if(-dx<dy) /*6*/
ScanLine ( a_beg.y, a_beg.x,a_beg.z, dy,-dx, dz,sw4);
else if((dx<=dy) && (dy<0) ) /*7*/
ScanLine ( a_end.x, a_end.y,a_end.z,-dx,-dy,-dz,sw1);
else if(dy<dx) /*8*/
ScanLine ( a_end.y, a_end.x,a_end.z,-dy,-dx,-dz,sw2);
}
}
static void ScanLine(ZPos a_x,ZPos a_y,ZZ a_z,
ZPos a_dx,ZPos a_dy,ZZ a_dz,
scan_writer& a_proc){
typedef void(buffer::*scan_write_point_func)(ZPos,ZPos,ZZ,ZPos,unsigned int,ZPixel);
void ScanLine(ZPos a_x,ZPos a_y,ZZ a_z,
ZPos a_dx,ZPos a_dy,ZZ a_dz,
unsigned int a_size,ZPixel a_pixel,
scan_write_point_func a_func){
// Mid point algorithm
// assume 0<dx 0<=dy<=dx
@@ -578,25 +346,25 @@ protected:
ZPos beg = a_y;
ZZ incz = a_dz/(ZZ)a_dx;
if(a_dy==0) {
a_proc.write(a_x,a_y,a_z,beg);
(this->*a_func)(a_x,a_y,a_z,beg,a_size,a_pixel);
while(a_x<end){
a_x++;
a_z += incz;
a_proc.write(a_x,a_y,a_z,beg);
(this->*a_func)(a_x,a_y,a_z,beg,a_size,a_pixel);
}
} else if(a_dy==a_dx) {
a_proc.write(a_x,a_y,a_z,beg);
(this->*a_func)(a_x,a_y,a_z,beg,a_size,a_pixel);
while(a_x<end){
a_x++;
a_y++;
a_z += incz;
a_proc.write(a_x,a_y,a_z,beg);
(this->*a_func)(a_x,a_y,a_z,beg,a_size,a_pixel);
}
} else {
ZPos d = 2 * a_dy - a_dx;
ZPos incrE = 2 * a_dy;
ZPos incrNE = 2 * ( a_dy - a_dx);
a_proc.write(a_x,a_y,a_z,beg);
(this->*a_func)(a_x,a_y,a_z,beg,a_size,a_pixel);
while(a_x<end){
if(d<=0){
d += incrE;
@@ -607,11 +375,57 @@ protected:
a_y++;
}
a_z += incz;
a_proc.write(a_x,a_y,a_z,beg);
(this->*a_func)(a_x,a_y,a_z,beg,a_size,a_pixel);
}
}
}
void WriteLine(const point& a_beg,
const point& a_end,
unsigned int a_size,ZPixel a_pixel){
ZPos dx = a_end.x - a_beg.x;
ZPos dy = a_end.y - a_beg.y;
ZZ dz = a_end.z - a_beg.z;
// 6 2
// 5 1
// 7 3
// 8 4
if( (dx==0) && (dy==0) ) {
write_point(a_beg.x,a_beg.y,a_beg.z,a_size,a_pixel);
write_point(a_end.x,a_end.y,a_end.z,a_size,a_pixel);
} else if(dx==0) {
if(dy>0)
ScanLine ( a_beg.y, a_beg.x,a_beg.z, dy, dx, dz, a_size,a_pixel,&buffer::scan_write_point_2);
else
ScanLine ( a_end.y, a_end.x,a_end.z,-dy, dx,-dz, a_size,a_pixel,&buffer::scan_write_point_2);
} else if(dx>0) {
if((0<=dy) && (dy<=dx)) /*1*/
ScanLine ( a_beg.x, a_beg.y,a_beg.z, dx, dy, dz, a_size,a_pixel,&buffer::scan_write_point_1);
else if(dx<dy) /*2*/
ScanLine ( a_beg.y, a_beg.x,a_beg.z, dy, dx, dz, a_size,a_pixel,&buffer::scan_write_point_2);
else if((-dx<=dy) && (dy<0) ) /*3*/
ScanLine ( a_beg.x, a_beg.y,a_beg.z, dx,-dy, dz, a_size,a_pixel,&buffer::scan_write_point_3);
else if(dy<-dx) /*4*/
ScanLine ( a_end.y, a_end.x,a_end.z,-dy, dx,-dz, a_size,a_pixel,&buffer::scan_write_point_4);
} else { //dx<0
if((0<=dy) && (dy<=-dx)) /*5*/
ScanLine ( a_end.x, a_end.y,a_end.z,-dx, dy,-dz, a_size,a_pixel,&buffer::scan_write_point_3);
else if(-dx<dy) /*6*/
ScanLine ( a_beg.y, a_beg.x,a_beg.z, dy,-dx, dz, a_size,a_pixel,&buffer::scan_write_point_4);
else if((dx<=dy) && (dy<0) ) /*7*/
ScanLine ( a_end.x, a_end.y,a_end.z,-dx,-dy,-dz, a_size,a_pixel,&buffer::scan_write_point_1);
else if(dy<dx) /*8*/
ScanLine ( a_end.y, a_end.x,a_end.z,-dy,-dx,-dz, a_size,a_pixel,&buffer::scan_write_point_2);
}
}
protected:
bool m_depth_test;
bool m_blend;
+60 -40
View File
@@ -6,92 +6,112 @@
#include "../sg/GL_viewer"
#include <QtCore/qglobal.h> //For QT_VERSION.
#if QT_VERSION < 0x060000
#include <QtOpenGL/qgl.h>
#else
#include <QtOpenGLWidgets/qopenglwidget.h>
#endif
#include <QtGui/qevent.h>
#include <tools/sg/device_interactor>
#ifdef TOOLS_MEM
#include <tools/mem>
#endif
namespace toolx {
namespace Qt {
class glarea : public QGLWidget {
typedef QGLWidget parent;
public:
virtual void initialieGL() {}
virtual void resizeGL(int a_w,int a_h){
//::printf("debug : resize : %d %d\n",a_w,a_h);
#if QT_VERSION < 0x050000
class glarea
#if QT_VERSION < 0x060000
:public QGLWidget
#else
m_width = a_w;
m_height = a_h;
:public QOpenGLWidget
#endif
}
//virtual void paintGL();
{
#if QT_VERSION < 0x060000
typedef QGLWidget parent;
#else
using parent = QOpenGLWidget;
#endif
TOOLS_SCLASS(toolx::Qt::glarea)
public:
virtual void initializeGL() {}
virtual void paintGL() {
#if QT_VERSION < 0x050000
//::printf("debug : toolx::Qt::glarea::paintGL %d %d\n",width(),height());
m_viewer.set_size(width(),height());
#else
//::printf("debug : toolx::Qt::glarea::paintGL %d %d\n",m_width,m_height);
m_viewer.set_size(m_width,m_height);
m_viewer.set_size(devicePixelRatio()*width(),devicePixelRatio()*height());
#endif
m_viewer.render();
}
virtual void keyPressEvent(QKeyEvent* a_event) {
if(!m_interactor) return;
tools::sg::key_down_event event(convert(a_event->key()));
m_interactor->key_press(event);
tools::sg::key_down_event _event(convert_key(a_event->key()));
m_interactor->key_press(_event);
}
virtual void keyReleaseEvent(QKeyEvent* a_event) {
if(!m_interactor) return;
tools::sg::key_up_event event(convert(a_event->key()));
m_interactor->key_release(event);
tools::sg::key_up_event _event(convert_key(a_event->key()));
m_interactor->key_release(_event);
}
virtual void mousePressEvent(QMouseEvent* a_event) {
if(!m_interactor) return;
tools::sg::mouse_down_event event(a_event->x(),a_event->y());
m_interactor->mouse_press(event);
#if QT_VERSION < 0x060000
tools::sg::mouse_down_event _event(a_event->x(),a_event->y());
#else
tools::sg::mouse_down_event _event(a_event->position().x(),a_event->position().y());
#endif
m_interactor->mouse_press(_event);
}
virtual void mouseReleaseEvent(QMouseEvent* a_event) {
if(!m_interactor) return;
tools::sg::mouse_up_event event(a_event->x(),a_event->y());
m_interactor->mouse_release(event);
#if QT_VERSION < 0x060000
tools::sg::mouse_up_event _event(a_event->x(),a_event->y());
#else
tools::sg::mouse_up_event _event(a_event->position().x(),a_event->position().y());
#endif
m_interactor->mouse_release(_event);
}
virtual void mouseMoveEvent(QMouseEvent* a_event) {
if(!m_interactor) return;
tools::sg::mouse_move_event event(a_event->x(),a_event->y(),0,0,false);
m_interactor->mouse_move(event);
#if QT_VERSION < 0x060000
tools::sg::mouse_move_event _event(a_event->x(),a_event->y(),0,0,false);
#else
tools::sg::mouse_move_event _event(a_event->position().x(),a_event->position().y(),0,0,false);
#endif
m_interactor->mouse_move(_event);
}
//virtual void mouseDoubleClickEvent(QMouseEvent* a_event) {
// if(!m_interactor) return;
// tools::sg::mouse_double_click_event event(a_event->x(),a_event->y());
// m_interactor->mouse_double_click(event);
//}
virtual void wheelEvent(QWheelEvent* a_event) {
if(!m_interactor) return;
tools::sg::wheel_rotate_event event(a_event->angleDelta().y());
m_interactor->wheel_rotate(event);
tools::sg::wheel_rotate_event _event(a_event->angleDelta().y());
m_interactor->wheel_rotate(_event);
}
public:
glarea(QWidget* a_parent,toolx::sg::GL_viewer& a_viewer):parent(a_parent),m_viewer(a_viewer),m_interactor(0){}
virtual ~glarea(){}
glarea(QWidget* a_parent,toolx::sg::GL_viewer& a_viewer):parent(a_parent),m_viewer(a_viewer),m_interactor(0){
#ifdef TOOLS_MEM
tools::mem::increment(s_class().c_str());
#endif
}
virtual ~glarea(){
#ifdef TOOLS_MEM
tools::mem::decrement(s_class().c_str());
#endif
}
public:
void set_device_interactor(tools::sg::device_interactor* a_interactor) {m_interactor = a_interactor;} //we do not have ownership.
protected:
tools::key_code convert(/*Qt::Key*/int a_key) {return a_key;}
tools::key_code convert_key(/*Qt::Key*/int a_key) {return a_key;}
protected:
toolx::sg::GL_viewer& m_viewer;
#if QT_VERSION < 0x050000
#else
protected:
int m_width;
int m_height;
#endif
tools::sg::device_interactor* m_interactor;
};
}}
#endif
+121
View File
@@ -0,0 +1,121 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef toolx_Qt_pixwin
#define toolx_Qt_pixwin
#include <QtCore/qglobal.h> //For QT_VERSION.
#if QT_VERSION < 0x050000
#include <QtGui/qwidget.h>
#else
#include <QtWidgets/qwidget.h>
#endif
#include <QtGui/qevent.h>
#include <QtGui/qpainter.h>
#include <tools/sg/zb_viewer>
#include <tools/sg/device_interactor>
#ifdef TOOLS_MEM
#include <tools/mem>
#endif
namespace toolx {
namespace Qt {
class pixwin : public QWidget
{
typedef QWidget parent;
TOOLS_SCLASS(toolx::Qt::pixwin)
public:
virtual void resizeEvent(QResizeEvent* a_event) {
//::printf("debug : toolx::Qt::pixwin::resizeEvent : %d, %d\n",a_event->size().width(),a_event->size().height());
parent::resizeEvent(a_event);
m_viewer.set_size(a_event->size().width(),a_event->size().height());
}
virtual void paintEvent(QPaintEvent* a_event) {
parent::paintEvent(a_event);
if(!m_viewer.render(tools::sg::zb_viewer::get_rgbas,false)) return; //rgbas because Qt wants an image 32 bits aligned.
//static size_t s_count = 0;::printf("debug : toolx::Qt::pixwin::paintEvent %d %d : %lu\n",parent::m_ww,parent::m_wh,s_count);s_count++;
QImage image(tools::vec_data(m_viewer.out_buffer()),m_viewer.width(),m_viewer.height(),QImage::Format_RGBA8888);
QPixmap pixmap = QPixmap::fromImage(image);
QPainter painter(this);
painter.drawPixmap(0,0,pixmap);
//parent::after_render();
m_viewer.out_buffer_clear();
}
virtual void keyPressEvent(QKeyEvent* a_event) {
if(!m_interactor) return;
tools::sg::key_down_event _event(convert_key(a_event->key()));
m_interactor->key_press(_event);
}
virtual void keyReleaseEvent(QKeyEvent* a_event) {
if(!m_interactor) return;
tools::sg::key_up_event _event(convert_key(a_event->key()));
m_interactor->key_release(_event);
}
virtual void mousePressEvent(QMouseEvent* a_event) {
if(!m_interactor) return;
#if QT_VERSION < 0x060000
tools::sg::mouse_down_event _event(a_event->x(),a_event->y());
#else
tools::sg::mouse_down_event _event(a_event->position().x(),a_event->position().y());
#endif
m_interactor->mouse_press(_event);
}
virtual void mouseReleaseEvent(QMouseEvent* a_event) {
if(!m_interactor) return;
#if QT_VERSION < 0x060000
tools::sg::mouse_up_event _event(a_event->x(),a_event->y());
#else
tools::sg::mouse_up_event _event(a_event->position().x(),a_event->position().y());
#endif
m_interactor->mouse_release(_event);
}
virtual void mouseMoveEvent(QMouseEvent* a_event) {
if(!m_interactor) return;
#if QT_VERSION < 0x060000
tools::sg::mouse_move_event _event(a_event->x(),a_event->y(),0,0,false);
#else
tools::sg::mouse_move_event _event(a_event->position().x(),a_event->position().y(),0,0,false);
#endif
m_interactor->mouse_move(_event);
}
//virtual void mouseDoubleClickEvent(QMouseEvent* a_event) {
// if(!m_interactor) return;
// tools::sg::mouse_double_click_event _event(a_event->x(),a_event->y());
// m_interactor->mouse_double_click(_event);
//}
virtual void wheelEvent(QWheelEvent* a_event) {
if(!m_interactor) return;
tools::sg::wheel_rotate_event _event(a_event->angleDelta().y());
m_interactor->wheel_rotate(_event);
}
public:
pixwin(QWidget* a_parent,tools::sg::zb_viewer& a_viewer):parent(a_parent),m_viewer(a_viewer),m_interactor(0){
#ifdef TOOLS_MEM
tools::mem::increment(s_class().c_str());
#endif
}
virtual ~pixwin(){
#ifdef TOOLS_MEM
tools::mem::decrement(s_class().c_str());
#endif
}
public:
void set_device_interactor(tools::sg::device_interactor* a_interactor) {m_interactor = a_interactor;} //we do not have ownership.
protected:
tools::key_code convert_key(/*Qt::Key*/int a_key) {return a_key;}
protected:
tools::sg::zb_viewer& m_viewer;
tools::sg::device_interactor* m_interactor;
};
}}
#endif
+25
View File
@@ -0,0 +1,25 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef toolx_Qt_s2q
#define toolx_Qt_s2q
#include <QtCore/qglobal.h> //For QT_VERSION.
#include <QtCore/qstring.h>
namespace toolx {
namespace Qt {
inline QString s2q(const std::string& a_s) {
#if QT_VERSION < 0x050000
return QString::fromAscii(a_s.c_str());
#else
return QString::fromLatin1(a_s.c_str());
#endif
}
}}
#endif
+11 -12
View File
@@ -6,10 +6,8 @@
// pure Qt code, no GL.
#include <ostream>
#include <vector>
#include "s2q"
#include <QtCore/qglobal.h> //For QT_VERSION.
#include <QtCore/qnamespace.h>
#if QT_VERSION < 0x050000
@@ -20,6 +18,9 @@
#include <QtWidgets/qwidget.h>
#endif
#include <ostream>
#include <vector>
namespace toolx {
namespace Qt {
@@ -38,6 +39,11 @@ public:
m_own_qapp = true;
}
}
session(std::ostream& a_out,QApplication* a_qapp)
:m_out(a_out)
,m_qapp(a_qapp)
,m_own_qapp(false)
{}
virtual ~session() {
if(m_own_qapp && m_qapp) delete m_qapp;
m_qapp = 0;
@@ -63,7 +69,7 @@ public:
}
bool sync() {
if(!m_qapp) return false;
//m_qapp->exec();
m_qapp->processEvents();
return true;
}
public:
@@ -89,14 +95,6 @@ public:
if(!m_qapp) return;
delete a_window;
}
protected:
QString s2q(const std::string& a_s) {
#if QT_VERSION < 0x050000
return QString::fromAscii(a_s.c_str());
#else
return QString::fromLatin1(a_s.c_str());
#endif
}
protected:
std::ostream& m_out;
QApplication* m_qapp;
@@ -105,5 +103,6 @@ protected:
}}
#endif
+106
View File
@@ -0,0 +1,106 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef toolx_Qt_zb_viewer
#define toolx_Qt_zb_viewer
#include "pixwin"
#include <tools/sg/zb_viewer>
#include "session"
#include <QtCore/qglobal.h> //For QT_VERSION.
#if QT_VERSION < 0x050000
#include <QtGui/qboxlayout.h>
#else
#include <QtWidgets/qboxlayout.h>
#endif
namespace toolx {
namespace Qt {
class zb_viewer: public tools::sg::zb_viewer {
typedef tools::sg::zb_viewer parent;
typedef pixwin render_area_t;
public:
zb_viewer(session& a_session,
int a_x = 0,int a_y = 0,
unsigned int a_width = 500,unsigned int a_height = 500,
const std::string& a_win_title = "")
:parent(a_session.out(),a_width,a_height)
,m_session(a_session)
,m_shell(0)
,m_own_shell(false)
,m_render_area(0)
{
if(!m_session.is_valid()) return; //throw
m_shell = m_session.create_window(a_win_title.c_str(),a_x,a_y,a_width,a_height);
if(!m_shell) return; //throw
m_own_shell = true;
m_render_area = new render_area_t(0,*this);
QVBoxLayout* layout = new QVBoxLayout;
layout->setContentsMargins(0,0,0,0);
layout->addWidget(m_render_area);
m_shell->setLayout(layout);
}
virtual ~zb_viewer() {
if(m_render_area) m_render_area->set_device_interactor(0);
if(m_shell && m_own_shell) {
m_session.delete_window(m_shell);
//m_session.sync();
}
}
protected:
zb_viewer(const zb_viewer& a_from)
:parent(a_from)
,m_session(a_from.m_session)
,m_shell(0)
,m_own_shell(false)
,m_render_area(0)
{}
zb_viewer& operator=(const zb_viewer& a_from){
parent::operator=(a_from);
return *this;
}
public:
bool has_window() const {return m_shell?true:false;} //for SWIG
bool show() {
if(!m_shell) return false;
m_shell->show();
return true;
}
void win_render() {
if(!m_render_area) return;
//m_render_area->update(); //delayed.
m_render_area->repaint(); //immediate.
}
void set_device_interactor(tools::sg::device_interactor* a_interactor) { //we do not have ownership.
if(!m_render_area) return;
m_render_area->set_device_interactor(a_interactor);
}
public:
QWidget* shell() {return m_shell;}
void set_own_shell(bool a_value) {m_own_shell = a_value;}
render_area_t* render_area() {return m_render_area;}
void enable_keyboard_focus() {
if(!m_render_area) return;
m_render_area->setFocusPolicy(::Qt::StrongFocus);
}
protected:
session& m_session;
QWidget* m_shell;
bool m_own_shell;
render_area_t* m_render_area;
};
}}
#endif
+365
View File
@@ -0,0 +1,365 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef toolx_Windows_pixwin
#define toolx_Windows_pixwin
#include <windows.h>
#include <windowsx.h>
#include <wingdi.h>
#include <tools/sg/device_interactor>
#include <tools/sg/keys>
//#include <tools/log_file>
#include <string>
#if defined(_MSC_VER) && _MSC_VER < 1900
#elif defined(__MINGW32__)
#else
#define TOOLX_WINDOWS_TOUCH
#endif
namespace toolx {
namespace Windows {
#ifdef TOOLX_WINDOWS_TOUCH
inline bool is_message_touch_event() {
// from https://stackoverflow.com/questions/29857587/detect-if-wm-mousemove-is-caused-by-touch-pen.
static const LONG_PTR c_SIGNATURE_MASK = 0xFFFFFF00;
static const LONG_PTR c_MOUSEEVENTF_FROMTOUCH = 0xFF515700;
LONG_PTR extraInfo = ::GetMessageExtraInfo();
return ( ( extraInfo & c_SIGNATURE_MASK ) == c_MOUSEEVENTF_FROMTOUCH );
}
#endif
class pixwin {
static const std::string& s_class() {
static const std::string s_v("toolx::Windows::pixwin");
return s_v;
}
static void register_class(){
static bool s_done = false; //not const, then not thread safe.
if(!s_done) {
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)proc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = ::GetModuleHandle(NULL);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
wc.lpszMenuName = s_class().c_str();
wc.lpszClassName = s_class().c_str();
::RegisterClass(&wc);
s_done = true;
}
}
public:
virtual void resize(unsigned int,unsigned int){}
//virtual void paint(HDC) {}
virtual void paint() {}
virtual void close(){}
virtual void left_button_up(unsigned int a_x,unsigned int a_y) {}
virtual void left_button_down(unsigned int a_x,unsigned int a_y) {}
virtual void mouse_move(unsigned int a_x,unsigned int a_y,bool) {}
public:
pixwin(HWND a_parent,unsigned int a_w,unsigned int a_h)
:m_parent(a_parent)
,m_hwnd(0)
,m_touch_available(false)
,m_interactor(0)
{
register_class();
m_hwnd = ::CreateWindow(s_class().c_str(),
//m_hwnd = ::CreateWindowEx(WS_EX_LAYERED,s_class().c_str(),
NULL,
WS_CHILD | WS_VISIBLE,
0,0,
a_w,a_h,
m_parent,NULL,
GetWindowInstance(m_parent),
NULL);
if(!m_hwnd) return;
::SetWindowLongPtr(m_hwnd,GWLP_USERDATA,LONG_PTR(this));
#ifdef TOOLX_WINDOWS_TOUCH
{BYTE digitizer_status = (BYTE)::GetSystemMetrics(SM_DIGITIZER);
if((digitizer_status & (0x80 + 0x40)) == 0) {
m_touch_available = false;
} else {
//BYTE nInputs = (BYTE)::GetSystemMetrics(SM_MAXIMUMTOUCHES);
m_touch_available = true;
if(!::RegisterTouchWindow(m_hwnd,0)) m_touch_available = false;
}}
#endif
}
virtual ~pixwin(){
if(m_hwnd) {
::SetWindowLongPtr(m_hwnd,GWLP_USERDATA,LONG_PTR(NULL));
::DestroyWindow(m_hwnd);
m_hwnd = 0;
}
}
protected:
pixwin(const pixwin& a_from)
:m_parent(a_from.m_parent)
,m_hwnd(0)
,m_touch_available(a_from.m_touch_available)
,m_interactor(0)
{
if(!m_parent) return;
register_class();
RECT rect;
::GetClientRect(m_parent,&rect);
m_hwnd = ::CreateWindow(s_class().c_str(),
NULL,
WS_CHILD | WS_VISIBLE,
0,0,
rect.right-rect.left,
rect.bottom-rect.top,
m_parent,NULL,
GetWindowInstance(m_parent),
NULL);
if(!m_hwnd) return;
::SetWindowLongPtr(m_hwnd,GWLP_USERDATA,LONG_PTR(this));
#ifdef TOOLX_WINDOWS_TOUCH
if(a_from.m_touch_available) {
if(!::RegisterTouchWindow(m_hwnd,0)) m_touch_available = false;
}
#endif
}
protected:
pixwin& operator=(const pixwin&){return *this;}
public:
void set_client_area_size(unsigned int a_w,unsigned int a_h) {
RECT rect;
::GetClientRect(m_hwnd,&rect);
::MoveWindow(m_hwnd,rect.left,rect.top,a_w,a_h,TRUE);
}
HWND hwnd() const {return m_hwnd;}
void wm_paint() {paint();}
public:
void set_device_interactor(tools::sg::device_interactor* a_interactor) {m_interactor = a_interactor;} //we do not have ownership.
protected:
bool is_touch_event() {
#ifdef TOOLX_WINDOWS_TOUCH
if(!m_touch_available) return false;
return is_message_touch_event();
#else
return false;
#endif
}
protected:
static LRESULT CALLBACK proc(HWND a_hwnd,UINT a_msg,WPARAM a_wparam,LPARAM a_lparam) {
switch (a_msg) {
case WM_SIZE:{
int width = LOWORD(a_lparam);
int height = HIWORD(a_lparam);
pixwin* _this = (pixwin*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
if(_this) {
_this->resize(width,height);
} else {
// CreateWindow send a WM_SIZE but GWLP_USERDATA not yet set.
}
}return 0;
case WM_PAINT:{
pixwin* _this = (pixwin*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
if(_this) _this->wm_paint();
}return 0;
case WM_KEYDOWN:{
pixwin* _this = (pixwin*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
if(_this) {
if(_this->m_interactor) {
tools::sg::key_down_event event(_this->convert_key(a_wparam));
_this->m_interactor->key_press(event);
}
}
} return 0;
case WM_KEYUP:{
pixwin* _this = (pixwin*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
if(_this) {
if(_this->m_interactor) {
tools::sg::key_up_event event(_this->convert_key(a_wparam));
_this->m_interactor->key_release(event);
}
}
} return 0;
case WM_LBUTTONDOWN:{
pixwin* _this = (pixwin*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
if(_this) {
if(_this->m_interactor) {
tools::sg::mouse_down_event event(LOWORD(a_lparam),HIWORD(a_lparam));
_this->m_interactor->mouse_press(event);
} else {
RECT rect;
::GetClientRect(a_hwnd,&rect);
unsigned int h = rect.bottom-rect.top;
if(!_this->is_touch_event()) _this->left_button_down(LOWORD(a_lparam),h-HIWORD(a_lparam));
}
}
}return 0;
case WM_LBUTTONUP:{
pixwin* _this = (pixwin*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
if(_this) {
if(_this->m_interactor) {
tools::sg::mouse_up_event event(LOWORD(a_lparam),HIWORD(a_lparam));
_this->m_interactor->mouse_release(event);
} else {
RECT rect;
::GetClientRect(a_hwnd,&rect);
unsigned int h = rect.bottom-rect.top;
if(!_this->is_touch_event()) _this->left_button_up(LOWORD(a_lparam),h-HIWORD(a_lparam));
}
}
} return 0;
case WM_MOUSEMOVE:{
pixwin* _this = (pixwin*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
if(_this) {
if(_this->m_interactor) {
tools::sg::mouse_move_event event(LOWORD(a_lparam),HIWORD(a_lparam),0,0,false);
_this->m_interactor->mouse_move(event);
} else {
WPARAM state = a_wparam;
bool ldown = ((state & MK_LBUTTON)==MK_LBUTTON)?true:false;
RECT rect;
::GetClientRect(a_hwnd,&rect);
unsigned int h = rect.bottom-rect.top;
if(!_this->is_touch_event()) _this->mouse_move(LOWORD(a_lparam),h-HIWORD(a_lparam),ldown);
}
}
}return 0;
case WM_MOUSEWHEEL:{
pixwin* _this = (pixwin*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
if(_this) {
if(_this->m_interactor) {
tools::sg::wheel_rotate_event event(GET_WHEEL_DELTA_WPARAM(a_wparam));
_this->m_interactor->wheel_rotate(event);
}
}
} return 0;
#ifdef TOOLX_WINDOWS_TOUCH
case WM_TOUCH:{
pixwin* _this = (pixwin*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
if(_this && _this->m_touch_available) {
RECT rect;
//::GetWindowRect(hwnd,&rect); ???
::GetClientRect(a_hwnd,&rect);
unsigned int h = rect.bottom-rect.top;
unsigned int num_inputs = (int)a_wparam;
//::printf("debug : WM_TOUCH : 001 : %d\n",num_inputs);
TOUCHINPUT* ti = new TOUCHINPUT[num_inputs];
POINT p;
if(::GetTouchInputInfo((HTOUCHINPUT)a_lparam,num_inputs,ti,sizeof(TOUCHINPUT))) {
for(unsigned int i=0;i<num_inputs; ++i) {
p.x = TOUCH_COORD_TO_PIXEL(ti[i].x);
p.y = TOUCH_COORD_TO_PIXEL(ti[i].y);
if(!::ScreenToClient(a_hwnd,&p)) {}
if(ti[i].dwFlags & TOUCHEVENTF_DOWN) {
//::printf("debug : TOUCHEVENTF_DOWN %lu %lu\n",p.x,p.y);
_this->left_button_down(p.x,h-p.y);
} else if (ti[i].dwFlags & TOUCHEVENTF_UP) {
//::printf("debug : TOUCHEVENTF_UP %lu %lu\n",p.x,p.y);
_this->left_button_up(p.x,h-p.y);
} else if (ti[i].dwFlags & TOUCHEVENTF_MOVE) {
bool ldown = true; //we assume that a TOUCHEVENT_DOWN had been done.
//::printf("debug : TOUCHEVENTF_MOVE %lu %lu\n",p.x,p.y);
_this->mouse_move(p.x,h-p.y,ldown);
}
}
}
::CloseTouchInputHandle((HTOUCHINPUT)a_lparam);
delete [] ti;
}
}return 0;
#endif //TOOLX_WINDOWS_TOUCH
case WM_DESTROY:wm__destroy(a_hwnd);return 0;
}
return (DefWindowProc(a_hwnd,a_msg,a_wparam,a_lparam));
}
static bool SetWindowPixelFormat(HDC a_HDC){
PIXELFORMATDESCRIPTOR pfd;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags =
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER |
PFD_STEREO_DONTCARE;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cRedBits = 8;
pfd.cRedShift = 16;
pfd.cGreenBits = 8;
pfd.cGreenShift = 8;
pfd.cBlueBits = 8;
pfd.cBlueShift = 0;
pfd.cAlphaBits = 0;
pfd.cAlphaShift = 0;
pfd.cAccumBits = 64;
pfd.cAccumRedBits = 16;
pfd.cAccumGreenBits = 16;
pfd.cAccumBlueBits = 16;
pfd.cAccumAlphaBits = 0;
pfd.cDepthBits = 32;
pfd.cStencilBits = 8;
pfd.cAuxBuffers = 0;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.bReserved = 0;
pfd.dwLayerMask = 0;
pfd.dwVisibleMask = 0;
pfd.dwDamageMask = 0;
int pixelIndex = ::ChoosePixelFormat(a_HDC,&pfd);
if (pixelIndex==0) {
// Let's choose a default index.
pixelIndex = 1;
if (::DescribePixelFormat(a_HDC,
pixelIndex,
sizeof(PIXELFORMATDESCRIPTOR),
&pfd)==0) {
return false;
}
}
if (::SetPixelFormat(a_HDC,pixelIndex,&pfd)==FALSE) return false;
return true;
}
static void wm__destroy(HWND a_hwnd) {
pixwin* _this = (pixwin*)::GetWindowLongPtr(a_hwnd,GWLP_USERDATA);
if(_this) { //How to be sure that we have a pixwin* ???
_this->close();
if(_this->m_hwnd!=a_hwnd) {
//::printf("WinTk::Component::wm_destroy : HWND mismatch !\n");
}
_this->m_hwnd = 0;
}
::SetWindowLongPtr(a_hwnd,GWLP_USERDATA,LONG_PTR(NULL));
}
protected:
tools::key_code convert_key(WPARAM a_key) {
if(a_key==VK_SHIFT) return tools::sg::key_shift();
return (tools::key_code)a_key;
}
protected:
HWND m_parent;
HWND m_hwnd;
bool m_touch_available;
tools::sg::device_interactor* m_interactor;
};
}}
#endif
@@ -8,6 +8,7 @@
#include <windowsx.h>
#include <ostream>
#include <cstdlib>
namespace toolx {
namespace Windows {
@@ -64,10 +65,18 @@ public:
//void post(HWND a_hwnd,LPARAM a_msg){
// ::PostMessage(a_hwnd,WM_USER,(WPARAM)0,a_msg);
//}
public:
bool tmp_dir(std::string& a_dir) {
char* env = ::getenv("TEMP");
if(!env) {a_dir.clear();return false;}
a_dir = env;
return true;
}
protected:
std::ostream& m_out;
};
}}
#endif
@@ -0,0 +1,91 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef toolx_Windows_zb_viewer
#define toolx_Windows_zb_viewer
#include "session"
#include "window"
#include "pixwin"
#include <tools/sg/zb_viewer>
// disable the warning about the usage of "this" in the constructor.
#pragma warning(disable:4355)
namespace toolx {
namespace Windows {
class zb_viewer : public window, protected pixwin, public tools::sg::zb_viewer {
typedef window parent_window;
typedef pixwin parent_render_area;
typedef tools::sg::zb_viewer parent_viewer;
protected:
virtual void paint() {
//parent_viewer::m_out << "toolx::Windows::zb_viewer::paint " << parent_viewer::m_ww << " " << parent_viewer::m_wh << std::endl;
if(!render(get_bgras,false)) return;
HDC hDC = ::GetDC(parent_render_area::m_hwnd);
HDC bitmap_DC = ::CreateCompatibleDC(hDC);
HBITMAP bitmap = ::CreateBitmap(parent_viewer::m_ww,parent_viewer::m_wh, 1, 32, tools::vec_data(m_out_buffer));
HBITMAP old_bitmap = (HBITMAP)::SelectObject(bitmap_DC, bitmap);
::BitBlt(hDC,0,0,parent_viewer::m_ww,parent_viewer::m_wh,bitmap_DC,0,0,SRCCOPY);
::SelectObject(bitmap_DC, old_bitmap);
::DeleteObject(bitmap);
::DeleteDC(bitmap_DC);
::ReleaseDC(parent_render_area::m_hwnd,hDC);
::ValidateRect(parent_render_area::m_hwnd,NULL);
m_out_buffer.clear();
}
virtual void resize(unsigned int a_w,unsigned int a_h){ //NOTE: it is not called at startup.
//parent_viewer::m_out << "toolx::Windows::zb_viewer::resize " << a_w << " " << a_h << std::endl;
set_size(a_w,a_h);
}
public:
virtual void close() {}
public:
zb_viewer(session& a_session,
int a_x = 0,int a_y = 0,
unsigned int a_width = 500,unsigned int a_height = 500,
const std::string& a_title = "")
:parent_window(a_title.c_str(),a_x,a_y,a_width,a_height)
,parent_render_area(parent_window::m_hwnd,a_width,a_height)
,parent_viewer(a_session.out(),a_width,a_height)
,m_session(a_session)
{
parent_window::set_focus_hwnd(parent_render_area::m_hwnd);
}
virtual ~zb_viewer() {}
protected:
zb_viewer(const zb_viewer& a_from)
:parent_window(a_from)
,parent_render_area(a_from)
,parent_viewer(a_from)
,m_session(a_from.m_session)
{}
zb_viewer& operator=(const zb_viewer& a_from){
parent_window::operator=(a_from);
return *this;
}
public:
bool has_window() const {return parent_window::m_hwnd?true:false;} //for SWIG
HWND window() const {return parent_window::m_hwnd;}
bool show() {
if(!parent_window::m_hwnd) return false;
m_session.show_window(parent_window::m_hwnd);
return true;
}
void win_render() {parent_render_area::wm_paint();}
void set_device_interactor(tools::sg::device_interactor* a_interactor) { //we do not have ownership.
parent_render_area::set_device_interactor(a_interactor);
}
protected:
session& m_session;
};
}}
#endif
+27 -3
View File
@@ -12,9 +12,9 @@
#include "dispatcher"
#include <tools/forit>
#include <X11/Xatom.h> //XA_INTEGER
#include <X11/Xatom.h> //XA_INTEGER
#include <X11/Xutil.h> //XVisualInfo
#include <X11/Xutil.h> //XVisualInfo
namespace toolx {
namespace X11 {
@@ -96,6 +96,7 @@ public:
bool is_valid() const {return m_display?true:false;}
unsigned int monitor() const {return m_monitor;}
Display* display() const {return m_display;}
bool steer() {
@@ -367,7 +368,29 @@ protected:
static Bool wait_unmap_notify(Display*,XEvent* a_event,char* a_arg){
return (a_event->type == UnmapNotify) && (a_event->xmap.window == (Window)a_arg);
}
/*
void wait_xxx(Window a_window) {
if(!m_display) return;
{wait_what arg;
arg.m_event_type = ConfigureNotify;
arg.m_window = a_window;
XEvent event;
::XIfEvent(m_display,&event,wait_for,(char*)&arg);
dispatch(event);}
}
struct wait_what {
int m_event_type;
Window m_window;
};
static Bool wait_for(Display*,XEvent* a_event,char* a_arg){
wait_what* arg = (wait_what*)a_arg;
return (a_event->type == arg->m_event_type) &&
(a_event->xmap.window == arg->m_window);
}
*/
void clear_dispatchers() {
tools_vforit_npp(dispatcher*,m_dispatchers,it) {
dispatcher* obj = *it;
@@ -388,5 +411,6 @@ protected:
}}
#endif
+122
View File
@@ -0,0 +1,122 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef toolx_X11_colors
#define toolx_X11_colors
#include <X11/Xlib.h>
#include <vector>
#include <map>
namespace toolx {
namespace X11 {
typedef unsigned long Pixel;
typedef std::vector<Pixel> pixels_t;
typedef unsigned int rgb_t;
typedef std::map<rgb_t,Pixel> colors_t;
inline bool rgb2pixel(Display* a_display,unsigned int a_monitor,
unsigned char a_r,unsigned char a_g,unsigned char a_b,
Pixel& a_pixel,bool& a_allocated) {
// read_write visual class: PseudoColor, GrayScale, DirectColor.
// read_only visual class: TrueColor, StaticColor, StaticGray.
// Could allocate a read_only cell in a read_write, read_only visual class.
// Could allocate a read_write cell only in read_write visual class.
// StaticGray + (depth=1) is monochrome.
Screen* screen = ::XScreenOfDisplay(a_display,a_monitor);
int vclass = ::XDefaultVisualOfScreen(screen)->c_class;
Colormap cmap = ::XDefaultColormapOfScreen(screen);
bool is_grey = ((vclass==GrayScale) || (vclass==StaticGray) ) ? true : false;
float r = float(a_r)/255.0f;
float g = float(a_g)/255.0f;
float b = float(a_b)/255.0f;
XColor xcolor;
xcolor.pixel = 0L;
if(is_grey) {
float grey = 0.30f * r + 0.59f * g + 0.11f * b;
xcolor.red = (unsigned short) (grey * 0xffff);
xcolor.green = (unsigned short) (grey * 0xffff);
xcolor.blue = (unsigned short) (grey * 0xffff);
} else {
xcolor.red = (unsigned short) (r * 0xffff);
xcolor.green = (unsigned short) (g * 0xffff);
xcolor.blue = (unsigned short) (b * 0xffff);
}
// Could be done on read_only/read_write visual class.
// Cell is taken from a common pool. It is read only.
if(::XAllocColor(a_display,cmap,&xcolor)==0) {
// Color not found. Try to allocate a private color cell :
if((vclass==TrueColor)||(vclass==StaticColor)||(vclass==StaticGray)) { //Viusal class is read_only.
a_pixel = 0;
a_allocated = false;
return false;
}
if(::XAllocColorCells(a_display,cmap,False,NULL,0,&(xcolor.pixel),1)==0) { //read/write cell.
a_pixel = 0;
a_allocated = false;
return false;
}
xcolor.flags = DoRed|DoGreen|DoBlue;
::XStoreColor(a_display,cmap,&xcolor);
a_pixel = xcolor.pixel;
a_allocated = true;
} else {
a_pixel = xcolor.pixel;
a_allocated = false;
}
return true;
}
inline bool get_pixel(Display* a_display,unsigned int a_monitor,
pixels_t& a_pixels,colors_t& a_colors,
unsigned int a_rgba,
Pixel& a_pixel) {
colors_t::const_iterator it = a_colors.find(a_rgba);
if(it!=a_colors.end()) {a_pixel = (*it).second;return true;}
unsigned char* pos = (unsigned char*)&a_rgba;
bool allocated;
if(!rgb2pixel(a_display,a_monitor,*(pos+0),*(pos+1),*(pos+2),a_pixel,allocated)) {
Screen* screen = ::XScreenOfDisplay(a_display,a_monitor);
a_pixel = XWhitePixelOfScreen(screen);
return false;
}
if(allocated) a_pixels.push_back(a_pixel);
a_colors[a_rgba] = a_pixel;
return true;
}
inline void set_rgb_t(unsigned char a_r,unsigned char a_g,unsigned char a_b,rgb_t& a_rgb) {
unsigned char* pos = (unsigned char*)&a_rgb;
*pos = a_r;pos++;
*pos = a_g;pos++;
*pos = a_b;pos++;
*pos = 0;pos++;
}
inline bool set_foreground(Display* a_display,unsigned int a_monitor,GC a_gc,pixels_t& a_pixels,colors_t& a_colors,unsigned char a_r,unsigned char a_g,unsigned char a_b) {
Pixel pixel;
rgb_t rgb;
set_rgb_t(a_r,a_g,a_b,rgb);
if(!get_pixel(a_display,a_monitor,a_pixels,a_colors,rgb,pixel)) return false;
XGCValues gcv;
gcv.foreground = pixel;
if(::XChangeGC(a_display,a_gc,GCForeground,&gcv)==0) return false;
return true;
}
inline bool set_foregroundf(Display* a_display,unsigned int a_monitor,GC a_gc,pixels_t& a_pixels,colors_t& a_colors,float a_r,float a_g,float a_b) {
return set_foreground(a_display,a_monitor,a_gc,a_pixels,a_colors,a_r*255.0f,a_g*255.0f,a_b*255.0f);
}
}}
#endif
+121
View File
@@ -0,0 +1,121 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef toolx_X11_pixwin
#define toolx_X11_pixwin
#include "colors"
#include <tools/sg/zb_viewer>
//#include <tools/sys/atime>
#include <X11/Xlib.h>
#include <X11/Xutil.h> //XPutPixel
namespace toolx {
namespace X11 {
class pixwin {
public:
pixwin(std::ostream& a_out,unsigned int a_monitor,Display* a_display)
:m_out(a_out)
,m_monitor(a_monitor)
,m_display(a_display)
,m_GC(0)
,m_image(0)
{
if(!m_display) return;
m_GC = ::XCreateGC(m_display,XRootWindow(m_display,m_monitor),0,0);
}
virtual ~pixwin() {
free_pixels();
m_colors.clear();
if(m_GC) ::XFreeGC(m_display,m_GC);
free_XImage();
}
protected:
pixwin(const pixwin& a_from)
:m_out(a_from.m_out)
,m_monitor(0)
,m_display(0)
,m_GC(0)
,m_image(0)
{}
pixwin& operator=(const pixwin&){
m_monitor = 0;
m_display = 0;
m_GC = 0;
m_image = 0;
return *this;
}
public:
void put_buffer(Window a_win,unsigned int a_ww,unsigned int a_wh,const unsigned char* a_rgbas) {
if(!m_display) return;
if(!m_GC) return;
if(!m_image) alloc_XImage(a_ww,a_wh);
if(!m_image) return;
//tools::atime start = tools::atime::now();
const unsigned int* pos = (const unsigned int*)a_rgbas;
unsigned int row,col;
toolx::X11::Pixel pixel;
for(row=0;row<a_wh;row++) {
for(col=0;col<a_ww;col++) {
if(!get_pixel(m_display,m_monitor,m_pixels,m_colors,*pos,pixel)) {}
pos++;
XPutPixel(m_image,col,row,pixel);
}}
//::printf("debug : map::colors %lu, pixels %lu\n",m_colors.size(),m_pixels.size());
::XPutImage(m_display,a_win,m_GC,m_image,0,0,0,0,a_ww,a_wh);
//m_out << "pu_buffer : XImage " << tools::atime::elapsed(start) << std::endl;
}
void set_size(unsigned int a_ww,unsigned int a_wh) {
free_XImage();
alloc_XImage(a_ww,a_wh);
}
protected:
void alloc_XImage(unsigned int a_ww,unsigned int a_wh) {
if(m_image) return; //done.
if(!m_display) return;
Screen* screen = ::XScreenOfDisplay(m_display,m_monitor);
m_image = ::XCreateImage(m_display,::XDefaultVisualOfScreen(screen),::XDefaultDepthOfScreen(screen),ZPixmap,0,NULL,a_ww,a_wh,8,0);
if(!m_image) {
m_out << "toolx::X11::pixwin::alloc_XImage : can't create an XImage." << std::endl;
return;
}
//warning : a priori, a_ww*3 != m_image->bytes_per_line.
m_image->data = new char[a_wh*m_image->bytes_per_line];
if(!m_image->data) {
m_out << "toolx::X11::pixwin::alloc_XImage : can't alloc buffer." << std::endl;
::XFree((char*)m_image);
m_image = 0;
return;
}
}
void free_XImage() {
if(!m_image) return;
delete [] m_image->data;
::XFree((char*)m_image);
m_image = 0;
}
void free_pixels() {
if(!m_display) return;
Screen* screen = ::XScreenOfDisplay(m_display,m_monitor);
tools_vforit(toolx::X11::Pixel,m_pixels,it) {
::XFreeColors(m_display,XDefaultColormapOfScreen(screen),&(*it),1,0);
}
m_pixels.clear();
}
protected:
std::ostream& m_out;
unsigned int m_monitor;
Display* m_display;
GC m_GC;
std::vector<toolx::X11::Pixel> m_pixels;
colors_t m_colors;
XImage* m_image;
};
}}
#endif
+151
View File
@@ -0,0 +1,151 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef toolx_X11_zb_viewer
#define toolx_X11_zb_viewer
#include "base_session"
#include "pixwin"
#include "simple_dispatcher"
#include <tools/sg/zb_viewer>
#include <tools/sg/device_interactor>
namespace toolx {
namespace X11 {
class zb_viewer
:public tools::sg::zb_viewer, protected pixwin {
typedef tools::sg::zb_viewer parent;
typedef pixwin parent_pixwin;
private:
class dispatcher : public simple_dispatcher {
typedef simple_dispatcher parent;
public:
virtual bool dispatch(XEvent& a_event) {
if(parent::dispatch(a_event)) return true;
if(a_event.type==ButtonPress && a_event.xbutton.button==1) {
if(!m_viewer.device_interactor()) return false;
tools::sg::mouse_down_event event(a_event.xbutton.x,a_event.xbutton.y);
m_viewer.device_interactor()->mouse_press(event);
return true;
} else if(a_event.type==ButtonRelease && a_event.xbutton.button==1) {
if(!m_viewer.device_interactor()) return false;
tools::sg::mouse_up_event event(a_event.xbutton.x,a_event.xbutton.y);
m_viewer.device_interactor()->mouse_release(event);
return true;
} else if(a_event.type==MotionNotify) {
if(!m_viewer.device_interactor()) return false;
if((a_event.xmotion.state & Button1MotionMask)==Button1MotionMask) {
tools::sg::mouse_move_event event(a_event.xmotion.x,a_event.xmotion.y,0,0,false);
m_viewer.device_interactor()->mouse_move(event);
}
} else if((a_event.type==ButtonPress)&&(a_event.xbutton.button==4)) { // mouse scrollwheel down :
if(!m_viewer.device_interactor()) return false;
tools::sg::wheel_rotate_event event(8); //8=cooking.
m_viewer.device_interactor()->wheel_rotate(event);
return true;
} else if((a_event.type==ButtonPress)&&(a_event.xbutton.button==5)) { // mouse scrollwheel up :
if(!m_viewer.device_interactor()) return false;
tools::sg::wheel_rotate_event event(-8); //8=cooking.
m_viewer.device_interactor()->wheel_rotate(event);
return true;
}
return false;
}
virtual void win_render() {m_viewer.win_render();}
virtual void set_size(unsigned int a_width,unsigned int a_height) {m_viewer.set_size(a_width,a_height);}
virtual dispatcher* copy() const {return new dispatcher(*this);}
public:
dispatcher(zb_viewer& a_viewer)
:parent(a_viewer.m_session,a_viewer.m_win)
,m_viewer(a_viewer){}
virtual ~dispatcher(){}
protected:
dispatcher(const dispatcher& a_from)
:parent(a_from)
,m_viewer(a_from.m_viewer)
{}
dispatcher& operator=(const dispatcher& a_from) {
parent::operator=(a_from);
return *this;
}
protected:
zb_viewer& m_viewer;
};
public:
virtual void set_size(unsigned int a_w,unsigned int a_h) {
parent::set_size(a_w,a_h);
free_XImage();
alloc_XImage(a_w,a_h);
}
public:
zb_viewer(base_session& a_session,
int a_x = 0,int a_y = 0,
unsigned int a_width = 500,unsigned int a_height = 500,
const std::string& a_win_title = "")
:parent(a_session.out(),a_width,a_height)
,parent_pixwin(a_session.out(),a_session.monitor(),a_session.display())
,m_session(a_session)
,m_win(0)
,m_interactor(0)
{
if(!m_session.display()) return; //throw
m_win = m_session.create_window(a_win_title.c_str(),a_x,a_y,a_width,a_height);
if(!m_win) return; //throw
m_session.add_dispatcher(new dispatcher(*this));
}
virtual ~zb_viewer() {
if(m_win) {
m_session.remove_dispatchers_with_window(m_win);
m_session.delete_window(m_win);
m_session.sync();
}
}
protected:
zb_viewer(const zb_viewer& a_from)
:parent(a_from)
,parent_pixwin(a_from)
,m_session(a_from.m_session)
,m_win(a_from.m_win)
,m_interactor(0)
{}
zb_viewer& operator=(const zb_viewer& a_from){
parent::operator=(a_from);
parent_pixwin::operator=(a_from);
m_win = a_from.m_win;
return *this;
}
public:
bool has_window() const {return m_win?true:false;} //for SWIG
Window window() const {return m_win;}
bool show() {
if(!m_win) return false;
m_session.show_window(m_win);
return true;
}
void win_render() {
if(!m_win) return;
if(!render(get_rgbas,false)) return;
put_buffer(m_win,m_ww,m_wh,tools::vec_data(m_out_buffer));
m_out_buffer.clear();
}
void set_device_interactor(tools::sg::device_interactor* a_interactor) {m_interactor = a_interactor;}
public:
tools::sg::device_interactor* device_interactor() {return m_interactor;}
protected:
base_session& m_session;
Window m_win;
tools::sg::device_interactor* m_interactor;
};
}}
#endif
+222
View File
@@ -0,0 +1,222 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef toolx_Xt_ImageArea
#define toolx_Xt_ImageArea
#include <X11/IntrinsicP.h>
#include <X11/CoreP.h>
#include <X11/CompositeP.h>
#include <X11/StringDefs.h>
//#define TOOLX_XT_IMAGEAREA_DEBUG
#ifdef TOOLX_XT_IMAGEAREA_DEBUG
#include <stdio.h>
#endif
namespace toolx {
namespace Xt {
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
typedef struct _ImageAreaClassRec* ImageAreaWidgetClass;
typedef struct _ImageAreaRec* ImageAreaWidget;
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
typedef struct {
void* extension;
} ImageAreaClassPart;
typedef struct _ImageAreaClassRec {
CoreClassPart core_class;
CompositeClassPart composite_class;
ImageAreaClassPart imageArea_class;
} ImageAreaClassRec;
typedef struct {
XtCallbackList resizeCallback;
XtCallbackList paintCallback;
XtCallbackList eventCallback;
} ImageAreaPart;
typedef struct _ImageAreaRec {
CorePart core;
CompositePart composite;
ImageAreaPart imageArea;
} ImageAreaRec;
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
class ImageArea {
protected:
static const char* class_name() {static const char* s_s = "ImageArea";return s_s;}
public:
typedef struct {
int reason;
XEvent* event;
} XoAnyCallbackStruct;
static int XoCR_RESIZE() {return 1;}
static int XoCR_PAINT() {return 2;}
static int XoCR_EVENT() {return 3;}
static const char* XoN_resizeCallback() {static const char* s_s = "resizeCallback";return s_s;}
static const char* XoN_paintCallback() {static const char* s_s = "paintCallback";return s_s;}
static const char* XoN_eventCallback() {static const char* s_s = "eventCallback";return s_s;}
public:
static void paint(Widget a_this) {
if(!XtIsRealized(a_this)) return;
XoAnyCallbackStruct value;
value.reason = XoCR_PAINT();
value.event = 0;
::XtCallCallbacks(a_this,XoN_paintCallback(),(XtPointer)&value);
}
protected:
static void initialize_class(void) {
#ifdef TOOLX_XT_IMAGEAREA_DEBUG
::printf("debug: toolx::Xt::ImageArea: initialize_class.\n");
#endif
}
static void initialize_widget(Widget a_request,Widget a_this,ArgList,Cardinal*) {
if(a_request->core.width<=0) a_this->core.width = 100;
if(a_request->core.height<=0) a_this->core.height = 100;
#ifdef TOOLX_XT_IMAGEAREA_DEBUG
::printf("debug: toolx::Xt::ImageArea: initialize_widget: %s\n",::XtName(a_this));
#endif
::XtAddEventHandler
(a_this,ButtonPressMask|ButtonReleaseMask|ButtonMotionMask|KeyPressMask|KeyReleaseMask,0,event_handler,NULL);
#ifdef TOOLX_XT_IMAGEAREA_DEBUG
::printf("debug: toolx::Xt::ImageArea: initialize_widget: end\n");
#endif
}
static void destroy_widget(Widget) {
#ifdef TOOLX_XT_IMAGEAREA_DEBUG
::printf("debug: toolx::Xt::ImageArea: destroy_widget.\n");
#endif
}
static void change_widget_size(Widget a_this) {
#ifdef TOOLX_XT_IMAGEAREA_DEBUG
::printf("debug: toolx::Xt::ImageArea: change_widget_size: %s\n",XtName(a_this));
#endif
if(widget_class()->core_class.superclass->core_class.resize!=NULL)
(widget_class()->core_class.superclass->core_class.resize)(a_this);
XoAnyCallbackStruct value;
value.reason = XoCR_RESIZE();
value.event = 0;
::XtCallCallbacks(a_this,XoN_resizeCallback(),(XtPointer)&value);
#ifdef TOOLX_XT_IMAGEAREA_DEBUG
::printf("debug: toolx::Xt::ImageArea: change_widget_size: end\n");
#endif
}
static void draw_widget(Widget a_this,XEvent* a_event,Region a_region) {
#ifdef TOOLX_XT_IMAGEAREA_DEBUG
::printf("debug: toolx::Xt::ImageArea: draw_widget: %s\n",XtName(a_this));
#endif
if(widget_class()->core_class.superclass->core_class.expose!=NULL)
(widget_class()->core_class.superclass->core_class.expose)(a_this,a_event,a_region);
XoAnyCallbackStruct value;
value.reason = XoCR_PAINT();
value.event = a_event;
::XtCallCallbacks(a_this,XoN_paintCallback(),(XtPointer)&value);
#ifdef TOOLX_XT_IMAGEAREA_DEBUG
::printf("debug: toolx::Xt::ImageArea: draw_widget: end\n");
#endif
}
protected:
static void event_handler(Widget a_this,XtPointer,XEvent* a_event ,Boolean*) {
XoAnyCallbackStruct value;
value.reason = XoCR_EVENT();
value.event = a_event;
::XtCallCallbacks(a_this,XoN_eventCallback(),(XtPointer)&value);
}
public:
static WidgetClass widget_class() {
static XtResource s_resources[] = {
{(String)XoN_resizeCallback(),XtCCallback,XtRCallback,sizeof(XtCallbackList),
XtOffset(ImageAreaWidget,imageArea.resizeCallback),XtRImmediate,(XtPointer)NULL},
{(String)XoN_paintCallback(),XtCCallback,XtRCallback,sizeof(XtCallbackList),
XtOffset(ImageAreaWidget,imageArea.paintCallback),XtRImmediate,(XtPointer)NULL},
{(String)XoN_eventCallback(),XtCCallback,XtRCallback,sizeof(XtCallbackList),
XtOffset(ImageAreaWidget,imageArea.eventCallback),XtRImmediate,(XtPointer)NULL}
};
static ImageAreaClassRec s_imageAreaClassRec = {
// Core Class Part :
{
(WidgetClass) &compositeClassRec, // pointer to superclass ClassRec
(String)class_name(), // widget resource class name
sizeof(ImageAreaRec), // size in bytes of widget record
initialize_class, // class_initialize
NULL, // dynamic initialization
FALSE, // has class been initialized?
initialize_widget, // initialize
NULL, // notify that initialize called
XtInheritRealize, // XCreateWindow for widget
NULL, // widget semantics name to proc mapWidget
0, // number of entries in actions
s_resources, // resources for subclass fields
XtNumber(s_resources), // number of entries in resources
NULLQUARK, // resource class quarkified
TRUE, // compress MotionNotify for widget
TRUE, // compress Expose events for widget
TRUE, // compress enter and leave events
TRUE, // select for VisibilityNotify
destroy_widget, // free data for subclass pointers
change_widget_size, // geom manager changed widget size
draw_widget, // rediplay window
NULL, // set subclass resource values
NULL, // notify that SetValues called
XtInheritSetValuesAlmost, // SetValues got "Almost" geo reply
NULL, // notify that get_values called
XtInheritAcceptFocus, // assign input focus to widget
XtVersion, // version of intrinsics used
NULL, // list of callback offsets
XtInheritTranslations, // translations
XtInheritQueryGeometry, // return preferred geometry
XtInheritDisplayAccelerator, // display your accelerator
NULL // pointer to extension record
},
// Composite Class Part :
{
XtInheritGeometryManager, // geometry manager for children
XtInheritChangeManaged, // change managed state of child
XtInheritInsertChild, // physically add child to parent
XtInheritDeleteChild, // physically remove child
NULL // pointer to extension record
},
// ImageArea :
{
NULL
}
};
return (WidgetClass)&s_imageAreaClassRec;
}
};
}}
#endif
+18 -6
View File
@@ -4,19 +4,25 @@
#ifndef toolx_Xt_session
#define toolx_Xt_session
#include <ostream>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/StringDefs.h>
#include <ostream>
namespace toolx {
namespace Xt {
class session {
public:
session(std::ostream& a_out)
:m_out(a_out),m_app_context(0),m_app_widget(0),m_app_owner(false)
{}
session(std::ostream& a_out,XtAppContext a_app_context,Widget a_app_widget)
:m_out(a_out),m_app_context(a_app_context),m_app_widget(a_app_widget),m_app_owner(false)
{}
session(std::ostream& a_out,int& a_argc,char** a_argv)
:m_out(a_out),m_app_context(0),m_app_widget(0)
:m_out(a_out),m_app_context(0),m_app_widget(0),m_app_owner(false)
{
//LookDSM_Problem();
Arg args[1];
@@ -25,11 +31,16 @@ public:
if(!m_app_context || !m_app_widget) {
m_app_context = 0;
m_app_widget = 0;
m_app_owner = false;
return;
}
m_app_owner = true;
}
virtual ~session() {
if(m_app_widget) {::XtDestroyWidget(m_app_widget);m_app_widget = 0;}
if(m_app_context) {::XtDestroyApplicationContext(m_app_context);m_app_context = 0;}
if(m_app_owner) {
if(m_app_widget) {::XtDestroyWidget(m_app_widget);m_app_widget = 0;}
if(m_app_context) {::XtDestroyApplicationContext(m_app_context);m_app_context = 0;}
}
}
protected:
session(const session& a_from)
@@ -67,11 +78,12 @@ public:
Widget get_app_widget() {return m_app_widget;}
protected:
std::ostream& m_out;
bool m_app_owner;
XtAppContext m_app_context;
Widget m_app_widget;
bool m_app_owner;
};
}}
#endif
+208
View File
@@ -0,0 +1,208 @@
// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef toolx_Xt_zb_viewer
#define toolx_Xt_zb_viewer
#include "session"
#include "ImageArea"
#include "../X11/pixwin"
#include <X11/Shell.h>
#include <X11/StringDefs.h>
#include <X11/IntrinsicP.h>
#include <tools/sg/zb_viewer>
#include <tools/num2s>
#include <tools/sg/device_interactor>
#include <tools/sg/keys>
namespace toolx {
namespace Xt {
class zb_viewer: public tools::sg::zb_viewer {
typedef tools::sg::zb_viewer parent;
public:
zb_viewer(session& a_session,
int a_x = 0,int a_y = 0,
unsigned int a_width = 500,unsigned int a_height = 500,
const std::string& a_win_title = "")
:parent(a_session.out(),a_width,a_height)
,m_session(a_session)
,m_shell(0)
,m_image_area(0)
,m_pixwin(0)
,m_interactor(0)
{
Widget app_widget = a_session.get_app_widget();
if(!app_widget) return;
std::string sgeom;
tools::numas(a_width,sgeom);
sgeom += "x";
tools::numas(a_height,sgeom);
sgeom += "+";
tools::numas(a_x,sgeom);
sgeom += "+";
tools::numas(a_y,sgeom);
Arg args[2];
XtSetArg(args[0],XtNgeometry,XtNewString(sgeom.c_str()));
XtSetArg(args[1],XtNborderWidth,0);
m_shell = ::XtAppCreateShell((char*)a_win_title.c_str(),
(char*)"zb_viewer_shell",
topLevelShellWidgetClass,XtDisplay(app_widget),args,2);
::XtSetMappedWhenManaged(m_shell,True);
m_image_area = ::XtCreateManagedWidget("imagearea",ImageArea::widget_class(),m_shell,args,0);
::XtAddCallback(m_image_area,ImageArea::XoN_resizeCallback(),resize_cbk,(XtPointer)this);
::XtAddCallback(m_image_area,ImageArea::XoN_paintCallback(),paint_cbk,(XtPointer)this);
::XtAddCallback(m_image_area,ImageArea::XoN_eventCallback(),event_cbk,(XtPointer)this);
::XtAddCallback(m_shell,XtNdestroyCallback,destroy_shell_callback,(XtPointer)this);
::XtRealizeWidget(m_shell);
{int iscreen;
Screen* screen = XtScreen(m_image_area);
iscreen = XScreenNumberOfScreen(screen);
m_pixwin = new toolx::X11::pixwin(a_session.out(),iscreen,XtDisplay(app_widget));
m_pixwin->set_size(m_image_area->core.width,m_image_area->core.height);}
//Atom WM_DELETE_WINDOW_atom = ::XInternAtom(XtDisplay(m_shell),"WM_DELETE_WINDOW",False);
//::XSetWMProtocols(XtDisplay(m_shell),XtWindow(m_shell),&WM_DELETE_WINDOW_atom,1);
}
virtual ~zb_viewer() {
delete m_pixwin;
if(m_shell) {
::XtRemoveCallback(m_image_area,ImageArea::XoN_paintCallback(),paint_cbk,(XtPointer)this);
::XtRemoveCallback(m_shell,XtNdestroyCallback,destroy_shell_callback,(XtPointer)this);
::XtDestroyWidget(m_shell);
}
m_shell = 0;
m_image_area = 0;
}
protected:
zb_viewer(const zb_viewer& a_from)
:parent(a_from)
,m_session(a_from.m_session)
,m_shell(0)
,m_image_area(0)
,m_interactor(0)
{}
zb_viewer& operator=(const zb_viewer& a_from){
parent::operator=(a_from);
return *this;
}
public:
bool has_window() const {return m_image_area?true:false;}
bool show() {
if(!m_shell) return false;
::XtRealizeWidget(m_shell);
::XtMapWidget(m_shell);
// Raise window :
if(XtIsWidget(m_shell) && XtIsRealized(m_shell) ) {
Display* display = XtDisplay(m_shell);
Atom atom = ::XInternAtom(display,"WM_DELETE_WINDOW",False);
::XSetWMProtocols(display,XtWindow(m_shell),&atom,1);
::XRaiseWindow(display,XtWindow(m_shell));
}
return true;
}
void win_render() {
if(m_image_area) ImageArea::paint(m_image_area);
}
void set_device_interactor(tools::sg::device_interactor* a_interactor) {m_interactor = a_interactor;} //we do not have ownership.
protected:
static void resize_cbk(Widget a_widget,XtPointer a_tag,XtPointer){
if(!XtIsRealized(a_widget)) return;
unsigned int width = a_widget->core.width;
unsigned int height = a_widget->core.height;
//::printf("debug: toolx::Xt::zb_viewer::resize_cbk: %d %d\n",width,height);
zb_viewer* _this = (zb_viewer*)a_tag;
_this->set_size(width,height);
_this->m_pixwin->set_size(width,height);
}
static void paint_cbk(Widget a_widget,XtPointer a_tag,XtPointer){
if(!XtIsRealized(a_widget)) return;
unsigned int width = a_widget->core.width;
unsigned int height = a_widget->core.height;
//::printf("debug: toolx::Xt::zb_viewer::paint_cbk: %d %d\n",width,height);
zb_viewer* _this = (zb_viewer*)a_tag;
if(!_this->render(tools::sg::zb_viewer::get_rgbas,false)) return; //rgbas because Qt wants an image 32 bits aligned.
_this->m_pixwin->put_buffer(XtWindow(a_widget),width,height,tools::vec_data(_this->out_buffer()));
//parent::after_render();
_this->out_buffer_clear();
}
static void event_cbk(Widget,XtPointer a_tag,XtPointer a_data){
zb_viewer* _this = (zb_viewer*)a_tag;
if(!_this->m_interactor) return;
ImageArea::XoAnyCallbackStruct* data = (ImageArea::XoAnyCallbackStruct*)a_data;
XEvent* xevent = data->event;
switch( xevent->type ) {
case KeyPress:{
KeySym key_sym;
::XLookupString(&(xevent->xkey),NULL,0,&key_sym,NULL);
tools::sg::key_down_event event(_this->convert_key(key_sym));
_this->m_interactor->key_press(event);
}return;
case KeyRelease:{
KeySym key_sym;
::XLookupString(&(xevent->xkey),NULL,0,&key_sym,NULL);
tools::sg::key_up_event event(_this->convert_key(key_sym));
_this->m_interactor->key_release(event);
}return;
case ButtonPress:{
if(xevent->xbutton.button==Button4) { //4=wheel down, or move down double touch on trackpad = zoom in.
tools::sg::wheel_rotate_event event(8); //8=cooking.
_this->m_interactor->wheel_rotate(event);
} else if(xevent->xbutton.button==Button5) { //5=wheel up, or move up double touch on trackpad = zoom out.
tools::sg::wheel_rotate_event event(-8); //8=cooking.
_this->m_interactor->wheel_rotate(event);
} else if(xevent->xbutton.button==Button1) {
tools::sg::mouse_down_event event(xevent->xbutton.x,xevent->xbutton.y);
_this->m_interactor->mouse_press(event);
}
}return;
case ButtonRelease:{
if(xevent->xbutton.button==Button1) {
tools::sg::mouse_up_event event(xevent->xbutton.x,xevent->xbutton.y);
_this->m_interactor->mouse_release(event);
}
}return;
case MotionNotify:{
if((xevent->xmotion.state & Button1MotionMask)==Button1MotionMask) {
tools::sg::mouse_move_event event(xevent->xmotion.x,xevent->xmotion.y,0,0,false);
_this->m_interactor->mouse_move(event);
}
}return;
default:return;}
}
static void destroy_shell_callback(Widget,XtPointer a_tag,XtPointer) {
zb_viewer* _this = (zb_viewer*)a_tag;
_this->m_shell = 0;
_this->m_image_area = 0;
}
protected:
tools::key_code convert_key(KeySym a_key) {
if(a_key==XK_Shift_L) return tools::sg::key_shift();
if(a_key==XK_Shift_R) return tools::sg::key_shift();
return (tools::key_code)a_key;
}
protected:
session& m_session;
Widget m_shell;
Widget m_image_area;
toolx::X11::pixwin* m_pixwin;
tools::sg::device_interactor* m_interactor;
};
}}
#endif
-1
View File
@@ -685,7 +685,6 @@ protected:
std::map<unsigned int,gsto_t*> m_gstos;
bool m_gsto_on_card;
unsigned int m_gen_id;
tools::sg::gsto_mode m_gsto_mode;
bool m_warned;
+6 -2
View File
@@ -10,6 +10,7 @@
#include <tools/img>
#include <ostream>
#include <sstream>
namespace toolx {
namespace sg {
@@ -42,7 +43,9 @@ inline bool gl_dump_if_errors(std::ostream& a_out,const std::string& a_head) {
//#define GL_INVALID_FRAMEBUFFER_OPERATION 0x0506
a_out << "0x" << std::hex << glerror << std::endl;
std::ostringstream oss;
oss << "0x" << std::hex << glerror;
a_out << oss.str() << std::endl;
glerror = ::glGetError();
}
@@ -87,7 +90,7 @@ inline bool gl_tex_img(std::ostream& a_out,const tools::img_byte& a_img) {
#if defined(ANDROID) || TARGET_OS_IPHONE
tools::img_byte res;
if(!a_img.bw2x(3,res)) {
a_out << "toolx::sg::gl::tex_img : toolx::img.bw2x() failed." << std::endl;
a_out << "toolx::sg::gl::tex_img : tools::img.bw2x() failed." << std::endl;
return false;
}
::glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,res.width(),res.height(),0,GL_RGB,GL_UNSIGNED_BYTE,res.buffer());
@@ -107,4 +110,5 @@ inline bool gl_tex_img(std::ostream& a_out,const tools::img_byte& a_img) {
}}
#endif