From 18a3472df7f11b01c2f187bf5f2b4e1218283658 Mon Sep 17 00:00:00 2001 From: PucklaMotzer09 Date: Fri, 28 Jun 2019 08:26:39 +0200 Subject: [PATCH] Added a hello world example to the OpenGL guide (#32670) * Added hello world example to OpenGL guide * Fixed command * Fixed command * Added command for building * Update index.md --- .../opengl/helloworld/index.md | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 guide/english/game-development/opengl/helloworld/index.md diff --git a/guide/english/game-development/opengl/helloworld/index.md b/guide/english/game-development/opengl/helloworld/index.md new file mode 100644 index 0000000000..d1f222de24 --- /dev/null +++ b/guide/english/game-development/opengl/helloworld/index.md @@ -0,0 +1,69 @@ +--- +title: Hello World +--- +# Hello World + +This is a Hello World application of OpenGL + +```C + #include + +GLFWwindow* window = NULL; + +int main(void) +{ + // Initialize GLFW + if (!glfwInit()) + return -1; + + // Create window with dimensions 640 x 480 + window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); + if (!window) + { + glfwTerminate(); + return -1; + } + + // Make the window to the current OpenGL context + glfwMakeContextCurrent(window); + + // Loop until the window gets closed + while (!glfwWindowShouldClose(window)) + { + // Clear Screen with default color black + glClear(GL_COLOR_BUFFER_BIT); + + // Render triangle + glBegin(GL_TRIANGLES); + // Left Down + glColor3f(1.0f,0.0,0.0f); + glVertex2f(-0.5f,-0.5f); + // Right Down + glColor3f(0.0f,1.0,0.0f); + glVertex2f( 0.5f,-0.5f); + // Mid Up + glColor3f(0.0f,0.0f,1.0f); + glVertex2f( 0.0f, 0.5f); + glEnd(); + + // Swap Buffers to render the new frame to the window + glfwSwapBuffers(window); + + // Poll events to get the window close event + glfwPollEvents(); + } + + // Terminate GLFW + glfwTerminate(); + return 0; +} +``` + +This code renders a triangle to a black window. To build this example you need the library [GLFW](https://www.glfw.org).
+``` +sudo apt-get install libglfw3-dev +``` +To build it just run
+``` +gcc -o hello_world source.c -lglfw -lGL +```