blob: 08e36ac026da4d0f80033fd6767c65af5041e9d4 [file] [log] [blame]
Jason Samse5ffb872009-08-09 17:01:55 -07001/*
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#include "rsContext.h"
18
19using namespace android;
20using namespace android::renderscript;
21
22#include <GLES/gl.h>
23#include <GLES/glext.h>
24
25SimpleMesh::SimpleMesh()
26{
27}
28
29SimpleMesh::~SimpleMesh()
30{
31}
32
33void SimpleMesh::render() const
34{
35 if (mPrimitiveType.get()) {
36 renderRange(0, mPrimitiveType->getDimX());
37 return;
38 }
39
40 if (mIndexType.get()) {
41 renderRange(0, mIndexType->getDimX());
42 return;
43 }
44
45 renderRange(0, mVertexTypes[0]->getDimX());
46}
47
48void SimpleMesh::renderRange(uint32_t start, uint32_t len) const
49{
50 if (len < 1) {
51 return;
52 }
53
54 glDisableClientState(GL_VERTEX_ARRAY);
55 glDisableClientState(GL_NORMAL_ARRAY);
56 glDisableClientState(GL_COLOR_ARRAY);
57 for (uint32_t ct=0; ct < RS_MAX_TEXTURE; ct++) {
58 glClientActiveTexture(GL_TEXTURE0 + ct);
59 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
60 }
61 glClientActiveTexture(GL_TEXTURE0);
62
63 for (uint32_t ct=0; ct < mVertexTypeCount; ct++) {
64 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffers[ct]->getBufferObjectID());
65 mVertexTypes[ct]->enableGLVertexBuffer();
66 }
67
68 if (mIndexType.get()) {
69 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID());
70 glDrawElements(mGLPrimitive, len, GL_UNSIGNED_SHORT, (GLvoid *)(start * 2));
71 } else {
72 glDrawArrays(mGLPrimitive, start, len);
73 }
74}
75
76
77
78SimpleMeshContext::SimpleMeshContext()
79{
80}
81
82SimpleMeshContext::~SimpleMeshContext()
83{
84}
85
86
87namespace android {
88namespace renderscript {
89
90
91RsSimpleMesh rsi_SimpleMeshCreate(Context *rsc, RsType prim, RsType idx, RsType *vtx, uint32_t vtxCount, uint32_t primType)
92{
93 SimpleMesh *sm = new SimpleMesh();
94 sm->incRef();
95
96 sm->mIndexType.set((const Type *)idx);
97 sm->mPrimitiveType.set((const Type *)prim);
98
99 sm->mVertexTypeCount = vtxCount;
100 sm->mVertexTypes = new ObjectBaseRef<const Type>[vtxCount];
101 sm->mVertexBuffers = new ObjectBaseRef<Allocation>[vtxCount];
102 for (uint32_t ct=0; ct < vtxCount; ct++) {
103 sm->mVertexTypes[ct].set((const Type *)vtx[ct]);
104 }
105
106 sm->mPrimitive = (RsPrimitive)primType;
107 switch(sm->mPrimitive) {
108 case RS_PRIMITIVE_POINT: sm->mGLPrimitive = GL_POINTS; break;
109 case RS_PRIMITIVE_LINE: sm->mGLPrimitive = GL_LINES; break;
110 case RS_PRIMITIVE_LINE_STRIP: sm->mGLPrimitive = GL_LINE_STRIP; break;
111 case RS_PRIMITIVE_TRIANGLE: sm->mGLPrimitive = GL_TRIANGLES; break;
112 case RS_PRIMITIVE_TRIANGLE_STRIP: sm->mGLPrimitive = GL_TRIANGLE_STRIP; break;
113 case RS_PRIMITIVE_TRIANGLE_FAN: sm->mGLPrimitive = GL_TRIANGLE_FAN; break;
114 }
115 return sm;
116}
117
118void rsi_SimpleMeshBindVertex(Context *rsc, RsSimpleMesh mv, RsAllocation va, uint32_t slot)
119{
120 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
121 rsAssert(slot < sm->mVertexTypeCount);
122
123 sm->mVertexBuffers[slot].set((Allocation *)va);
124}
125
126void rsi_SimpleMeshBindIndex(Context *rsc, RsSimpleMesh mv, RsAllocation va)
127{
128 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
129 sm->mIndexBuffer.set((Allocation *)va);
130}
131
132void rsi_SimpleMeshBindPrimitive(Context *rsc, RsSimpleMesh mv, RsAllocation va)
133{
134 SimpleMesh *sm = static_cast<SimpleMesh *>(mv);
135 sm->mPrimitiveBuffer.set((Allocation *)va);
136}
137
138void rsi_SimpleMeshDestroy(Context *rsc, RsSimpleMesh vtm)
139{
140 SimpleMesh * tm = static_cast<SimpleMesh *>(vtm);
141 tm->decRef();
142}
143
144
145
146
147}}
148