OpenGL fragment shader behaves differently when launched from Visual Studio 2022

Viewed 48

I am reading the OpenGL Red Book and following the code examples:

https://github.com/openglredbook/examples/blob/master/src/01-triangles/01-triangles.cpp

https://github.com/openglredbook/examples/tree/master/bin/media/shaders/triangles

Here is the output when running from Visual Studio (either Release, Debug, with or without debugger) enter image description here

And here is the output when I click on the exe manually:

enter image description here

What I have tried so far:

  • I made sure the program always runs with my NVidia GTX 1060 card instead of Intel HD 630 by looking at NVidia's GPU Activity window.

  • I tried disabling my Intel HD 630 device from Device Manager, the program would simply crash on line "glBindVertexArray( VAOs[Triangles] )"

  • If I comment out line "fColor = vec4(0.5, 0.4, 0.0, 0.0);" in the fragment shader, the white triangles would still appear when running through Visual Studio 2022, but a black screen would appear (I guess as expected?) when running manually.

Here is the code I'm working with:

    //////////////////////////////////////////////////////////////////////////////
//
//  Triangles.cpp
//
//////////////////////////////////////////////////////////////////////////////

#include "vgl.h"
#include "LoadShaders.h"

enum VAO_IDs { Triangles, NumVAOs };
enum Buffer_IDs { ArrayBuffer, NumBuffers };
enum Attrib_IDs { vPosition = 0 };

GLuint  VAOs[NumVAOs];
GLuint  Buffers[NumBuffers];

const GLuint  NumVertices = 6;

//----------------------------------------------------------------------------
//
// init
//

void
init( void )
{
    glGenVertexArrays( NumVAOs, VAOs );
    glBindVertexArray( VAOs[Triangles] );

    GLfloat  vertices[NumVertices][2] = {
        { -0.90f, -0.90f }, {  0.85f, -0.90f }, { -0.90f,  0.85f },  // Triangle 1
        {  0.90f, -0.85f }, {  0.90f,  0.90f }, { -0.85f,  0.90f }   // Triangle 2
    };

    glCreateBuffers( NumBuffers, Buffers );
    glBindBuffer( GL_ARRAY_BUFFER, Buffers[ArrayBuffer] );
    glBufferStorage( GL_ARRAY_BUFFER, sizeof(vertices), vertices, 0);

    ShaderInfo  shaders[] =
    {
        { GL_VERTEX_SHADER, "media/shaders/triangles/triangles.vert" },
        { GL_FRAGMENT_SHADER, "media/shaders/triangles/triangles.frag" },
        { GL_NONE, NULL }
    };

    GLuint program = LoadShaders( shaders );
    glUseProgram( program );

    glVertexAttribPointer( vPosition, 2, GL_FLOAT,
                           GL_FALSE, 0, BUFFER_OFFSET(0) );
    glEnableVertexAttribArray( vPosition );
}

//----------------------------------------------------------------------------
//
// display
//

void
display( void )
{
    static const float black[] = { 0.0f, 0.0f, 0.0f, 0.0f };

    glClearBufferfv(GL_COLOR, 0, black);

    glBindVertexArray( VAOs[Triangles] );
    glDrawArrays( GL_TRIANGLES, 0, NumVertices );
}

//----------------------------------------------------------------------------
//
// main
//
//#ifdef __cplusplus
//extern "C" {
//#endif
//
//    __declspec(dllexport) DWORD NvOptimusEnablement = 1;
//    __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
//
//#ifdef __cplusplus
//}
//#endif

#ifdef _WIN32
int CALLBACK WinMain(
  _In_ HINSTANCE hInstance,
  _In_ HINSTANCE hPrevInstance,
  _In_ LPSTR     lpCmdLine,
  _In_ int       nCmdShow
)
#else
int
main( int argc, char** argv )
#endif
{

    glfwInit();

    GLFWwindow* window = glfwCreateWindow(800, 600, "Triangles", NULL, NULL);

    glfwMakeContextCurrent(window);
    gl3wInit();

    init();

    while (!glfwWindowShouldClose(window))
    {
        display();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);

    glfwTerminate();
}

triangles.vert:

#version 400 core

layout( location = 0 ) in vec4 vPosition;

void
main()
{
    gl_Position = vPosition;
}

triangles.frag:

#version 450 core

out vec4 fColor;

void main()
{
    fColor = vec4(0.5, 0.4, 0.0, 0.0);
}

Intel HD driver version: 30.0.101.1340

NVidia driver version: 31.0.15.1694

VS2022 version: 17.3.3

Windows 10 Home version: 10.0.19043 Build 19043

1 Answers

The directories to your exe file are most likely different for VS2022. I don't know when you read those files but when you do read them you should make sure that opening the file was successful:

if (!file_var_name.is_open()) {
    printf("failed to open: %s", file_path);
}

Also, you should not be using different versions for your vertex and fragment shaders. The version should be the OpenGL version times 100 - for example, OpenGL 3.3 = #version 330

Related