반응형










설치


$ sudo apt-get install freeglut3-dev




예제 코드 컴파일


$ gcc test.c -lglut -lGL -lGLU




실행결과





visual studio code에서 컴파일 및 실행하기


visual studio code 설치는 다음 글을 참고하세요..

[리눅스/개발환경] - Ubuntu Linux에 Visual Studio Code 설치해서 C/C++컴파일 해보기


설치 완료 후, tasks.json파일에서 아래 빨간색으로 된 부분만 수정하시면 됩니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
   "version": "0.1.0",
   "command": "bash",
   "isShellCommand": true,
   "args": ["-c"],
   "tasks": [
       {
           "taskName": "saveNcompile",
           "suppressTaskName": true,
           "isBuildCommand": true,
           "args": ["gcc main.c -lglut -lGL -lGLU -o main"]
       }, {
           "taskName": "execute",
           "suppressTaskName": true,
           "isTestCommand": true,
           "args": ["gnome-terminal -e \"bash -ic  ./main;bash \""]
       }
   ]
}
cs





예제코드


출처 - http://www.lighthouse3d.com/tutorials/glut-tutorial/animation/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "GL/freeglut.h"
 
 
void changeSize(int w, int h) {
 
    // Prevent a divide by zero, when window is too short
    // (you cant make a window of zero width).
    if (h == 0)
        h = 1;
 
    float ratio = w * 1.0 / h;
 
    // Use the Projection Matrix
    glMatrixMode(GL_PROJECTION);
 
    // Reset Matrix
    glLoadIdentity();
 
    // Set the viewport to be the entire window
    glViewport(00, w, h);
 
    // Set the correct perspective.
    gluPerspective(45.0f, ratio, 0.1f, 100.0f);
 
    // Get Back to the Modelview
    glMatrixMode(GL_MODELVIEW);
}
 
float angle = 0.0f;
 
void renderScene(void) {
 
    // Clear Color and Depth Buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
    // Reset transformations
    glLoadIdentity();
    // Set the camera
    gluLookAt(0.0f, 0.0f, 10.0f,
        0.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f);
 
    glRotatef(angle, 0.0f, 1.0f, 0.0f);
 
    glBegin(GL_TRIANGLES);
    glVertex3f(-2.0f, -2.0f, 0.0f);
    glVertex3f(2.0f, 0.0f, 0.0);
    glVertex3f(0.0f, 2.0f, 0.0);
    glEnd();
 
    angle += 0.1f;
 
    glutSwapBuffers();
}
 
int main(int argc, char **argv) {
 
    // init GLUT and create window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100100);
    glutInitWindowSize(320320);
    glutCreateWindow("Lighthouse3D- GLUT Tutorial");
 
    // register callbacks
    glutDisplayFunc(renderScene);
    glutReshapeFunc(changeSize);
    glutIdleFunc(renderScene);
 
    // enter GLUT event processing cycle
    glutMainLoop();
 
    return 1;
}
 
cs


반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts