因此,昨天我写了WaveFront .obj 3D模型加载器,该加载器现在可以正常工作(不支持所有功能),因此我编写了简单的测试以在屏幕上绘制任何3D模型,这些模型在我向场景添加照明之前都可以正常工作。灯光出现了,但法线似乎仍处于默认状态。我不确定如何在生成缓冲区时使用什么作为目标(因为有GL_NORMAL_ARRAY,GL_ARRAY_BUFFER等),因为我找不到使用缓冲区对象的关于它的任何教程:
package com.Ruuhkis.opengl;
import static javax.microedition.khronos.opengles.GL10.GL_COLOR_BUFFER_BIT;
import static javax.microedition.khronos.opengles.GL10.GL_VERTEX_ARRAY;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.util.Log;
import com.Ruuhkis.opengl.model.Indices;
import com.Ruuhkis.opengl.model.Loader;
import com.Ruuhkis.opengl.model.Model;
import com.Ruuhkis.opengl.model.Polygon;
import com.Ruuhkis.opengl.model.Vertex;
public class TextureRenderer implements Renderer {
private FloatBuffer vertexBuffer, normalBuffer;
private ShortBuffer indexBuffer;
private int attribVBO, attribIBO, attribNBO;
private Context context;
private float rotation = 0;
private float[] vertices =
{-0.8f, -0.8f, 0f,
0.8f, -0.8f, 0f,
0.8f, 0.8f, 0f,
-0.8f, 0.8f, 0f};
private float[] normals =
{0f};
private short[] indices =
{0, 3, 2,
0, 2, 1};
public TextureRenderer(Context context) {
this.context = context;
Model model = Loader.loadModel(context.getAssets(), "test.txt");
vertices = new float[model.getVerticeList().size() * 3];
int i = 0;
for(Vertex v: model.getVerticeList()) {
vertices[i++] = v.getX();
vertices[i++] = v.getY();
vertices[i++] = v.getZ();
//Log.v("vertice", v.toString() + " sa");
}
i = 0;
indices = new short[model.getPolygonList().size() * 3];
normals = new float[model.getPolygonList().size() * 3];
for(Polygon p: model.getPolygonList()) {
for(Indices in: p.getIndiceList()) {
normals[i] = vertices[in.getNormalIndex()];
indices[i++] = (short) in.getVertexIndex();
}
}
ByteBuffer buffer = ByteBuffer.allocateDirect(vertices.length * 4);
buffer.order(ByteOrder.nativeOrder());
vertexBuffer = buffer.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.flip();
buffer = ByteBuffer.allocateDirect(normals.length * 4);
buffer.order(ByteOrder.nativeOrder());
normalBuffer = buffer.asFloatBuffer();
normalBuffer.put(normals);
normalBuffer.flip();
buffer = ByteBuffer.allocateDirect(indices.length * 2);
buffer.order(ByteOrder.nativeOrder());
indexBuffer = buffer.asShortBuffer();
indexBuffer.put(indices);
indexBuffer.flip();
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glColor4f(1f, 0f, 0f, 1f);
gl.glClear(GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glTranslatef(0f, 0f, -10f);
rotation += 1f;
gl.glRotatef(rotation, 1f, 1f, 0f);
gl.glEnableClientState(GL_VERTEX_ARRAY);
gl.glEnableClientState(GL11.GL_NORMAL_ARRAY);
GL11 gl11 = (GL11) gl;
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, attribVBO);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, attribIBO);
gl11.glBindBuffer(GL11.GL_NORMAL_ARRAY, attribNBO);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
gl11.glNormalPointer(3, GL10.GL_FLOAT, 0);
gl11.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, 0);
gl.glDisableClientState(GL11.GL_NORMAL_ARRAY);
gl.glDisableClientState(GL_VERTEX_ARRAY);
gl.glPopMatrix();
gl.glFlush();
}
@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, 45f, (float)width / (float)height, 1f, 100f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glEnable(GL10.GL_DEPTH_TEST);
//gl.glEnable(GL10.GL_LIGHTING);
gl.glEnable(GL10.GL_LIGHTING);
gl.glEnable(GL10.GL_LIGHT1);
gl.glEnable(GL10.GL_COLOR_MATERIAL);
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, FloatBuffer.wrap(new float[]{0f, 0f, 0f, 1f}));
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, FloatBuffer.wrap(new float[]{1f, 1f, 1f, 1f}));
gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_SPECULAR, FloatBuffer.wrap(new float[]{1f, 1f, 1f, 1f}));
gl.glMaterialfv(GL10.GL_FRONT, GL10.GL_SPECULAR, FloatBuffer.wrap(new float[]{1f, 1f, 1f, 1f}));
gl.glMaterialf(GL11.GL_FRONT, GL11.GL_SHININESS,128f);
gl.glShadeModel(GL10.GL_SMOOTH);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.8f, 0.8f, 0.8f, 1f);
GL11 gl11 = (GL11) gl;
int[] buffer = new int[1];
gl11.glGenBuffers(1, buffer, 0);
attribVBO = buffer[0];
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, attribVBO);
gl11.glBufferData(GL11.GL_ARRAY_BUFFER, vertices.length * 4, vertexBuffer, GL11.GL_STATIC_DRAW);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, -1);
gl11.glGenBuffers(1, buffer, 0);
attribIBO = buffer[0];
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, attribIBO);
gl11.glBufferData(GL11.GL_ELEMENT_ARRAY_BUFFER, indices.length * 2, indexBuffer, GL11.GL_STATIC_DRAW);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, -1);
gl11.glGenBuffers(1, buffer, 0);
attribNBO = buffer[0];
gl11.glBindBuffer(GL11.GL_NORMAL_ARRAY, attribNBO);
gl11.glBufferData(GL11.GL_NORMAL_ARRAY, normals.length * 4, normalBuffer, GL11.GL_STATIC_DRAW);
gl11.glBindBuffer(GL11.GL_NORMAL_ARRAY, -1);
}
}
因此,默认的法线是0、0,-1或0、0、1,默认的相机注视着-5,并且当模型旋转时,灯光消失了,就像没有法线时一样,在计算机上有类似的工作,但是我没有使用VBO等。因此我无法比较代码,我敢肯定上传普通缓冲区或将其绑定有问题! :( 帮我! :(
最佳答案
为了进一步解释我对您的问题的评论:
您需要确保您的法线被“规范化”;这意味着它们的大小(长度)必须为1。某些.obj出口商会为您这样做;但自己做是一件好事。
引用自:http://www.fundza.com/vectors/normalize/index.html
给定向量“ a”(或法向“ a”):a [3 1 2](因此:ax = 3,ay = 1,az = 2)
向量的大小(长度)是:| a | = sqrt((ax * ax)+(ay * ay)+(az * az))
length = sqrt((ax * ax) + (ay * ay) + (az * az))
length = sqrt(9 + 1 + 4) = 3.742
为了归一化向量“ a”,我们除以长度:
x = ax/|a|;
y = ay/|a|;
z = az/|a|;
x = 3.0 / 3.742 = 0.802
y = 1.0 / 3.742 = 0.267
z = 2.0 / 3.742 = 0.534
在创建缓冲区之前,需要将此方法应用于法线,以获取正确的照明。