java - LWJGL 改变顶点之间线条的颜色

标签 java opengl 3d rendering lwjgl

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

RawModel 的构造函数:

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

渲染器:

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);
}

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

片段着色器:

#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);

}

顶点着色器:

#version 400 core

in vec3 position;
in vec2 textureCoords;
in int selected;

out vec3 colour;
out vec2 pass_textureCoords;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

void main(void){

    gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position, 1.0);
    pass_textureCoords = textureCoords;
    if(selected == 1){
        colour = vec3(200, 200, 200);
    }
    else{
        colour = vec3(0, 0, 0);
    }
    gl_BackColor = vec4(colour, 1.0);
}

a picture of how it looks like

最佳答案

I'm using GL11.GL_TRIANGLES cuz that's how I can make models' lines show up instead of faces.

嗯,GL_TRIANGLES用于渲染三角形,它们面。如果您只需要模型的线条,则可以使用线条绘制模式之一( GL_LINESGL_LINE_LOOPGL_LINE_STRIP 等)。

但是,更好的方法是启用

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

,仅显示三角形的轮廓。

您可以使用以下命令再次将其关闭

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

How could I make it so that it kind of combines those 2 colors depending on the distance of each vertex and the colors?

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

<小时/>

编辑:

在顶点着色器中:

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);

关于java - LWJGL 改变顶点之间线条的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31165540/

相关文章:

java - 从远程机器访问 Grails ActiveMQ

c++ - OpenGL 中对象的当前坐标

3d - 在 3ds max 中以 16 位分辨率获取 z-Buffer

java - 将表面拟合到 java 中的 3D 数据点集合

java - 在 JTree 中的 parent 之间共享 child

java - java中使用CSV解析器实现键值解析器

java - 如何在Java中使用sql.Array获取列的值?

c++ - “glCreateShader”未在此范围内声明?

opengl - 使用 opengl 全屏

javascript - 分离 ThreeJS 模型和动画数据