rsMesh.cpp revision fb6b614bcea88a587a7ea4530be45ff0ffa0210e
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_RS_BUILD_FOR_HOST
18#include "rsContext.h"
19
20#include <GLES/gl.h>
21#include <GLES2/gl2.h>
22#include <GLES/glext.h>
23#else
24#include "rsContextHostStub.h"
25
26#include <OpenGL/gl.h>
27#include <OpenGl/glext.h>
28#endif
29
30
31using namespace android;
32using namespace android::renderscript;
33
34Mesh::Mesh(Context *rsc) : ObjectBase(rsc)
35{
36    mAllocFile = __FILE__;
37    mAllocLine = __LINE__;
38    mVerticies = NULL;
39    mVerticiesCount = 0;
40    mPrimitives = NULL;
41    mPrimitivesCount = 0;
42}
43
44Mesh::~Mesh()
45{
46}
47
48void Mesh::serialize(OStream *stream) const
49{
50    // Need to identify ourselves
51    stream->addU32((uint32_t)getClassId());
52
53    String8 name(getName());
54    stream->addString(&name);
55
56    stream->addU32(mVerticiesCount);
57
58    for(uint32_t vCount = 0; vCount < mVerticiesCount; vCount ++) {
59        Verticies_t *verts = mVerticies[vCount];
60
61        stream->addU32(verts->mAllocationCount);
62
63        for (uint32_t aCount = 0; aCount < verts->mAllocationCount; aCount++) {
64            verts->mAllocations[aCount]->serialize(stream);
65        }
66        stream->addU32(verts->mVertexDataSize);
67
68        stream->addU32(verts->mOffsetCoord);
69        stream->addU32(verts->mOffsetTex);
70        stream->addU32(verts->mOffsetNorm);
71
72        stream->addU32(verts->mSizeCoord);
73        stream->addU32(verts->mSizeTex);
74        stream->addU32(verts->mSizeNorm );
75    }
76
77    stream->addU32(mPrimitivesCount);
78    // Store the primitives
79    for (uint32_t pCount = 0; pCount < mPrimitivesCount; pCount ++) {
80        Primitive_t * prim = mPrimitives[pCount];
81
82        stream->addU8((uint8_t)prim->mType);
83
84        // We store the index to the vertices
85        // So iterate over our vertices to find which one we point to
86        uint32_t vertexIndex = 0;
87        for(uint32_t vCount = 0; vCount < mVerticiesCount; vCount ++) {
88            if(prim->mVerticies == mVerticies[vCount]) {
89                vertexIndex = vCount;
90                break;
91            }
92        }
93        stream->addU32(vertexIndex);
94
95        stream->addU32(prim->mIndexCount);
96        for (uint32_t ct = 0; ct < prim->mIndexCount; ct++) {
97            stream->addU16(prim->mIndicies[ct]);
98        }
99
100        stream->addU32(prim->mRestartCounts);
101        for (uint32_t ct = 0; ct < prim->mRestartCounts; ct++) {
102            stream->addU16(prim->mRestarts[ct]);
103        }
104    }
105}
106
107Mesh *Mesh::createFromStream(Context *rsc, IStream *stream)
108{
109    // First make sure we are reading the correct object
110    A3DClassID classID = (A3DClassID)stream->loadU32();
111    if(classID != A3D_CLASS_ID_MESH) {
112        LOGE("mesh loading skipped due to invalid class id");
113        return NULL;
114    }
115
116    Mesh * mesh = new Mesh(rsc);
117
118    String8 name;
119    stream->loadString(&name);
120    mesh->setName(name.string(), name.size());
121
122    mesh->mVerticiesCount = stream->loadU32();
123    if(mesh->mVerticiesCount) {
124        mesh->mVerticies = new Verticies_t *[mesh->mVerticiesCount];
125    }
126    else {
127        mesh->mVerticies = NULL;
128    }
129
130    for(uint32_t vCount = 0; vCount < mesh->mVerticiesCount; vCount ++) {
131        Verticies_t *verts = new Verticies_t();
132        // Store our vertices one the mesh
133        mesh->mVerticies[vCount] = verts;
134
135        verts->mAllocationCount = stream->loadU32();
136        verts->mAllocations = new Allocation *[verts->mAllocationCount];
137
138        LOGE("processChunk_Verticies count %i", verts->mAllocationCount);
139        for (uint32_t aCount = 0; aCount < verts->mAllocationCount; aCount++) {
140            verts->mAllocations[aCount] = Allocation::createFromStream(rsc, stream);
141        }
142        verts->mVertexDataSize = stream->loadU32();
143
144        verts->mOffsetCoord = stream->loadU32();
145        verts->mOffsetTex = stream->loadU32();
146        verts->mOffsetNorm = stream->loadU32();
147
148        verts->mSizeCoord = stream->loadU32();
149        verts->mSizeTex = stream->loadU32();
150        verts->mSizeNorm = stream->loadU32();
151    }
152
153    mesh->mPrimitivesCount = stream->loadU32();
154    if(mesh->mPrimitivesCount) {
155        mesh->mPrimitives = new Primitive_t *[mesh->mPrimitivesCount];
156    }
157    else {
158        mesh->mPrimitives = NULL;
159    }
160
161    // load all primitives
162    for (uint32_t pCount = 0; pCount < mesh->mPrimitivesCount; pCount ++) {
163        Primitive_t * prim = new Primitive_t;
164        mesh->mPrimitives[pCount] = prim;
165
166        prim->mType = (RsPrimitive)stream->loadU8();
167
168        // We store the index to the vertices
169        uint32_t vertexIndex = stream->loadU32();
170        if(vertexIndex < mesh->mVerticiesCount) {
171            prim->mVerticies = mesh->mVerticies[vertexIndex];
172        }
173        else {
174            prim->mVerticies = NULL;
175        }
176
177        prim->mIndexCount = stream->loadU32();
178        if(prim->mIndexCount){
179            prim->mIndicies = new uint16_t[prim->mIndexCount];
180            for (uint32_t ct = 0; ct < prim->mIndexCount; ct++) {
181                prim->mIndicies[ct] = stream->loadU16();
182            }
183        }
184        else {
185            prim->mIndicies = NULL;
186        }
187
188        prim->mRestartCounts = stream->loadU32();
189        if (prim->mRestartCounts) {
190            prim->mRestarts = new uint16_t[prim->mRestartCounts];
191            for (uint32_t ct = 0; ct < prim->mRestartCounts; ct++) {
192                prim->mRestarts[ct] = stream->loadU16();
193
194            }
195        }
196        else {
197            prim->mRestarts = NULL;
198        }
199
200    }
201
202    return mesh;
203}
204
205
206MeshContext::MeshContext()
207{
208}
209
210MeshContext::~MeshContext()
211{
212}
213
214