Material and TriMesh

Hello,



I try to use Material States for TriMeshes. But the Problem is the one you can see on the picture: Only the first triangle has the green Ambient light.





The important Code is:


            TriMesh m = new TriMesh("My Mesh");
            Vector3f[] vertexes={
                     new Vector3f(-1,-1,0),
                     new Vector3f(1,-1,0),
                     new Vector3f(-1,1,0),
                     new Vector3f(1,1,0)
                    };
                    // Normal directions for each vertex position
                    Vector3f[] normals={
                     new Vector3f(0,0,1),
                     new Vector3f(0,0,1),
                     new Vector3f(0,0,1),
                     new Vector3f(0,0,1)
                    };
                    // Color for each vertex position
                    ColorRGBA[] colors={
                     new ColorRGBA(1,1,1,1),
                     new ColorRGBA(1,1,1,1),
                     new ColorRGBA(1,1,1,1),
                     new ColorRGBA(1,1,1,1)
                    };
                    // Texture Coordinates for each position
                    Vector2f[] texCoords={
                     new Vector2f(0,0),
                     new Vector2f(1,0),
                     new Vector2f(0,1),
                     new Vector2f(1,1)
                    };
                    // The indexes of Vertex/Normal/Color/TexCoord sets. Every 3
                    // makes a triangle.
                    int[] indexes={
                     0,1,2,1,2,3
                    };
                    // Feed the information to the TriMesh
                    m.reconstruct(BufferUtils.createFloatBuffer(vertexes),BufferUtils.createFloatBuffer(normals),
                     BufferUtils.createFloatBuffer(colors),TexCoords.makeNew(texCoords),
                     BufferUtils.createIntBuffer(indexes));
            m.setLocalTranslation(0, 0, 2);
            rootNode.attachChild(m);

            File gfile = new File("Textures/grassb.png");
            URL url = null;
         try {
            url = new URL("File:"+gfile.getAbsolutePath());
         } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
         }

            Texture grass = TextureManager.loadTexture(url,Texture.MinificationFilter.Trilinear,
                   Texture.MagnificationFilter.Bilinear);
            grass.setWrap(WrapMode.Repeat);

            TextureState ts=this.getRenderer().createTextureState();
            ts.setTexture(grass);
            ts.setEnabled(true);
            m.setRenderState(ts);
            m.updateRenderState();
           
            MaterialState ms = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
            //ms.setDiffuse(ColorRGBA.red);
            ms.setAmbient(ColorRGBA.green.clone());
            //ms.setEmissive(ColorRGBA.white);
            //ms.setColorMaterial(ColorMaterial.AmbientAndDiffuse);
            ms.setEnabled(true);
            m.setRenderState(ms);
            m.updateRenderState();
           
            DirectionalLight l = new DirectionalLight();
            l.setDirection(new Vector3f(1,1,-1));
            l.setDiffuse(ColorRGBA.black.clone());
            l.setAmbient(ColorRGBA.white.clone());
            l.setAttenuate(true);
            l.setEnabled(true);
           
            LightState ls = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
            ls.setTwoSidedLighting(true);
            ls.attach(l);
            m.setRenderState(ls);
            m.updateRenderState();



I would be very happy if someone could help me. I searched in the forum but wasn't sucessfull.

Regards
David

Havent tried the code, but from the looks of it you have different windings on the triangles in your polygon.

It appears the vertices are :-



2 ---- 3

!      !

!      !

0 ---- 1



Triangle 1 = 0, 1, 2  -  Counter clockwise

Triangle 2 = 1, 2, 3  -  Clockwise



Try changing the



int[] indexes={0,1,2,1,2,3};



to



int[] indexes={0,1,2,1,3,2};

What a simple stupid mistake. You're asolutely right. Now it works.



The Problem I had to solve was that I load a complete Model. And then I tried to set the Material parameters for the models but this had no effect. The cause was that I cut away the front with Cullstate and so the Materials took no effect. Now I flipped the orientation of all the surfaces and now I works like I want.



Thank you for looking at the code.



Regards

David