g4tools  5.4.0
Classes | Public Types | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
tools::zb::buffer Class Reference
Collaboration diagram for tools::zb::buffer:
Collaboration graph
[legend]

Classes

class  point_writer
 
class  scan_writer
 
class  scan_writer_1
 
class  scan_writer_2
 
class  scan_writer_3
 
class  scan_writer_4
 
class  writer
 

Public Types

typedef unsigned int ZPixel
 

Public Member Functions

 buffer ()
 
virtual ~buffer ()
 
void set_depth_test (bool a_on)
 
bool change_size (unsigned int a_width, unsigned int a_height)
 
ZPixelget_color_buffer (unsigned int &a_width, unsigned int &a_height) const
 
void clear_color_buffer (ZPixel a_pixel)
 
void clear_depth_buffer ()
 
bool get_clipped_pixel (ZPos a_x, ZPos a_y, ZPixel &a_pixel) const
 
void set_clip_region (ZPos a_x, ZPos a_y, unsigned int a_width, unsigned int a_height)
 
void draw_point (const point &a_p, ZPixel a_pixel, unsigned int a_size)
 
bool get_pixel (const point &a_p, ZPixel &a_pixel)
 
void draw_line (const point &a_beg, const point &a_end, ZPixel a_pixel, unsigned int a_size)
 
void draw_lines (int a_number, const point *a_list, ZPixel a_pixel, unsigned int a_size)
 
void draw_segments (int a_number, const point *a_list, ZPixel a_pixel, unsigned int a_size)
 
void draw_markers (int a_number, const point *a_list, ZPixel a_pixel, unsigned int a_size)
 
void draw_polygon (int a_number, const point *a_list, ZZ a_A, ZZ a_B, ZZ a_C, ZZ a_D, ZPixel a_pixel)
 

Protected Member Functions

 buffer (const buffer &a_from)
 
bufferoperator= (const buffer &a_from)
 

Static Protected Member Functions

static void WriteScanLine (void *a_tag, int a_beg, int a_end, int a_y)
 
static void WriteLine (const point &a_beg, const point &a_end, writer &a_writer)
 
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)
 

Protected Attributes

bool m_depth_test
 
ZReal * m_zbuffer
 
ZPixelm_zimage
 
unsigned int m_zbw
 
unsigned int m_zbh
 
ZPos m_begX
 
ZPos m_begY
 
ZPos m_endX
 
ZPos m_endY
 
ZPixel m_scan_pixel
 
ZZ m_planeAC
 
ZZ m_planeBC
 
ZZ m_planeDC
 
polygon m_polygon
 

Detailed Description

Definition at line 16 of file buffer.

Member Typedef Documentation

◆ ZPixel

typedef unsigned int tools::zb::buffer::ZPixel

Definition at line 22 of file buffer.

Constructor & Destructor Documentation

◆ buffer() [1/2]

tools::zb::buffer::buffer ( )
inline

Definition at line 157 of file buffer.

158  :m_depth_test(true)
159  ,m_zbuffer(0)
160  //,m_zmin(0),m_zmax(0)
161  ,m_zimage(0)
162  ,m_zbw(0),m_zbh(0)
163  ,m_begX(0),m_begY(0),m_endX(0),m_endY(0)
164  ,m_scan_pixel(0L)
165  ,m_planeAC(0),m_planeBC(0),m_planeDC(0)
166  //,m_zboundPrec(10)
167  {}

◆ ~buffer()

virtual tools::zb::buffer::~buffer ( )
inlinevirtual

Definition at line 168 of file buffer.

168  {
171  m_zbw = 0;
172  m_zbh = 0;
173  m_polygon.clear();
174  }

◆ buffer() [2/2]

tools::zb::buffer::buffer ( const buffer a_from)
inlineprotected

Definition at line 176 of file buffer.

177  :m_depth_test(a_from.m_depth_test)
178  {}

Member Function Documentation

◆ change_size()

bool tools::zb::buffer::change_size ( unsigned int  a_width,
unsigned int  a_height 
)
inline

Definition at line 187 of file buffer.

