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