0

I made solar system with sun in the centre and planets rotating around him. I wanted to add skybox, it is added properly but I cant see my planets.

Before adding skybox: enter image description here

After adding skybox: enter image description here

main skybox code:

    // skybox VAO
unsigned int skyboxVAO, skyboxVBO;
glGenVertexArrays(1, &skyboxVAO);
glGenBuffers(1, &skyboxVBO);
glBindVertexArray(skyboxVAO);
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);

vector<std::string> faces
{
    "res/textures/skybox/right.png",
    "res/textures/skybox/left.png",
    "res/textures/skybox/top.png",
    "res/textures/skybox/bot.png",
    "res/textures/skybox/front.png",
    "res/textures/skybox/back.png"
};
unsigned int cubemapTexture = loadCubemap(faces);

window skybox code:

        // draw skybox as last
    glDepthFunc(GL_LEQUAL);  // change depth function so depth test passes when values are equal to depth buffer's content
    skyboxShader.use();
    view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix
    skyboxShader.setMat4("view", view);
    skyboxShader.setMat4("projection", projection);

            // skybox cube
    glBindVertexArray(skyboxVAO);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
    glDrawArrays(GL_TRIANGLES, 0, 36);
    glBindVertexArray(0);
    glDepthFunc(GL_LESS); // set depth function back to default

vertex shader and fragment shader:

#version 330 core
layout (location = 0) in vec3 aPos;

out vec3 TexCoords;

uniform mat4 projection;
uniform mat4 view;

void main()
{
    TexCoords = aPos;
    vec4 pos = projection * view * vec4(aPos, 1.0);
    gl_Position = projection * view * vec4(aPos, 1.0);
}  

#version 330 core
out vec4 FragColor;

in vec3 TexCoords;

uniform samplerCube skybox;

void main()
{    
    FragColor = texture(skybox, TexCoords);
}
dronikk
  • 39
  • 5
  • Do you draw the skybox before or after the objects? If the skybox is drawn in before the objects, disable the depth test while you draw the skybox. – Rabbid76 Feb 24 '21 at 17:45
  • @Rabbid76 i tried to draw skybox before objects and deleted glDepthFunc lines but now there is only skybox drawing, not even a spaceship – dronikk Feb 24 '21 at 17:52
  • Usually, you render the skybox first, but disable depth writes. Then you draw all other stuff as usual – BDL Feb 24 '21 at 18:31
  • @BDL unfortunately still doesnt work – dronikk Feb 24 '21 at 18:46
  • Yes you should draw skybox first then your normal objects, make sure face culling is opposite for skybox vs normal objects. It seems that your spaceship is still in view, but planets and star are gone, is your skybox too small? – Mudkip Hacker Feb 24 '21 at 20:12
  • @MudkipHacker that is possible, how can I change its size? – dronikk Feb 25 '21 at 10:03
  • the easiest (but not fastest) is to 1. render skybox 2. `glClear(GL_DEPTH_BUFFER_BIT);` 3. render objects/planets whatever ... No need to disable depth test nor writes ... Also take a look at this [Is it possible to make realistic n-body solar system simulation in matter of size and mass?](https://stackoverflow.com/a/28020934/2521214) – Spektre Feb 25 '21 at 13:59
  • @dronikk Your size is defined by your skyboxVertices – Mudkip Hacker Feb 25 '21 at 20:14

0 Answers0