【问题标题】:glfw openGL c++ window background and titleglfw openGL c++ 窗口背景和标题
【发布时间】:2013-01-09 20:56:15
【问题描述】:

这是我正在查看有关 opengl 3+ 的一系列教程的源代码。

//#include <stdio.h>
//#include <stdlib.h>

#include <GL/glew.h>
#include <GL/glfw.h>
#include <glm/glm.hpp>
using namespace glm;

#include <iostream>
using namespace std;



int main()
{

    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        return -1;
    }

    glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4); // 4x antialiasing
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); // We want OpenGL 3.3
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
    glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL

    // Open a window and create its OpenGL context
    if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
    {
        fprintf( stderr, "Failed to open GLFW window\n" );
        glfwTerminate();
        return -1;
    }
    else
    {
        glfwSetWindowTitle( "Tutorial 01" );
    }

    // Initialize GLEW
    glewExperimental=true; // Needed in core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }



    glfwEnable( GLFW_STICKY_KEYS );

    do{
        // Draw nothing, see you in tutorial 2 !

        // Swap buffers
        glfwSwapBuffers();

    } // Check if the ESC key was pressed or the window was closed
    while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
    glfwGetWindowParam( GLFW_OPENED ) );

    return 0;
}

一切都很好,除了当窗口初始化时,它的背景颜色为白色,标题为“GLFW WINDOW”,但在 1-2 秒后,标题更改为 Tutorial 01,因为它本来应该放在首位,并且背景变成黑色,应该也是这样。

在我几年前做过的关于 opengl (2.x) 的过剩研究中,我没有遇到过这样的问题,有人能解释一下这里出了什么问题吗?

【问题讨论】:

    标签: c++ opengl glew glfw


    【解决方案1】:

    我在 ATI FirePro V5700 上得到了相同的行为(甚至在 IDE 之外运行发行版 exe)。如果你真的被它困扰,download the GLFW source 并更改 carbon_window.c 的第 764 行、win32_window.c 的第 1210 行和 x11_window.c 的第 962 行。

    .\lib\carbon\carbon_window.c

    (void)SetWindowTitleWithCFString( _glfwWin.window, CFSTR( "GLFW Window" ) );
    

    .\lib\win32\win32_window.c

    _glfwWin.window = CreateWindowEx( _glfwWin.dwExStyle,    // Extended style
                                      _GLFW_WNDCLASSNAME,    // Class name
                                      "GLFW Window",         // Window title
                                      _glfwWin.dwStyle,      // Defined window style
                                      wa.left, wa.top,       // Window position
                                      fullWidth,             // Decorated window width
                                      fullHeight,            // Decorated window height
                                      NULL,                  // No parent window
                                      NULL,                  // No menu
                                      _glfwLibrary.instance, // Instance
                                      NULL );                // Nothing to WM_CREATE
    

    .\lib\x11\x11_window.c

    _glfwPlatformSetWindowTitle( "GLFW Window" );
    

    【讨论】:

      【解决方案2】:

      我没有遇到您描述的任何问题。我复制并粘贴、编译,然后在您发布代码时运行您的代码(注释掉对 GLM 的引用,因为我没有安装该库)。对我来说,标题会立即改变——我什至从未看到标题为“GLFW WINDOW”的窗口,并且图形区域的颜色立即变为黑色。会不会是你的电脑不够快?

      如果您执行以下操作会发生什么?

      do{
          // Draw nothing, see you in tutorial 2 !
          glClear(GL_COLOR_BUFFER_BIT);
          // Swap buffers
          glfwSwapBuffers();
          glfwSleep(0.016);
      } // Check if the ESC key was pressed or the window was closed
      while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS && glfwGetWindowParam( GLFW_OPENED ) );
      

      编辑:我的 GPU 是 Nvidia GTX 580(至少支持 OpenGL 4.3)。

      【讨论】:

      • 一样的没区别 忘了说我用的是mv c++ studio 2012 express,可能跟这个有关系吧?
      • 您似乎正在学习本教程:opengl-tutorial.org/beginners-tutorials/…。我自己遵循了该教程,但在 Linux 上,从未遇到过您描述的任何问题。这很可能不是代码问题,但可能是您的系统(硬件或操作系统)或您正在使用的软件(IDE?!或编译器)的问题。在我看来,这很难说。 :-/ 由于它会在一段时间后自行纠正,可能只是您的 CPU/GPU 需要一段时间才能使用该代码进行设置。
      • 我遇到了透明背景渲染的问题。 glClear 帮我修复了它!谢谢。
      【解决方案3】:

      窗口的背景色通过调用openGL函数glClearColor()配置,并通过glClear()函数刷新。

      窗口标题更改的延迟可能与创建 openGL 3.x 需要创建标准 OpenGL 上下文(版本 1.x 或 2.x)有关然后让您的应用程序选择加入使用 OpenGL 3.x 上下文。 1-2 秒的延迟似乎很多。

      【讨论】:

      • GLFW_WINDOW 表示“创建一个窗口”,而不是GLFW_FULLSCREEN。它本身并没有真正设置窗口标题。else 块不会创建一个新窗口,它只是设置当前窗口的窗口标题。
      • 谢谢,我应该更加小心。
      • 是的,Victor 是正确的,但我无法理解延迟可能是因为我使用的是 microsoft visual studio 2012 express 吗?
      【解决方案4】:

      它似乎与 IDE 有关,如果您运行实际的 .exe,它会按预期工作并且完全没有延迟。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-04-25
        • 2018-05-04
        • 1970-01-01
        • 2019-06-11
        • 2014-10-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多