OpenGL容器运行

参考

容器启动

1
2
3
4
5
6
7
nvidia-docker run -ti  
--name liqiang_test
-v /etc/localtime:/etc/localtime:ro
--net=host
-e DISPLAY=:10.0
-v $HOME/.Xauthority:/root/.Xauthority
cuda:8.0 bash

安装OpenGL

1
2
3
4
5
6
7
apt-get install -y 
build-essential
libgl1-mesa-dev libglu1-mesa-dev
freeglut3-dev
libglew1.10 libglew-dev libgl1-mesa-glx libxmu-dev
libglew-dev libsdl2-dev libsdl2-image-dev libglm-dev libfreetype6-dev
mesa-utils

代码

Light

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

此程序利用GLUT绘制一个OpenGL窗口,并显示一个加以光照的球。
*/
/* 由于头文件glut.h中已经包含了头文件gl.h和glu.h,所以只需要include 此文件*/

# include <stdlib.h>

/* 初始化材料属性、光源属性、光照模型,打开深度缓冲区 */
void ( void )
{
GLfloat mat_specular [ ] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess [ ] = { 50.0 };
GLfloat light_position [ ] = { 1.0, 1.0, 1.0, 0.0 };
glClearColor ( 0.0, 0.0, 0.0, 0.0 );
glShadeModel ( GL_SMOOTH );
glMaterialfv ( GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv ( GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv ( GL_LIGHT0, GL_POSITION, light_position);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glEnable (GL_DEPTH_TEST);
}
/*调用GLUT函数,绘制一个球*/
void display ( void )
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere (1.0, 40, 50);
glFlush ();
}

int main(int argc, char** argv)
{
/* GLUT环境初始化*/
glutInit (&argc, argv);
/* 显示模式初始化 */
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
/* 定义窗口大小 */
glutInitWindowSize (300, 300);
/* 定义窗口位置 */
glutInitWindowPosition (100, 100);
/* 显示窗口,窗口标题为执行函数名 */
glutCreateWindow ( argv [ 0 ] );
/* 调用OpenGL初始化函数 */
init();
/* 注册OpenGL绘图函数 */
glutDisplayFunc ( display );
// /* 进入GLUT消息循环,开始执行程序 */
glutMainLoop( );
return 0;
}

gcc light.c -o light -lGL -lglut

Quad

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// -- Written in C -- //

#include<stdio.h>
#include<stdlib.h>
#include<X11/X.h>
#include<X11/Xlib.h>
#include<GL/gl.h>
#include<GL/glx.h>
#include<GL/glu.h>

Display *dpy;
Window root;
GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
Window win;
GLXContext glc;
XWindowAttributes gwa;
XEvent xev;

void DrawAQuad() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1., 1., -1., 1., 1., 20.);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.);

glBegin(GL_QUADS);
glColor3f(1., 0., 0.); glVertex3f(-.75, -.75, 0.);
glColor3f(0., 1., 0.); glVertex3f( .75, -.75, 0.);
glColor3f(0., 0., 1.); glVertex3f( .75, .75, 0.);
glColor3f(1., 1., 0.); glVertex3f(-.75, .75, 0.);
glEnd();
}

int main(int argc, char *argv[]) {

dpy = XOpenDisplay(NULL);

if(dpy == NULL) {
printf("ntcannot connect to X servernn");
exit(0);
}

root = DefaultRootWindow(dpy);

vi = glXChooseVisual(dpy, 0, att);

if(vi == NULL) {
printf("ntno appropriate visual foundnn");
exit(0);
}
else {
printf("ntvisual %p selectedn", (void *)vi->visualid); /* %p creates hexadecimal output like in glxinfo */
}


cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);

swa.colormap = cmap;
swa.event_mask = ExposureMask | KeyPressMask;

win = XCreateWindow(dpy, root, 0, 0, 600, 600, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);

XMapWindow(dpy, win);
XStoreName(dpy, win, "VERY SIMPLE APPLICATION");

glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
glXMakeCurrent(dpy, win, glc);

glEnable(GL_DEPTH_TEST);

while(1) {
XNextEvent(dpy, &xev);

if(xev.type == Expose) {
XGetWindowAttributes(dpy, win, &gwa);
glViewport(0, 0, gwa.width, gwa.height);
DrawAQuad();
glXSwapBuffers(dpy, win);
}

else if(xev.type == KeyPress) {
glXMakeCurrent(dpy, None, NULL);
glXDestroyContext(dpy, glc);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
exit(0);
}
} /* this closes while(1) { */
} /* this is the } which closes int main(int argc, char *argv[]) { */

gcc -o quad quad.c -lX11 -lGL -lGLU

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
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
void init();
void display();
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(300, 300);
glutCreateWindow("OpenGL 3D View");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glOrtho(-5, 5, -5, 5, 5, 15);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0, 0);
glutWireTeapot(3);
glFlush();
}

gcc example.c -o example.out -lGL -lGLU -lglut