#OpenGL lights not working c++

1 messages · Page 1 of 1 (latest)

untold acorn
#

Hi so I am trying to make a solar system. Atm every object is just black so I assume my lighting is not working. Help ples

earnest current
#

@untold acorn you know how to ask a question properly dont be lazy

untold acorn
#

In this case I actually dont :(

#

I dont really know what to look for

earnest current
#

code

untold acorn
#
struct celestial_body {

    inline void set_parent(celestial_body& _parent) {
        parent = &_parent;
    }

    inline void calculate_world_matrix() {
        m_world = glm::mat4(1.0f);

        m_world = glm::scale(m_world, m_scale);
        m_world = glm::rotate(m_world, rotation, glm::vec3(0, 1, 0));
        m_world = glm::translate(m_world, m_position);
        //m_world = glm::rotate(m_world, orbital_speed, glm::vec3(1, 0, 0));

        if (parent != NULL) {
            m_world = glm::translate(m_world, parent->m_position);
            m_world = glm::rotate(m_world, orbital_speed, glm::vec3(1, 0, 0));
            m_world = glm::translate(m_world, m_position);
        }


        m_world = glm::translate(m_world, m_position);

    }

    inline void update(timespan_t delta) {
        rotation += spin_speed * delta.elapsed_seconds();
        if (rotation >= 360) {
            rotation = 0;
        }
        calculate_world_matrix();
    }
    inline void draw(Shader& shader) {
        shader.setMat4("model", m_world);
        shader.use();
        shader.setInt("material.diffuse", diffuse);


        glDrawArrays(GL_TRIANGLES, 0, 36);
    }

    glm::vec3 m_position;
    glm::vec3 m_scale = { 1,1,1 };
    glm::mat4 m_world;
    glm::quat m_rotation;


    float rotation = 0;
    float spin_speed = 0;
    float orbital_speed = 0;
    float dist_from_parent = 0;

    GLuint diffuse;

    celestial_body* parent; 

};
#

This is one planet

#

Its mainly the draw function we should look at

#

This is how I set it up

    sun.m_position = { 0, 0, 0 };
    sun.spin_speed = 1;
    sun.diffuse = loadTexture("assets/maps/0_diffuse.png");

    solar_system.push_back(&sun);

    shader.use();
    shader.setInt("material.diffuse", 0);

#

sun is a celestial_body

earnest current
#

just so you know i have no idea what im looking at

#

but i will try my bestest

untold acorn
#
void Scene::draw() {


    shader.use();

    shader.setMat4("projection", camera.m_projection);
    shader.setMat4("view", camera.get_viewmat());
    shader.setVec3("viewPos", camera.m_position);

    shader.setVec3("light.direction", light_direction);
    shader.setVec3("light.ambient", ambient);
    shader.setVec3("light.diffuse", diffuse);
    shader.setVec3("light.specular", specular);

    for (auto& it : solar_system) {

        glBindVertexArray(VAO);
        it->draw(shader);

    }
}
#

This is where I think the error is

earnest current
#

was it ever working

untold acorn
#

I use the same setup in another project and it works yeah

#

This is how it looks

#

And since the texture id is fine I can only assume its the light

earnest current
#

what does the shader look like

#

it is a frag shader?

#

color every pixel magenta for debug

untold acorn
#
#version 330 core
out vec4 FragColor;

struct Material {
    sampler2D diffuse;
    sampler2D specular;
    float shininess;
};

struct Light {
    vec3 direction;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;

uniform vec3 viewPos;
uniform Material material;
uniform Light light;

void main()
{

    vec3 ambient = light.ambient * texture(material.diffuse, TexCoords).rgb;


    vec3 norm = normalize(Normal);
    vec3 lightDir = normalize(-light.direction);
    float diff = max(dot(norm, lightDir), 0.0);
    vec3 diffuse = light.diffuse * diff * texture(material.diffuse, TexCoords).rgb;


    vec3 viewDir = normalize(viewPos - FragPos);
    vec3 reflectDir = reflect(-lightDir, norm);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
    vec3 specular = light.specular * spec * texture(material.specular, TexCoords).rgb;

    vec3 result = ambient + diffuse + specular;
    FragColor = vec4(result, 1.0);
}
#

This is the fragment shader

#
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;

out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoords;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    FragPos = vec3(model * vec4(aPos, 1.0));
    Normal = mat3(transpose(inverse(model))) * aNormal;
    TexCoords = aTexCoords;

    gl_Position = projection * view * vec4(FragPos, 1.0);
}
#

This is the vertex shader

earnest current
#

okay so the last line of your frag shader change it to something like FragColor = vec4(1.0, 0.0, 1.0, 1.0)

#

this will test whether your fragment shader is actually coloring the pixels

untold acorn
#

Ooh

#

They turn magenta

#

So its not the light

earnest current
#

yes

#

so perhaps it is your shader itself

#

let us look more closely at it

#

let us do a simple dot product shading

untold acorn
#

Hmm

#

I think I got it

#

I'll test it in a second once its booted up

#

I got a weird issue where it takes 40 second to load

#

Idk why

earnest current
#

what

#

compiling?

untold acorn
#

Building and executing the .exe

earnest current
#

compiling

untold acorn
#

So