opengl. Объект рисуется поверх другого объекта

Проблема: в первую очередь рисуется изображение, которое отрисовано раньше всего, игнорируя тест глубины. Рисуется поверх всего остальноговведите сюда описание изображения введите сюда описание изображения

вот код

void Process::render() {

            glClearColor(0.8f, 1.0f, 1.0f, 1.f);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            this->renderObjects();
            this->shader[0]->set1i(0, "f_texture");
            this->updateUniforms();

            glfwSwapBuffers(this->window);
            glFlush();
            glBindVertexArray(0);
            glUseProgram(0);
            glActiveTexture(0);
            glBindTexture(GL_TEXTURE_2D, 0);
}

void Process::renderObjects() {

    for(auto &i : this->object) i->render(this->shader[0]);
}

Object::Object(Primitive* primitive, glm::vec3 position, glm::vec3 origin, glm::vec3 rotation, glm::vec3 scale) {

        this->position = position;
        this->origin = origin;
        this->rotation = rotation;
        this->scale = scale;
        
        this->nrOfVertices = primitive->getNrOfVertices();
        this->nrOfIndices = primitive->getNrOfIndices();
        this->vertexArray = new Vertex[this->nrOfVertices];
        for(size_t i = 0; i  < this->nrOfVertices; i++) {
            this->vertexArray[i] = primitive->getVertices()[i];
        }
        this->indexArray = new GLuint[this->nrOfIndices];
        for(size_t i = 0; i  < this->nrOfIndices; i++) {
            this->indexArray[i] = primitive->getIndices()[i];
        }
        this->init();
        this->updateModelMatrix();
}

void Object::init() {

    glGenVertexArrays(1, &this->VAO);
    glBindVertexArray(this->VAO);
    glGenBuffers(1, &this->VBO);
    glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
    glBufferData(GL_ARRAY_BUFFER, this->nrOfVertices * sizeof(Vertex), this->vertexArray, GL_DYNAMIC_DRAW);
    glGenBuffers(1, &this->EBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->nrOfIndices * sizeof(GLuint), this->indexArray, GL_DYNAMIC_DRAW);
        
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position));
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color));
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texpos));
    glEnableVertexAttribArray(2);   
    glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, normal));
    glEnableVertexAttribArray(3);
    glBindVertexArray(0);
}

void Object::render(Shader* shader, GLenum type) {

    this->updateModelMatrix();
    this->updateUniforms(shader);
    shader->use();
    for(size_t i = 0; i < this->texture.size(); i++) this->texture[i]->bind(i);
    glBindVertexArray(VAO);
    glDrawElements(type, this->nrOfIndices, GL_UNSIGNED_INT, 0);
    
    glBindVertexArray(0);
    glUseProgram(0);
    glActiveTexture(0);
    glBindTexture(GL_TEXTURE_2D, 0);
}

void Window::initOpenGLOptions() {

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glFrontFace(GL_CCW);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}

Texture::Texture(const char* path) {

    glGenTextures(1, &this->texture);
    glBindTexture(GL_TEXTURE_2D, this->texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    
    unsigned char* image = SOIL_load_image(path, &this->width, &this->height, 0, SOIL_LOAD_RGB);
    if(image) {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, this->width, this->height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
        glGenerateMipmap(GL_TEXTURE_2D);
    } else {
        std::cout << "cannot load image: " << path << std::endl;
    }
    glActiveTexture(0);
    glBindTexture(GL_TEXTURE_2D, 0); 
    SOIL_free_image_data(image);
}

Ответы (1 шт):

Автор решения: Илья

Проблема была в значении переменной nearPlane, которая передавалась в матрицу проекции:

this->ProjectionMatrix = 
  glm::perspective(glm::radians(this->fov),
    static_cast<float>(this->fbufferW) / this->fbufferH,
    this->nearPlane, this->farPlane);

было установлено слишком маленькое значение (0.000001f). Проблема была решена, когда я поднял его до 0.0001f.

→ Ссылка