Hello Stack溢出社区,
过去一周,我一直在努力解决这个问题,但没有成功。我搜寻了互联网来解决此问题,但无济于事。我正在尝试使用以下代码在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和Windows10。是否应该将所有内容都安装在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
}
最后,只需在main 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
}
我希望对其他对此有帮助的人有所帮助!