-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopengles-imx6.c
289 lines (224 loc) · 7.43 KB
/
opengles-imx6.c
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <wait.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <signal.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <EGL/egl.h>
#include <EGL/eglvivante.h>
#ifdef INITROOT_STARTUP
#include <initroot_startup.h>
#endif
#ifdef DrawTriangle
// 加载着色器函数
GLuint LoadShader(GLenum type, const char *shaderSrc)
{
GLuint shader;
GLint compiled;
// 创建着色器项目
shader = glCreateShader(type);
//
if(shader == 0) return 0;
// 制定加载的着色器
glShaderSource(shader, 1, &shaderSrc, NULL);
// 编译着色器
glCompileShader(shader);
// 检查编译结果
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char *infoLog = malloc (sizeof ( char ) * infoLen);
glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
printf("Error compiling shader:\n%s\n", infoLog);
free(infoLog);
}
glDeleteShader(shader);
return 0;
}
// 成功返回着色器句柄
return shader;
}
int init(GLuint * programID)
{
// 顶点着色器
char vShaderStr[] =
"#version 100 \n"
"attribute vec4 vPosition; \n"
"void main() \n"
"{ \n"
" gl_Position = vPosition; \n"
"} \n";
// 片元着色器
char fShaderStr[] =
"#version 100 \n"
"precision mediump float; \n"
"void main() \n"
"{ \n"
" gl_FragColor = vec4 ( 0.0, 1.0, 0.0, 1.0 ); \n"
"} \n";
GLuint vertexShader;
GLuint fragmentShader;
GLuint programObject;
GLint linked;
// 载入顶点/片元着色器,并得到返回句柄
vertexShader = LoadShader(GL_VERTEX_SHADER, vShaderStr);
fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fShaderStr);
// 创建项目,获得项目句柄
programObject = glCreateProgram();
if (programObject == 0) return 0;
// 为项目指定顶点/片元着色器
glAttachShader(programObject, vertexShader);
glAttachShader(programObject, fragmentShader);
// 链接
glLinkProgram(programObject);
// 获得链接结果
glGetProgramiv ( programObject, GL_LINK_STATUS, &linked );
// 判断链接状态
if (!linked) {
GLint infoLen = 0;
glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char *infoLog = malloc (sizeof ( char ) * infoLen);
glGetProgramInfoLog(programObject, infoLen, NULL, infoLog);
printf("Error linking program:\n%s\n", infoLog);
free(infoLog);
}
glDeleteProgram(programObject);
return -1;
}
// 把项目句柄传出
*programID = programObject;
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
return 0;
}
void drawTriangle(int display_width, int display_height, GLuint programID)
{
// 顶点着色器数据
GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
};
// 设置视窗
glViewport(0, 0, (GLsizei)display_width, (GLsizei)display_height);
// 指定项目
glUseProgram(programID);
// 获得顶点着色器的句柄
GLuint positionIndex = glGetAttribLocation(programID, "vPosition");
// 将顶点数据关联到一个顶点属性数组,
glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
// 激活顶点着色器
glEnableVertexAttribArray(positionIndex);
// 绘制顶点(轮廓)
glDrawArrays(GL_TRIANGLES,0, 3);
}
#endif
struct egl_device {
EGLNativeDisplayType display_type;
EGLDisplay display;
const EGLint *config_attributes;
EGLConfig config;
EGLNativeWindowType window;
EGLSurface surface;
const EGLint *context_attributes;
EGLContext context;
};
int egl_initialize(struct egl_device *device)
{
device->display_type = (EGLNativeDisplayType)fbGetDisplayByIndex(0);
device->display = eglGetDisplay(device->display_type);
eglInitialize(device->display, NULL, NULL);
eglBindAPI(EGL_OPENGL_ES_API);
static const EGLint config_attributes[] = {
EGL_SAMPLES, 0,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, EGL_DONT_CARE,
EGL_DEPTH_SIZE, 0,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_NONE
};
EGLint config_count = 0;
eglChooseConfig(device->display, config_attributes, &device->config, 1, &config_count);
device->config_attributes = config_attributes;
device->window = fbCreateWindow(device->display_type, 0, 0, 0, 0);
device->surface = eglCreateWindowSurface(device->display, device->config, device->window, NULL);
static const EGLint context_attributes[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
device->context = eglCreateContext(device->display, device->config, EGL_NO_CONTEXT, context_attributes);
device->context_attributes = context_attributes;
eglMakeCurrent(device->display, device->surface, device->surface, device->context);
return 0;
}
int egl_deinitialize(struct egl_device *device)
{
eglMakeCurrent(device->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroyContext(device->display, device->context);
device->context = (EGLContext)0;
eglDestroySurface(device->display, device->surface);
device->surface = (EGLSurface)0;
fbDestroyWindow(device->window);
device->window = (EGLNativeWindowType)0;
eglTerminate(device->display);
device->display = (EGLDisplay)0;
eglReleaseThread();
return 0;
}
int server_main(int argc, char *argv[])
{
int display_width;
int display_height;
GLuint programID;
struct egl_device device
= { 0 };
egl_initialize(&device);
fbGetDisplayGeometry(device.display_type, &display_width, &display_height);
glClearColor(255.0f / 255.0f, 0.0 / 255.0f, 0.0 / 255.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
#ifdef DrawTriangle
int ret = init(&programID);
drawTriangle(display_width, display_height, programID);
#endif
eglSwapBuffers(device.display, device.surface);
egl_deinitialize(&device);
// don't return, just loop
for (;;);
return 0;
}
#ifdef INITROOT_STARTUP
int startup_pre_end(void) { return 0; }
#endif
int main(int argc, char *argv[])
{
pid_t pid = -1;
unsigned continue_loop = 1;
#ifdef INITROOT_STARTUP
startup_begin();
startup_cend(startup_pre_end);
#endif
do {
if (0 == (pid = fork())) {
return server_main(argc, argv);
}
else if (pid > 0) {
int status;
wait(&status);
fprintf(stdout, "server_main exit with status: %d\n", status);
}
else {
fprintf(stdout, "main fork server fail: %d\n", pid);
break;
}
} while (continue_loop);
#ifndef INITROOT_STARTUP
return 0;
#else
return startup_end(0);
#endif
}