0

当我尝试使用定向光着色器渲染我的 vbo 时,我注意到 vbo 的颜色部分与阴影混淆了。
这是我的 vbo 的格式 (x,y,z,w ,r,g,b,a ,u,v ,nx,ny,nz)

顶点颜色 顶点、法线和 uv 坐标存储在数组中, 像这样

float[] array = new float {
    0f,0f,0f,0f,
    so on and so on...
}

这是顶点和片段着色器代码

#version 150 core
//VERTEX SHADER
in vec4 in_Position;
in vec4 in_Color;
in vec2 in_TextureCoord;
in vec3 in_Normal;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
uniform mat3 normal;

out Data {
    float intensity;
    vec2 st;
} Pass;

void main(void) {
    vec3 l_dir = normalize(mat3(model) * vec3(0,0,1));
    vec3 n = normalize(normal * in_Normal);
    Pass.intensity = max(dot(n, l_dir), 0.0);
    Pass.st = in_TextureCoord;
    gl_Position = projection * view * model * in_Position;
}


#version 150 core
//FRAGMENT SHADER
uniform sampler2D texture_diffuse;

in Data {
    float intensity;
    vec2 st;
} Pass;

out vec4 color;

void main(void) {
    vec4 diffuse = texture(texture_diffuse, Pass.st);
    vec4 test = vec4(1);
    color = Pass.intensity * diffuse;
}

构建 vbo
变量的代码:v = 顶点,c = 颜色数据,st = 纹理坐标,n = 法线向量。
classes:VertexData = 用于组织顶点数据的类(VertexData 类的代码将在该类之后)

    public void construct() {
        Manager.print(getClass(), "Normals" + Arrays.toString(n));
        int p = 0;
        int o = 0;
        int q = 0;
        vertices = new VertexData[v.length/4];
        for (int j=0;j<vertices.length;j++) {
            vertices[j] = new VertexData();
            vertices[j].setXYZW(v[p], v[p+1], v[p+2], v[p+3]);
            vertices[j].setRGBA(c[p], c[p+1], c[p+2], c[p+3]);
            vertices[j].setST(st[o], st[o+1]);
            vertices[j].setNormal(n[q], n[q+1], n[q+2]);
            p += 4;
            q += 3;
            o += 2;
        }
        FloatBuffer vBuffer = BufferUtils.createFloatBuffer(vertices.length * VertexData.elementCount);
        for (VertexData vertex : vertices) {
            vBuffer.put(vertex.getElements());
        }
        vBuffer.flip();

        IntBuffer iBuffer = BufferUtils.createIntBuffer(i.length);
        iBuffer.put(i);
        iBuffer.flip();

        vaoId = glGenVertexArrays();
        glBindVertexArray(vaoId);

        vboId = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboId);
        glBufferData(GL_ARRAY_BUFFER, vBuffer, GL_STATIC_DRAW);

        glVertexAttribPointer(0, VertexData.pec, GL_FLOAT,
                false, VertexData.stride, VertexData.pbo);

        glVertexAttribPointer(1, VertexData.cec, GL_FLOAT,
                false, VertexData.stride, VertexData.cbo);

        glVertexAttribPointer(2, VertexData.tec, GL_FLOAT,
                false, VertexData.stride, VertexData.tbo);

        glVertexAttribPointer(3, VertexData.nec, GL_FLOAT,
                false, VertexData.stride, VertexData.nbo);

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);

        vboiId = glGenBuffers();
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboiId);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, iBuffer, GL_STATIC_DRAW);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    }

VertexData 类(如前面的代码片段所示)

public class VertexData {

    public VertexData() {}

    public void setXYZW(float x, float y, float z, float w) {
        xyzw = new float[] {x,y,z,w};
    }

    public void setRGBA(float r, float g, float b, float a) {
        rgba = new float[] {r,g,b,a};
    }

    public void setST(float s, float t) {
        st = new float[] {s,t};
    }

    public void setNormal(float x, float y, float z) {
        norm = new float[] {x,y,z};
    }

    public float[] getElements() {
        float[] out = new float[VertexData.elementCount];
        int i = 0;

        //XYZW Elements
        out[i++] = xyzw[0];
        out[i++] = xyzw[1];
        out[i++] = xyzw[2];
        out[i++] = xyzw[3];
        //RGBA Elements
        out[i++] = rgba[0];
        out[i++] = rgba[1];
        out[i++] = rgba[2];
        out[i++] = rgba[3];
        //ST Elements
        out[i++] = st[0];
        out[i++] = st[1];
        //NORMAL Elements
        out[i++] = norm[0];
        out[i++] = norm[1];
        out[i++] = norm[2];

        return out;
    }

    public float[] getXYZW() {
        return new float[] {xyzw[0], xyzw[1], xyzw[2], xyzw[3]};
    }

    public float[] getRGBA() {
        return new float[] {rgba[0], rgba[1], rgba[2], rgba[3]};
    }

    public float[] getST() {
        return new float[] {st[0], st[1]};
    }

    public float[] getNORM() {
        return new float[] {norm[0], norm[1], norm[2]};
    }

    private float[] xyzw = new float[] {0f, 0f, 0f, 0f};
    private float[] rgba = new float [] {0f, 0f, 0f, 0f};
    private float[] st = new float[] {0f, 0f};
    private float[] norm = new float[] {0f,0f,0f};

    private int v,c,t,n;

    public static final int elementBytes = 4;

    public static final int pec = 4;
    public static final int cec = 4;
    public static final int tec = 2;
    public static final int nec = 3;

    public static final int pbc = pec * elementBytes;
    public static final int cbc = cec * elementBytes;
    public static final int tbc = tec * elementBytes;
    public static final int nbc = nec * elementBytes;

    public static final int pbo = 0;
    public static final int cbo = pbo + pbc;
    public static final int tbo = cbo + cbc;
    public static final int nbo = tbo + tbc;

    public static final int elementCount = pec + cec + tec + nec;

    public static final int stride = pbc + cbc + tbc + nbc;    
}

现在这里有一些图像和问题的原因

颜色数组 =

float[] {
    1,0,0,1,
    0,1,0,1,
    0,0,1,1,
    1,1,1,1
}

结果 =结果 1

现在我改变了顶点颜色,这发生了

颜色数组 =

float[] {
    0,0,1,1,
    0,1,0,1,
    1,0,0,1,
    1,1,1,1
}

结果 =结果 2

最后一个例子

颜色数组 =

float[] {
    1,1,1,1,
    1,1,1,1,
    1,1,1,1,
    1,1,1,1
}

结果 = 平面似乎有平坦的阴影,即使它应该是每个顶点。(我认为我不需要另一张图片来解释这一点。)

我认为问题出在着色器或 vbo 的某个地方,但我不能确定。

4

0 回答 0