How to proper position skybox camera using openGL and everything appears inside the skybox

  Kiến thức lập trình

I created a skybox for my project and it looks the way I wanted it to; however, there are a few issues I cannot figure out how to fix and I have read some tutorials on this subject, but I was not able to find something that would help me. The first problem is that I don’t know how to get the box to always move with my camera. I am able to rotate the camera to left or right only i can’t zoom out, zoom in, go up ,or down.

The second image that i encounter is thee planets aren’t appear inside the skybox.here’s the sky box and how it appears without the planets

`void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Camera
    view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
    glUniformMatrix4fv(viewUniform, 1, GL_FALSE, glm::value_ptr(view));
    glUniform3fv(cameraUniform, 1, glm::value_ptr(cameraPos));


    // Render the planets
    glUseProgram(program);
    // Render the skybox
    glUseProgram(skyboxShader);
    view = glm::mat4(glm::mat3(glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp)));
    glUniformMatrix4fv(glGetUniformLocation(skyboxShader, "view"), 1, GL_FALSE, glm::value_ptr(view));
    glUniformMatrix4fv(glGetUniformLocation(skyboxShader, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
    glBindVertexArray(skyboxVAO);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_CUBE_MAP, skyboxTexture);
    glDrawArrays(GL_TRIANGLES, 0, 36);



    // Draw Sun
    glBindBuffer(GL_ARRAY_BUFFER, vbo1);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo1);
    glBindTexture(GL_TEXTURE_2D, sunTexture);
    glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, sun.getInterleavedStride(), BUFFER_OFFSET(0));
    glVertexAttribPointer(vNormal, 3, GL_FLOAT, false, sun.getInterleavedStride(), (void*)(3 * sizeof(float)));
    glVertexAttribPointer(vTexture, 2, GL_FLOAT, false, sun.getInterleavedStride(), (void*)(6 * sizeof(float)));

    trans = glm::mat4(1.0f);
    trans = glm::scale(trans, glm::vec3(0.35f, 0.35f, 0.35f));
    trans = glm::rotate(trans, rotate, glm::vec3(0.0f, 1.0f, 0.0f));
    // Send transformation matrix to the shader
    glUniformMatrix4fv(modelUnifrom, 1, GL_FALSE, glm::value_ptr(trans));
    glDrawElements(GL_TRIANGLES, sun.getIndexCount(), GL_UNSIGNED_INT, (void*)0);

    //mercury
    glBindTexture(GL_TEXTURE_2D, mercuryTexture);
    trans = glm::mat4(0.7f);
    trans = glm::rotate(trans, rotate, glm::vec3(0.0f, 1.0f, 0.0f));
    trans = glm::scale(trans, glm::vec3(0.15f, 0.15f, 0.15f));
    trans = glm::translate(trans, glm::vec3(1.2f, 0.0f, 0.0f));
    trans = glm::rotate(trans, rotate, glm::vec3(0.0, 1.0, 0.0));
    // Send transformation matrix to the shader
    glUniformMatrix4fv(modelUnifrom, 1, GL_FALSE, glm::value_ptr(trans));
    glDrawElements(GL_TRIANGLES, sun.getIndexCount(), GL_UNSIGNED_INT, (void*)0);

    //draw venus
    glBindTexture(GL_TEXTURE_2D, venusTexture);
    trans = glm::mat4(0.7f);
    trans = glm::rotate(trans, rotate, glm::vec3(0.0, 1.0, 0.0));
    trans = glm::scale(trans, glm::vec3(0.18f, 0.18f, 0.18f));
    trans = glm::translate(trans, glm::vec3(1.5f, 0.f, 0.f));
    trans = glm::rotate(trans, rotate, glm::vec3(0.0f, 1.0f, 0.0f));
    // Send transformation matrix to the shader
    glUniformMatrix4fv(modelUnifrom, 1, GL_FALSE, glm::value_ptr(trans));
    glDrawElements(GL_TRIANGLES, sun.getIndexCount(), GL_UNSIGNED_INT, (void*)0);

    //draw earth
    glBindTexture(GL_TEXTURE_2D, earthTexture);
    trans = glm::mat4(1.0f);
    trans = glm::rotate(trans, rotate, glm::vec3(0.0, 1.0, 0.0));
    trans = glm::scale(trans, glm::vec3(0.19f, 0.19f, 0.19f));
    trans = glm::translate(trans, glm::vec3(1.6f, 0.f, 0.f));
    trans = glm::rotate(trans, rotate, glm::vec3(0.0, 1.0, 0.0));
    // Send transformation matrix to the shader
    glUniformMatrix4fv(modelUnifrom, 1, GL_FALSE, glm::value_ptr(trans));
    glDrawElements(GL_TRIANGLES, sun.getIndexCount(), GL_UNSIGNED_INT, (void*)0);

    //draw Mars
    glBindTexture(GL_TEXTURE_2D, marsTexture);
    trans = glm::mat4(1.0f);
    trans = glm::rotate(trans, rotate, glm::vec3(0.0f, 1.0f, 0.0f));
    trans = glm::scale(trans, glm::vec3(0.2f, 0.2f, 0.2f));
    trans = glm::translate(trans, glm::vec3(2.f, 0.f, 0.f));
    trans = glm::rotate(trans, rotate, glm::vec3(0.0f, 1.0f, 0.0f));
    // Send transformation matrix to the shader
    glUniformMatrix4fv(modelUnifrom, 1, GL_FALSE, glm::value_ptr(trans));
    glDrawElements(GL_TRIANGLES, sun.getIndexCount(), GL_UNSIGNED_INT, (void*)0);


    //Draw Jupiter
    glBindTexture(GL_TEXTURE_2D, jupiterTexture);
    trans = glm::mat4(1.0f);
    trans = glm::rotate(trans, rotate, glm::vec3(0.0f, 1.0f, 0.0f));
    trans = glm::scale(trans, glm::vec3(0.40f, 0.40f, 0.40f));
    trans = glm::translate(trans, glm::vec3(1.35f, 0.f, 0.f));
    trans = glm::rotate(trans, rotate, glm::vec3(0.0, 1.0, 0.0));
    // Send transformation matrix to the shader
    glUniformMatrix4fv(modelUnifrom, 1, GL_FALSE, glm::value_ptr(trans));
    glDrawElements(GL_TRIANGLES, sun.getIndexCount(), GL_UNSIGNED_INT, (void*)0);

    //Draw Saturn
    glBindTexture(GL_TEXTURE_2D, saturnTexture);
    trans = glm::mat4(1.0f);
    trans = glm::rotate(trans, rotate, glm::vec3(0.0f, 1.0f, 0.0f));
    trans = glm::scale(trans, glm::vec3(0.40f, 0.40f, 0.40f));
    trans = glm::translate(trans, glm::vec3(1.8f, 0.f, 0.f));
    trans = glm::rotate(trans, rotate, glm::vec3(0.0, 1.0, 0.0));
    // Send transformation matrix to the shader
    glUniformMatrix4fv(modelUnifrom, 1, GL_FALSE, glm::value_ptr(trans));
    glDrawElements(GL_TRIANGLES, sun.getIndexCount(), GL_UNSIGNED_INT, (void*)0);
    //Ring
    trans = glm::mat4(1.0f);
    trans = glm::rotate(trans, rotate, glm::vec3(0.0f, 1.0f, 0.0f));
    trans = glm::scale(trans, glm::vec3(0.24f, 0.24f, 0.24f));
    trans = glm::translate(trans, glm::vec3(4.5f, 0.f, 0.f));
    trans = glm::rotate(trans, rotate, glm::vec3(0.0, 1.0, 0.0));

    //Draw Uranus
    glBindTexture(GL_TEXTURE_2D, uruansTexture);
    trans = glm::mat4(1.0f);
    trans = glm::rotate(trans, rotate, glm::vec3(0.0f, 1.0f, 0.0f));
    trans = glm::scale(trans, glm::vec3(0.25f, 0.25f, 0.25f));
    trans = glm::translate(trans, glm::vec3(3.5f, 0.f, 0.f));
    trans = glm::rotate(trans, rotate, glm::vec3(0.0, 1.0, 0.0));
    // Send transformation matrix to the shader
    glUniformMatrix4fv(modelUnifrom, 1, GL_FALSE, glm::value_ptr(trans));
    glDrawElements(GL_TRIANGLES, sun.getIndexCount(), GL_UNSIGNED_INT, (void*)0);

    //Draw Neptune
    glBindTexture(GL_TEXTURE_2D, neptunTexture);
    trans = glm::mat4(1.0f);
    trans = glm::rotate(trans, rotate, glm::vec3(0.0f, 1.0f, 0.0f));
    trans = glm::scale(trans, glm::vec3(0.20f, 0.20f, 0.20f));
    trans = glm::translate(trans, glm::vec3(5.f, 0.f, 0.f));
    trans = glm::rotate(trans, rotate, glm::vec3(0.0, 1.0, 0.0));
    // Send transformation matrix to the shader
    glUniformMatrix4fv(modelUnifrom, 1, GL_FALSE, glm::value_ptr(trans));
    glDrawElements(GL_TRIANGLES, sun.getIndexCount(), GL_UNSIGNED_INT, (void*)0);

}`

i’ve tried to render the skybox first then the planet’s but actually the plaent’s appeared without the lighting, texture, and the skybox, however the planets are appearing coorrectly with the light and the texture on it in another project.

New contributor

Ahmed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT