CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Oct 2017
    Posts
    50

    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.
    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 Attachment 35149

    I was expecting awesomeface.png on top of it.
    Last edited by noobofcpp; January 17th, 2018 at 05:40 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured