openGL es2.0 创建颜色平面和文理平面
一、创建颜色平面的Java代码和Shader Language:
package com.gzdxid.utils;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import android.opengl.GLES20;
public class DrawRectColor {
int mProgram;
int muMVPMatrixHandle;
int maPositionHandle;
int muColorRHandle;
int muColorGHandle;
int muColorBHandle;
int muColorAHandle;
FloatBuffer mVertexBuffer;
int vCount = 0;
float r,g,b,a;
public DrawRectColor(float width,float height,int mProgam) {
initVertexData(width,height);
intShader(mProgam);
}
private void initVertexData(float width,float height) {
// TODO Auto-generated method stub
vCount = 6;
float w = width / 2;
float h = height / 2;
float vertices[] = new float[] {
-w, h, 0,
-w, -h, 0,
w, -h, 0,
w, -h, 0,
w, h, 0,
-w, h, 0,
};
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
}
private void intShader(int mProgram) {
// TODO Auto-generated method stub
this.mProgram=mProgram;
muMVPMatrixHandle=GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
maPositionHandle=GLES20.glGetAttribLocation(mProgram, "aPosition");
muColorRHandle=GLES20.glGetUniformLocation(mProgram, "uColorR");
muColorGHandle=GLES20.glGetUniformLocation(mProgram, "uColorG");
muColorBHandle=GLES20.glGetUniformLocation(mProgram, "uColorB");
muColorAHandle=GLES20.glGetUniformLocation(mProgram, "uColorA");
}
public void drawSelf(float r,float g,float b,float a){
GLES20.glUseProgram(mProgram);
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
GLES20.glUniform1f(muColorRHandle, r);
GLES20.glUniform1f(muColorGHandle, g);
GLES20.glUniform1f(muColorBHandle, b);
GLES20.glUniform1f(muColorAHandle, a);
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3*4, mVertexBuffer);
GLES20.glEnableVertexAttribArray(maPositionHandle);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
}
}
定点着色器代码:
uniform mat4 uMVPMatrix;
attribute vec3 aPosition;
void main(){
gl_Position=uMVPMatrix*vec4(aPosition,1);
}
片源着色器代码:
precision mediump float;
uniform float uColorR;
uniform float uColorG;
uniform float uColorB;
uniform float uColorA;
void main(){
vec4 finalColor;
finalColor=vec4(uColorR,uColorG,uColorB,uColorA);
gl_FragColor=finalColor;
}
二、创建文理平面的Java代码和Shader Language:
package com.gzdxid.utils;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import android.opengl.GLES20;
public class DrawRectTexture {
int mProgram;
int muMVPMatrixHandle;
int maPositionHandle;
int maTexCoorHandle;
FloatBuffer mVertexBuffer;
FloatBuffer mTexCoorBuffer;
int vCount = 0;
public DrawRectTexture(float width, float height, int mProgam) {
// TODO Auto-generated constructor stub
initVertexData(width, height);
intShader(mProgam);
}
private void initVertexData(float width, float height) {
// TODO Auto-generated method stub
vCount = 6;
float w = width / 2;
float h = height / 2;
float vertices[] = new float[] {
-w, h, 0,
-w, -h, 0,
w, -h, 0,
w, -h, 0,
w, h, 0,
-w, h, 0,
};
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
float texCoor[] = new float[] { 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0 };
ByteBuffer cbb = ByteBuffer.allocateDirect(texCoor.length * 4);
cbb.order(ByteOrder.nativeOrder());
mTexCoorBuffer = cbb.asFloatBuffer();
mTexCoorBuffer.put(texCoor);
mTexCoorBuffer.position(0);
}
private void intShader(int mProgam) {
// TODO Auto-generated method stub
this.mProgram = mProgam;
muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgam, "uMVPMatrix");
maPositionHandle = GLES20.glGetAttribLocation(mProgam, "aPosition");
maTexCoorHandle = GLES20.glGetAttribLocation(mProgam, "aTexCoor");
}
public void drawSelf(int texId) {
GLES20.glUseProgram(mProgram);
GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer);
GLES20.glVertexAttribPointer(maTexCoorHandle, 2, GLES20.GL_FLOAT, false, 2 * 4, mTexCoorBuffer);
GLES20.glEnableVertexAttribArray(maPositionHandle);
GLES20.glEnableVertexAttribArray(maTexCoorHandle);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
}
}
定点着色器代码:
uniform mat4 uMVPMatrix;
attribute vec3 aPosition;
attribute vec2 aTexCoor;
varying vec2 vTextureCoord;
void main(){
gl_Position=uMVPMatrix*vec4(aPosition,1);
vTextureCoord=aTexCoor;
}
片源着色器代码:
precision mediump float;
varying vec2 vTextureCoord;
uniform sampler2D sTexture;
void main(){
gl_FragColor=texture2D(sTexture,vTextureCoord);
}
还没有评论,来说两句吧...