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 
15 #include "PerspectiveMeshNode.h"
16 
17 #include "PerspectiveProgram.h"
18 
PerspectiveMeshNode(const Mesh * mesh,const GLuint textureId)19 PerspectiveMeshNode::PerspectiveMeshNode(const Mesh* mesh, const GLuint textureId) :
20         TexturedMeshNode(mesh, textureId) {
21 }
22 
before(Program & program,Matrix & model,Matrix & view,Matrix & projection)23 void PerspectiveMeshNode::before(Program& program, Matrix& model, Matrix& view, Matrix& projection) {
24     PerspectiveProgram& prog = (PerspectiveProgram&) program;
25 
26     int textureUniformHandle = glGetUniformLocation(prog.mProgramId, "u_Texture");
27     int positionHandle = glGetAttribLocation(prog.mProgramId, "a_Position");
28     int normalHandle = glGetAttribLocation(prog.mProgramId, "a_Normal");
29     int texCoordHandle = glGetAttribLocation(prog.mProgramId, "a_TexCoordinate");
30 
31     // Set the texture.
32     glActiveTexture (GL_TEXTURE0);
33     glBindTexture(GL_TEXTURE_2D, mTextureId);
34     glUniform1i(textureUniformHandle, 0);
35 
36     glEnableVertexAttribArray(positionHandle);
37     glVertexAttribPointer(positionHandle, 3, GL_FLOAT, false, 0, mMesh->mVertices);
38     glEnableVertexAttribArray(normalHandle);
39     glVertexAttribPointer(normalHandle, 3, GL_FLOAT, false, 0, mMesh->mNormals);
40     glEnableVertexAttribArray(texCoordHandle);
41     glVertexAttribPointer(texCoordHandle, 2, GL_FLOAT, false, 0, mMesh->mTexCoords);
42 
43     // This multiplies the view matrix by the model matrix, and stores the result in the MVP
44     // matrix (which currently contains model * view).
45     prog.mMVMatrix.multiply(view, model);
46 
47     // Pass in the modelview matrix.
48     glUniformMatrix4fv(prog.mMVMatrixHandle, 1, false, prog.mMVMatrix.mData);
49 
50     // This multiplies the modelview matrix by the projection matrix, and stores the result in
51     // the MVP matrix (which now contains model * view * projection).
52     prog.mMVPMatrix.multiply(projection, prog.mMVMatrix);
53 
54     // Pass in the combined matrix.
55     glUniformMatrix4fv(prog.mMVPMatrixHandle, 1, false, prog.mMVPMatrix.mData);
56 
57     // Pass in the light position in eye space.
58     glUniform3f(prog.mLightPosHandle, prog.mLightPosInEyeSpace[0], prog.mLightPosInEyeSpace[1],
59             prog.mLightPosInEyeSpace[2]);
60 
61     glDrawArrays(GL_TRIANGLES, 0, mMesh->mNumVertices);
62 }
63 
after(Program & program,Matrix & model,Matrix & view,Matrix & projection)64 void PerspectiveMeshNode::after(Program& program, Matrix& model, Matrix& view, Matrix& projection) {
65 }
66