OpenGL 混合两个纹理总是拾取第一个纹理
Posted
技术标签:
【中文标题】OpenGL 混合两个纹理总是拾取第一个纹理【英文标题】:OpenGL mixing two textures always picks up the first texture 【发布时间】:2017-08-22 12:46:11 【问题描述】:这是我混合两种纹理的代码,
main.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 的分步说明进行操作。
如果您访问上面链接中的网站,程序的输出必须混合两个图像,如本文所示。
【问题讨论】:
您确定在设置制服时程序处于活动状态吗?在此之前,您是否使用正确的程序致电glUseProgram
?
您还应该决定是要opengl 还是opengl-es。你不能两者兼得。
【参考方案1】:
在您的代码中,指令的顺序存在问题,因为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
。统一变量face
和face 2
默认初始化为0。因此,输出总是“face1.jpg”,因为这个纹理绑定到纹理单元GL_TEXTURE0
。
由于您只使用一个着色器程序并且不更改它,因此使用该程序并在主循环之前设置制服就足够了:
ourShader->Use();
glUniform1i(texLocationFace1, 0);
glUniform1i(texLocationFace2, 1);
while ( !glfwWindowShouldClose( window ) )
....
【讨论】:
啊,我在代码中搜索了一个明显的错误,但无法解决。就是这个。在这个例子中,我认为他也不需要在循环内使用。做一次就够了。【参考方案2】:您没有在片段着色器中混合任何颜色。您正在从 face1 和 face2 进行纹理查找,然后只返回后者并丢弃前者。试试
color = 0.5 * (colorFace1 + colorFace2);
在您的片段着色器中。
如果您看到的确实是 face1 而不是 face2,那么我在其他地方也没有发现错误。
【讨论】:
以他现在的代码,他不应该得到face2的颜色吗?但他声称得到 face1 的颜色。 是的,看我的最后一句话。 :) 我只是不明白为什么你甚至期望看到这两种纹理的混合。 因为这就是我所需要的。我不能吗?以上是关于OpenGL 混合两个纹理总是拾取第一个纹理的主要内容,如果未能解决你的问题,请参考以下文章