gpt4 book ai didi

Android openGL ES 法线

转载 作者:行者123 更新时间:2023-11-29 02:07:35 28 4
gpt4 key购买 nike

所以昨天我写了 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);
}
}

enter image description here

因此,默认法线为 0、0、-1 或 0、0、1,并且默认相机正注视着 -5,随着模型旋转,光线就像没有法线时一样消失,我在计算机上有类似的东西,但我没有使用 VBO 等。所以我无法比较代码,我很确定上传普通缓冲区或绑定(bind)它有问题! :( 帮帮我!:(

最佳答案

进一步解释我对你的问题的评论:

你需要确保你的法线是“规范化的”;这意味着它们的量级(长度)必须为 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

您需要在创建缓冲区之前将此应用到法线以获得正确的光照。

关于Android openGL ES 法线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9180935/

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