PDA

View Full Version : OpenGL second Texture is not showing up (OpenGL Texture Unit).



noobofcpp
01-17-2018, 09:13 AM
I only got first texture show up.

What's wrong with this my code,I can't find where's the mistake even after two hours looking.

int main()
{
sf::ContextSettings settings;
settings.majorVersion = 4;
settings.minorVersion = 5;
settings.attributeFlags = settings.Core;

sf::Window window;
window.create(sf::VideoMode(1600, 900), "Texture Unit Rectangle", sf::Style::Close, settings);
window.setActive(true);
window.setVerticalSyncEnabled(true);

glewExperimental = true;
glewInit();

GLuint shaderProgram = createShaderProgram("FX/Rectangle.vss", "FX/Rectangle.fss");

float vertex[] =
{
-0.5f,0.5f,0.0f, 0.0f,0.0f,
-0.5f,-0.5f,0.0f, 0.0f,1.0f,
0.5f,0.5f,0.0f, 1.0f,0.0f,
0.5,-0.5f,0.0f, 1.0f,1.0f,
};

GLuint indices[] =
{
0,1,2,
1,2,3,
};

GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);

GLuint ebo;
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, false, sizeof(float) * 5, (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, false, sizeof(float) * 5, (void*)(sizeof(float) * 3));
glEnableVertexAttribArray(1);

GLuint texture[2];
glGenTextures(2, texture);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture[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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

sf::Image* imageOne = new sf::Image;
bool isImageOneLoaded = imageOne->loadFromFile("Texture/container.jpg");
if (isImageOneLoaded)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageOne->getSize().x, imageOne->getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageOne->getPixelsPtr());
glGenerateMipmap(GL_TEXTURE_2D);
}

delete imageOne;

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture[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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

sf::Image* imageTwo = new sf::Image;
bool isImageTwoLoaded = imageTwo->loadFromFile("Texture/awesomeface.png");
if (isImageTwoLoaded)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageTwo->getSize().x, imageTwo->getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageTwo->getPixelsPtr());
glGenerateMipmap(GL_TEXTURE_2D);
}

delete imageTwo;

glUniform1i(glGetUniformLocation(shaderProgram, "inTextureOne"), 0);
glUniform1i(glGetUniformLocation(shaderProgram, "inTextureTwo"), 1);

sf::Event event;

bool isRunning = true;

while (isRunning)
{
while (window.pollEvent(event))
{
if (event.type == event.Closed)
{
isRunning = false;
}
}

glClear(GL_COLOR_BUFFER_BIT);

if (isImageOneLoaded && isImageTwoLoaded)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glUseProgram(shaderProgram);
}

glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
glBindVertexArray(0);

window.display();
}

glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ebo);
glDeleteProgram(shaderProgram);
glDeleteTextures(2,texture);

return 0;
}

The Vertex Shader

#version 450 core

layout(location=0) in vec3 inPos;
layout(location=1) in vec2 inTexCoord;

out vec2 TexCoord;

void main()
{
gl_Position=vec4(inPos,1.0);
TexCoord=inTexCoord;
}

The fragment shader

#version 450 core

in vec2 TexCoord;
uniform sampler2D inTextureOne;
uniform sampler2D inTextureTwo;

out vec4 FragmentColor;

void main()
{
FragmentColor=mix(texture(inTextureOne,TexCoord),t exture(inTextureTwo,TexCoord),0.2);
}

And this what i got 2600
I was expecting awesomeface.png on top of it.

noobofcpp
01-17-2018, 03:23 PM
Not only glactivetexture(GL_TEXTURE1) not working,glactivetexture(GL_TEXTURE2) ,glactivetexture(GL_TEXTURE3) and so on is not working and only show nothing.Only glactivetexture(GL_TEXTURE0) is working and drawn and show the texture.

mhagain
01-17-2018, 08:44 PM
Assuming your hardware supports multitexture - it sounds like you're using the fixed pipeline and didn't do glEnable (GL_TEXTURE_2D) after each glActiveTexture.

Silence
01-17-2018, 11:20 PM
At first (and quick) glance, there's nothing bad in your code.
Can you check these things please:

Ensure you have data sent in both the textures (what you give to glTexImage2D).
Display only the second texture in your shader.
Call glGetError after main steps, and then go to a finer grain.

noobofcpp
01-18-2018, 02:22 AM
Still not working.Well my spec is Intel Core i7 6700hq,nvidia GTX 960M.

mhagain
01-18-2018, 02:27 AM
Is this the same problem as here: https://www.opengl.org/discussion_boards/showthread.php/200373-OpenGL-second-Texture-is-not-showing-up-%28OpenGL-Texture-Unit%29

Can you confirm please, because you seem to have double-posted, and that's just going to waste people's time as they try to figure out what's going on in the first place.

noobofcpp
01-18-2018, 02:28 AM
glGetError return 1280

noobofcpp
01-18-2018, 02:51 AM
I figured my problem is around glActiveTexture,so I create this thread.Sorry about the that, after I create this thread.,I can't find delete button to delete the first thread.
Only this program return 1280 error.2601

Dark Photon
01-18-2018, 05:35 AM
No need to delete the thread. However, you might post your solution for others so they can benefit from your experience.

Silence
01-18-2018, 06:17 AM
I guess that the OP still has no answers for his issue.