1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 #include "Scene.h"
15 
16 #include <graphics/GLUtils.h>
17 #include <graphics/ProgramNode.h>
18 
19 #include <Trace.h>
20 
Scene(int width,int height)21 Scene::Scene(int width, int height) :
22         mWidth(width), mHeight(height) {
23 }
24 
setUpContext()25 bool Scene::setUpContext() {
26     SCOPED_TRACE();
27     if (!setUpPrograms()) {
28         return false;
29     }
30     mModelMatrix = setUpModelMatrix();
31     if (mModelMatrix == NULL) {
32         return false;
33     }
34     mViewMatrix = setUpViewMatrix();
35     if (mViewMatrix == NULL) {
36         return false;
37     }
38     mProjectionMatrix = setUpProjectionMatrix(mWidth, mHeight);
39     if (mProjectionMatrix == NULL) {
40         return false;
41     }
42     return true;
43 }
44 
tearDown()45 bool Scene::tearDown() {
46     SCOPED_TRACE();
47     for (size_t i = 0; i < mTextureIds.size(); i++) {
48         glDeleteTextures(1, &(mTextureIds[i]));
49     }
50     for (size_t i = 0; i < mMeshes.size(); i++) {
51         delete mMeshes[i];
52     }
53     for (size_t i = 0; i < mSceneGraphs.size(); i++) {
54         delete mSceneGraphs[i];
55     }
56     delete mModelMatrix;
57     mModelMatrix = NULL;
58     delete mViewMatrix;
59     mViewMatrix = NULL;
60     delete mProjectionMatrix;
61     mProjectionMatrix = NULL;
62     return true;
63 }
64 
update(int frame)65 bool Scene::update(int frame) {
66     SCOPED_TRACE();
67     // Delete the old scene graphs.
68     for (size_t i = 0; i < mSceneGraphs.size(); i++) {
69         delete mSceneGraphs[i];
70     }
71     mSceneGraphs.clear();
72     return updateSceneGraphs(frame);
73 }
74 
drawSceneGraph(int index)75 void Scene::drawSceneGraph(int index) {
76     mModelMatrix->identity();
77     mSceneGraphs[index]->drawProgram(*mModelMatrix, *mViewMatrix, *mProjectionMatrix);
78 }
79