renderModeAttrib.cxx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Filename: renderModeAttrib.cxx
  2. // Created by: drose (14Mar02)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "renderModeAttrib.h"
  19. #include "graphicsStateGuardianBase.h"
  20. #include "dcast.h"
  21. #include "bamReader.h"
  22. #include "bamWriter.h"
  23. #include "datagram.h"
  24. #include "datagramIterator.h"
  25. TypeHandle RenderModeAttrib::_type_handle;
  26. ////////////////////////////////////////////////////////////////////
  27. // Function: RenderModeAttrib::make
  28. // Access: Published, Static
  29. // Description: Constructs a new RenderModeAttrib object that specifies
  30. // whether to draw polygons in the normal, filled mode,
  31. // or wireframe mode, or in some other yet-to-be-defined
  32. // mode.
  33. //
  34. // The line_width is relevant only if mode is
  35. // M_wireframe, and specifies the thickness of the
  36. // lines, in pixels, to use for wireframe.
  37. ////////////////////////////////////////////////////////////////////
  38. CPT(RenderAttrib) RenderModeAttrib::
  39. make(RenderModeAttrib::Mode mode, float line_width) {
  40. if (mode != M_wireframe) {
  41. line_width = 0.0f;
  42. }
  43. RenderModeAttrib *attrib = new RenderModeAttrib(mode, line_width);
  44. return return_new(attrib);
  45. }
  46. ////////////////////////////////////////////////////////////////////
  47. // Function: RenderModeAttrib::issue
  48. // Access: Public, Virtual
  49. // Description: Calls the appropriate method on the indicated GSG
  50. // to issue the graphics commands appropriate to the
  51. // given attribute. This is normally called
  52. // (indirectly) only from
  53. // GraphicsStateGuardian::set_state() or modify_state().
  54. ////////////////////////////////////////////////////////////////////
  55. void RenderModeAttrib::
  56. issue(GraphicsStateGuardianBase *gsg) const {
  57. gsg->issue_render_mode(this);
  58. }
  59. ////////////////////////////////////////////////////////////////////
  60. // Function: RenderModeAttrib::output
  61. // Access: Public, Virtual
  62. // Description:
  63. ////////////////////////////////////////////////////////////////////
  64. void RenderModeAttrib::
  65. output(ostream &out) const {
  66. out << get_type() << ":";
  67. switch (get_mode()) {
  68. case M_filled:
  69. out << "filled";
  70. break;
  71. case M_wireframe:
  72. out << "wireframe(" << get_line_width() << ")";
  73. break;
  74. }
  75. }
  76. ////////////////////////////////////////////////////////////////////
  77. // Function: RenderModeAttrib::compare_to_impl
  78. // Access: Protected, Virtual
  79. // Description: Intended to be overridden by derived RenderModeAttrib
  80. // types to return a unique number indicating whether
  81. // this RenderModeAttrib is equivalent to the other one.
  82. //
  83. // This should return 0 if the two RenderModeAttrib objects
  84. // are equivalent, a number less than zero if this one
  85. // should be sorted before the other one, and a number
  86. // greater than zero otherwise.
  87. //
  88. // This will only be called with two RenderModeAttrib
  89. // objects whose get_type() functions return the same.
  90. ////////////////////////////////////////////////////////////////////
  91. int RenderModeAttrib::
  92. compare_to_impl(const RenderAttrib *other) const {
  93. const RenderModeAttrib *ta;
  94. DCAST_INTO_R(ta, other, 0);
  95. if (_mode != ta->_mode) {
  96. return (int)_mode - (int)ta->_mode;
  97. }
  98. if (_line_width != ta->_line_width) {
  99. return _line_width < ta->_line_width ? -1 : 1;
  100. }
  101. return 0;
  102. }
  103. ////////////////////////////////////////////////////////////////////
  104. // Function: RenderModeAttrib::make_default_impl
  105. // Access: Protected, Virtual
  106. // Description: Intended to be overridden by derived RenderModeAttrib
  107. // types to specify what the default property for a
  108. // RenderModeAttrib of this type should be.
  109. //
  110. // This should return a newly-allocated RenderModeAttrib of
  111. // the same type that corresponds to whatever the
  112. // standard default for this kind of RenderModeAttrib is.
  113. ////////////////////////////////////////////////////////////////////
  114. RenderAttrib *RenderModeAttrib::
  115. make_default_impl() const {
  116. return new RenderModeAttrib(M_filled, 0.0f);
  117. }
  118. ////////////////////////////////////////////////////////////////////
  119. // Function: RenderModeAttrib::register_with_read_factory
  120. // Access: Public, Static
  121. // Description: Tells the BamReader how to create objects of type
  122. // RenderModeAttrib.
  123. ////////////////////////////////////////////////////////////////////
  124. void RenderModeAttrib::
  125. register_with_read_factory() {
  126. BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
  127. }
  128. ////////////////////////////////////////////////////////////////////
  129. // Function: RenderModeAttrib::write_datagram
  130. // Access: Public, Virtual
  131. // Description: Writes the contents of this object to the datagram
  132. // for shipping out to a Bam file.
  133. ////////////////////////////////////////////////////////////////////
  134. void RenderModeAttrib::
  135. write_datagram(BamWriter *manager, Datagram &dg) {
  136. RenderAttrib::write_datagram(manager, dg);
  137. dg.add_int8(_mode);
  138. dg.add_float32(_line_width);
  139. }
  140. ////////////////////////////////////////////////////////////////////
  141. // Function: RenderModeAttrib::make_from_bam
  142. // Access: Protected, Static
  143. // Description: This function is called by the BamReader's factory
  144. // when a new object of type RenderModeAttrib is encountered
  145. // in the Bam file. It should create the RenderModeAttrib
  146. // and extract its information from the file.
  147. ////////////////////////////////////////////////////////////////////
  148. TypedWritable *RenderModeAttrib::
  149. make_from_bam(const FactoryParams &params) {
  150. RenderModeAttrib *attrib = new RenderModeAttrib(M_filled, 0.0f);
  151. DatagramIterator scan;
  152. BamReader *manager;
  153. parse_params(params, scan, manager);
  154. attrib->fillin(scan, manager);
  155. return attrib;
  156. }
  157. ////////////////////////////////////////////////////////////////////
  158. // Function: RenderModeAttrib::fillin
  159. // Access: Protected
  160. // Description: This internal function is called by make_from_bam to
  161. // read in all of the relevant data from the BamFile for
  162. // the new RenderModeAttrib.
  163. ////////////////////////////////////////////////////////////////////
  164. void RenderModeAttrib::
  165. fillin(DatagramIterator &scan, BamReader *manager) {
  166. RenderAttrib::fillin(scan, manager);
  167. _mode = (Mode)scan.get_int8();
  168. _line_width = scan.get_float32();
  169. }