Still me, Having issues with lighting in opengl objects turn black

I tried my best to make the Shader as self explanatory as possible without the use of comments. My colors are definitely showing up because when I add 0.8 to the final result color you can see them just barely. I have yet to do grade ten math but I will once I get there so please understand I do not know how most of this math works with trigonometry and linear algebra. I have not done any code for lighting in main other than a uniform vec3 called Light_Position and enabling lighting, is there code required in main to grab surface normals and other stuff?? if so please let me know what I need to do

in main for lighting:


glm::vec3 Light_Position(0.2f, 0.1f, 0.2f);
int lightpos = glGetUniformLocation(shader, “light_position”);
glUniformMatrix4fv(lightpos, 1, GL_FALSE, glm::value_ptr(Light_Position));

#shader vertex
#version 330 core

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;

uniform mat4 model_matrix;

uniform mat4 perspective;

uniform mat4 just_translations;

uniform vec3 light_position;

out vec3 fragment_position;
out vec3 Normals;

void main()
    gl_Position = model_matrix * vec4(position.x, position.y, position.z, 1.0);
    fragment_position = vec3(just_translations * vec4(position.x, position.y, position.z, 1.0));
    Normals = mat3(transpose(inverse(just_translations))) * normal;


#shader fragment
#version 330 core

layout(location = 0) out vec4 color;

uniform vec4 Color_u;
uniform vec3 light_position;

in vec3 fragment_position;
in vec3 Normals;

void main()

    float ambient_strength = 0.1f;
    vec4 ambient_light = ambient_stength * Color_u;

    vec3 norm = normalize(Normals);
	    vec3 light_direction = normalize(light_position - fragment_position);
    float diffuse = max(dot(norm, light_direction), 0.0);
    color = ambient_light * diffuse;

this statement makes no sense:

you’ve found the wrong hobby

main() can refer to three different functions

this is legacy code that cannot be used in a core profile

question does not make sense

GL_COLOR_MATERIAL is likely a legacy construct you cannot use

you write a vec3 to a matrix4 slot

this will likely clutter up your math understanding and use: just_translations is part of model_matrix. It’s not an error.

variable light_position is present in both shaders. Assign an independant variable in the vertex-shader, endowed with the out qualifier … ang grip it with a corresponding in variable in the fragment-shader.

As I understand this construct it will write the color to the vertexpositions you’ve allocated at location 0 once before. If you’ve given it location=3 and allocated a corresponding buffer, it could make some sense.

I can assume that you’ve written a color-value to this uniform?

since you have two versions of light_position light_direction may be incorrect

I cannot really comment on what you want to know: is the light-model (frag-main) mathematically sound?

this Basic-Lighting-link gives a good description with code

As for the dark output: you do tone down color a lot with ambient_strength = 0.1f. It will also reduce transparency to 10%

… edits …
Since you seem to do have a visual output, you could test your shader-variables by using a translation-matrix, move the triangle and draw it again with different uniform values. That would give a much less abstract impression of what goes on. Remember to employ the translation in both matrixes (model & just-translations)

@CarstenT I really appreciate the help, you are the only person who replied to my topics and I thank you for that. But why the unnecessary comments? I knew learning computer graphics would be hard especially as a middle school student. I do not have a teacher so people like yourself are my only source when I get stuck on stuff I don’t understand. I’ll delete the topic. I tried learning some of this math but I do not have the foundation for it. I don’t understand why people are so rude and stubborn. Ya some of my questions don’t make sense I can see that I don’t KNOW like any of the math that goes behind surface normals though I do know what they are. Maybe I take things too personally but it’s not a good feeling when you are stuck and people are downvoting questions or making comments that are off topic just to put you even lower. I am trying my best.

Sorry for this comment. You’ve maneged to get something on the screen and that has bacically solved your problem. It’s not a minor feat.
Most of what I comment about is not math and trig, so don’t hide behind that. It’s errors or likely errors.
I stick to the critique of the two model-matrixes but regret that I didn’t pop a solution: you can extract the translation out of the full model-matrix (it’s the last column of the 4-column block of numbers) … and thus don’t have to set it as a separate uniform.
Math, coding and opengl has one thing in common: exact-ness … it spills over on how you must express yourself on the topic. I whip you on that since 90% of the newby questions are simply difficult to understand due to that. You’ve expressed yourself well in your critique of my post, so you can do it.
A teacher is nice to have, but nothing that most can afford. I can understand the math of your fragment-shader as little as you can. That’s not an obstacle, and I gave you suggestion and means to do something about it.

1 Like