我试图让这个网站的每像素着色器工作: http://antonholmquist.com/blog/opengl-es-2-0-shader-lighting-examples/ 我在Shape3d类中添加了一些代码。现在它看起来像这样:
class DirectionalLight{
float direction[];
float halfplane[];
float ambientColor[];
float diffuseColor[];
float specularColor[];
};
class Material{
float ambientFactor[];
float diffuseFactor[];
float specularFactor[];
float shininess;
};
public class Shape3d {
private final FloatBuffer Positions;
private final FloatBuffer Normals;
private final ShortBuffer Order;
/** This will be used to pass in the transformation matrix. */
private int mMVPMatrixHandle;
/** This will be used to pass in the modelview matrix. */
private int mMVMatrixHandle;
/** This will be used to pass in model normal information. */
private int mNormalHandle;
/** This will be used to pass in model position information. */
private int mPositionHandle;
private int lightDirection;
private int lightHalfplane;
private int lightAmbientColor;
private int lightDiffuseColor;
private int lightSpecularColor;
private int matAmbientFactor;
private int matDiffuseFactor;
private int matSpecularFactor;
private int matShininess;
/** This will be used to pass in model color information. */
private int mColorHandle;
// New class members
/** Allocate storage for the final combined matrix. This will be passed into the shader program. */
private float[] mMVPMatrix = new float[16];
private final int mPositionDataSize = 3;
private final int mNormalDataSize=3;
Material material = new Material();
DirectionalLight directionallight = new DirectionalLight();
int programHandle = GLES20.glCreateProgram();
int vertexShaderHandle;
int fragmentShaderHandle;
short[] drawOrder;
Shape3d(float[] cubePositionData,short[]dOrder ,float[] colorData,float[]cubeNormalData) {
int mBytesPerFloat = 4;
for (int i=0;i<dOrder.length;i++) {
dOrder[i]--;
}
drawOrder=new short[dOrder.length];
drawOrder=dOrder;
material.ambientFactor= new float[]{0.5f, 0.5f, 0.5f, 0.5f};
material.diffuseFactor= new float[]{0.5f, 0.5f, 0.5f, 0.5f};
material.specularFactor= new float[]{0.5f, 0.5f, 0.5f, 0.5f};
material.shininess=0.1f;
directionallight.ambientColor=new float[]{1.0f,1.0f,1.0f,1.0f};
directionallight.diffuseColor=new float[]{1.0f,1.0f,1.0f,1.0f};
directionallight.specularColor=new float[]{1.0f,1.0f,1.0f,1.0f};
directionallight.direction=new float[]{0.0f,0.0f,1.0f};
directionallight.halfplane=new float[]{0.0f,0.0f,0.0f};
// Initialize the buffers.
Positions = ByteBuffer.allocateDirect(cubePositionData.length * mBytesPerFloat)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
Positions.put(cubePositionData).position(0);
Normals = ByteBuffer.allocateDirect(cubeNormalData.length * mBytesPerFloat)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
Normals.put(cubeNormalData).position(0);
Order = ByteBuffer.allocateDirect(dOrder.length * 4)
.order(ByteOrder.nativeOrder()).asShortBuffer();
Order.put(dOrder).position(0);
//Shader
vertexShaderHandle = Shaders.loadShader(GLES20.GL_VERTEX_SHADER, Shaders.vertexShader2);
fragmentShaderHandle = Shaders.loadShader(GLES20.GL_FRAGMENT_SHADER,Shaders.fragmentShader2);
programHandle = Shaders.createProgram(programHandle, vertexShaderHandle, fragmentShaderHandle);
GLES20.glUseProgram(programHandle);
}
public void draw(float[]mProjectionMatrix,float[]mViewMatrix,float[]mModelMatrix)
{
//mMVPMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVPMatrix");
//mMVMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVMatrix");
mPositionHandle = GLES20.glGetAttribLocation(programHandle, "a_Position");
mNormalHandle = GLES20.glGetAttribLocation(programHandle, "a_Normal");
mMVMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVMatrix");
mMVPMatrixHandle = GLES20.glGetUniformLocation(programHandle, "u_MVPMatrix");
lightDirection = GLES20.glGetUniformLocation(programHandle, "u_directionalLight.direction");
lightHalfplane = GLES20.glGetUniformLocation(programHandle, "u_directionalLight.halfplane");
lightAmbientColor = GLES20.glGetUniformLocation(programHandle, "u_directionalLight.ambientColor");
lightDiffuseColor = GLES20.glGetUniformLocation(programHandle, "u_directionalLight.diffuseColor");
lightSpecularColor = GLES20.glGetUniformLocation(programHandle, "u_directionalLight.specularColor");
System.out.println(GLU.gluErrorString(GLES20.glGetError()));
matAmbientFactor = GLES20.glGetUniformLocation(programHandle, "u_material.ambientFactor");
matDiffuseFactor = GLES20.glGetUniformLocation(programHandle, "u_material.diffuseFactor");
matSpecularFactor = GLES20.glGetUniformLocation(programHandle, "u_material.specularFactor");
matShininess = GLES20.glGetUniformLocation(programHandle, "u_material.shininess");
System.out.println(GLU.gluErrorString(GLES20.glGetError()));
System.out.println(GLU.gluErrorString(GLES20.glGetError()));
// Pass in the position information
fbPositions.position(0);
GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize, GLES20.GL_FLOAT, false,
0, fbPositions);
System.out.println(GLU.gluErrorString(GLES20.glGetError()));
GLES20.glEnableVertexAttribArray(mPositionHandle);
System.out.println(GLU.gluErrorString(GLES20.glGetError()));
// Pass in the normal information
fbNormals.position(0);
System.out.println(GLU.gluErrorString(GLES20.glGetError()));
GLES20.glVertexAttribPointer(mNormalHandle, mNormalDataSize, GLES20.GL_FLOAT, false,
0, fbNormals);
System.out.println(GLU.gluErrorString(GLES20.glGetError()));
GLES20.glEnableVertexAttribArray(mNormalHandle);
System.out.println(GLU.gluErrorString(GLES20.glGetError()));
// This multiplies the view matrix by the model matrix, and stores the result in the MVP matrix
// (which currently contains model * view).
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
System.out.println(GLU.gluErrorString(GLES20.glGetError()));
GLES20.glUniformMatrix4fv(mMVMatrixHandle, 1, false, mMVPMatrix, 0);
System.out.println(GLU.gluErrorString(GLES20.glGetError()));
// This multiplies the modelview matrix by the projection matrix, and stores the result in the MVP matrix
// (which now contains model * view * projection).
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
System.out.println(GLU.gluErrorString(GLES20.glGetError()));
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_LINES, 0,drawOrderLength);
System.out.println(GLU.gluErrorString(GLES20.glGetError()));
}
加载着色器:
public static int loadShader(int type, String shaderCode){
int shader = GLES20.glCreateShader(type);
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
final int[] compileStatus = new int[1];
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
if (compileStatus[0] == 0)
{
GLES20.glDeleteShader(shader);
throw new RuntimeException("Error creating shader.");
}
return shader;}
public static int createProgram(int programHandle,int vertexShaderHandle,int fragmentShaderHandle){
GLES20.glAttachShader(programHandle, vertexShaderHandle);
GLES20.glAttachShader(programHandle, fragmentShaderHandle);
GLES20.glBindAttribLocation(programHandle, 0, "a_Position");
GLES20.glBindAttribLocation(programHandle, 1, "a_Normal");
GLES20.glLinkProgram(programHandle);
final int[] linkStatus = new int[1];
GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);
if (linkStatus[0] == 0)
{
GLES20.glDeleteProgram(programHandle);
programHandle = 0;
}
if (programHandle == 0)
{
throw new RuntimeException("Error creating program.");
}
return programHandle;}
它返回
之后的无效操作GLES20.glUniform3f(lightDirection, directionallight.direction[0], directionallight.direction[1], directionallight.direction[2]);
GLES20.glUniform3f(lightHalfplane, directionallight.halfplane[0], directionallight.halfplane[1], directionallight.halfplane[2]);
GLES20.glUniform4f(lightAmbientColor, directionallight.ambientColor[0], directionallight.ambientColor[1], directionallight.ambientColor[2], directionallight.ambientColor[3]);
GLES20.glUniform4f(lightDiffuseColor, directionallight.diffuseColor[0], directionallight.diffuseColor[1], directionallight.diffuseColor[2], directionallight.diffuseColor[3]);
GLES20.glUniform4f(lightSpecularColor, directionallight.specularColor[0], directionallight.specularColor[1], directionallight.specularColor[2], directionallight.specularColor[3]);
GLES20.glUniform4f(matAmbientFactor, material.ambientFactor[0], material.ambientFactor[1], material.ambientFactor[2], material.ambientFactor[3]);
GLES20.glUniform4f(matDiffuseFactor, material.diffuseFactor[0], material.diffuseFactor[1], material.diffuseFactor[2], material.diffuseFactor[3]);
GLES20.glUniform4f(matSpecularFactor, material.specularFactor[0], material.specularFactor[1], material.specularFactor[2], material.specularFactor[3]);
GLES20.glUniform1f(matShininess, material.shininess);
有人可以帮我这个吗?