对于其他遇到此问题的人,终于弄清楚并回答了我自己的问题。
找到了一个名为 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
}
我希望这可以帮助其他可能遇到此问题的人!