opengl – Skybox not texturing

opengl – Skybox not texturing

[ad_1]

I’ve seemed for an answer for a short time now nonetheless I am unsure if different individuals have encountered my identical downside. I’m attempting to attract a skybox in a clean opengl window, I’ve digicam controls and I can zoom out and see the skybox is there however not being textured accurately/ in any respect. I’ve used code from opengl and I’ve solely tried to implement it into a category.

Skybox.h

#pragma as soon as

#embrace "wrapper_glfw.h"
#embrace <vector>
#embrace <glm/glm.hpp>
#embrace <vector>
#embrace <string>
#embrace "shader_m.h"
#embrace "digicam.h"



class Skybox
{
public:
    Skybox();
    ~Skybox();
  
  void init(GLWrapper *glw);
  void makeSkybox();
  void drawSkybox(Digicam digicam, glm::mat4 projection);
  void setFaces(std::string skyboxPath);
  unsigned int loadCubemap();
 
    personal:
    std::vector<std::string> faces
    {
        "../src/pictures/skybox/default/proper.jpg",
        "../src/pictures/skybox/default/left.jpg",
        "../src/pictures/skybox/default/top.jpg",
        "../src/pictures/skybox/default/backside.jpg",
        "../src/pictures/skybox/default/entrance.jpg",
        "../src/pictures/skybox/default/again.jpg"
    };
    
    unsigned int cubemapTexture;
    unsigned int skyboxVAO, skyboxVBO;
    GLuint skyboxShader;

    //skybox, mannequin, view, projection
    GLuint skyboxID, modelID, viewID, projectionID;
;

};

Skybox.cpp

#embrace "skybox.h"
#embrace <iostream>
#embrace <filesystem>

#outline STB_IMAGE_IMPLEMENTATION
#embrace "stb_image.h"


Skybox::Skybox() 
{
}
Skybox::~Skybox()
{
  glDeleteVertexArrays(1, &skyboxVAO);
  glDeleteBuffers(1, &skyboxVBO);
}

void Skybox::init(GLWrapper *glw)
{
  skyboxShader = glw->LoadShader("shaders/skyboxShader/skybox.vert", "shaders/skyboxShader/skybox.frag");
  skyboxID = glGetUniformLocation(skyboxShader, "skybox");
  projectionID = glGetUniformLocation(skyboxShader, "projection");
  viewID = glGetUniformLocation(skyboxShader, "view");
  glUseProgram(skyboxShader);
}

void Skybox::makeSkybox()
{

float skyboxVertices[] = {
    // positions          
    -5.0f,  5.0f, -5.0f,
    -5.0f, -5.0f, -5.0f,
     5.0f, -5.0f, -5.0f,
     5.0f, -5.0f, -5.0f,
     5.0f,  5.0f, -5.0f,
    -5.0f,  5.0f, -5.0f,

    -5.0f, -5.0f,  5.0f,
    -5.0f, -5.0f, -5.0f,
    -5.0f,  5.0f, -5.0f,
    -5.0f,  5.0f, -5.0f,
    -5.0f,  5.0f,  5.0f,
    -5.0f, -5.0f,  5.0f,

     5.0f, -5.0f, -5.0f,
     5.0f, -5.0f,  5.0f,
     5.0f,  5.0f,  5.0f,
     5.0f,  5.0f,  5.0f,
     5.0f,  5.0f, -5.0f,
     5.0f, -5.0f, -5.0f,

    -5.0f, -5.0f,  5.0f,
    -5.0f,  5.0f,  5.0f,
     5.0f,  5.0f,  5.0f,
     5.0f,  5.0f,  5.0f,
     5.0f, -5.0f,  5.0f,
    -5.0f, -5.0f,  5.0f,

    -5.0f,  5.0f, -5.0f,
     5.0f,  5.0f, -5.0f,
     5.0f,  5.0f,  5.0f,
     5.0f,  5.0f,  5.0f,
    -5.0f,  5.0f,  5.0f,
    -5.0f,  5.0f, -5.0f,

    -5.0f, -5.0f, -5.0f,
    -5.0f, -5.0f,  5.0f,
     5.0f, -5.0f, -5.0f,
     5.0f, -5.0f, -5.0f,
    -5.0f, -5.0f,  5.0f,
     5.0f, -5.0f,  5.0f
}; 
  
  // skybox VAO
  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); 
    
  cubemapTexture = loadCubemap();

  // glUniform1ui(skyboxID, 0);
  glGetError();
}

void Skybox::drawSkybox(Digicam digicam, glm::mat4 projection)
 GL_DEPTH_BUFFER_BIT);

  // draw skybox as final
  glDepthFunc(GL_LEQUAL);  // change depth operate so depth check passes when values are equal to depth buffer's content material
 
  glUniformMatrix4fv(viewID, 1, GL_FALSE, &digicam.getView()[0][0]);
    glUniformMatrix4fv(projectionID, 1, GL_FALSE, &projection[0][0]); 
 
  // skybox dice
  glBindVertexArray(skyboxVAO);
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
  glDrawArrays(GL_TRIANGLES, 0, 36);
  glBindVertexArray(0);
  glDepthFunc(GL_LESS); // set depth operate again to default
  glUniform1ui(skyboxID, cubemapTexture);
  glGetError();




unsigned int Skybox::loadCubemap()
{
    unsigned int textureID;
    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);

    int width, top, nrChannels;
    for (unsigned int i = 0; i < faces.dimension(); i++)
    {
        unsigned char *information = stbi_load(faces[i].c_str(), &width, &top, &nrChannels, 0);
        if (information)
        {
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, top, 0, GL_RGB, GL_UNSIGNED_BYTE, information);
            stbi_image_free(information);
        }
        else
        {
            std::cout << "Cubemap texture did not load at path: " << faces[i] << std::endl;
            stbi_image_free(information);
        }
    }
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

    return textureID;
}

I’m utilizing an M2 Macbook and I should not have a shader class like I’ve seen lots of people use, I implement my shaders the best way my lecturer has completed so. I hope I’ve offered sufficient and thanks upfront if anybody will help!

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply