这是我的代码,我混合了两个纹理,
的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);
}
现在的问题是每当我运行代码时,代码运行并且输出总是&#34; face1.jpg&#34;。我按照mentioned here的步骤说明进行了操作。
如果您在上面的链接中访问该网站,该程序的输出必须混合两个图像,如本文所示。
答案 0 :(得分:5)
在您的代码中是指令顺序的问题,因为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.因此,输出始终为&#34; face1.jpg&#34;因为此纹理绑定到纹理单元GL_TEXTURE0
。
由于您只使用一个着色器程序并且您没有对其进行更改,因此使用程序并在主循环之前设置制服就足够了:
ourShader->Use();
glUniform1i(texLocationFace1, 0);
glUniform1i(texLocationFace2, 1);
while ( !glfwWindowShouldClose( window ) )
{
....
}
答案 1 :(得分:1)
您没有在片段着色器中混合任何颜色。您正在从face1和face2进行纹理查找,然后您只返回后者并丢弃前者。尝试
color = 0.5 * (colorFace1 + colorFace2);
片段着色器中的。
如果你看到的是真正的face1而不是face2,那么我也没有发现其他错误。