187  {
188  if(!a_width||!a_height) return false;
189 
190  if(m_zbuffer && (m_zbw==a_width) && (m_zbh==a_height) ) return true;
191 
192  if(m_zbuffer){
195  }
196 
197  //printf ("debug:ZBufferChangeSize:%d %d\n",a_width,a_height);
198  m_zbw = a_width;
199  m_zbh = a_height;
200  m_zbuffer = cmem_alloc<ZReal>(m_zbw*m_zbh);
201  if(!m_zbuffer){
202  m_zbw = 0;
203  m_zbh = 0;
204  return false;
205  }
206 
207  m_zimage = cmem_alloc<ZPixel>(m_zbw*m_zbh);
208  if(!m_zimage){
210  m_zbw = 0;
211  m_zbh = 0;
212  return false;
213  }
214 
215  // Init buffer done by further call to ZBufferErase.
216  /*
217  unsigned int size = m_zbw * m_zbh;
218  ZReal* zbuffer = m_zbuffer;
219  ZPixel* zimage = m_zimage;
220  for(unsigned int count=0;count<size;count++,zbuffer++,zimage++){
221  *zimage = 0;
222  *zbuffer = - ZREAL_HUGE();
223  }
224  */
225 
227  m_polygon.clear();
228  return true;
229  }

◆ clear_color_buffer()

void tools::zb::buffer::clear_color_buffer ( ZPixel  a_pixel)
inline

Definition at line 238 of file buffer.

238  {
239  // Erase acoording clip region.
240  ZPos row,col;
241  for(row=m_begY;row<=m_endY;row++){
242  ZPixel* zimage = m_zimage + row * m_zbw + m_begX;
243  for(col=m_begX;col<=m_endX;col++,zimage++) *zimage = a_pixel;
244  }
245  }

◆ clear_depth_buffer()

void tools::zb::buffer::clear_depth_buffer ( )
inline

Definition at line 247 of file buffer.

247  {
248  // Erase acoording clip region.
249  ZPos row,col;
250  //printf("debug:ZBufferClearDepthBuffer: %g.\n",a_depth);
251 
252  for(row=m_begY;row<=m_endY;row++) {
253  ZReal* zbuff = m_zbuffer + row * m_zbw + m_begX;
254  for(col=m_begX;col<=m_endX;col++,zbuff++){
255  *zbuff = - ZREAL_HUGE();
256  }
257  }
258  }

◆ draw_line()

void tools::zb::buffer::draw_line ( const point a_beg,
const point a_end,
ZPixel  a_pixel,
unsigned int  a_size 
)
inline

Definition at line 296 of file buffer.

296  {
297  point_writer pw(a_pixel,*this,a_size);
298  WriteLine(a_beg,a_end,pw);
299  }

◆ draw_lines()

void tools::zb::buffer::draw_lines ( int  a_number,
const point a_list,
ZPixel  a_pixel,
unsigned int  a_size 
)
inline

Definition at line 301 of file buffer.

301  {
302  point_writer pw(a_pixel,*this,a_size);
303  for(int count=1;count<a_number;count++) {
304  WriteLine(a_list[count-1],a_list[count],pw);
305  }
306  }

◆ draw_markers()

void tools::zb::buffer::draw_markers ( int  a_number,
const point a_list,
ZPixel  a_pixel,
unsigned int  a_size 
)
inline

Definition at line 315 of file buffer.

315  {
316  point_writer pw(a_pixel,*this,a_size);
317  for(int count=0;count<a_number;count++){
318  const point& p = a_list[count];
319  pw.write(p.x,p.y,p.z);
320  }
321  }

◆ draw_point()

void tools::zb::buffer::draw_point ( const point a_p,
ZPixel  a_pixel,
unsigned int  a_size 
)
inline

Definition at line 286 of file buffer.

286  {
287  point_writer pw(a_pixel,*this,a_size);
288  pw.write(a_p.x,a_p.y,a_p.z);
289  }

◆ draw_polygon()

void tools::zb::buffer::draw_polygon ( int  a_number,
const point a_list,
ZZ  a_A,
ZZ  a_B,
ZZ  a_C,
ZZ  a_D,
ZPixel  a_pixel 
)
inline

Definition at line 323 of file buffer.

326  {
327  // Assume a_list is closed.
328  if(a_number<3) return;
329  if(a_C==0) return; //polygone seen from edge
330  //if(m_zboundPrec<0) m_zboundPrec = 0;
331 
332  m_scan_pixel = a_pixel;
333  m_planeAC = a_A/a_C;
334  m_planeBC = a_B/a_C;
335  m_planeDC = a_D/a_C;
336 
337  //if this polygon A is quite perpandicular to screen and close
338  //to an other B than |dz| then some pixel of A could overwrite
339  //pixel of B. Your have then to give a lower m_zboundPrec
340 
341  //ZZ dz = m_zboundPrec * (a_zmax - a_zmin)/100.;
342  //m_zmin = (ZReal)(a_zmin - dz);
343  //m_zmax = (ZReal)(a_zmax + dz);
344 
345  m_polygon.scan(a_number,a_list,0,WriteScanLine,this);
346 
347  }

