gpt4 book ai didi

android - 为什么这个 Android 镜面照明示例没有按预期工作?

转载 作者:搜寻专家 更新时间:2023-11-01 08:16:18 24 4
gpt4 key购买 nike

为什么以下代码生成的二进制文件中没有出现镜面高光?

package com.example.helloandroid;

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.GLSurfaceView.Renderer;

public class TestRenderer implements Renderer {
private FloatBuffer vb;
private FloatBuffer mab, mdb, msb;

private FloatBuffer lPos;
private FloatBuffer lab, ldb, lsb;

public TestRenderer() {
vb = ByteBuffer.allocateDirect(24 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
vb.put(new float[]{
110.0f, 240.0f,
110.0f, 190.0f,
160.0f, 240.0f,
160.0f, 190.0f,
210.0f, 240.0f,
210.0f, 190.0f,
110.0f, 290.0f,
110.0f, 240.0f,
160.0f, 290.0f,
160.0f, 240.0f,
210.0f, 290.0f,
210.0f, 240.0f});
vb.position(0);
mab = ByteBuffer.allocateDirect(4 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
mab.put(new float[]{1.0f, 0.0f, 0.0f, 1.0f});
mab.position(0);
mdb = ByteBuffer.allocateDirect(4 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
mdb.put(new float[]{0.0f, 1.0f, 0.0f, 1.0f});
mdb.position(0);
msb = ByteBuffer.allocateDirect(4 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
msb.put(new float[]{0.0f, 0.0f, 1.0f, 1.0f});
msb.position(0);

lPos = ByteBuffer.allocateDirect(4 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
lPos.put(new float[]{160.0f, 240.0f, 10.0f, 1.0f});
lPos.position(0);
lab = ByteBuffer.allocateDirect(4 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
// THIS WORKS..
lab.put(new float[]{0.0f, 0.0f, 0.0f, 1.0f});
lab.position(0);
ldb = ByteBuffer.allocateDirect(4 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
// SO DOES THIS..
ldb.put(new float[]{0.0f, 0.0f, 0.0f, 1.0f});
ldb.position(0);
// BUT NOT THIS
lsb = ByteBuffer.allocateDirect(4 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
lsb.put(new float[]{1.0f, 1.0f, 1.0f, 1.0f});
lsb.position(0);
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

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

gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_AMBIENT, mab);
gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_DIFFUSE, mdb);
gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_SPECULAR, msb);

gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lPos);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lab);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, ldb);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, lsb);
gl.glEnable(GL10.GL_LIGHT0);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);

gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0.0f, width, 0.0f, height, -1.0f, 1.0f);

gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -1.0f);
}

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

gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vb);
gl.glNormal3f(0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 6);
gl.glDrawArrays(GL10.GL_LINE_STRIP, 6, 6);
}
}

这是我从代码中看到的原样(只有镜面照明组件):http://i.imgur.com/4kvIx.png

只有一个环境照明组件:http://i.imgur.com/80Tjx.png

只有一个漫射照明组件:http://i.imgur.com/r5PkO.png

可能有趣的是,我对 glOrtho 的调用中的 zNear/zFar 参数似乎没有剪切任何内容。我是否正确设置了场景以查看镜面反射高光(我确实尝试将光定位在顶点附近)?

最佳答案

OpenGL ES 只接受 GL_FRONT_AND_BACK。在这里查看 glMaterial

http://www.khronos.org/opengles/sdk/1.1/docs/man/glMaterial.xml

关于android - 为什么这个 Android 镜面照明示例没有按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4432194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com