How to install openGL
openGL in Ubuntu, graphics library and API used for creating and manipulating 2D and 3D graphics.
- Install compiler, GCC(GNU Compiler Collection)
sudo apt-get install g++
- Install OpenGL library and developement header file
sudo apt-get install libgl1-mesa-dev
-
Install Visual studio code
-
Install C/C++ extension
-
Generate project and move to project file
-
Generate project file ‘main.cpp’
-
example code
- generate OpenGL window and display triangle
#include <GL/glut.h>
// Display callback function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
glBegin(GL_TRIANGLES); // Begin drawing triangles
glColor3f(1.0f, 0.0f, 0.0f); // Set color to red
glVertex2f(0.0f, 1.0f); // Vertex 1 (top)
glColor3f(0.0f, 1.0f, 0.0f); // Set color to green
glVertex2f(-1.0f, -1.0f); // Vertex 2 (bottom-left)
glColor3f(0.0f, 0.0f, 1.0f); // Set color to blue
glVertex2f(1.0f, -1.0f); // Vertex 3 (bottom-right)
glEnd(); // End drawing triangles
glFlush(); // Flush the OpenGL pipeline
}
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("Simple OpenGL Example"); // Create a window
glutDisplayFunc(display); // Set the display callback function
glutMainLoop(); // Enter the main loop
return 0;
}
- Build
- main.cpp: C++ 소스 파일 이름
- -o main: 빌드된 실행 파일 이름을 “main”으로 지정합니다.
- -lGL -lGLU -lglut: OpenGL 관련 라이브러리를 링크합니다.
g++ -o main main.cpp -lGL -lGLU -lglut
- Execute
./main
- Result