glfw openglc++窗口背景和标题

glfw openGL c++ window background and title

本文关键字:标题 背景 窗口 openglc++ glfw      更新时间:2023-10-16

这是我的一系列教程的源代码,我正在看一下关于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 GLFWn" );
        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 windown" );
        glfwTerminate();
        return -1;
    }
    else
    {
        glfwSetWindowTitle( "Tutorial 01" );
    }
    // Initialize GLEW
    glewExperimental=true; // Needed in core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEWn");
        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窗口",但在1-2秒后,标题变成了教程01,因为它应该在第一时间,背景变成了黑色,因为它应该也。

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

我在ATI FirePro V5700上得到相同的行为(甚至在IDE之外运行发布exe)。如果您真的对此感到困扰,请下载GLFW源代码并更改carbon_window.c的第764行、win32_window.c的第1210行和x11_window.c的第962行。

lib 碳 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" );

我没有遇到你描述的任何问题。我复制和粘贴,编译,然后运行你的代码,因为你张贴它(注释掉引用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)。

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

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

这似乎与IDE有关,如果你运行实际的。exe,它会按预期工作,没有任何延迟。