Here is my code where I mix two texture,
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 );
}
}
Here is my vertex shader:
#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);
}
And this is the fragment shader,
#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);
}
Now the problem is that the whenever I run the code, the code runs and the output is always "face1.jpg". I have followed the step by step instructions as mentioned here.
If you visit the website in the link above the output of the program must be mixing of two images as shown in this post.
In your code is an issue with the order of the instructions, because glUniform
specifies the value of a uniform variable for the current program object.
The uniform location can be retrieved at any time by glGetUniformLocation
, and stored for later use, after the program is linked successfully (glLinkProgram
):
int texLocationFace1 = glGetUniformLocation(ourShader->Program, "face1");
int texLocationFace2 = glGetUniformLocation(ourShader->Program, "face2");
But to set the value of a uniform variable, the program has to be the active program (glUseProgram
). Note, there is no parameter to select the program, in the sinature of glUniform
.
You have to use the program,
ourShader->Use();
before you set the values of the uniforms:
glUniform1i(texLocationFace1, 0);
glUniform1i(texLocationFace2, 1);
Note, you never set the texture sampler uniforms, because you call glUniform
, before any shader program is used. The uniform variables face
and face 2
by default are initialized with 0. Therefore, the output is always "face1.jpg" because this texture is bound to texture unit GL_TEXTURE0
.
Since you only use one shader program and you don't change it, it is sufficient to use the program and to set the uniforms before the main loop:
ourShader->Use();
glUniform1i(texLocationFace1, 0);
glUniform1i(texLocationFace2, 1);
while ( !glfwWindowShouldClose( window ) )
{
....
}