gpt4 book ai didi

c++ - 混合两个纹理的 OpenGL 总是选择第一个纹理

转载 作者:搜寻专家 更新时间:2023-10-31 00:08:59 27 4
gpt4 key购买 nike

这是我混合两个纹理的代码,

主要.cpp

#include <iostream>

// GLEW
#include <GL/glew.h>

// GLFW
#include <GLFW/glfw3.h>


// Shader Class
#include "Shader.h"

// SOIL2 JSpartan
#include "SOIL2.h"

const int WIDTH=500, HEIGHT=600;

int main() {

// Init GLFW
glfwInit( );

// Set all the required options for GLFW
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );

glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );

// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow *window = glfwCreateWindow( WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr );

int screenWidth, screenHeight;
glfwGetFramebufferSize( window, &screenWidth, &screenHeight );

if ( nullptr == window )
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate( );

return EXIT_FAILURE;
}

glfwMakeContextCurrent( window );

// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
if ( GLEW_OK != glewInit( ) )
{
std::cout << "Failed to initialize GLEW" << std::endl;
return EXIT_FAILURE;
}

// Define the viewport dimensions
glViewport( 0, 0, screenWidth, screenHeight );
// enable alpha support
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );


// load shaders
Shader *ourShader = new Shader("res/shaders/core.vs","res/shaders/core.fs");

GLfloat vertices[] = {
1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right corner
1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right corner
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left corner
-1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left corner
};

GLuint indices[] = {
0, 1, 2, // trainagle 1 right sided
2, 3, 0
};

GLuint VAO, VBO, EBO;

glGenVertexArrays( 1, &VAO );
glBindVertexArray( VAO );

glGenBuffers( 1, &VBO );
glBindBuffer( GL_ARRAY_BUFFER, VBO );
glBufferData( GL_ARRAY_BUFFER, sizeof( vertices ), vertices, GL_STATIC_DRAW );

glGenBuffers( 1, &EBO );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, EBO );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( indices ), indices, GL_STATIC_DRAW );


glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid *) 0 );
glEnableVertexAttribArray(0);

glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof( GLfloat ), (GLvoid *) ( 3 * sizeof( GLfloat ) ) );
glEnableVertexAttribArray(1);

glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof( GLfloat ), (GLvoid *) ( 6 * sizeof( GLfloat ) ) );
glEnableVertexAttribArray(2);


// Load textures
GLuint textures[2];
glGenTextures(2, textures);

int width, height;
unsigned char* image;

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0]);
image = SOIL_load_image("res/face1.jpg", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
glUniform1i(glGetUniformLocation(ourShader->Program, "face1"), 0);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textures[1]);
image = SOIL_load_image("res/face2.jpg", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
glUniform1i(glGetUniformLocation(ourShader->Program, "face2"), 1);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glBindVertexArray(0);



// Game loop
while ( !glfwWindowShouldClose( window ) )
{
// Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents( );


glClearColor(0.2f, 0.3f, 0.5f, 1.0f);
glClear( GL_COLOR_BUFFER_BIT );
ourShader->Use();

//glBindVertexArray(VAO);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
//glDrawArrays( GL_TRIANGLES, 0, 3 );


// Swap the screen buffers
glfwSwapBuffers( window );
}

}

这是我的顶点着色器:

#version 330 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec2 texCoord;


out vec3 ourColor;
out vec2 TexCoord;

void main() {
ourColor = color;
TexCoord = vec2(texCoord.x, 1-texCoord.y);
gl_Position = vec4(position.x, position.y, position.z, 1.0);
}

这是片段着色器,

#version 330 core
in vec3 ourColor;
in vec2 TexCoord;

out vec4 color;
uniform sampler2D face1;
uniform sampler2D face2;


void main() {
vec4 colorFace1 = texture(face1, TexCoord);
vec4 colorFace2 = texture(face2, TexCoord);

color = colorFace2; //* vec4(ourColor, 1.0);;//vec4(ourColor, 1.0f);
}

现在的问题是,每当我运行代码时,代码都会运行并且输出始终是“face1.jpg”。我已按照 mentioned here 的分步说明进行操作。 .

如果您访问上面链接中的网站,程序的输出必须混合两个图像,如本文所示。

最佳答案

在您的代码中,指令的顺序存在问题,因为 glUniform为当前程序对象指定统一变量的值。

可以随时通过glGetUniformLocation检索到统一位置。 ,并在程序链接成功后存储以备后用(glLinkProgram):

int texLocationFace1 = glGetUniformLocation(ourShader->Program, "face1");
int texLocationFace2 = glGetUniformLocation(ourShader->Program, "face2");

但是要设置统一变量的值,程序必须是事件程序 ( glUseProgram )。注意,没有选择程序的参数,在glUniform的签名中.
你必须使用这个程序,

ourShader->Use();

您设置制服的值之前:

glUniform1i(texLocationFace1, 0);
glUniform1i(texLocationFace2, 1);

请注意,您永远不会设置纹理采样器制服,因为您调用了 glUniform ,在使用任何着色器程序之前。 uniform 变量 faceface 2 默认初始化为 0。因此,输出始终为“face1.jpg”,因为此纹理绑定(bind)到纹理单元 GL_TEXTURE0

由于您只使用一个着色器程序并且不更改它,因此使用该程序并在主循环之前设置制服就足够了:

ourShader->Use();
glUniform1i(texLocationFace1, 0);
glUniform1i(texLocationFace2, 1);

while ( !glfwWindowShouldClose( window ) )
{
....
}

关于c++ - 混合两个纹理的 OpenGL 总是选择第一个纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45818073/

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