【问题标题】:Receiving error message when testing GLUT in VSCode on Windows 10在 Windows 10 上的 VSCode 中测试 GLUT 时收到错误消息
【发布时间】:2020-03-29 16:08:00
【问题描述】:

你好 Stack Overflow 社区,

过去一周我一直在尝试解决这个问题,但没有成功。我搜索了互联网以解决问题,但无济于事。我正在尝试使用以下代码在 C 中创建一个简单的 GLUT Open GL 应用程序:

// This is a very basic Windows C application for testing GLUT (and compatible implementations such as freeglut).
#include <GL/glut.h>

/* Main method */
int main(int argc, char **argv)
{
  // Initialize GLUT functionality
  glutInit(&argc, argv);

  // Create a single window
  glutCreateWindow("GLUT Test");

  // Run the GLUT event loop
  glutMainLoop();

  return 0;
}

我按照以下教程进行操作,一切都成功构建:

https://www.transmissionzero.co.uk/computing/using-glut-with-mingw/

不幸的是,我在尝试运行程序时不断收到以下错误消息:

freeglut (C:\users\jason\Google Drive\code\c\test\example.exe): fgPlatformInitialize: CreateDC failed, Screen size info may be incorrect
This is quite likely caused by a bad '-display' parameter

我尝试了很多方法来解决这个问题,从尝试直接在 fgPlatformInitialize 中设置显示值,到在 Windows 本身中设置 DISPLAY 环境变量,以及许多其他解决方案。没有任何东西可以修复该错误消息。

作为参考,我使用的是 Visual Studio Code、FreeGLUT 和 Windows 10。我是否应该将所有东西都安装在我的 Arch Linux 机器上? :)

请帮忙,谢谢!

【问题讨论】:

    标签: c windows visual-studio-code glut freeglut


    【解决方案1】:

    对于其他遇到此问题的人,终于弄清楚并回答了我自己的问题。

    找到了一个名为 OpenGL Programming Guide 的优秀网站。

    这是学习 OpenGL 的官方指南(尽管是 1.1 版),但很棒的是所有示例都是专门用 C 语言编写的。

    网址:https://www.glprogramming.com/red/index.html

    首先,您需要声明 OpenGL 和 OpenGL 实用程序库:

    #include <GL/gl.h>
    #include <GL/glut.h>
    

    然后创建一个显示函数(可以命名任何东西,但这是约定):

    {
      glClear(GL_COLOR_BUFFER_BIT); // Clear all pixels from buffer
    
      glColor3f(1, 1, 1); // set the color palette to be white
    
      glBegin(GL_POLYGON);           // Create a polygonal object (a while box in this case)
      glVertex3f(0.25f, 0.25f, 0.0); // This means the polygon's "corners" as defined by these commands
      glVertex3f(0.75f, 0.25f, 0.0); // This means the polygon's "corners" as defined by these commands
      glVertex3f(0.75f, 0.75f, 0.0); // This means the polygon's "corners" as defined by these commands
      glVertex3f(0.25f, 0.75f, 0.0); // This means the polygon's "corners" as defined by these commands
      glEnd();
    
      glFlush(); // Start processing buffered OpenGL routines
    }
    

    然后创建一个初始化函数来连接 OpenGL 视图(同样可以命名任何东西):

    void init(void)
    {
      glClearColor(0.0, 0.0, 0.0, 0.0); // Set background color to black (last param is opacity, but doesn't seem to work)
    
      // Initialize viewing values
      glMatrixMode(GL_PROJECTION);            // Look this guy up
      glLoadIdentity();                       // Still have no idea what this means
      glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); // Specifies the coordinate system OpenGL assumes as it draws the final image and how the image gets mapped to the screen
    }
    

    最后,只需在主 c 函数中调用所有必要的函数,瞧!

    // Declare initial window size and display mode (single buffer and RGBA)
    // Open window with "hello" in its title bar
    // Call initialization routines
    // Register callback function to display graphics
    // Enter main loop and process events
    int main(int argc, char **argv)
    {
      glutInit(&argc, argv); // Initialize the GLUT OpenGL Utility Toolkit framework
    
      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Display mode which is RGB (Red Green Blue) type, single buffer as we're not switching screen buffers
    
      glutInitWindowSize(250, 250); // Declares window size in width and height
    
      glutCreateWindow("Hello"); // Set the name for the window box
    
      init(); // call the init function defined previously
    
      glutDisplayFunc(display); // call the glut display function, passing in your display function
    
      glutMainLoop(); // Enter main loop and process events
    
      return 0; // ANSI C standard requires main to return int
    }
    

    我希望这可以帮助其他可能遇到此问题的人!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 2020-07-08
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多