如何在我的opengl场景中添加光以显示阴影

时间:2018-10-15 02:51:26

标签: android opengl-es

我为opengl创建了一个渲染器,并在场景中显示了一些对象,我试图添加一盏灯以使对象中具有阴影,但是这里不起作用,场景与以前相同。

x,y,z在我的gl.glTranslatef(x,y,z)中的0,0.1,0处初始化;

请注意,我的对象是.obj对象。

这是我的代码,我做错什么了吗?

public CstOpenGLRender(Context context) {
    mObj = new ObjDrawer(context.getResources(), R.raw.hoop_1);
    mObj2 = new ObjDrawer(context.getResources(), R.raw.star_1);
    mObj3 = new ObjDrawer(context.getResources(), R.raw.hoop_2);
    mObj4 = new ObjDrawer(context.getResources(), R.raw.object_2);

    camera = new Camera();
    grid = new Grid();
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glEnable(GL10.GL_LIGHTING);
    gl.glEnable(GL10.GL_LIGHT0);

    float[] lightAmbient = new float[] { 0.2f, 0.2f, 0.2f, 1.0f };
    float[] lightPosition = new float[] { 10f, 1.0f, 0.0f, 1.0f };

    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient,     0);
    gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPosition, 0);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glDepthFunc(GL10.GL_LEQUAL);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnable(gl.GL_COLOR_MATERIAL);

    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    gl.glClearDepthf(1.0f);

    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glShadeModel(GL10.GL_SMOOTH);

    gl.glClearStencil(0);
}

@Override
public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT
            | GL10.GL_DEPTH_BUFFER_BIT);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();


    float ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
    float diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
    float specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
    float position[] ={ 10f, 1.0f, 0.0f, 1.0f };

    gl.glLightfv(gl.GL_LIGHT0, gl.GL_AMBIENT, ambientLight,0);
    gl.glLightfv(gl.GL_LIGHT0, gl.GL_DIFFUSE, diffuseLight,0);
    gl.glLightfv(gl.GL_LIGHT0, gl.GL_SPECULAR, specularLight,0);
    gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION, position,0);

    gl.glShadeModel(gl.GL_SMOOTH);
    gl.glEnable(gl.GL_COLOR_MATERIAL);
    float shin[] = {0.0f, 0.0f, 1.0f, 1.0f};
    float specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
    float ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
    float diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
    gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT,ambient , 0);
    gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE,diffuse , 0);
    gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR,specular , 0);

    gl.glRotatef(roll,1.0f, 0.0f, 0.0f);
    gl.glRotatef(yaw,0.0f, 1.0f, 0.0f);
    gl.glRotatef(pitch,0.0f, 0.0f, 1.0f);
    gl.glTranslatef(x,y,z);
    GLU.gluLookAt(gl, -10, 0f, 0f, 0, 0, 0, 0, 1, 0);

    grid.drawGrid(gl);
    gl.glPushMatrix();

    gl.glTranslatef(0f,2f,0f);
    gl.glRotatef(90f,0f,1f,0f);
    gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f);
    mObj.draw(gl);

    gl.glPushMatrix();
    gl.glTranslatef(0f,0f,10f);
    gl.glColor4f(1f, 0.5f, 0.5f, 1.0f);
    mObj2.draw(gl);

    gl.glPushMatrix();
    gl.glTranslatef(0f,0f,20f);
    mObj3.draw(gl);

    gl.glPushMatrix();
    gl.glTranslatef(0f,0f,30f);
    gl.glRotatef(90f,1f,0f,0f);

    mObj4.draw(gl);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
    gl.glViewport(0, 0, width, height);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
}

0 个答案:

没有答案