Could you tell me how to fix it?, if the texture is transparent, then why is not what is behind it displayed?
Here is the texture of the lock applied to the square, and you can see that it is transparent and does not completely cover the square, but the second texture, which is further along the z axis, is cut off from this square:
here is my code:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <IL/il.h>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
const char* vertexShaderSource = R"glsl(
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
out vec2 TexCoord;
uniform mat4 projection;
void main()
{
gl_Position = projection * vec4(aPos, 1.0);
TexCoord = aTexCoord;
}
)glsl";
const char* fragmentShaderSource = R"glsl(
#version 330 core
out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D texture3;
uniform sampler2D texture4;
void main()
{
FragColor = texture(texture1, TexCoord);
FragColor = texture(texture2, TexCoord);
FragColor = texture(texture3, TexCoord);
FragColor = texture(texture4, TexCoord);
}
)glsl";
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(1280, 720, "OpenGL Texture Example", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
ilInit();
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
float vertices[] = {
0.0f, 250.0f, 0.0f, 0.0f, 0.0f,
1280.5f, 250.0f, 0.0f, 1.0f, 0.0f,
1280.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
75.0f, 576.0f, 1.0f, 0.0f, 0.0f,
215.0f, 576.0f, 1.0f, 1.0f, 0.0f,
215.0f, 190.0f, 1.0f, 1.0f, 1.0f,
75.0f, 190.0f, 1.0f, 0.0f, 1.0f,
0.0f, 250.0f, 0.0f, 0.0f, 0.0f,
1000.5f, 250.0f, 0.1f, 1.0f, 0.0f,
1000.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 620.0f, 0.01f, 0.0f, 0.0f,
1000.5f, 620.0f, 0.01f, 1.0f, 0.0f,
1000.0f, 0.0f, 0.01f, 1.0f, 1.0f,
0.0f, 0.0f, 0.01f, 0.0f, 1.0f,
};
unsigned int indices[] = {
0, 1, 3,
1, 2, 3,
4, 5, 7,
5, 6, 7,
8, 9, 11,
9, 10, 11,
12, 13, 15,
13, 14, 15
};
GLuint VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
GLuint texture;
glGenTextures(1, &texture);
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);
ILuint imageID;
ilGenImages(1, &imageID);
ilBindImage(imageID);
if (ilLoadImage(L"land.png"))
{
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH),
ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_RGBA, GL_UNSIGNED_BYTE,
ilGetData());
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
ilDeleteImages(1, &imageID);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
GLuint texture2;
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
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);
ilGenImages(1, &imageID);
ilBindImage(imageID);
if (ilLoadImage(L"castle.png"))
{
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH),
ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_RGBA, GL_UNSIGNED_BYTE,
ilGetData());
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
ilDeleteImages(1, &imageID);
GLuint texture3;
glGenTextures(1, &texture3);
glBindTexture(GL_TEXTURE_2D, texture3);
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);
ilGenImages(1, &imageID);
ilBindImage(imageID);
if (ilLoadImage(L"grass and rocks.png"))
{
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH),
ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_RGBA, GL_UNSIGNED_BYTE,
ilGetData());
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
ilDeleteImages(1, &imageID);
GLuint texture4;
glGenTextures(1, &texture4);
glBindTexture(GL_TEXTURE_2D, texture4);
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);
ilGenImages(1, &imageID);
ilBindImage(imageID);
if (ilLoadImage(L"cloud.png"))
{
ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ilGetInteger(IL_IMAGE_WIDTH),
ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_RGBA, GL_UNSIGNED_BYTE,
ilGetData());
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
ilDeleteImages(1, &imageID);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glm::mat4 projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f, -1.0f, 1.0f); // (left, right, bottom, top, near, far)
GLint projectionLoc = glGetUniformLocation(shaderProgram, "projection");
while (!glfwWindowShouldClose(window))
{
glClearColor(0.08f, 0.6f, 0.8f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shaderProgram);
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
glBindVertexArray(VAO);
glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, texture);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindTexture(GL_TEXTURE_2D, texture2);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(6 * sizeof(GLuint)));
glBindTexture(GL_TEXTURE_2D, texture3);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(12 * sizeof(GLuint)));
glBindTexture(GL_TEXTURE_2D, texture4);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(18 * sizeof(GLuint)));
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glDeleteProgram(shaderProgram);
glfwTerminate();
return 0;
}
the gpt chatbot wrote me a solution, but something didn’t help..
for some reason, the cloud has come to the fore, although it is further along the z axis
//The main rendering cycle during
while (!glfwWindowShouldClose(window)) {
// Очистка буферов цвета и глубины
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Rendering opaque objects
// ...
// Disabling writing to the depth
glDepthMask(GL_FALSE);
// Отключение теста глубины
glDisable(GL_DEPTH_TEST);
// Rendering transparent objects
// ...
// Enabling
glEnable(GL_DEPTH_TEST);
// Enabling writing to the depth buffer glDepthMask(GL_TRUE);
glDepthMask(GL_TRUE);
// Buffer exchange and event handling
glfwSwapBuffers(window);
glfwPollEvents();
}
Илья Дударь is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.