Kshitij Tripathi
Kshitij Tripathi

Reputation: 21

The same code not running on two different hardware in opengl

The code runs successfully on rtx2060 but not on rtx 3060. It uses the same glm library, but it shows a black screen on rtx 3060 and normal screen on rtx2060

#include <iostream>

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

// GLM Mathematics
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"


GLint Width{ 400 }, Height{ 600 }, NumChannels;
glm::mat4 transform;
glm::mat4 view;

void GetLastError();
void KeyPressedCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
void OnResize(GLFWwindow* window, int width, int height);


int main()
{
    if (GLFW_FALSE == glfwInit())
    {
        GetLastError();
        return -1;
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);


    GLFWwindow* windowPtr = glfwCreateWindow(Width, Height, "???-----))(((", nullptr, nullptr);
    if (windowPtr == nullptr)
    {
        GetLastError();
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(windowPtr);
    glewExperimental = GL_TRUE;

    if (glewInit() != GLEW_OK)
    {
        std::cout << "glewinit failed";
        glfwTerminate();
        return -1;
    }

    glfwSetKeyCallback(windowPtr, KeyPressedCallback);


    const GLchar* VertexShaderCode
    {
        "#version 330 core \n"
        "layout(location = 0) in vec3 position; \n"
        "layout(location = 1) in vec2 TexCoord; \n"
        "uniform mat4 projection; \n"
        "uniform mat4 model; \n"
        "uniform mat4 view; \n"
        "out vec2 texCoord \n;"
        "void main() \n"
        "{ \n"
        "   gl_Position = projection * view * model * vec4(position, 1.0f); \n"
        //"   gl_Position = vec4(position, 1.0f); \n"
        "   texCoord = TexCoord; \n"
        "} \n"
    };


    const GLchar* FragmentShaderCode
    {
        "#version 330 core \n"
         "out vec4 color; \n"
         "in vec2 texCoord; \n"
         "uniform sampler2D imageData; \n"
         "void main() \n"
         "{ \n"
         "  color = texture(imageData, texCoord); \n"
        //"  color = vec4(1.0f, 0.0f, 0.0f, 1.0f); \n"
        "} \n"
    };


    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &VertexShaderCode, 0);
    glCompileShader(vertexShader);
    GLint success;
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if (success == GL_FALSE)
    {
        GLchar info[512];
        glGetShaderInfoLog(vertexShader, 512, nullptr, info);
        std::cout << "Error Info on vertex shader: " << info;
    }

    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &FragmentShaderCode, 0);
    glCompileShader(fragmentShader);
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (success == GL_FALSE)
    {
        GLchar info[512];
        glGetShaderInfoLog(fragmentShader, 512, nullptr, info);
        std::cout << "Error info of fragment shader: " << info;
    }


    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);

    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (success == GL_FALSE)
    {
        GLchar info[512];
        glGetProgramInfoLog(shaderProgram, 512, nullptr, info);
        std::cout << "Error info of shader program: " << info;
    }

    //GLfloat vertices[]
    //{
    //    //vertices          //tex coordinates
    //    -0.8f, -0.8f, 0.0f,  0.0f, 0.0f,
    //    -0.8f, 0.8f, 0.0f,   0.0f, 1.0f,
    //    0.8f, -0.8f, 0.0f,   1.0f, 0.0f,
    //    0.8f, 0.8f, 0.0f,   1.0f, 1.0f,

    //    //vertices            //tex coordinates
    //    -0.8f, -0.8f, -0.8f,  0.0f, 0.0f,
    //    -0.8f,  0.8f, -0.8f,   0.0f, 1.0f,
    //     0.8f, -0.8f, -0.8f,   1.0f, 0.0f,
    //     0.8f,  0.8f, -0.8f,   1.0f, 1.0f,



    //};

    GLfloat vertices[]
    {
        //vertices          //tex coordinates
        -0.8f, -0.8f, 0.0f,  0.0f, 0.0f,
        -0.8f, 0.8f, 0.0f,   0.0f, 1.0f,
        0.8f, -0.8f, 0.0f,   1.0f, 0.0f,
        -0.8f, 0.8f, 0.0f,   0.0f, 1.0f,
        0.8f, -0.8f, 0.0f,   1.0f, 0.0f,
        0.8f, 0.8f, 0.0f,   1.0f, 1.0f,   //front

        -0.8f, -0.8f, 0.0f,  0.0f, 0.0f,
        -0.8f, 0.8f, 0.0f,   0.0f, 1.0f,
        -0.8f, -0.8f, -0.8f,   1.0f, 0.0f,
        -0.8f, 0.8f, 0.0f,   0.0f, 1.0f,
        -0.8f, 0.8f, -0.8f,   1.0f, 1.0f,
        -0.8f, -0.8f, -0.8f,  1.0f, 0.0f, //left

        -0.8f, 0.8f, 0.0f,  0.0f, 0.0f,
        0.8f, 0.8f, 0.0f,   0.0f, 1.0f,
        -0.8f, 0.8f, -0.8f,   1.0f, 0.0f,
        0.8f, 0.8f, 0.0f,   0.0f, 1.0f,
        -0.8f, 0.8f, -0.8f,   1.0f, 0.0f,
        0.8f, 0.8f, -0.8f,  1.0f, 1.0f,  //top

        0.8f, -0.8f, 0.0f,  0.0f, 0.0f,
        0.8f, 0.8f, 0.0f,   0.0f, 1.0f,
        0.8f, -0.8f, -0.8f,   1.0f, 0.0f,
        0.8f, 0.8f, 0.0f,   0.0f, 1.0f,
        0.8f, -0.8f, -0.8f,   1.0f, 0.0f,
        0.8f, 0.8f, -0.8f,  1.0f, 1.0f,    //right

        -0.8f, -0.8f, -0.8f,  0.0f, 0.0f,
        -0.8f, 0.8f, -0.8f,   0.0f, 1.0f,
        0.8f, -0.8f, -0.8f,   1.0f, 0.0f,
        -0.8f, 0.8f, -0.8f,   0.0f, 1.0f,
        0.8f, -0.8f, -0.8f,   1.0f, 0.0f,
        0.8f, 0.8f, -0.8f,  1.0f, 1.0f,    //back
       
        -0.8f, -0.8f, 0.0f,  0.0f, 0.0f,
        -0.8f, -0.8f, -0.8f,   0.0f, 1.0f,
        0.8f, -0.8f, 0.0f,   1.0f, 0.0f,
        -0.8f, -0.8f, -0.8f,   0.0f, 1.0f,
        0.8f, -0.8f, 0.0f,   1.0f, 0.0f,
        0.8f, -0.8f, -0.8f,  1.0f, 1.0f,    //bottom


    };

    //this messes the texture
    //GLuint indices[]
    //{
    //    0, 1, 2,
    //    2, 1, 3, //front face

    //    0, 1, 4,
    //    4, 1, 5, //left face

    //    4, 5, 6,
    //    6, 5, 7, //back face

    //    2, 3, 6,
    //    6, 3, 7, //right face

    //    1, 5, 3,
    //    3, 5, 7, //top face

    //    0, 4, 2,
    //    2, 4, 6, // bottom face

    //};

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

    GLint img_width, img_height, img_comps;
    const unsigned char* img_pix = stbi_load("bg.jpg", &img_width, &img_height, &img_comps, STBI_rgb);
    std::cout << "Image width {" << img_width << "}, image heigth {" << img_height << "}, imgage components {" << img_comps << "} " << std::endl;

    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    // Set texture filtering
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img_width, img_height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_pix);
    glGenerateMipmap(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);

    GLuint VAO, VBO, EBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);

    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);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));


    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    glUseProgram(shaderProgram);

    glfwSetWindowSizeCallback(windowPtr, OnResize);
    
    //transform = glm::translate(transform, glm::vec3(0.0f, -0.1f, 0.0f));   
    //transform = glm::rotate(transform, 180.0f, glm::vec3(1.0f, 0.0f, 0.0f));
    
    
    view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));

    glm::mat4 projection;
    projection = glm::perspective(45.0f, (GLfloat)Width / (GLfloat)Height, 0.1f, 100.0f);
    GLint projectLoc = glGetUniformLocation(shaderProgram, "projection");
    glUniformMatrix4fv(projectLoc, 1, GL_FALSE, glm::value_ptr(projection));
    //glDisable(GL_CULL_FACE);

    while (!glfwWindowShouldClose(windowPtr))
    {
        //no need to get uniform location of sampler2D as there is only one, so it automatically binds
        glfwPollEvents();
        glClear(GL_COLOR_BUFFER_BIT);

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, texture);
        
        
        GLint modelLoc = glGetUniformLocation(shaderProgram, "model");        
        GLint viewLoc = glGetUniformLocation(shaderProgram, "view");

        
        glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(transform));
        glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));

        
        //std::cout << "Transform : " << ;

        glBindVertexArray(VAO);
        //glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);        
        glDrawArrays(GL_TRIANGLES, 0, 36);
        glBindVertexArray(0);
        glBindTexture(GL_TEXTURE_2D, 0);

        glfwSwapBuffers(windowPtr);

    }

    glfwTerminate();
    return 0;
}

