繁体   English   中英

glfw openGL c ++窗口背景和标题

[英]glfw openGL c++ window background and title

这是我从一系列关于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)的研究中,我没有遇到过类似的问题,有人可以解释这里有什么问题吗?

在ATI FirePro V5700上,我得到相同的行为(甚至在IDE外部运行发行版exe)。 如果您真的不满意 ,请下载GLFW源,并更改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" );

我没有遇到您描述的任何问题。 我复制并粘贴,编译,然后在发布代码时运行您的代码(注释掉对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()函数进行刷新。

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

看起来好像与IDE有关,如果您运行实际的.exe,则它会按预期运行,并且没有任何延迟。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM