blob: 6f5e60541a2a3a0fb91c4cfcde167cc1e1b5b520 [file] [log] [blame]
Magnus Jedvert80cf97c2015-06-11 10:08:59 +02001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
Magnus Jedvert80cf97c2015-06-11 10:08:59 +02003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
Magnus Jedvert80cf97c2015-06-11 10:08:59 +02009 */
10
11package org.webrtc;
12
13import android.opengl.GLES20;
Magnus Jedvert80cf97c2015-06-11 10:08:59 +020014
15import java.nio.ByteBuffer;
16import java.nio.ByteOrder;
17import java.nio.FloatBuffer;
18
19/**
20 * Some OpenGL static utility functions.
21 */
22public class GlUtil {
Magnus Jedvert80cf97c2015-06-11 10:08:59 +020023 private GlUtil() {}
24
25 // Assert that no OpenGL ES 2.0 error has been raised.
26 public static void checkNoGLES2Error(String msg) {
27 int error = GLES20.glGetError();
28 if (error != GLES20.GL_NO_ERROR) {
29 throw new RuntimeException(msg + ": GLES20 error: " + error);
30 }
31 }
32
33 public static FloatBuffer createFloatBuffer(float[] coords) {
34 // Allocate a direct ByteBuffer, using 4 bytes per float, and copy coords into it.
35 ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * 4);
36 bb.order(ByteOrder.nativeOrder());
37 FloatBuffer fb = bb.asFloatBuffer();
38 fb.put(coords);
39 fb.position(0);
40 return fb;
41 }
Magnus Jedvert1a591dd2015-09-02 14:43:03 +020042
43 /**
44 * Generate texture with standard parameters.
45 */
46 public static int generateTexture(int target) {
47 final int textureArray[] = new int[1];
48 GLES20.glGenTextures(1, textureArray, 0);
49 final int textureId = textureArray[0];
50 GLES20.glBindTexture(target, textureId);
51 GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
52 GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
53 GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
54 GLES20.glTexParameterf(target, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
55 checkNoGLES2Error("generateTexture");
56 return textureId;
57 }
Magnus Jedvert80cf97c2015-06-11 10:08:59 +020058}