OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chromoting.cardboard; | |
6 | |
7 import static android.opengl.GLES20.GL_LINEAR; | |
8 import static android.opengl.GLES20.GL_NEAREST; | |
9 import static android.opengl.GLES20.GL_TEXTURE_2D; | |
10 import static android.opengl.GLES20.GL_TEXTURE_CUBE_MAP; | |
11 import static android.opengl.GLES20.GL_TEXTURE_CUBE_MAP_NEGATIVE_X; | |
12 import static android.opengl.GLES20.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y; | |
13 import static android.opengl.GLES20.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; | |
14 import static android.opengl.GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_X; | |
15 import static android.opengl.GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_Y; | |
16 import static android.opengl.GLES20.GL_TEXTURE_CUBE_MAP_POSITIVE_Z; | |
17 import static android.opengl.GLES20.GL_TEXTURE_MAG_FILTER; | |
18 import static android.opengl.GLES20.GL_TEXTURE_MIN_FILTER; | |
19 import static android.opengl.GLES20.glBindTexture; | |
20 import static android.opengl.GLES20.glDeleteTextures; | |
21 import static android.opengl.GLES20.glGenTextures; | |
22 import static android.opengl.GLES20.glTexParameteri; | |
23 import static android.opengl.GLUtils.texImage2D; | |
24 | |
25 import android.graphics.Bitmap; | |
26 | |
27 /** | |
28 * Helper class for working with OpenGL textures. | |
29 */ | |
30 public class TextureHelper { | |
31 /** | |
32 * Create new texture handle. | |
33 * @return New texture handle. | |
34 */ | |
35 public static int createTextureHandle() { | |
36 int[] textureDataHandle = new int[1]; | |
37 glGenTextures(1, textureDataHandle, 0); | |
38 if (textureDataHandle[0] != 0) { | |
39 return textureDataHandle[0]; | |
40 } else { | |
41 throw new RuntimeException("Error generating texture handle."); | |
42 } | |
43 } | |
44 | |
45 /** | |
46 * Link desktop texture with a handle. | |
47 * @param textureDataHandle the handle to attach texture to | |
48 */ | |
49 public static void linkTexture(int textureDataHandle, Bitmap bitmap) { | |
50 // Delete previously attached texture. | |
51 glDeleteTextures(1, new int[]{textureDataHandle}, 0); | |
52 | |
53 // Bind to the texture in OpenGL. | |
54 glBindTexture(GL_TEXTURE_2D, textureDataHandle); | |
55 | |
56 glTexParameteri(GL_TEXTURE_2D, | |
57 GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
58 glTexParameteri(GL_TEXTURE_2D, | |
59 GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
60 | |
61 // Load the bitmap into the bound texture. | |
62 texImage2D(GL_TEXTURE_2D, 0, bitmap, 0); | |
63 } | |
64 | |
65 /** | |
66 * Link the cubemap images with a given texture handle. | |
67 */ | |
68 public static void linkCubeMap(int textureDataHandle, Bitmap[] cubeBitmaps)
{ | |
69 glBindTexture(GL_TEXTURE_CUBE_MAP, textureDataHandle); | |
70 | |
71 // Linear filtering for minification and magnification. | |
72 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
73 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
74 | |
75 // Link left, right, bottom, top, back and front image in order. | |
76 texImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, cubeBitmaps[0], 0); | |
77 texImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, cubeBitmaps[1], 0); | |
78 | |
79 texImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, cubeBitmaps[2], 0); | |
80 texImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, cubeBitmaps[3], 0); | |
81 | |
82 texImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, cubeBitmaps[4], 0); | |
83 texImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, cubeBitmaps[5], 0); | |
84 } | |
85 } | |
OLD | NEW |