这是我将两个纹理混合在一起的代码,
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”。我已按照此处提到的逐步说明进行操作。
如果您访问上面的链接中的网站,则程序输出必须混合两个图像,如本文所示。
在您的代码中,指令的顺序存在问题,因为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 ) )
{
....
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句