Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: When working with OpenGL, the second Texture is not showing up  (Read 1359 times)

0 Members and 1 Guest are viewing this topic.

Hythloday

  • Newbie
  • *
  • Posts: 10
    • View Profile
I am learning through the tutorials on learnopengl.com, and I met a unsolvable problem when I try to use a second Texture.

The problem is the same to this guy's post on official forum:
https://www.opengl.org/discussion_boards/showthread.php/200373-OpenGL-second-Texture-is-not-showing-up-%28OpenGL-Texture-Unit%29

However that guy didn't get an answer eventually, so I posted it here hope someone could help me.


The main code is like this:

#include <Alef\alef_application.h>
#include <Alef\gl_specific\alef_gl_shader.h>

#include <GL\glew.h>

#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
#include <glm\gtc\type_ptr.hpp>

#include <lua\lua.h>


using namespace alef;

unsigned int VBO, VAO, EBO;

Application* app = nullptr;

IntPoint screen_size;
alef::gl_specific::Shader* my_shader = NULL;

glm::mat4 model, view, projection;

void _onDrawing(ObjectBase* sender)
{
        my_shader = getAsset2<alef::gl_specific::Shader>("shader1");
        my_shader->use();

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, getAsset<Texture>("card_front.png").getNativeHandle());
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, getAsset<Texture>("card_back.png").getNativeHandle());


        my_shader->setInt("card_front", 0);
        my_shader->setInt("card_back", 1);

        model = glm::rotate(glm::mat4(1.0f), (app->getElapsedTime().asSeconds() * 0.75f) * glm::radians(50.0f), glm::vec3(0.0f, 1.0f, 0.0f));

        view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -3.0f));

        projection = glm::perspective(glm::radians(45.0f), (float)screen_size.x / screen_size.y, 0.1f, 100.0f);

        my_shader->setMat4("model", model);
        my_shader->setMat4("view", view);
        my_shader->setMat4("projection", projection);
       
        glBindVertexArray(VAO);
               
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        //glDrawArrays(GL_TRIANGLES, 0, 3);
       
        glBindVertexArray(0);
}

void _onClosing(ObjectBase* sender)
{
        alef::DialogResult result = app->showMsgBox(L"&#36864;&#20986;&#28216;&#25103;&#65311;", alef::BUTTON_OKCANCEL);
        if (result == RESULT_OK)
        {
                app->closing = true;
        }
}

FloatPoint getGLSize(IntPoint size)
{
        FloatPoint gl_size;
        IntPoint screen_size = app->getScreenSize();

        gl_size.x = (size.x / (float)screen_size.x) * 2;
        gl_size.y = (size.y / (float)screen_size.y) * 2;

        return gl_size;

}

int main()
{

#ifdef _DEBUG
    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
   
       
    try
    {
                GLenum ERR = glewInit();
               
                app = Application::getInstance();
        app->init("test");
               
                app->onDrawing.Connect(_onDrawing);
                app->onClosing.Connect(_onClosing);
               
                IntPoint card_size;
                card_size.x = 200;
                card_size.y = 285;

                screen_size = app->getScreenSize();

                FloatPoint card_size_f = getGLSize(card_size);
               
                float vertices[] = {
                        //     ---- &#20301;&#32622; ----       ---- &#39068;&#33394; ----     - &#32441;&#29702;&#22352;&#26631; -
                        0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // &#21491;&#19978;
                        0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // &#21491;&#19979;
                        -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // &#24038;&#19979;
                        -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f    // &#24038;&#19978;
                };
                unsigned int indices[] = {
                        0, 1, 3, // first triangle
                        1, 2, 3  // second triangle
                };
                               
                glGenVertexArrays(1, &VAO);
                glGenBuffers(1, &VBO);
                glGenBuffers(1, &EBO);
                // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).

                glBindVertexArray(VAO);

                glBindBuffer(GL_ARRAY_BUFFER, VBO);
                glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

                glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
                glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

                // position attribute
                glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
                glEnableVertexAttribArray(0);
                // color attribute
                glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
                glEnableVertexAttribArray(1);
                // coords attribute
                glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
                glEnableVertexAttribArray(2);
               
        app->run();

        app->dispose();
    }
    catch (Exception& e)
    {
        cout << e.description() << endl << e.info << endl;
        system("Pause");
    }
 
    return EXIT_SUCCESS;
}


the shaders:




#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;


uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;


out vec4 ourColor;
out vec2 TexCoord;


void main()
{
    gl_Position = projection * view * model * vec4(aPos, 1.0);
    ourColor = vec4(aColor, 1.0);
    TexCoord = aTexCoord;
}
 



#version 330 core
out vec4 FragColor;


in vec4 ourColor;
in vec2 TexCoord;


uniform sampler2D frontTexture;
uniform sampler2D backTexture;


void main() {

   if(gl_FrontFacing) {
      FragColor = texture(frontTexture, TexCoord);
   }
   else {
      FragColor = texture(backTexture, TexCoord);
   }
}
 


Hythloday

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: When working with OpenGL, the second Texture is not showing up
« Reply #1 on: June 13, 2018, 09:55:27 am »
ah.I found where's the problem, it's a silly mistake

these lines:

        my_shader->setInt("card_front", 0);
        my_shader->setInt("card_back", 1);

should be:

        my_shader->setInt("frontTexture", 0);
        my_shader->setInt("backTexture", 1);

 

anything