Last active
August 29, 2016 08:29
-
-
Save aboutqx/06b4279a0ea6693271840c0dbe0a54bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.learnopengles.android.lesson9; | |
import java.nio.ByteBuffer; | |
import java.nio.ByteOrder; | |
import java.nio.FloatBuffer; | |
import javax.microedition.khronos.egl.EGLConfig; | |
import javax.microedition.khronos.opengles.GL10; | |
import android.opengl.GLES20; | |
import android.opengl.GLSurfaceView; | |
import android.opengl.Matrix; | |
import android.util.Log; | |
public class LessonNineRenderer implements GLSurfaceView.Renderer | |
{ | |
private float[] mModelMatrix = new float[16]; | |
private float[] mViewMatrix = new float[16]; | |
private float[] mProjectionMatrix = new float[16]; | |
private float[] mMVPMatrix = new float[16]; | |
private int mMVPMatrixHandle; | |
private int mPositionHandle; | |
private static final String TAG = "LessonNineRenderer"; | |
float[] vertices = { | |
0.0f,0.0f,0.0f | |
}; | |
FloatBuffer vertexBuf; | |
private int mProgram; | |
@Override | |
public void onSurfaceCreated(GL10 glUnused, EGLConfig config) | |
{ | |
vertexBuf = ByteBuffer.allocateDirect(vertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); | |
vertexBuf.put(vertices).position(0); | |
// Set the background clear color to gray. | |
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
float eyeX=0.0f; | |
float eyeY=0.0f; | |
float eyeZ=-3f; | |
float centerX=0.0f; | |
float centerY=0.0f; | |
float centerZ=0.0f; | |
float upX=0.0f; | |
float upY=1.0f; | |
float upZ=0.0f; | |
// Set the view matrix. This matrix can be said to represent the camera position. | |
// NOTE: In OpenGL 1, a ModelView matrix is used, which is a combination of a model and | |
// view matrix. In OpenGL 2, we can keep track of these matrices separately if we choose. | |
Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ); | |
final String vertexShaderCode = | |
"uniform mat4 u_MVPMatrix; \n" | |
+ "attribute vec4 a_Position; \n" | |
+ "void main() \n" | |
+ "{ \n" | |
+ " gl_Position = u_MVPMatrix \n" | |
+ " * a_Position; \n" | |
+ " gl_PointSize = 10.0; \n" | |
+ "} \n"; | |
final String fragmentShaderCode = | |
"precision mediump float; \n" | |
+ "void main() \n" | |
+ "{ \n" | |
+ " gl_FragColor = vec4(1.0, \n" | |
+ " 1.0, 1.0, 1.0); \n" | |
+ "} \n"; | |
// Tell OpenGL to use this program when rendering. | |
// prepare shaders and OpenGL program | |
int vertexShader = loadShader( | |
GLES20.GL_VERTEX_SHADER, | |
vertexShaderCode); | |
int fragmentShader = loadShader( | |
GLES20.GL_FRAGMENT_SHADER, | |
fragmentShaderCode); | |
mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program | |
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program | |
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program | |
GLES20.glLinkProgram(mProgram); // create OpenGL program executables | |
} | |
public static int loadShader(int type, String shaderCode){ | |
// create a vertex shader type (GLES20.GL_VERTEX_SHADER) | |
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER) | |
int shader = GLES20.glCreateShader(type); | |
// add the source code to the shader and compile it | |
GLES20.glShaderSource(shader, shaderCode); | |
GLES20.glCompileShader(shader); | |
return shader; | |
} | |
/** | |
* Utility method for debugging OpenGL calls. Provide the name of the call | |
* just after making it: | |
* | |
* <pre> | |
* mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor"); | |
* MyGLRenderer.checkGlError("glGetUniformLocation");</pre> | |
* | |
* If the operation is not successful, the check throws an error. | |
* | |
* @param glOperation - Name of the OpenGL call to check. | |
*/ | |
public static void checkGlError(String glOperation) { | |
int error; | |
while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) { | |
Log.e(TAG, glOperation + ": glError " + error); | |
throw new RuntimeException(glOperation + ": glError " + error); | |
} | |
} | |
@Override | |
public void onSurfaceChanged(GL10 glUnused, int width, int height) | |
{ | |
// Set the OpenGL viewport to the same size as the surface. | |
GLES20.glViewport(0, 0, width, height); | |
// Create a new perspective projection matrix. The height will stay the same | |
// while the width will vary as per aspect ratio. | |
final float ratio = (float) width / height; | |
final float bottom = -1.0f; | |
final float top = 1.0f; | |
final float near = 3.0f; | |
final float far = 7.0f; | |
Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, bottom, top, near, far); | |
} | |
@Override | |
public void onDrawFrame(GL10 glUnused) | |
{ | |
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); | |
GLES20.glUseProgram(mProgram); | |
// Set program handles. These will later be used to pass in values to the program. | |
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "u_MVPMatrix"); | |
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "a_Position"); | |
checkGlError("glGetUniformLocation"); | |
Matrix.setIdentityM(mModelMatrix, 0); | |
//Push to the distance - note this will have no effect on a point size | |
Matrix.multiplyMV(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); | |
Matrix.multiplyMV(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0); | |
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0); | |
checkGlError("glUniformMatrix4fv"); | |
//Send the vertex | |
GLES20.glEnableVertexAttribArray(mPositionHandle); | |
GLES20.glVertexAttribPointer(mPositionHandle, 3, GLES20.GL_FLOAT, false, 0, vertexBuf); | |
//Draw the point | |
GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1); | |
GLES20.glDisableVertexAttribArray(mPositionHandle); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment