如何明确Opengl ES材质属性以避免错误的材质被重复使用?

发布于 2024-11-02 04:13:07 字数 1655 浏览 0 评论 0原文

我正在开发 Android opengl lib,遇到了一个小问题。我知道这很容易解决,但我在追踪它时遇到了很多麻烦。简而言之,我正在开发一个 3D 场景图形库,它基于非常轻量且很棒的 Scenario 2D 场景图形库。深入解释该结构需要很长时间,但本质上树中的每个节点都有一个 load(GL10 gl) 方法,在该方法中发生任何纹理和/或材质初始化。然后有形状节点的draw()方法,以及材质的killDraw()方法。

我的材质类旨在对像我这样来自 3d 建模应用程序程序角度(对我来说是 3ds max)的人来说是用户友好的。问题是:

我在场景中创建了一些 3D 形状:

FXShape shape4 = new FXShape();
shape4.setShape(new Model("cup.obj"));

Material material = new Material();
material.setAmientAndDiffuse(1,0,0,1);          
shape5.addMaterial(material);

add(shape4);

FXShape shape5 = new FXShape();
shape5.setShape(new Cube());
shape5.addMaterial(new TextureMaterial(shape5.getShape(),R.drawable.crate));
add(shape5);

一切都很好,除了为第一个形状创建的红色材质不仅应用于该形状,而且应用于第二个形状,即使它有自己的纹理。

以下是相关的材质方法

@Override
public void loadMaterial(GL10 gl) { 
}

@Override
public void draw(GL10 gl){
    gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT,his.ambientBuffer);
    gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, this.diffuseBuffer);
    gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, this.specularBuffer);
    gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, Math.min(shininess, 128));    
}

@Override
public void killDraw(GL10 gl) {
    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}

如果我在场景中放置 10 个形状,并向形状 1 和 5 添加彩色材质,则 1-5 是第一个材质,6-10 是第二个材质。

我认为我应该能够做的只是调用像 gl.glLoadIdentity() 这样的东西并将其清除,但我找不到材质参数。有人可以帮忙吗?

谢谢

抱歉,我的篇幅很长,我试图尽可能简洁。如果有人对库或更多信息感兴趣,我在 git hub https://github.com/ghostandthemachine/ DroidGraph

I am working on an Android opengl lib and have hit a small issue. I know it is something easy to fix but I am having a lot of trouble tracking it down. In a nut shell, I am working on a 3d scene graph library which is based on the really light weight and awesome Scenario 2D scene graph library. It would take too long to explain the structure in depth, but essentially every node in the tree has a load(GL10 gl) method in which any texture and/or material initialization happens. There are then draw() methods for shape nodes, and also killDraw() for materials.

My material classes are intended to be user friendly for people like me who come from a 3d modeling app program standpoint (3ds max for me). Here is the issue:

I create a few 3d shapes in a scene:

FXShape shape4 = new FXShape();
shape4.setShape(new Model("cup.obj"));

Material material = new Material();
material.setAmientAndDiffuse(1,0,0,1);          
shape5.addMaterial(material);

add(shape4);

FXShape shape5 = new FXShape();
shape5.setShape(new Cube());
shape5.addMaterial(new TextureMaterial(shape5.getShape(),R.drawable.crate));
add(shape5);

Everything works great except that the red material which is create for the first shape is applying not only to that one, but the second shape even though it has it's own texture.

here are the relevant Material methods

@Override
public void loadMaterial(GL10 gl) { 
}

@Override
public void draw(GL10 gl){
    gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT,his.ambientBuffer);
    gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, this.diffuseBuffer);
    gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, this.specularBuffer);
    gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, Math.min(shininess, 128));    
}

@Override
public void killDraw(GL10 gl) {
    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}

If I put 10 shapes in the scene and add a colored material to shape 1 and 5, then 1-5 are the first material, and 6-10 are the second.

What I thought I should be able to do is just call something like gl.glLoadIdentity() and clear it out, but I can't find that for the material parameters. Can any one help?

thanks

sorry for the length, I tried to be as concise as possible. If anyone is interested in the lib or more info, I'm on git hub https://github.com/ghostandthemachine/DroidGraph

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只涨不跌 2024-11-09 04:13:07

您创建的每个对象都确切地知道它使用的材料属性。只需将其压入/弹出堆栈上的相关状态即可。

Each object you create knows exactly what material properties it's using. Just have it push/pop the relevant state on the stack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文