Panda3D
Loading...
Searching...
No Matches
fltMeshPrimitive.cxx
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file fltMeshPrimitive.cxx
10 * @author drose
11 * @date 2001-02-28
12 */
13
14#include "fltMeshPrimitive.h"
15#include "fltRecordReader.h"
16#include "fltRecordWriter.h"
17#include "fltHeader.h"
18#include "fltMaterial.h"
19
20TypeHandle FltMeshPrimitive::_type_handle;
21
22/**
23 *
24 */
25FltMeshPrimitive::
26FltMeshPrimitive(FltHeader *header) : FltBead(header) {
27 _primitive_type = PT_tristrip;
28}
29
30/**
31 * Fills in the information in this bead based on the information given in the
32 * indicated datagram, whose opcode has already been read. Returns true on
33 * success, false if the datagram is invalid.
34 */
35bool FltMeshPrimitive::
36extract_record(FltRecordReader &reader) {
37 if (!FltBead::extract_record(reader)) {
38 return false;
39 }
40
41 nassertr(reader.get_opcode() == FO_mesh_primitive, false);
42 DatagramIterator &iterator = reader.get_iterator();
43
44 _primitive_type = (PrimitiveType)iterator.get_be_int16();
45
46 int vertex_width = iterator.get_be_int16();
47 int num_vertices = iterator.get_be_int32();
48
49 if (vertex_width == 1) {
50 for (int i = 0; i < num_vertices; i++) {
51 _vertices.push_back(iterator.get_uint8());
52 }
53
54 } else if (vertex_width == 2) {
55 for (int i = 0; i < num_vertices; i++) {
56 _vertices.push_back(iterator.get_be_uint16());
57 }
58
59 } else if (vertex_width == 4) {
60 for (int i = 0; i < num_vertices; i++) {
61 _vertices.push_back(iterator.get_be_int32());
62 }
63
64 } else {
65 nout << "Invalid vertex width in mesh primitive: " << vertex_width
66 << "\n";
67 return false;
68 }
69
70 check_remaining_size(iterator);
71 return true;
72}
73
74/**
75 * Fills up the current record on the FltRecordWriter with data for this
76 * record, but does not advance the writer. Returns true on success, false if
77 * there is some error.
78 */
79bool FltMeshPrimitive::
80build_record(FltRecordWriter &writer) const {
81 if (!FltBead::build_record(writer)) {
82 return false;
83 }
84
85 writer.set_opcode(FO_mesh_primitive);
86 Datagram &datagram = writer.update_datagram();
87
88 datagram.add_be_int16(_primitive_type);
89
90 // Determine the optimum index width, based on the largest vertex index.
91 int max_index = 0;
92 Vertices::const_iterator vi;
93 for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
94 max_index = std::max(max_index, (*vi));
95 }
96
97 int vertex_width;
98 if (max_index < 0x100) {
99 vertex_width = 1;
100 } else if (max_index < 0x10000) {
101 vertex_width = 2;
102 } else {
103 vertex_width = 4;
104 }
105
106 datagram.add_be_int16(vertex_width);
107 datagram.add_be_int32(_vertices.size());
108
109 if (vertex_width == 1) {
110 for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
111 datagram.add_uint8(*vi);
112 }
113
114 } else if (vertex_width == 2) {
115 for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
116 datagram.add_be_uint16(*vi);
117 }
118
119 } else {
120 for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
121 datagram.add_be_int32(*vi);
122 }
123 }
124
125 return true;
126}
A class to retrieve the individual data elements previously stored in a Datagram.
uint8_t get_uint8()
Extracts an unsigned 8-bit integer.
uint16_t get_be_uint16()
Extracts an unsigned 16-bit big-endian integer.
int32_t get_be_int32()
Extracts a signed 32-bit big-endian integer.
int16_t get_be_int16()
Extracts a signed 16-bit big-endian integer.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition datagram.h:38
void add_be_uint16(uint16_t value)
Adds an unsigned 16-bit big-endian integer to the datagram.
Definition datagram.I:172
void add_be_int32(int32_t value)
Adds a signed 32-bit big-endian integer to the datagram.
Definition datagram.I:154
void add_uint8(uint8_t value)
Adds an unsigned 8-bit integer to the datagram.
Definition datagram.I:50
void add_be_int16(int16_t value)
Adds a signed 16-bit big-endian integer to the datagram.
Definition datagram.I:145
A base class for any of a broad family of flt records that represent particular beads in the hierarch...
Definition fltBead.h:29
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition fltHeader.h:44
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly.
void set_opcode(FltOpcode opcode)
Sets the opcode associated with the current record.
Datagram & update_datagram()
Returns a modifiable reference to the datagram associated with the current record.
void check_remaining_size(const DatagramIterator &di, const std::string &name=std::string()) const
Checks that the iterator has no bytes left, as it should at the end of a successfully read record.
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.