简体   繁体   English

OpenGL-ES 中的材质影响其他纹理

[英]Materials in OpenGL-ES affecting other textures

I have encountered a problem working with OpenGL-ES (1.0) for android that i'm not able to wrap my head around.我在使用 OpenGL-ES (1.0) for android 时遇到了一个问题,我无法解决这个问题。 I have multiple 3D objects showing through OpenGL-ES that iv'e decided to give some material using:我有多个 3D 对象通过 OpenGL-ES 显示,我决定使用以下方法提供一些材料:

gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_... , ... , 0); gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_... , ... , 0);

For each object i'm drawing I do it something like this:对于我正在绘制的每个对象,我都会这样做:

gl.glPushMatrix();
*make some adjustment to object*
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.glPopMatrix();

Where "ambient", "diffuse" and "specular" is unique to each object.其中“环境”、“漫反射”和“镜面反射”对于每个对象都是独一无二的。

The result being that if I use a higher amount of red ambient for example in one material, this will affect the other visible object to also be showing of a little bit in red.结果是,如果我在一种材料中使用更多的红色环境,这将影响另一个可见对象也显示为红色。

As seen below, two out of three objects to the left is set to get more red ambient in it's material.如下所示,左侧的三个对象中的两个设置为在其材质中获得更多的红色环境。 The material to the right shouldn't be glowing since it's material hasn't but still it does.右边的材料不应该发光,因为它的材料没有但仍然发光。 (picture obviously a bit modified to make it clearer). (图片显然稍作修改以使其更清晰)。

Every object i use has an own material class consisting of information regarding it's material.我使用的每个对象都有自己的材质类,其中包含有关其材质的信息。

Any idea of something iv'e missed or is this how materials acctually work in OpenGL?知道我遗漏了什么,或者这就是材质在 OpenGL 中的实际工作方式吗?

(I use a direcitonal light lighting up the whole scene in a direction) (我使用一个方向灯照亮整个场景)

Edit: Just to make this clearer, it's not just ambient colors that affect other objects, if for example an object with a material that recievs 0.2 of all colors in ambient, specular and diffuse comes close to another object with higher values of for example specular, the first object will be much more lightned up as well.编辑:为了更清楚地说明这一点,影响其他对象的不仅仅是环境颜色,例如,如果一个对象的材质在环境、镜面反射和漫反射中的所有颜色都为 0.2,那么它接近另一个具有更高值的物体,例如镜面反射,第一个物体也会更加明亮。

Edit2:编辑2:

This is the function for drawing all the objects这是绘制所有对象的功能

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);


    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);

    Enumeration<String> key = objects.keys();
    while (key.hasMoreElements())
    {
        String k = key.nextElement();

        if(objects.get(k).visible)
        {
            gl.glPushMatrix();
            try
            {
                gl.glBindTexture(GL10.GL_TEXTURE_2D, texturemanager._tm.get(k.trim())
                    .getTexture()[filter]);
            }
            catch(Exception e){};

            adjust(gl, objects.get(k));
            objects.get(k).draw(gl, 1);
            gl.glPopMatrix();
        }
    }

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);

What happens on draw in object class is following在对象类中绘制时发生的事情如下

    protected void draw(GL10 gl, int filter)
{
    updatePhysics();
    bounds.center.set(this);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, data.vertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, data.textureBuffer);
    gl.glNormalPointer(GL10.GL_FLOAT, 0, data.normalBuffer);

    gl.glDrawElements(GL10.GL_TRIANGLES, data.numIndices,
            GL10.GL_UNSIGNED_SHORT, data.indicesBuffer);
}

adjust(gl, objects.get(k));调整(GL,objects.get(k)); leads to following导致跟随

// rotating, translating and scaling object

    if (obj.blend)
    {
        gl.glEnable(GL10.GL_BLEND);
        gl.glDisable(GL10.GL_DEPTH_TEST);
    } else
    {
        gl.glDisable(GL10.GL_BLEND);
        gl.glEnable(GL10.GL_DEPTH_TEST);
    }
    if(obj.enableMaterial)
    {
        obj.getMaterial().enable(gl);
    }

and where obj.getMaterial().enable(gl);哪里obj.getMaterial().enable(gl); will be the material for the object.将是对象的材料。 Following is my material class以下是我的材料类

    public void enable(GL10 gl)
{
    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.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, shininess);
}

Where the variables for ambient, diffuse, specular and shininess is set like following环境、漫反射、镜面反射和光泽度的变量设置如下

    public void setAmbient(float r, float g, float b, float a)
{
    ambient[0] = r;
    ambient[1] = g;
    ambient[2] = b;
    ambient[3] = a;
}

So what happens if you have an object for which "enableMaterial" is false?那么如果您有一个“enableMaterial”为假的对象会发生什么? Do you ever reset the material?你有没有重置材料? It would seem that it will still be applied for any future objects.似乎它仍将应用于任何未来的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM