gpt4 book ai didi

c++ - Opengl 3.3 在我的程序中加载纹理的问题

转载 作者:行者123 更新时间:2023-11-30 01:34:11 26 4
gpt4 key购买 nike

我目前遇到了使用 OpenGL 3.3 Core 同时渲染多个纹理的问题。我通过 SOIL(图像加载库)加载了 2 个单独的图像,但只有一个图像出现在最终程序中。这是我当前的代码:

#include <iostream>
#include <GL\glew.h>
#include <GL\GL.h>
#include <GLFW\glfw3.h>
#include <SOIL.h>
#include "Shader.h"
#include "texture2D.h"

using namespace std;

void processInput(GLFWwindow *window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
}

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(800, 600, "OpenGL", NULL, NULL);
if (window == NULL) {
cout << "GLFW WINDOW CREATION FAILED! " << endl;
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);

cout << "Made By Rial Seebran " << endl;
cout << glfwGetVersionString() << endl;

glewInit();
if (glewInit() != GLEW_OK) {
cout << "GLEW INITIALIZATION FAILED! " << endl;
glfwTerminate();
return -1;
}

float positions[] = {
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.0f, 1.0f, 1.0f
};

unsigned int indices[] = {
0, 1, 2,
3, 2, 1
};

unsigned int VAO;
unsigned int VBO;
unsigned int EBO;

glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);

glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);

glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, ((GLvoid*)(0)));
glEnableVertexAttribArray(0);

glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, ((GLvoid*)(sizeof(float) * 3)));
glEnableVertexAttribArray(1);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

Shader shader("shader.vs", "shader.fs");

Texture2D texture1;
texture1.LoadTexture("container.jpg");

Texture2D texture2;
texture2.LoadTexture("awesomeface.png");

shader.Use();

while (!glfwWindowShouldClose(window)) {

glClearColor(0.1f, 0.15f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

texture1.BindTexture(0);
texture2.BindTexture(1);

shader.Uniform1I("myTexture1", 0);
shader.Uniform1I("myTexture2", 1);

glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);

processInput(window);
glfwPollEvents();
glfwSwapBuffers(window);
}

glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);


glfwTerminate();
return 0;
}

这是纹理类(全部在 .h 文件中):

#ifndef TEXTURE2D
#define TEXTURE2D

#include <GL\glew.h>
#include <GL\GL.h>
#include <SOIL.h>
#include <iostream>

using namespace std;

class Texture2D {
public:
Texture2D();
~Texture2D();
void LoadTexture(const char* texPath);
void BindTexture(unsigned int texUnit);
unsigned int texture_M;
};

Texture2D::Texture2D() {

}

Texture2D::~Texture2D() {
glDeleteTextures(1, &texture_M);
}

void Texture2D::LoadTexture(const char* texPath) {
int width;
int height;
unsigned char *image = SOIL_load_image(texPath, &width, &height, 0, SOIL_LOAD_RGBA);
if (image == NULL) {
cout << "Failed to load Image! " << endl;
}

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glBindTexture(GL_TEXTURE_2D, texture_M);
if (image != NULL) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
}

SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0);
}

void Texture2D::BindTexture(unsigned int texUnit) {
glActiveTexture(GL_TEXTURE0 + texUnit);
glBindTexture(GL_TEXTURE_2D, texture_M);
}

#endif

在片段着色器中,我使用 mix() 函数对 2 个纹理进行线性插值,但“awesomeface.png”图像是最终程序中唯一出现的纹理。为了让程序显示两种纹理,应该修复什么?

final program output

最佳答案

您必须在 Texture::LoadTexture() 的开头添加以下行以生成纹理 ID:

glGenTextures(1, &texture_M);

这将保留一个用于识别纹理对象的有效 ID(稍后必须通过调用 glBindTexture() 进行初始化)。它可用于在后续 OpenGL 调用中引用纹理(包括使用 glDeleteTextures() 删除纹理)。

关于c++ - Opengl 3.3 在我的程序中加载纹理的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56820822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com