在 Windows 10 上 VSCode 中测试 GLUT 时收到错误消息

Receiving error message when testing GLUT in VSCode on Windows 10

你好 Stack Overflow 社区,

过去一周我一直在尝试解决这个问题,但没有成功。我已经搜索了 Internet 来解决这个问题,但无济于事。我正在尝试使用以下代码在 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 代码、FreeGLUT 和 Windows 10。我应该把所有东西都安装在我的 Arch Linux 盒子上吗? :)

请帮忙,谢谢!

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

找到了一个名为 OpenGL 编程指南的优秀网站。

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

URL: 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
}

我希望这能帮助其他可能被困在这个问题上的人!