![](/img/trans.png)
[英]lib OSMesa off-screen context creation fails in C++, but only when statically linked
[英]Context creation fails
我正在學習OpenGL,所以我正在嘗試繪制2D圖像。 我首先將所有內容都放在我的C ++程序的主要功能中。 它運行良好(繪制了2個三角形)。
我決定通過制作單獨的課程來使程序更清晰......但現在它已不再適用了。 當我想制作OpenGL上下文時,它失敗了。 當我顯示錯誤時,我得到:
Failed creating OpenGL context at version requested
在我正在閱讀的教程中,他們說這個錯誤很可能是因為您的顯卡不支持OpenGL版本,但如果是這種情況,那么當我把所有代碼放入時,它就不起作用了。主要程序。
這是它出錯的部分(上下文創建):
bool OpenGL_Scene::initializeWindow() {
// Initialize the SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
cout << "Error while initializing the SDL : " << SDL_GetError() << endl;
SDL_Quit();
return false;
}
// Configure OpenGL
// Use version 3.1
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); // OpenGL 3.x
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); // OpenGL x.1 --> OpenGL 3.1
// Double buffering
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // Use double buffering (0 to not use it)
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); // Buffer has a depth of 24 bits
// Make the window
this->window = SDL_CreateWindow(this->windowTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, this->windowWidth, this->windowHeight, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); // SDL_WINDOW_OPENGL necessary to specify that the window will have an OpenGL context attached to it.
if(this->window == 0) // Initialization failed
{
cout << "Error while creating the window : " << SDL_GetError() << endl;
SDL_Quit();
return false;
}
// Make the OpenGL context given the SDL window
this->OpenGL_Context = SDL_GL_CreateContext(this->window);
// Make sure the creation of the context succeeded. If not the problem is probably that the version of OpenGL isn't supported by the graphics card.
if(this->OpenGL_Context == 0)
{
cout << "Could not create the OpenGL context : " << SDL_GetError() << endl;
SDL_DestroyWindow(window);
SDL_Quit();
return false;
}
return true;
};
在最后幾行中顯示錯誤,因此程序導致:
Could not create the OpenGL context : Failed creating OpenGL context at version requested
我搜索了很多尋找解決方案並發現了這個: SO主題
所以在上面的代碼我試過:
// Configure OpenGL
// Use version 3.1
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); // OpenGL 3.x
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); // OpenGL x.1 --> OpenGL 3.1
// Double buffering
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); // ADDING THIS AVOIDS THAT THE CONTEXT COULD NOT BE CREATED BUT THEN WHEN WE DRAW SOMETHING WE DON'T SEE ANYTHING
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // Use double buffering (0 to not use it)
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); // Buffer has a depth of 24 bits
現在,當我使用這個額外的行運行程序時, 創建了上下文 (沒有錯誤,所以OpenGL_Context != 0
) 但沒有繪制任何內容 。 我省略了我正在繪制的代碼部分,因為它以前工作過,我沒有改變任何東西。
有人知道可能是什么問題嗎?
PS:我正在開發Macbook Pro(OS X Yosemite(10.10.4)),我的顯卡是NVIDIA GeForce GT 650M 1024 MB
編輯 :我嘗試調試繪制三角形的代碼但我真的沒有看到錯誤(主要是因為我真的是OpenGL的新手)。 在下面你可以找到繪圖代碼。 請注意,我不再初始化GLEW,因為它沒有必要(根據評論)。
void OpenGL_Scene::mainLoop() {
bool end = false;
// Make vertices (punten) in a table
// !!! WARNING : Use 1 table for ALL vertices !!! Don't use a separate table for each of the forms, this would slow down the program because you have to send each of the tables to OpenGL !!!
float vertices[] = {-0.5, -0.5, 0.0, 0.5, 0.5, -0.5, // 3 Points for first triangle --> (-0.5, -0.5) , (0.0, 0.5) and (0.5, -0.5) (All in (x, y) --> 2D)
-0.8, -0.8, -0.3, -0.8, -0.8, -0.3}; // 3 Points for second triangle
// Before we start drawing, clear the screen
glClear(GL_COLOR_BUFFER_BIT);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(0); // Activate the table we passed to OpenGL using the identifier (index) passed to OpenGL (in this case 0)
// Now that OpenGL knows which vertices it has to display we are going to specify what it has to do with these vertices
glDrawArrays(GL_TRIANGLES, 0, 6); // Draw both triangles
glDisableVertexAttribArray(0); // Because it isn't necessary anymore
SDL_GL_SwapWindow(this->window); // Refresh the screen
while(!end) {
// Listen to events and play with them
SDL_WaitEvent(&this->events); // Will wait for an event and assign it to "events" variable
if(this->events.window.event == SDL_WINDOWEVENT_HIDDEN)
cout << "The user has hidden the window !" << endl;
else if(this->events.window.event == SDL_WINDOWEVENT_CLOSE) {
cout << "The user closed the window !" << endl;
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
SDL_GL_SwapWindow(this->window); // Refresh the window
end = true;
}
}
// Quitting the SDL and OpenGL properly is done by the destructor
}
所以現在當我執行項目時,首先我創建窗口(這是成功的),然后我調用mainLoop
過程(從上面)。 我使用調試器逐步繪制了繪圖代碼,調用了所有過程(glClear,...),但窗口中沒有任何內容(它保持黑色)。
我發現了錯誤。 我在初始化SDL后初始化OpenGL,這就是為什么在不強迫進入“核心配置文件”的情況下無法創建上下文的原因。
“initializeWindow”中的正確順序是:
// Configure OpenGL
// Use version 3.1
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); // OpenGL 3.x
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); // OpenGL x.1 --> OpenGL 3.1
// Double buffering
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // Use double buffering (0 to not use it)
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); // Buffer has a depth of 24 bits
// Initialize the SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
cout << "Error while initializing the SDL : " << SDL_GetError() << endl;
SDL_Quit();
return false;
}
這樣做可以成功創建上下文。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.