将Orange Book着色器移植到OpenGL ES 2.0

问题描述

| 我正在尝试将Orange Book砖着色器移植到OpenGL ES 2.0,但是我遇到了照明问题。在没有照明的情况下,我看到的是砖块,但砖块的形状只是黑色。 垂直
const float ZERO = 0.0;
const float ONE = 1.0;

// Should be built in,but this is GLES
uniform mat4 ModelViewMatrix;
uniform mat4 ModelViewProjectionMatrix;
uniform mat3 normalMatrix;
attribute vec4 Vertex;
attribute vec3 normal;

// Other variables
uniform vec3 LightPosition;

const float specularContribution = 0.3;
const float DiffuseContribution = 1.0 - specularContribution;

varying float LightIntensity;
varying vec2 MCPosition;

void main()
{
    vec3 ecPosition = vec3(ModelViewMatrix * Vertex);
    vec3 tnorm      = normalize(normalMatrix * normal);
    vec3 lightVec   = normalize(LightPosition - ecPosition);
    vec3 reflectVec = reflect(-lightVec,tnorm);
    vec3 viewVec    = normalize(-ecPosition);
    float diffuse   = max(dot(lightVec,tnorm),ZERO);
    float spec      = ZERO;

        if(diffuse > ZERO)
    {
        spec = max(dot(reflectVec,viewVec),ZERO);
        spec = pow(spec,16.0);
    }

    LightIntensity  = DiffuseContribution * diffuse +
              specularContribution * spec;

    MCPosition  = Vertex.xy;
    gl_Position = ModelViewProjectionMatrix * Vertex;
}
易碎
precision mediump float;

const float ONE = 1.0;
const float HALF = 0.5;

uniform vec3 BrickColor,MortarColor;
uniform vec2 BrickSize,BrickPercent;

varying float LightIntensity;
varying vec2 MCPosition;

void main()
{
    vec3 color;
    vec2 position,useBrick;

    position = MCPosition / BrickSize;

    if(fract(position.y * HALF) > HALF)
        position.x += HALF;

    position = fract(position);

    useBrick = step(position,BrickPercent);

    color = mix(MortarColor,BrickColor,useBrick.x * useBrick.y);
    color *= LightIntensity;
    gl_FragColor = vec4(color,ONE);
}
这是我的Java
@Override
public void onSurfaceCreated(GL10 glunused,EGLConfig config)
{
    GLES20.glClearColor(0.3922f,0.5843f,0.9294f,1.0f);
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);

    try
    {
        mTorus.genTorus(60,1.25f,0.5f);
        mAngle = 45.0f;

        mProgramObject = ESShader.loadProgram(mContext,\"cube.vert\",\"cube.frag\");

        mModelViewLoc = GLES20.glGetUniformlocation(mProgramObject,\"ModelViewMatrix\");
        mMVPLoc = GLES20.glGetUniformlocation(mProgramObject,\"ModelViewProjectionMatrix\");
        mnormalMatrixLoc = GLES20.glGetUniformlocation(mProgramObject,\"normalMatrix\");
        mLightPositionLoc = GLES20.glGetUniformlocation(mProgramObject,\"LightPosition\");

        mBrickColorLoc = GLES20.glGetUniformlocation(mProgramObject,\"BrickColor\");
        mMortarColorLoc = GLES20.glGetUniformlocation(mProgramObject,\"MortarColor\");
        mBrickSizeLoc = GLES20.glGetUniformlocation(mProgramObject,\"BrickSize\");
        mBrickPercentLoc = GLES20.glGetUniformlocation(mProgramObject,\"BrickPercent\");

        mPositionLoc = GLES20.glGetAttribLocation(mProgramObject,\"Vertex\");
            mnormalLoc = GLES20.glGetAttribLocation(mProgramObject,\"normal\");
    }
    catch(IOException ioe)
    {
        System.err.println(\"Failed to load shaders\");
    }
}

@Override
public void onSurfaceChanged(GL10 glunused,int width,int height)
{
    mWidth = width;
    mHeight = height;

    mProjection.loadIdentity();
    mProjection.perspective(60.0f,(float)mWidth / (float)mHeight,1.0f,20.0f);

    GLES20.glViewport(0,mWidth,mHeight);
}

private void update()
{
    if(mLastTime == 0) mLastTime = SystemClock.uptimeMillis();
    long curTime = SystemClock.uptimeMillis();
    long elapsedtime = curTime - mLastTime;
    float deltaTime = elapsedtime / 1000.0f;
    mLastTime = curTime;

    // Compute a rotation angle based on time to rotate the cube
    mAngle += (deltaTime * 40.0f);
    if(mAngle >= 360.0f) mAngle -= 360.0f;
}

@Override
public void onDrawFrame(GL10 glunused)
{
    update();
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    // Use the program object
    GLES20.gluseProgram(mProgramObject);

    mModelView.loadIdentity();
    mModelView.translate(0.0f,0.0f,-5.0f);
    mModelView.rotate(mAngle,0.75f);

    mnormalMatrix.load(mModelView);
    mnormalMatrix.invert();
    mnormalMatrix.transpose();

    mMVPMatrix.multiply(mModelView,mProjection);

    GLES20.gluniformMatrix4fv(mModelViewLoc,1,false,mModelView.getAsFloatBuffer());
    GLES20.gluniformMatrix4fv(mMVPLoc,mMVPMatrix.getAsFloatBuffer());
    GLES20.gluniformMatrix4fv(mnormalMatrixLoc,mnormalMatrix.getAsFloatBuffer());
    GLES20.gluniform3f(mLightPositionLoc,5.0f,5.0f);

    GLES20.gluniform3f(mBrickColorLoc,0.549f,0.239f,0.271f);
    GLES20.gluniform3f(mMortarColorLoc,0.8f,0.8f);
    GLES20.gluniform2f(mBrickSizeLoc,0.3f,0.15f);
    GLES20.gluniform2f(mBrickPercentLoc,0.9f,0.85f);

    GLES20.glVertexAttribPointer(mPositionLoc,3,GLES20.GL_FLOAT,mTorus.getVertices());
    GLES20.glEnabLevertexAttribArray(mPositionLoc);

    GLES20.glVertexAttribPointer(mnormalLoc,mTorus.getnormals());
    GLES20.glEnabLevertexAttribArray(mnormalLoc);

    GLES20.glDrawElements(GLES20.GL_TRIANGLES,mTorus.getNumIndices(),GLES20.GL_UNSIGNED_SHORT,mTorus.getIndices());
}
    

解决方法

        问题是我为NormalMatrix传递了错误的值。 NormalMatrix是ModelViewMatrix逆矩阵的转置的左上3x3。为此,我有以下代码:
mNormalMatrix.load(mModelView);
mNormalMatrix.invert();
mNormalMatrix.transpose();
这产生了一个4x4矩阵,但是在我的着色器中,我将其作为mat3,这改变了值并使其失败。