◆ draw_segments()

void tools::zb::buffer::draw_segments ( int  a_number,
const point a_list,
ZPixel  a_pixel,
unsigned int  a_size 
)
inline

Definition at line 308 of file buffer.

308  {
309  point_writer pw(a_pixel,*this,a_size);
310  int segment_number = a_number/2;
311  for(int count=0;count<segment_number;count++) {
312  WriteLine(a_list[2*count],a_list[2*count+1],pw);
313  }
314  }

◆ get_clipped_pixel()

bool tools::zb::buffer::get_clipped_pixel ( ZPos  a_x,
ZPos  a_y,
ZPixel a_pixel 
) const
inline

Definition at line 264 of file buffer.

264  {
265  if((a_x<m_begX) || (a_x>m_endX)) return false;
266  if((a_y<m_begY) || (a_y>m_endY)) return false;
267  a_pixel = *(m_zimage + a_y * m_zbw + a_x);
268  return true;
269  }

◆ get_color_buffer()

ZPixel* tools::zb::buffer::get_color_buffer ( unsigned int &  a_width,
unsigned int &  a_height 
) const
inline

Definition at line 231 of file buffer.

232  {
233  a_width = m_zbw;
234  a_height = m_zbh;
235  return m_zimage;
236  }

◆ get_pixel()

bool tools::zb::buffer::get_pixel ( const point a_p,
ZPixel a_pixel 
)
inline

Definition at line 291 of file buffer.

291  {
292  point_writer pw(a_pixel,*this,1);
293  return pw.get_pixel(a_p.x,a_p.y,a_p.z,a_pixel);
294  }

◆ operator=()

buffer& tools::zb::buffer::operator= ( const buffer a_from)
inlineprotected

Definition at line 179 of file buffer.

179  {
180  m_depth_test = a_from.m_depth_test;
181  return *this;
182  }

◆ ScanLine()

static void tools::zb::buffer::ScanLine ( ZPos  a_x,
ZPos  a_y,
ZZ  a_z,
ZPos  a_dx,
ZPos  a_dy,
ZZ  a_dz,
scan_writer a_proc 
)
inlinestaticprotected

Definition at line 552 of file buffer.

554  {
555  // Mid point algorithm
556  // assume 0<dx 0<=dy<=dx
557 
558  ZPos end = a_x + a_dx;
559  ZPos beg = a_y;
560  ZZ incz = a_dz/(ZZ)a_dx;
561  if(a_dy==0) {
562  a_proc.write(a_x,a_y,a_z,beg);
563  while(a_x<end){
564  a_x++;
565  a_z += incz;
566  a_proc.write(a_x,a_y,a_z,beg);
567  }
568  } else if(a_dy==a_dx) {
569  a_proc.write(a_x,a_y,a_z,beg);
570  while(a_x<end){
571  a_x++;
572  a_y++;
573  a_z += incz;
574  a_proc.write(a_x,a_y,a_z,beg);
575  }
576  } else {
577  ZPos d = 2 * a_dy - a_dx;
578  ZPos incrE = 2 * a_dy;
579  ZPos incrNE = 2 * ( a_dy - a_dx);
580  a_proc.write(a_x,a_y,a_z,beg);
581  while(a_x<end){
582  if(d<=0){
583  d += incrE;
584  a_x++;
585  }else{
586  d += incrNE;
587  a_x++;
588  a_y++;
589  }
590  a_z += incz;
591  a_proc.write(a_x,a_y,a_z,beg);
592  }
593  }
594  }

◆ set_clip_region()

void tools::zb::buffer::set_clip_region ( ZPos  a_x,
ZPos  a_y,
unsigned int  a_width,
unsigned int  a_height 
)
inline

Definition at line 272 of file buffer.

272  {
273  // if a_width or a_height is zero, clip region is empty.
274 
275  m_begX = a_x;
276  m_begY = a_y;
277  m_endX = a_x + a_width - 1;
278  m_endY = a_y + a_height - 1;
279 
280  if(m_begX<0) m_begX = 0;
281  if(m_begY<0) m_begY = 0;
282  if(m_endX>ZPos(m_zbw-1)) m_endX = m_zbw-1;
283  if(m_endY>ZPos(m_zbh-1)) m_endY = m_zbh-1;
284  }

◆ set_depth_test()

