[英]OpenGL fragment shader behaves differently when launched from Visual Studio 2022
I am reading the OpenGL Red Book and following the code examples:我正在阅读 OpenGL 红皮书并遵循代码示例:
https://github.com/openglredbook/examples/blob/master/src/01-triangles/01-triangles.cpp https://github.com/openglredbook/examples/blob/master/src/01-triangles/01-triangles.cpp
https://github.com/openglredbook/examples/tree/master/bin/media/shaders/triangles 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)这是从 Visual Studio 运行时的 output(发布、调试、带或不带调试器)
And here is the output when I click on the exe manually:当我手动单击 exe 时,这里是 output:
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.通过查看 NVidia 的 GPU Activity window,我确保程序始终使用我的 NVidia GTX 1060 卡而不是 Intel HD 630 运行。
I tried disabling my Intel HD 630 device from Device Manager, the program would simply crash on line "glBindVertexArray( VAOs[Triangles] )"我尝试从设备管理器中禁用我的 Intel HD 630 设备,程序只会在“glBindVertexArray(VAOs[Triangles])”线上崩溃
If I comment out line "fColor = vec4(0.5, 0.4, 0.0, 0.0);"如果我注释掉“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.在片段着色器中,通过 Visual Studio 2022 运行时仍会出现白色三角形,但手动运行时会出现黑屏(我猜是预期的?)。
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:三角形.vert:
#version 400 core
layout( location = 0 ) in vec4 vPosition;
void
main()
{
gl_Position = vPosition;
}
triangles.frag:三角形.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英特尔高清驱动程序版本:30.0.101.1340
NVidia driver version: 31.0.15.1694 NVidia 驱动程序版本:31.0.15.1694
VS2022 version: 17.3.3 VS2022版本:17.3.3
Windows 10 Home version: 10.0.19043 Build 19043 Windows 10 家庭版:10.0.19043 Build 19043
The directories to your exe file are most likely different for VS2022.对于 VS2022,exe 文件的目录很可能不同。 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
版本应该是 OpenGL 版本乘以 100 - 例如,OpenGL 3.3 = #version 330
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.