void GetLastError()
{
    const char* error;
    glfwGetError(&error);
    std::cout << error;
}

void KeyPressedCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if ((key == GLFW_KEY_ESCAPE) && (action == GLFW_PRESS))
    {
        //std::cout << "Escape Key Pressed \n";
        glfwSetWindowShouldClose(window, GL_TRUE);
    }

    else if ((key == GLFW_KEY_LEFT) && (action == GLFW_PRESS))
    {                
        view = glm::rotate(view, 15.0f, glm::vec3(0.0f, 1.0f, 0.0f));
    }

    else if ((key == GLFW_KEY_RIGHT) && (action == GLFW_PRESS))
    {
        view = glm::rotate(view, -15.0f, glm::vec3(0.0f, 1.0f, 0.0f));
    }

    else if ((key == GLFW_KEY_UP) && (action == GLFW_PRESS))
    {
        view = glm::rotate(view, -15.0f, glm::vec3(1.0f, 0.0f, 0.0f));
    }
    else if ((key == GLFW_KEY_DOWN) && (action == GLFW_PRESS))
    {
        view = glm::rotate(view, 15.0f, glm::vec3(1.0f, 0.0f, 0.0f));
    }
    else if ((key == GLFW_KEY_W) && (action == GLFW_PRESS))
    {                
        view = glm::translate(view, glm::vec3(0.0f, 0.0f, 0.1f));
    }
    else if ((key == GLFW_KEY_S) && (action == GLFW_PRESS))
    {
        view = glm::translate(view, glm::vec3(0.0f, 0.0f, -0.1f));
    }
    else if ((key == GLFW_KEY_A) && (action == GLFW_PRESS))
    {
        view = glm::translate(view, glm::vec3(-0.1f, 0.0f, 0.0f));
    }
    else if ((key == GLFW_KEY_D) && (action == GLFW_PRESS))
    {
        view = glm::translate(view, glm::vec3(0.1f, 0.0f, 0.0f));
    }
}

void OnResize(GLFWwindow* window, int width, int height)
{
    Width = width;
    Height = height;
    glViewport(0, 0, width, height);
}

Ideally it should produce the same output, but it is not.

What is the error here?

Upvotes: 1

Views: 77

Answers (1)

rafix07
rafix07

Reputation: 20949

It is hard to believe me that the current code may generate some sensible output due to this line:

view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
// which combines matrix as follows: view * translationMatrixBasedOnVec(0,0,-3)

where

view

is initialized by mat4 default constructor - all matrix's components are 0. So any vertex position generated by vertex shader is (0, 0, 0, 0).

If you want to position camera at (0,0,0) , view matrix must be identity. What can be achived by

glm::mat4 view(1.f)

Upvotes: 1

Related Questions