void tools::zb::buffer::set_depth_test ( bool  a_on)
inline

Definition at line 184 of file buffer.

184 {m_depth_test = a_on;}

◆ WriteLine()

static void tools::zb::buffer::WriteLine ( const point a_beg,
const point a_end,
writer a_writer 
)
inlinestaticprotected

Definition at line 503 of file buffer.

505  {
506  ZPos dx = a_end.x - a_beg.x;
507  ZPos dy = a_end.y - a_beg.y;
508  ZZ dz = a_end.z - a_beg.z;
509 
510  // 6 2
511  // 5 1
512  // 7 3
513  // 8 4
514  scan_writer_1 sw1(a_writer);
515  scan_writer_2 sw2(a_writer);
516  scan_writer_3 sw3(a_writer);
517  scan_writer_4 sw4(a_writer);
518 
519  if( (dx==0) && (dy==0) ) {
520  a_writer.write(a_beg.x,a_beg.y,a_beg.z);
521  a_writer.write(a_end.x,a_end.y,a_end.z);
522 
523  } else if(dx==0) {
524  if(dy>0)
525  ScanLine ( a_beg.y, a_beg.x,a_beg.z, dy, dx, dz,sw2);
526  else
527  ScanLine ( a_end.y, a_end.x,a_end.z,-dy, dx,-dz,sw2);
528 
529  } else if(dx>0) {
530  if((0<=dy) && (dy<=dx)) /*1*/
531  ScanLine ( a_beg.x, a_beg.y,a_beg.z, dx, dy, dz,sw1);
532  else if(dx<dy) /*2*/
533  ScanLine ( a_beg.y, a_beg.x,a_beg.z, dy, dx, dz,sw2);
534  else if((-dx<=dy) && (dy<0) ) /*3*/
535  ScanLine ( a_beg.x, a_beg.y,a_beg.z, dx,-dy, dz,sw3);
536  else if(dy<-dx) /*4*/
537  ScanLine ( a_end.y, a_end.x,a_end.z,-dy, dx,-dz,sw4);
538 
539  } else { //dx<0
540  if((0<=dy) && (dy<=-dx)) /*5*/
541  ScanLine ( a_end.x, a_end.y,a_end.z,-dx, dy,-dz,sw3);
542  else if(-dx<dy) /*6*/
543  ScanLine ( a_beg.y, a_beg.x,a_beg.z, dy,-dx, dz,sw4);
544  else if((dx<=dy) && (dy<0) ) /*7*/
545  ScanLine ( a_end.x, a_end.y,a_end.z,-dx,-dy,-dz,sw1);
546  else if(dy<dx) /*8*/
547  ScanLine ( a_end.y, a_end.x,a_end.z,-dy,-dx,-dz,sw2);
548  }
549 
550  }

◆ WriteScanLine()

static void tools::zb::buffer::WriteScanLine ( void *  a_tag,
int  a_beg,
int  a_end,
int  a_y 
)
inlinestaticprotected

Definition at line 462 of file buffer.

462  {
463  buffer& a_buffer = *((buffer*)a_tag);
464 
465  if((a_y<a_buffer.m_begY) || (a_y>a_buffer.m_endY)) return;
466  if(a_end<=a_beg) return;
467 
468  if(a_beg>a_buffer.m_endX) return;
469  if(a_end<a_buffer.m_begX) return;
470 
471  // border clip :
472  int beg = mx(a_beg,(int)a_buffer.m_begX);
473  int end = mn(a_end,(int)a_buffer.m_endX);
474 
475  unsigned long offset = a_y * a_buffer.m_zbw + beg;
476  ZReal* zbuff = a_buffer.m_zbuffer + offset;
477  ZPixel* zimage = a_buffer.m_zimage + offset;
478 
479  ZReal zpoint;
480  for(int x=beg;x<=end;x++){
481  // Computing must be the same as in edge_point_writer routine.
482  zpoint =
483  (ZReal)(- a_buffer.m_planeDC
484  - a_buffer.m_planeAC * x
485  - a_buffer.m_planeBC * a_y);
486  if(a_buffer.m_depth_test) {
487  if((zpoint>=(*zbuff))
488 // &&(zpoint>=a_buffer.m_zmin) //for plane quite perpandicular to screen.
489 // &&(zpoint<=a_buffer.m_zmax)
490  ){
491  *zbuff = zpoint;
492  *zimage = a_buffer.m_scan_pixel;
493  }
494  } else {
495  *zbuff = zpoint;
496  *zimage = a_buffer.m_scan_pixel;
497  }
498  zbuff ++;
499  zimage ++;
500  }
501  }

