LWJGL在顶点之间改变线的颜色

时间:2015-07-01 15:25:38

标签: java opengl 3d rendering lwjgl

我正在使用java和LWJGL / openGL来创建一些图形。对于渲染,我使用以下内容:

RawModel的构造函数:

$( document ).ajaxComplete(function() {
  $("#functionSelect").change(function () {
    $('#selectPThrehold').prop('disabled', true);
  });
});

渲染器:

public RawModel(int vaoID, int vertexCount, String name){
    this.vaoID = vaoID;
    this.vertexCount = vertexCount;
    this.name = name;
}

我正在使用GL11.GL_TRIANGLES因为我可以让模特的线条显示而不是面孔。但是当我为顶点设置颜色时,它只是将颜色集的周围线条着色,在某些情况下,它的所有线条都只是采用周围顶点的颜色。我怎么能做到这样根据每个顶点的距离和颜色组合这两种颜色?

片段着色器:

public void render(Entity entity, StaticShader shader){
        RawModel rawModel = entity.getRaw(active);
        GL30.glBindVertexArray(rawModel.getVaoID());
        GL20.glEnableVertexAttribArray(0);
        GL20.glEnableVertexAttribArray(1);
        GL20.glEnableVertexAttribArray(3);
        Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(),
            entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale());
        shader.loadTransformationMatrix(transformationMatrix);
        GL11.glDrawElements(GL11.GL_TRIANGLES, rawModel.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
        GL20.glDisableVertexAttribArray(0);
        GL20.glDisableVertexAttribArray(1);
        GL20.glDisableVertexAttribArray(3);
        GL30.glBindVertexArray(0);
}

顶点着色器:

#version 400 core

in vec3 colour;
in vec2 pass_textureCoords;

out vec4 out_Color;

uniform sampler2D textureSampler;

void main(void){

    vec4 textureColour = texture(textureSampler, pass_textureCoords);

    //out_Color = texture(textureSampler, pass_textureCoords);
    out_Color = vec4(colour, 1.0);

}

a picture of how it looks like

1 个答案:

答案 0 :(得分:1)

  

我使用GL11.GL_TRIANGLES因为我可以制作模型'线条显示而不是面孔。

嗯,GL_TRIANGLES用于渲染三角形,其中面孔。如果你只想要模特'线条,您可以使用其中一种线条绘制模式(GL_LINESGL_LINE_LOOPGL_LINE_STRIP等。

但是,更好的方法是启用

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

,只显示三角形的轮廓。

您可以使用

重新关闭它
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  

我怎么能做到这样根据每个顶点的距离和颜色组合这两种颜色?

我不确定你对此的意思;默认情况下,从顶点着色器传递到片段着色器的值已经在网格中插入;所以片段接收的颜色已经取决于所有顶点'颜色和距离。

修改

在顶点着色器中:

if(selected == 1){
    colour = vec3(200, 200, 200);
}

我假设您要分配RGB值(200,200,200),这是一个非常浅的白色。但是,OpenGL使用0.0到1.0范围内的浮点颜色分量。高于或低于此范围的值将被剪裁。现在,colour的值在片段之间进行插值,片段将接收远远高于1.0的组件的值。这些将被剪裁为1.0,这意味着你的所有片段都显示为白色。

因此,为了解决这个问题,你必须使用像

这样的东西
colour = vec3(0.8, 0.8, 0.8);