Isle
Loading...
Searching...
No Matches
meshbuilder.cpp
Go to the documentation of this file.
1#include "impl.h"
2
3#include <assert.h>
4
5using namespace TglImpl;
6
9
10// FUNCTION: LEGO1 0x100a3830
11// FUNCTION: BETA10 0x1016c9f0
13{
14 return reinterpret_cast<void*>(&m_data);
15}
16
17// FUNCTION: LEGO1 0x100a3840
19 unsigned long faceCount,
20 unsigned long vertexCount,
21 float (*pPositions)[3],
22 float (*pNormals)[3],
23 float (*pTextureCoordinates)[2],
24 unsigned long (*pFaceIndices)[3],
25 unsigned long (*pTextureIndices)[3],
26 ShadingModel shadingModel
27)
28{
29 MeshImpl* pMeshImpl = new MeshImpl;
30 if (CreateMeshImpl(
31 pMeshImpl,
32 faceCount,
33 vertexCount,
34 pPositions,
35 pNormals,
36 pTextureCoordinates,
37 pFaceIndices,
38 pTextureIndices,
39 shadingModel
40 ) == Error) {
41 delete pMeshImpl;
42 pMeshImpl = NULL;
43 }
44
45 return pMeshImpl;
46}
47
49{
50 if (mode == PerspectiveCorrect) {
51 return ResultVal(pMesh->groupMesh->SetGroupMapping(pMesh->groupIndex, D3DRMMAP_PERSPCORRECT));
52 }
53 else {
54 return ResultVal(pMesh->groupMesh->SetGroupMapping(pMesh->groupIndex, 0));
55 }
56}
57
59 IDirect3DRMMesh* pD3DRM,
60 unsigned long faceCount,
61 unsigned long vertexCount,
62 float (*pPositions)[3],
63 float (*pNormals)[3],
64 float (*pTextureCoordinates)[2],
65 unsigned long (*pFaceIndices)[3],
66 unsigned long (*pTextureIndices)[3],
67 ShadingModel shadingModel,
69)
70{
71 unsigned long* faceIndices = (unsigned long*) pFaceIndices;
72 D3DRMGROUPINDEX groupIndex = 0;
73 int count = faceCount * 3;
74 int index = 0;
75
76 unsigned int* fData = new unsigned int[count];
77
78 D3DRMVERTEX* vertices = new D3DRMVERTEX[vertexCount];
79 memset(vertices, 0, sizeof(*vertices) * vertexCount);
80
81 rpMesh = new MeshImpl::MeshData;
82 rpMesh->groupMesh = pD3DRM;
83
84 for (int i = 0; i < count; i++) {
85 if ((*((unsigned short*) &faceIndices[i] + 1) >> 0x0f) & 0x01) {
86 unsigned long j = *(unsigned short*) &faceIndices[i];
87 vertices[index].position.x = pPositions[j][0];
88 vertices[index].position.y = pPositions[j][1];
89 vertices[index].position.z = pPositions[j][2];
90 j = *((unsigned short*) &faceIndices[i] + 1) & MAXSHORT;
91 vertices[index].normal.x = pNormals[j][0];
92 vertices[index].normal.y = pNormals[j][1];
93 vertices[index].normal.z = pNormals[j][2];
94
95 if (pTextureIndices != NULL && pTextureCoordinates != NULL) {
96 j = ((unsigned long*) pTextureIndices)[i];
97 vertices[index].tu = pTextureCoordinates[j][0];
98 vertices[index].tv = pTextureCoordinates[j][1];
99 }
100
101 fData[i] = index;
102 index++;
103 }
104 else {
105 fData[i] = *(unsigned short*) &faceIndices[i];
106 }
107 }
108
109 Result result;
110 result = ResultVal(pD3DRM->AddGroup(vertexCount, faceCount, 3, fData, &groupIndex));
111
112 if (Succeeded(result)) {
113 rpMesh->groupIndex = groupIndex;
114 result = ResultVal(pD3DRM->SetVertices(groupIndex, 0, vertexCount, vertices));
115 }
116
117 if (!Succeeded(result)) {
118 if (rpMesh) {
119 delete rpMesh;
120 }
121 rpMesh = NULL;
122 }
123 else {
125 }
126
127 if (fData != NULL) {
128 delete[] fData;
129 }
130
131 if (vertices != NULL) {
132 delete[] vertices;
133 }
134
135 return result;
136}
137
138inline Result MeshBuilderImpl::CreateMeshImpl(
139 MeshImpl* pMeshImpl,
140 unsigned long faceCount,
141 unsigned long vertexCount,
142 float (*pPositions)[3],
143 float (*pNormals)[3],
144 float (*pTextureCoordinates)[2],
145 unsigned long (*pFaceIndices)[3],
146 unsigned long (*pTextureIndices)[3],
147 ShadingModel shadingModel
148)
149{
150 return ::CreateMesh(
151 m_data,
152 faceCount,
153 vertexCount,
154 pPositions,
155 pNormals,
156 pTextureCoordinates,
157 pFaceIndices,
158 pTextureIndices,
159 shadingModel,
160 pMeshImpl->ImplementationData()
161 );
162}
163
164// FUNCTION: BETA10 0x1016e060
165inline Result MeshBuilderGetBoundingBox(IDirect3DRMMesh* pMesh, float min[3], float max[3])
166{
167 D3DRMBOX box;
168 Result result = ResultVal(pMesh->GetBox(&box));
169 if (Succeeded(result)) {
170 min[0] = box.min.x;
171 min[1] = box.min.y;
172 min[2] = box.min.z;
173 max[0] = box.max.x;
174 max[1] = box.max.y;
175 max[2] = box.max.z;
176 }
177 return result;
178}
179
180// FUNCTION: LEGO1 0x100a3ae0
181// FUNCTION: BETA10 0x1016ce00
182Result MeshBuilderImpl::GetBoundingBox(float min[3], float max[3]) const
183{
184 assert(m_data);
185
186 return MeshBuilderGetBoundingBox(m_data, min, max);
187}
188
189// FUNCTION: LEGO1 0x100a3b40
191{
192 MeshBuilderImpl* mesh = new MeshBuilderImpl();
193 int ret = m_data->Clone(0, IID_IDirect3DRMMesh, (void**) &mesh->m_data);
194 if (ret < 0) {
195 delete mesh;
196 mesh = NULL;
197 }
198 return mesh;
199}
[AI] Implements Tgl::MeshBuilder, manages mesh construction and provides the interface for creating g...
Definition: impl.h:1081
Mesh * CreateMesh(unsigned long faceCount, unsigned long vertexCount, float(*pPositions)[3], float(*pNormals)[3], float(*pTextureCoordinates)[2], unsigned long(*pFaceIndices)[3], unsigned long(*pTextureIndices)[3], ShadingModel shadingModel) override
Creates a mesh from arrays of data (positions, normals, etc.) [AI].
Definition: meshbuilder.cpp:18
Result GetBoundingBox(float min[3], float max[3]) const override
Gets the bounding box of the constructed mesh [AI].
MeshBuilder * Clone() override
Clones the mesh builder [AI].
void * ImplementationDataPtr() override
Returns mesh builder implementation pointer [AI].
Definition: meshbuilder.cpp:12
MeshBuilderImpl()
[AI] Initializes mesh builder to null [AI]
Definition: impl.h:1084
[AI] Implementation of Tgl::Mesh, manages a mesh object and geometry data with group index [AI]
Definition: impl.h:816
const MeshDataType & ImplementationData() const
Gets mesh implementation data (const) [AI].
Definition: impl.h:893
[AI] Builder class for assembling mesh data and extracting mesh objects.
Definition: tgl.h:768
[AI] Represents a renderable 3D mesh.
Definition: tgl.h:596
LONG D3DRMGROUPINDEX
Definition: d3drmdef.h:334
#define DECOMP_SIZE_ASSERT(T, S)
Definition: decomp.h:19
#define NULL
[AI] Null pointer value (C/C++ semantics).
Definition: legotypes.h:26
Result MeshBuilderGetBoundingBox(IDirect3DRMMesh *pMesh, float min[3], float max[3])
Result CreateMesh(IDirect3DRMMesh *pD3DRM, unsigned long faceCount, unsigned long vertexCount, float(*pPositions)[3], float(*pNormals)[3], float(*pTextureCoordinates)[2], unsigned long(*pFaceIndices)[3], unsigned long(*pTextureIndices)[3], ShadingModel shadingModel, MeshImpl::MeshDataType &rpMesh)
Definition: meshbuilder.cpp:58
Result MeshSetTextureMappingMode(MeshImpl::MeshData *pMesh, TextureMappingMode mode)
Definition: meshbuilder.cpp:48
[AI] Forward declaration for Direct3DRMTexture interface [AI]
Definition: impl.h:23
Result ResultVal(HRESULT result)
Returns a Tgl Result value for HRESULT [AI].
Definition: impl.h:35
int Succeeded(Result result)
[AI] Returns whether a Tgl::Result indicates success.
Definition: tgl.h:136
ShadingModel
[AI] Represents shading models available for rendering geometry.
Definition: tgl.h:29
TextureMappingMode
[AI] Methods for mapping textures onto geometry.
Definition: tgl.h:65
@ PerspectiveCorrect
[AI] Perspective-correct mapping (higher quality). [AI]
Definition: tgl.h:67
Result
[AI] Result type used throughout the Tgl API to report operation success or failure.
Definition: tgl.h:126
@ Error
[AI] Operation failed. [AI]
Definition: tgl.h:127
[AI] Holds D3DRMMesh pointer and group index for referencing geometry [AI]
Definition: impl.h:883
D3DRMGROUPINDEX groupIndex
[AI] Index within mesh group [AI]
Definition: impl.h:885
IDirect3DRMMesh * groupMesh
[AI] D3DRM mesh pointer [AI]
Definition: impl.h:884
D3DVECTOR min
Definition: d3drmdef.h:48
D3DVECTOR max
Definition: d3drmdef.h:48
D3DVECTOR normal
Definition: d3drmdef.h:329
D3DVALUE tv
Definition: d3drmdef.h:330
D3DVALUE tu
Definition: d3drmdef.h:330
D3DVECTOR position
Definition: d3drmdef.h:328
D3DVALUE z
Definition: d3dtypes.h:157
D3DVALUE y
Definition: d3dtypes.h:153
D3DVALUE x
Definition: d3dtypes.h:149