Member Data Documentation

◆ m_begX

ZPos tools::zb::buffer::m_begX
protected

Definition at line 604 of file buffer.

◆ m_begY

ZPos tools::zb::buffer::m_begY
protected

Definition at line 604 of file buffer.

◆ m_depth_test

bool tools::zb::buffer::m_depth_test
protected

Definition at line 597 of file buffer.

◆ m_endX

ZPos tools::zb::buffer::m_endX
protected

Definition at line 605 of file buffer.

◆ m_endY

ZPos tools::zb::buffer::m_endY
protected

Definition at line 605 of file buffer.

◆ m_planeAC

ZZ tools::zb::buffer::m_planeAC
protected

Definition at line 608 of file buffer.

◆ m_planeBC

ZZ tools::zb::buffer::m_planeBC
protected

Definition at line 609 of file buffer.

◆ m_planeDC

ZZ tools::zb::buffer::m_planeDC
protected

Definition at line 610 of file buffer.

◆ m_polygon

polygon tools::zb::buffer::m_polygon
protected

Definition at line 612 of file buffer.

◆ m_scan_pixel

ZPixel tools::zb::buffer::m_scan_pixel
protected

Definition at line 607 of file buffer.

◆ m_zbh

unsigned int tools::zb::buffer::m_zbh
protected

Definition at line 603 of file buffer.

◆ m_zbuffer

ZReal* tools::zb::buffer::m_zbuffer
protected

Definition at line 598 of file buffer.

◆ m_zbw

unsigned int tools::zb::buffer::m_zbw
protected

Definition at line 603 of file buffer.

◆ m_zimage

ZPixel* tools::zb::buffer::m_zimage
protected

Definition at line 601 of file buffer.


The documentation for this class was generated from the following file:
tools::zb::buffer::m_depth_test
bool m_depth_test
Definition: buffer:597
tools::zb::buffer::m_polygon
polygon m_polygon
Definition: buffer:612
tools::zb::buffer::m_zbuffer
ZReal * m_zbuffer
Definition: buffer:598
tools::cmem_free
void cmem_free(T *&a_p)
Definition: cmemT:16
tools::zb::polygon::clear
void clear()
Definition: polygon:33
tools::mn
T mn(const T &a, const T &b)
Definition: mnmx:10
tools::zb::buffer::m_planeAC
ZZ m_planeAC
Definition: buffer:608
tools::mx
T mx(const T &a, const T &b)
Definition: mnmx:13
tools::zb::buffer::m_zimage
ZPixel * m_zimage
Definition: buffer:601
tools::zb::buffer::WriteLine
static void WriteLine(const point &a_beg, const point &a_end, writer &a_writer)
Definition: buffer:503
tools::zb::ZZ
double ZZ
Definition: point:11
tools::zb::buffer::ScanLine
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)
Definition: buffer:552
tools::zb::buffer::m_zbh
unsigned int m_zbh
Definition: buffer:603
tools::zb::buffer::m_planeDC
ZZ m_planeDC
Definition: buffer:610
tools::zb::buffer::m_planeBC
ZZ m_planeBC
Definition: buffer:609
tools::zb::buffer::m_begX
ZPos m_begX
Definition: buffer:604
tools::zb::buffer::m_endX
ZPos m_endX
Definition: buffer:605
tools::zb::polygon::scan
void scan(int Count, const point *Pts, int rule, scan_func a_proc, void *a_tag)
Definition: polygon:48
tools::zb::buffer::m_scan_pixel
ZPixel m_scan_pixel
Definition: buffer:607
tools::zb::buffer::ZPixel
unsigned int ZPixel
Definition: buffer:22
tools::waxml::end
void end(std::ostream &a_writer)
Definition: begend:31
tools::zb::ZPos
int ZPos
Definition: point:10
tools::zb::buffer::m_begY
ZPos m_begY
Definition: buffer:604
tools::zb::buffer::buffer
buffer()
Definition: buffer:157
tools::zb::buffer::m_endY
ZPos m_endY
Definition: buffer:605
tools::zb::buffer::WriteScanLine
static void WriteScanLine(void *a_tag, int a_beg, int a_end, int a_y)
Definition: buffer:462
tools::zb::buffer::set_clip_region
void set_clip_region(ZPos a_x, ZPos a_y, unsigned int a_width, unsigned int a_height)
Definition: buffer:272
tools::zb::buffer::m_zbw
unsigned int m_zbw
Definition: buffer:603