blob: 37b037c3a4ff70121f06d0b7643d22f9e6b841b2 [file] [log] [blame]
Jason Sams0826a6f2009-06-15 19:04:56 -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
17package android.renderscript;
18
19import java.lang.Math;
Jason Samsb8c5a842009-07-31 20:40:47 -070020
21import android.renderscript.Element;
Jason Sams0826a6f2009-06-15 19:04:56 -070022import android.util.Log;
23
24
Jason Samse29d4712009-07-23 15:19:03 -070025/**
26 * @hide
27 *
28 **/
Jason Sams0826a6f2009-06-15 19:04:56 -070029public class ProgramVertexAlloc {
30 public static final int MODELVIEW_OFFSET = 0;
31 public static final int PROJECTION_OFFSET = 16;
32 public static final int TEXTURE_OFFSET = 32;
33
34 Matrix mModel;
35 Matrix mProjection;
36 Matrix mTexture;
37
Jason Samsb8c5a842009-07-31 20:40:47 -070038 public Allocation mAlloc;
Jason Sams0826a6f2009-06-15 19:04:56 -070039
40 public ProgramVertexAlloc(RenderScript rs) {
41 mModel = new Matrix();
42 mProjection = new Matrix();
43 mTexture = new Matrix();
44
Jason Samsb8c5a842009-07-31 20:40:47 -070045 mAlloc = Allocation.createSized(rs, Element.USER_FLOAT, 48);
Jason Sams0826a6f2009-06-15 19:04:56 -070046 mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat);
47 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
48 mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat);
49 }
50
51 public void loadModelview(Matrix m) {
52 mModel = m;
53 mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat);
54 }
55
56 public void loadProjection(Matrix m) {
57 mProjection = m;
58 mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat);
59 }
60
61 public void loadTexture(Matrix m) {
62 mTexture = m;
63 mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat);
64 }
65
66 public void setupOrthoWindow(int w, int h) {
67 mProjection.loadOrtho(0,w, h,0, -1,1);
68 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
69 }
70
71 public void setupOrthoNormalized(int w, int h) {
72 // range -1,1 in the narrow axis.
73 if(w > h) {
74 float aspect = ((float)w) / h;
75 mProjection.loadOrtho(-aspect,aspect, -1,1, -1,1);
76 } else {
77 float aspect = ((float)h) / w;
78 mProjection.loadOrtho(-1,1, -aspect,aspect, -1,1);
79 }
80 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
81 }
82
83 public void setupProjectionNormalized(int w, int h) {
84 // range -1,1 in the narrow axis at z = 0.
85 Matrix m1 = new Matrix();
86 Matrix m2 = new Matrix();
87
88 if(w > h) {
89 float aspect = ((float)w) / h;
90 m1.loadFrustum(-aspect,aspect, -1,1, 1,100);
91 } else {
92 float aspect = ((float)h) / w;
93 m1.loadFrustum(-1,1, -aspect,aspect, 1,100);
94 }
95
96 m2.loadRotate(180, 0, 1, 0);
97 m1.loadMultiply(m1, m2);
98
99 m2.loadScale(-2, 2, 1);
100 m1.loadMultiply(m1, m2);
101
102 m2.loadTranslate(0, 0, 2);
103 m1.loadMultiply(m1, m2);
104
105 mProjection = m1;
106 mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
107 }
108
109}
110
111
112
113
114
115