I'm using java and LWJGL/openGL to create some graphics. For rendering I use the following:
Constructor of RawModel:
public RawModel(int vaoID, int vertexCount, String name){
this.vaoID = vaoID;
this.vertexCount = vertexCount;
this.name = name;
}
Renderer:
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);
}
I'm using GL11.GL_TRIANGLES cuz that's how I can make models' lines show up instead of faces. But when I set color for a vertex, it just colors surrounding lines to the color set, in some cases all of it's lines just take the color of surrounding vertices. How could I make it so that it kind of combines those 2 colors depending on the distance of each vertex and the colors?
Fragment shader:
#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);
}
Vertex shader:
#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);
}
I'm using
GL11.GL_TRIANGLES
cuz that's how I can make models' lines show up instead of faces.
Well, GL_TRIANGLES
is for rendering triangles, which are faces. If you only want the models' lines, you can use one of the line drawing modes (GL_LINES
, GL_LINE_LOOP
, GL_LINE_STRIP
etc).
However, a better way is to enable
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
, which makes only the outline of the triangles show up.
You can switch it off again with
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?
I'm not sure what you mean with this; by default values passed from the vertex shader to the fragment shader are already interpolated across the mesh; so the color a fragment receives already depends on all the vertices' color and distance.
Edit:
In the vertex shader:
if(selected == 1){
colour = vec3(200, 200, 200);
}
I assume you want to assign an RGB value of (200, 200, 200), which is a very light white. However, OpenGL uses floating-point color components in the range 0.0 to 1.0. Values that go above or below this range are clipped. The value of colour
is now interpolated across the fragments, which will receive values with components far higher than 1.0. These will be clipped to 1.0, meaning all your fragments appear white.
So, in order to solve this issue, you have to instead use something like
colour = vec3(0.8, 0.8, 0.8);