Проблемы с глубиной OpenGL 4.0 - задние текстуры рисуются поверх передних
Задние текстуры рисуются поверх передних:
#include <GL/glew.h>
#include <GL/glut.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include "shader.h"
#include "stb_image.h"
#define speed 0.002f * time
int main()
{
sf::Window window(sf::VideoMode(640, 640), L"Window", sf::Style::Default);
window.setVerticalSyncEnabled(true);
sf::Clock clock;
float time;
glewInit();
Shader shader("vertexShader.vert", "fragmentShader.frag");
int width, height, channels;
unsigned char *data = stbi_load("grass.png", &width, &height, &channels, 0);
glEnable(GL_DEPTH_TEST);
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
GLuint VAO;
glGenVertexArrays(1, &VAO);
GLuint VBO;
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof (vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid *)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid *)(3 * sizeof(GLfloat)));
glm::vec4 vec(1.0f, 0.0f, 0.0f, 1.0f);
GLuint texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 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_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
glm::mat4 trans(1.0f);
trans = glm::translate(trans, glm::vec3(0.25f, 0.0f, 0.0f));
trans = glm::rotate(trans, glm::radians(55.0f), glm::vec3(0.0f, 1.0f, 0.0f));
trans = glm::scale(trans, glm::vec3(10.0f, 10.0f, 10.0f));
glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
glm::mat4 view = glm::mat4(1.0f);
view = glm::translate(view, glm::vec3(0.0f, 1.0f, -3.0f));
glm::mat4 projection;
projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
sf::Vector3f camera(0.0f, 0.0f, 0.0f);
GLint cameraPosition = glGetUniformLocation(shader.ID, "cameraPos");
GLuint modelPosition = glGetUniformLocation(shader.ID, "model");
GLuint viewPosition = glGetUniformLocation(shader.ID, "view");
GLuint projectionPosition = glGetUniformLocation(shader.ID, "projection");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
if (event.type == sf::Event::Resized)
{
glViewport(0, 0, event.size.width, event.size.height);
projection = glm::perspective(glm::radians(45.0f), event.size.width/(event.size.height * 1.0f), 0.1f, 100.0f);
}
if (event.type == sf::Event::KeyPressed)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
window.close();
}
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
camera.x -= speed;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
camera.x += speed;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
camera.y -= speed;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
camera.y += speed;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::E))
{
model = glm::rotate(model, glm::radians(1.0f), glm::vec3(1.0f, 1.0f, 0.0f));
}
time = clock.getElapsedTime().asMicroseconds() / 800.0;
clock.restart();
// std::cout << time << std::endl;
glClearDepth(1.0f);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
shader.use();
glUniform3f(cameraPosition, camera.x, camera.y, camera.z);
glUniformMatrix4fv(modelPosition, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewPosition, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projectionPosition, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
window.display();
}
return 0;
}
Шейдеры вершинный:
#version 400 core
layout (location = 0) in vec3 pos;
layout (location = 1) in vec2 texCoords;
uniform vec3 cameraPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
out vec2 TexCoords;
void main()
{
gl_Position = projection * view * model * vec4(pos, 1.0) + vec4(cameraPos, 1.0);
TexCoords = texCoords;
}
И фрагментный:
#version 400 core
out vec4 resultColor;
in vec2 TexCoords;
uniform sampler2D Texture;
void main()
{
resultColor = texture(Texture, TexCoords);
}
Код писал по уроку: https://ravesli.com/urok-8-sistemy-koordinat-v-opengl/
Ответы (1 шт):
Автор решения: Никита
→ Ссылка
Проблема решена. Помогло
sf::ContextSettings settings;
settings.depthBits = 32;
sf::Window window(sf::VideoMode(640, 640), "Window", sf::Style::Default, settings);
