Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 10

Thread: OpenGL second Texture is not showing up (OpenGL Texture Unit).

  1. #1
    Junior Member Newbie noobofcpp's Avatar
    Join Date
    Jan 2018
    Posts
    5

    glActiveTexture not working

    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.
    Code :
    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
    Code :
    #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
    Code :
    #version 450 core
     
    in vec2 TexCoord;
    uniform sampler2D inTextureOne;
    uniform sampler2D inTextureTwo;
     
    out vec4 FragmentColor;
     
    void main()
    {
        FragmentColor=mix(texture(inTextureOne,TexCoord),texture(inTextureTwo,TexCoord),0.2);
    }

    And this what i got Click image for larger version. 

Name:	failed texture unit.jpg 
Views:	110 
Size:	82.1 KB 
ID:	2600
    I was expecting awesomeface.png on top of it.
    Last edited by noobofcpp; 01-17-2018 at 03:33 PM.

  2. #2
    Junior Member Newbie noobofcpp's Avatar
    Join Date
    Jan 2018
    Posts
    5

    glactivetexture(GL_TEXTURE1) is not not working

    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.

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,789
    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.

  4. #4
    Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    459
    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.

  5. #5
    Junior Member Newbie noobofcpp's Avatar
    Join Date
    Jan 2018
    Posts
    5
    Still not working.Well my spec is Intel Core i7 6700hq,nvidia GTX 960M.

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,789
    Is this the same problem as here: https://www.opengl.org/discussion_bo...exture-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.

  7. #7
    Junior Member Newbie noobofcpp's Avatar
    Join Date
    Jan 2018
    Posts
    5
    glGetError return 1280

  8. #8
    Junior Member Newbie noobofcpp's Avatar
    Join Date
    Jan 2018
    Posts
    5
    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.Click image for larger version. 

Name:	error 1280.jpg 
Views:	76 
Size:	73.5 KB 
ID:	2601
    Last edited by noobofcpp; 01-18-2018 at 04:04 AM.

  9. #9
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,569
    No need to delete the thread. However, you might post your solution for others so they can benefit from your experience.

  10. #10
    Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    459
    I guess that the OP still has no answers for his issue.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •