gpt4 book ai didi

c++ - SDL_GL_GetAttribute() 输出零?

转载 作者:行者123 更新时间:2023-11-28 01:50:23 25 4
gpt4 key购买 nike

我目前正在开发一个使用 OpenGL 的 SDL 窗口。当我使用错误检查初始化 SDL_GL_SetAttribute(colours here... r, g, b etc.) 时,SDL_GL_GetAttribute() 为 alpha、buffer、r 返回 0 , g, b 并且我认为这会阻止我在 SDL 窗口中创建基本的 OpenGL 三角形...我不确定。

我希望得到一些关于如何解决这个问题的指导。

下面是初始化 OpenGL 和 SDL 的代码片段:

 void Window::InitialiseSDL()
{
SDL_Init( SDL_INIT_AUDIO | SDL_INIT_EVENTS | SDL_INIT_GAMECONTROLLER
| SDL_INIT_HAPTIC | SDL_INIT_JOYSTICK | SDL_INIT_TIMER
| SDL_INIT_VIDEO );

Has_SDL_Init = SDL_WasInit(SDL_INIT_EVERYTHING);

if (!(Has_SDL_Init & SDL_INIT_AUDIO)) // You may be thinking wtf here? But! if one sub system were to break, i'd know which one it exactly was.
{
error.Fatal_SDL_Error("Class Window.h ----> InitialiseSystem() ----> SDL_INIT_AUDIO Has failed to initialise.", state);
}
if (!(Has_SDL_Init & SDL_INIT_EVENTS))
{
error.Fatal_SDL_Error("Class Window.h ----> InitialiseSystem() ----> SDL_INIT_EVENTS Has failed to initialise.", state);
}
if (!(Has_SDL_Init & SDL_INIT_GAMECONTROLLER))
{
error.Fatal_SDL_Error("Class Window.h ----> InitialiseSystem() ----> SDL_INIT_GAMECONTROLLER Has failed to initialise.", state);
}
if (!(Has_SDL_Init & SDL_INIT_HAPTIC))
{
error.Fatal_SDL_Error("Class Window.h ----> InitialiseSystem() ----> SDL_INIT_HAPTIC Has failed to initialise.", state);
}
if (!(Has_SDL_Init & SDL_INIT_JOYSTICK))
{
error.Fatal_SDL_Error("Class Window.h ----> InitialiseSystem() ----> SDL_INIT_JOYSTICK Has failed to initialise.", state);
}
if (!(Has_SDL_Init & SDL_INIT_TIMER))
{
error.Fatal_SDL_Error("Class Window.h ----> InitialiseSystem() ----> SDL_INIT_TIMER Has failed to initialise.", state);
}
if (!(Has_SDL_Init & SDL_INIT_VIDEO))
{
error.Fatal_SDL_Error("Class Window.h ----> InitialiseSystem() ----> SDL_INIT_VIDEO Has failed to initialise.", state);
}

int r, g, b, a, minor, major, visual, depthSize, stencil, buffer, dblbuffer;
int core, flagsss;

SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &visual);


SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); // Forces the use of Modern OpenGL.
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &core);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_FLAGS, &flagsss);

// They all fail from this point below ( Only in SDL init method )

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 3);
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &r);

SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &g);

SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &b);

SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 1);
SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE,&a);

SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depthSize);

SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 5);
SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &stencil);

SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 24);
SDL_GL_GetAttribute(SDL_GL_BUFFER_SIZE, &buffer);

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &dblbuffer);

this->GameWindow = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
this->windowWidth, this->windowHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
if (!GameWindow)
{
ShutDownSystem();
}

SDL_SetWindowResizable(GameWindow, SDL_TRUE);
}

void Window::InitialiseOpenGL()
{
OpenGL_Context = SDL_GL_CreateContext(GameWindow);
SDL_GL_SetSwapInterval(1);

if (OpenGL_Context == nullptr)
{
error.Fatal_SDL_Error("LegendRenderer ----> InitialiseOpenGL() ----> Failed to create an OpenGL context for SDL.", state);
SetWindowEngineState(*state);
}

glewExperimental = GL_TRUE;
GLenum OpenGLError = glewInit();
if (OpenGLError != GLEW_OK)
{
error.Fatal_SDL_Error("LegendRenderer ----> InitialiseOpenGL() ----> Failed to Initialise GLEW.", state);
SetWindowEngineState(*state);
}

printf("OpenGL loaded\n");
printf("Vendor: %s\n", glGetString(GL_VENDOR));
printf("Renderer: %s\n", glGetString(GL_RENDERER));
printf("Version: %s\n", glGetString(GL_VERSION));


if (SDL_GL_GetSwapInterval < 0)
{
error.Fatal_SDL_Error("LegendRenderer ----> InitialiseOpenGL() ----> Failed to set frame buffer interval for SDL.", state);
SetWindowEngineState(*state);
}

int w, h;
SDL_GetWindowSize(GameWindow, &w, &h);
glViewport(0, 0, w, h);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

最佳答案

SDL_GL_GetAttribute() (强调我的):

Use this function to get the actual value for an attribute from the current [OpenGL] context.

您甚至在拥有窗口之前就调用了 SDL_GL_GetAttribute(),更不用说当前的 OpenGL 上下文了。

调用 SDL_GL_GetAttribute()您成功创建窗口和 OpenGL 上下文后,并通过 SDL_GL_MakeCurrent() 使上下文成为当前上下文(好吧,技术上不需要,因为 SDL_GL_CreateContext() 将使创建的上下文保持当前状态。

例如,您可能请求了 5 个红色位,但您最终获得的 OpenGL 上下文可能会提供 8 个位。

关于c++ - SDL_GL_GetAttribute() 输出零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43219832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com