-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEasyGL.cpp
More file actions
5188 lines (4558 loc) · 166 KB
/
Copy pathEasyGL.cpp
File metadata and controls
5188 lines (4558 loc) · 166 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 程序版本 REV 0.9
// 包含头文件
#include "EasyGL.h"
// Shader
#include "ShaderSrc.h"
// 角度转弧度
float EasyGL_DegToRad(float in_deg)
{
float deg = in_deg;
float re = (deg * 2.0f * M_PI) / 360.0f;
return (float)re;
}
//---------------------------------------------------------------------
// 纯几何骨架填充着色器
// 绘制数据为纯x y z的点信息
// 提供平移、三维旋转、缩放、骨骼颜色API
// 获取顶点着色器源码
const GLchar* CEasyGL_OnlyGeometryShader::GetVertexShaderSrc(void)
{
return CEasyGL_OnlyGeometryShader_VertexShader_c;
}
// 片段着色器源码
const GLchar* CEasyGL_OnlyGeometryShader::GetFragmentShaderSrc(void)
{
return CEasyGL_OnlyGeometryShader_FragmentShader_c;
}
// 构造函数
CEasyGL_OnlyGeometryShader::CEasyGL_OnlyGeometryShader()
{
// 初始化
this->ShaderHandle = 0;
this->pvertex = 0;
this->vertex_len = 0;
this->move_x = 0.0f;
this->move_y = 0.0f;
this->move_z = 0.0f;
this->move_center_x = 0.0f;
this->move_center_y = 0.0f;
this->move_center_z = 0.0f;
this->rotate_rad_x = 0.0f;
this->rotate_rad_y = 0.0f;
this->rotate_rad_z = 0.0f;
this->scale_x = 1.0f;
this->scale_y = 1.0f;
this->scale_z = 1.0f;
this->blendcolor_r = 1.0f;
this->blendcolor_g = 1.0f;
this->blendcolor_b = 1.0f;
this->blendcolor_a = 1.0f;
this->shader_valid = false;
pthread_mutex_init(&this->vertex_mutex, NULL);
pthread_mutex_init(&this->move_mutex, NULL);
pthread_mutex_init(&this->move_center_mutex, NULL);
pthread_mutex_init(&this->rotate_mutex, NULL);
pthread_mutex_init(&this->scale_mutex, NULL);
pthread_mutex_init(&this->blendcolor_mutex, NULL);
}
// 析构函数
CEasyGL_OnlyGeometryShader::~CEasyGL_OnlyGeometryShader()
{
if(this->ShaderHandle != 0)
{
glDeleteShader(this->ShaderHandle);
this->ShaderHandle = 0;
}
pthread_mutex_destroy(&this->vertex_mutex);
pthread_mutex_destroy(&this->move_mutex);
pthread_mutex_destroy(&this->move_center_mutex);
pthread_mutex_destroy(&this->rotate_mutex);
pthread_mutex_destroy(&this->scale_mutex);
pthread_mutex_destroy(&this->blendcolor_mutex);
}
// 初始化着色器,含载入、编译、链接等
// 返回着色器程序句柄
GLint CEasyGL_OnlyGeometryShader::ShaderInit(void)
{
// 定义相关变量
enum Consts {INFOLOG_LEN = EASYGL_INFOLOG_LEN};
GLint re = 0;
GLchar infoLog[INFOLOG_LEN];
GLint fragment_shader;
GLint shader_program = 0;
GLint success;
GLint vertex_shader;
GLchar* psrc_v[1];
GLchar* psrc_f[1];
std::string filename;
GLenum format = 0;
// 当着色器已经初始化过
if(this->shader_valid)
{
//glDeleteShader(this->ShaderHandle);
//this->ShaderHandle = 0;
// 返回着色器初始化失败,因为已经初始化过了
return 0;
}
// 检查是否支持二进制的Shader
GLint formats = 0;
glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &formats);
// 当不支持二进制格式
if(formats < 1)
{
printf("Driver does not support any binary formats!!\r\n");
}
// 支持二进制格式
else
{
// 载入格式
filename = EASYGL_SHADER_BINARY_PATH + this->ClassName() + ".sdf";
FILE* pfile = fopen(filename.c_str(), "rt");
// 当不存在文件
if(pfile == 0)
{
// 此时程序向下执行,然后创建Shader相关位二进制文件
}
// 当存在文件
else
{
// 识别格式
fscanf(pfile, "%08X", &format);
fclose(pfile);
// 创建程序
shader_program = glCreateProgram();
// 载入二进制文件
filename = EASYGL_SHADER_BINARY_PATH + this->ClassName() + ".sdb";
std::ifstream inputStream(filename.c_str(),
std::ios::binary
);
std::istreambuf_iterator<char> startIt(inputStream), endIt;
std::vector<char> buffer(startIt, endIt);
inputStream.close();
// 安装
glProgramBinary(shader_program, format, buffer.data(), buffer.size());
// 检查是否载入成功
GLint status;
glGetProgramiv(shader_program, GL_LINK_STATUS, &status);
// 当载入失败
if(status == GL_FALSE)
{
// 释放程序
glDeleteShader(shader_program);
// 提示失败
printf("Load binary Shader Error!!\r\n");
}
// 当载入成功
else
{
// 调试信息
printf("Load %s format = %08X, Length = %ld\r\n", this->ClassName().c_str(), format, buffer.size());
// 更新内部变量
this->ShaderHandle = shader_program;
// 着色器可用
this->shader_valid = true;
// 返回程序
return shader_program;
}
}
}
/* Vertex shader */
printf("Compile:%s_VertexShader.c\r\n", this->ClassName().c_str());
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
psrc_v[0] = (GLchar*)(this->GetVertexShaderSrc());
glShaderSource(vertex_shader, 1, psrc_v, NULL);
glCompileShader(vertex_shader);
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertex_shader, INFOLOG_LEN, NULL, infoLog);
printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n%s\n", infoLog);
re = 0;
}
// 当顶点着色器编译成功
else
{
/* Fragment shader */
printf("Compile:%s_FragmentShader.c\r\n", this->ClassName().c_str());
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
psrc_f[0] = (GLchar*)(this->GetFragmentShaderSrc());
glShaderSource(fragment_shader, 1, psrc_f, NULL);
glCompileShader(fragment_shader);
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragment_shader, INFOLOG_LEN, NULL, infoLog);
printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n%s\n", infoLog);
re = 0;
}
// 当片段着色器编译成功
else
{
/* Link shaders */
shader_program = glCreateProgram();
glAttachShader(shader_program, vertex_shader);
glAttachShader(shader_program, fragment_shader);
glLinkProgram(shader_program);
glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(shader_program, INFOLOG_LEN, NULL, infoLog);
printf("ERROR::SHADER::PROGRAM::LINKING_FAILED\n%s\n", infoLog);
re = 0;
}
// 当连接成功
else
{
// 检查shader是否合法,当合法
if(shader_program != 0)
{
// 当支持二进制格式
if(!(formats < 1))
{
// 将结果保存成二进制格式
// 获取二进制长度
GLint length = 0;
glGetProgramiv(shader_program, GL_PROGRAM_BINARY_LENGTH, &length);
// 接收二进制文件数据
std::vector<GLubyte> buffer(length);
GLenum format = 0;
glGetProgramBinary(shader_program, length, NULL, &format, buffer.data());
// 写入数据到文件
filename = EASYGL_SHADER_BINARY_PATH + this->ClassName() + ".sdb";
std::ofstream out(filename.c_str(), std::ios::binary);
out.write(reinterpret_cast<char*>(buffer.data()), length);
out.close();
// 写入格式到文件
filename = EASYGL_SHADER_BINARY_PATH + this->ClassName() + ".sdf";
FILE* pfile = fopen(filename.c_str(), "wt");
fprintf(pfile, "%08X", format);
fclose(pfile);
// 调试信息
printf("Save %s format = %08X, Length = %d\r\n", this->ClassName().c_str(), format, length);
}
// 设置返回值
re = shader_program;
// 更新内部变量
this->ShaderHandle = shader_program;
// 着色器可用
this->shader_valid = true;
}
// 当非法
else
{
re = 0;
}
}
}
}
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
return re;
}
// 着色器是否可用
bool CEasyGL_OnlyGeometryShader::IsValid(void)
{
return this->shader_valid;
}
// 着色器渲染,由操作对象调用,不可用由框架直接调用
void CEasyGL_OnlyGeometryShader::ShaderRender(void)
{
// 检查着色器
if(!this->shader_valid) return;
// 应用着色器
glUseProgram(this->ShaderHandle);
// 设置平移参数
int id_vec3_move = glGetUniformLocation(this->ShaderHandle, "vec3_move");
pthread_mutex_lock(&this->move_mutex);
glUniform3f(id_vec3_move, this->move_x, this->move_y, this->move_z);
pthread_mutex_unlock(&this->move_mutex);
// 设置移动中心参数
int id_vec3_move_center = glGetUniformLocation(this->ShaderHandle, "vec3_move_center");
pthread_mutex_lock(&this->move_center_mutex);
glUniform3f(id_vec3_move_center, this->move_center_x, this->move_center_y, this->move_center_z);
pthread_mutex_unlock(&this->move_center_mutex);
// 设置旋转参数
pthread_mutex_lock(&this->rotate_mutex);
GLfloat mat3_rotate_x[3*3] =
{
1.0, 0.0f, 0.0f,
0.0f, cos(this->rotate_rad_x), -sin(this->rotate_rad_x),
0.0f, sin(this->rotate_rad_x), cos(this->rotate_rad_x)
};
GLfloat mat3_rotate_y[3*3] =
{
cos(this->rotate_rad_y), 0.0f, sin(this->rotate_rad_y),
0.0f, 1.0f, 0.0f,
-sin(this->rotate_rad_y), 0.0f, cos(this->rotate_rad_y)
};
GLfloat mat3_rotate_z[3*3] =
{
cos(this->rotate_rad_z), -sin(this->rotate_rad_z), 0.0f,
sin(this->rotate_rad_z), cos(this->rotate_rad_z), 0.0f,
0.0f, 0.0f, 1.0f
};
pthread_mutex_unlock(&this->rotate_mutex);
int id_mat3_rotate_x = glGetUniformLocation(this->ShaderHandle, "mat3_rotate_x");
int id_mat3_rotate_y = glGetUniformLocation(this->ShaderHandle, "mat3_rotate_y");
int id_mat3_rotate_z = glGetUniformLocation(this->ShaderHandle, "mat3_rotate_z");
glUniformMatrix3fv(id_mat3_rotate_x, 1, GL_FALSE, mat3_rotate_x);
glUniformMatrix3fv(id_mat3_rotate_y, 1, GL_FALSE, mat3_rotate_y);
glUniformMatrix3fv(id_mat3_rotate_z, 1, GL_FALSE, mat3_rotate_z);
// 设置缩放参数
pthread_mutex_lock(&this->scale_mutex);
GLfloat mat3_scale[3*3] =
{
this->scale_x, 0.0f, 0.0f,
0.0f, this->scale_y, 0.0f,
0.0f, 0.0f, this->scale_z
};
pthread_mutex_unlock(&this->scale_mutex);
int id_mat3_scale = glGetUniformLocation(this->ShaderHandle, "mat3_scale");
glUniformMatrix3fv(id_mat3_scale, 1, GL_FALSE, mat3_scale);
// 设置混合颜色
int id_color = glGetUniformLocation(this->ShaderHandle, "in_blend_color");
pthread_mutex_lock(&this->blendcolor_mutex);
glUniform4f(id_color,
this->blendcolor_r,
this->blendcolor_g,
this->blendcolor_b,
this->blendcolor_a
);
pthread_mutex_unlock(&this->blendcolor_mutex);
// 渲染操作
GLint id_vertex = glGetAttribLocation(this->ShaderHandle, "in_pos");
// 检查顶点信息可用
pthread_mutex_lock(&this->vertex_mutex);
if((this->pvertex != 0) &&
(this->vertex_len > 0)
)
{
// 设置顶点数据
glVertexAttribPointer(id_vertex,
3,
GL_FLOAT,
GL_FALSE,
3*sizeof(GLfloat),
this->pvertex
);
// 使能当前顶点数据
glEnableVertexAttribArray(id_vertex);
// 执行绘图
glDrawArrays(EASYGL_GEOMETRY_LINE_TYPE,
0,
this->vertex_len/3 // 这里的单位是点的数量
// 除以3表示3个float数据为1个点信息
);
}
pthread_mutex_unlock(&this->vertex_mutex);
}
// 设置顶点原始数据
void CEasyGL_OnlyGeometryShader::SetVertex(GLfloat* pv, // 顶点数据首地址
int len // 总长度,单位为float的个数
)
{
pthread_mutex_lock(&this->vertex_mutex);
this->pvertex = pv;
this->vertex_len = len;
pthread_mutex_unlock(&this->vertex_mutex);
}
// 可选的设置顶点的平面元素索引数据
void CEasyGL_OnlyGeometryShader::SetVertexElements(
GLuint* pdata, // 索引数据首地址
int len // 索引数据长度,以数值个数为单位
)
{
// 本类无该功能
}
// 指定纹理
void CEasyGL_OnlyGeometryShader::SetTexture2D(
GLint imageformat, // 纹理图片格式
int width, // 宽度
int height, // 高度
void* pdata // 纹理数据首地址
)
{
// 本类无该功能
}
// 平移
void CEasyGL_OnlyGeometryShader::Move(float x, float y, float z)
{
// 更新参数
pthread_mutex_lock(&this->move_mutex);
this->move_x = x;
this->move_y = y;
this->move_z = z;
pthread_mutex_unlock(&this->move_mutex);
}
// 移动物体的中心点
void CEasyGL_OnlyGeometryShader::MoveCenter(float x, float y, float z)
{
// 更新参数
pthread_mutex_lock(&this->move_center_mutex);
this->move_center_x = x;
this->move_center_y = y;
this->move_center_z = z;
pthread_mutex_unlock(&this->move_center_mutex);
}
// 以角度旋转
void CEasyGL_OnlyGeometryShader::RotateDeg(float x, float y, float z)
{
// 更新参数
pthread_mutex_lock(&this->rotate_mutex);
this->rotate_rad_x = EasyGL_DegToRad(x);
this->rotate_rad_y = EasyGL_DegToRad(y);
this->rotate_rad_z = EasyGL_DegToRad(z);
pthread_mutex_unlock(&this->rotate_mutex);
}
// 以弧度旋转
void CEasyGL_OnlyGeometryShader::RotateRad(float x, float y, float z)
{
// 更新参数
pthread_mutex_lock(&this->rotate_mutex);
this->rotate_rad_x = x;
this->rotate_rad_y = y;
this->rotate_rad_z = z;
pthread_mutex_unlock(&this->rotate_mutex);
}
// 缩放
void CEasyGL_OnlyGeometryShader::Scale(float x, float y, float z)
{
pthread_mutex_lock(&this->scale_mutex);
this->scale_x = x;
this->scale_y = y;
this->scale_z = z;
pthread_mutex_unlock(&this->scale_mutex);
}
// 设置混合颜色
void CEasyGL_OnlyGeometryShader::SetBlendColor(float r, float g, float b, float a)
{
pthread_mutex_lock(&this->blendcolor_mutex);
this->blendcolor_r = r;
this->blendcolor_g = g;
this->blendcolor_b = b;
this->blendcolor_a = a;
pthread_mutex_unlock(&this->blendcolor_mutex);
}
// 当前类的名字
std::string CEasyGL_OnlyGeometryShader::ClassName(void)
{
std::string re_str = "CEasyGL_OnlyGeometryShader";
return re_str;
}
//---------------------------------------------------------------------
// 纯几何骨架填充着色器,以元素进行绘图
// 绘制数据为纯x y z的点信息
// 提供平移、三维旋转、缩放、骨骼颜色API
// 构造函数
CEasyGL_OnlyGeometryElementShader::CEasyGL_OnlyGeometryElementShader()
{
this->p_vertex_element = 0;
this->vertex_element_len = 0;
}
// 析构函数
CEasyGL_OnlyGeometryElementShader::~CEasyGL_OnlyGeometryElementShader()
{
}
// 着色器渲染,由操作对象调用,不可用由框架直接调用
void CEasyGL_OnlyGeometryElementShader::ShaderRender(void)
{
// 检查着色器
if(!this->shader_valid) return;
// 应用着色器
glUseProgram(this->ShaderHandle);
// 设置平移参数
int id_vec3_move = glGetUniformLocation(this->ShaderHandle, "vec3_move");
pthread_mutex_lock(&this->move_mutex);
glUniform3f(id_vec3_move, this->move_x, this->move_y, this->move_z);
pthread_mutex_unlock(&this->move_mutex);
// 设置移动中心参数
int id_vec3_move_center = glGetUniformLocation(this->ShaderHandle, "vec3_move_center");
pthread_mutex_lock(&this->move_center_mutex);
glUniform3f(id_vec3_move_center, this->move_center_x, this->move_center_y, this->move_center_z);
pthread_mutex_unlock(&this->move_center_mutex);
// 设置旋转参数
pthread_mutex_lock(&this->rotate_mutex);
GLfloat mat3_rotate_x[3*3] =
{
1.0, 0.0f, 0.0f,
0.0f, cos(this->rotate_rad_x), -sin(this->rotate_rad_x),
0.0f, sin(this->rotate_rad_x), cos(this->rotate_rad_x)
};
GLfloat mat3_rotate_y[3*3] =
{
cos(this->rotate_rad_y), 0.0f, sin(this->rotate_rad_y),
0.0f, 1.0f, 0.0f,
-sin(this->rotate_rad_y), 0.0f, cos(this->rotate_rad_y)
};
GLfloat mat3_rotate_z[3*3] =
{
cos(this->rotate_rad_z), -sin(this->rotate_rad_z), 0.0f,
sin(this->rotate_rad_z), cos(this->rotate_rad_z), 0.0f,
0.0f, 0.0f, 1.0f
};
pthread_mutex_unlock(&this->rotate_mutex);
int id_mat3_rotate_x = glGetUniformLocation(this->ShaderHandle, "mat3_rotate_x");
int id_mat3_rotate_y = glGetUniformLocation(this->ShaderHandle, "mat3_rotate_y");
int id_mat3_rotate_z = glGetUniformLocation(this->ShaderHandle, "mat3_rotate_z");
glUniformMatrix3fv(id_mat3_rotate_x, 1, GL_FALSE, mat3_rotate_x);
glUniformMatrix3fv(id_mat3_rotate_y, 1, GL_FALSE, mat3_rotate_y);
glUniformMatrix3fv(id_mat3_rotate_z, 1, GL_FALSE, mat3_rotate_z);
// 设置缩放参数
pthread_mutex_lock(&this->scale_mutex);
GLfloat mat3_scale[3*3] =
{
this->scale_x, 0.0f, 0.0f,
0.0f, this->scale_y, 0.0f,
0.0f, 0.0f, this->scale_z
};
pthread_mutex_unlock(&this->scale_mutex);
int id_mat3_scale = glGetUniformLocation(this->ShaderHandle, "mat3_scale");
glUniformMatrix3fv(id_mat3_scale, 1, GL_FALSE, mat3_scale);
// 设置混合颜色
int id_color = glGetUniformLocation(this->ShaderHandle, "in_blend_color");
pthread_mutex_lock(&this->blendcolor_mutex);
glUniform4f(id_color,
this->blendcolor_r,
this->blendcolor_g,
this->blendcolor_b,
this->blendcolor_a
);
pthread_mutex_unlock(&this->blendcolor_mutex);
// 渲染操作
GLint id_vertex = glGetAttribLocation(this->ShaderHandle, "in_pos");
// 检查顶点信息可用
pthread_mutex_lock(&this->vertex_mutex);
if((this->pvertex != 0) &&
(this->vertex_len > 0)
)
{
// 设置顶点数据
glVertexAttribPointer(id_vertex,
3,
GL_FLOAT,
GL_FALSE,
3*sizeof(GLfloat),
this->pvertex
);
// 使能顶点数据
glEnableVertexAttribArray(id_vertex);
// 执行绘图
glDrawElements(EASYGL_GEOMETRY_LINE_TYPE,
this->vertex_element_len,
GL_UNSIGNED_INT,
this->p_vertex_element
);
}
pthread_mutex_unlock(&this->vertex_mutex);
}
// 可选的设置顶点的平面元素索引数据
void CEasyGL_OnlyGeometryElementShader::SetVertexElements(
GLuint* pdata, // 索引数据首地址
int len // 索引数据长度,以数值个数为单位
)
{
pthread_mutex_lock(&this->vertex_mutex);
this->p_vertex_element = pdata;
this->vertex_element_len = len;
pthread_mutex_unlock(&this->vertex_mutex);
}
// 当前类的名字
std::string CEasyGL_OnlyGeometryElementShader::ClassName(void)
{
std::string re_str = "CEasyGL_OnlyGeometryElementShader";
return re_str;
}
//---------------------------------------------------------------------
// 无光照效果的3D纹理填充着色器
// 绘制数据为x y z的点信息 和 u v纹理坐标信息
// 提供平移、三维旋转、缩放、整体混合纯色的API
// 获取顶点着色器源码
const GLchar* CEasyGL_3DTextureShader::GetVertexShaderSrc(void)
{
return CEasyGL_3DTextureShader_VertexShader_c;
}
// 片段着色器源码
const GLchar* CEasyGL_3DTextureShader::GetFragmentShaderSrc(void)
{
return CEasyGL_3DTextureShader_FragmentShader_c;
}
// 构造函数
CEasyGL_3DTextureShader::CEasyGL_3DTextureShader()
{
this->TextureBuff.format = GL_RGBA;
this->TextureBuff.width = 0;
this->TextureBuff.height = 0;
this->TextureBuff.pdata = 0;
pthread_mutex_init(&this->texture_mutex, NULL);
// 配置纹理参数
this->texture_handle = 0;
glGenTextures(1, &this->texture_handle);
glBindTexture(GL_TEXTURE_2D, this->texture_handle);
}
// 析构函数
CEasyGL_3DTextureShader::~CEasyGL_3DTextureShader()
{
pthread_mutex_destroy(&this->texture_mutex);
}
// 着色器渲染,由操作对象调用,不可用由框架直接调用
void CEasyGL_3DTextureShader::ShaderRender(void)
{
// 检查着色器
if(!this->shader_valid) return;
// 应用着色器
glUseProgram(this->ShaderHandle);
// 设置平移参数
int id_vec3_move = glGetUniformLocation(this->ShaderHandle, "vec3_move");
pthread_mutex_lock(&this->move_mutex);
glUniform3f(id_vec3_move, this->move_x, this->move_y, this->move_z);
pthread_mutex_unlock(&this->move_mutex);
// 设置移动中心参数
int id_vec3_move_center = glGetUniformLocation(this->ShaderHandle, "vec3_move_center");
pthread_mutex_lock(&this->move_center_mutex);
glUniform3f(id_vec3_move_center, this->move_center_x, this->move_center_y, this->move_center_z);
pthread_mutex_unlock(&this->move_center_mutex);
// 设置旋转参数
pthread_mutex_lock(&this->rotate_mutex);
GLfloat mat3_rotate_x[3*3] =
{
1.0, 0.0f, 0.0f,
0.0f, cos(this->rotate_rad_x), -sin(this->rotate_rad_x),
0.0f, sin(this->rotate_rad_x), cos(this->rotate_rad_x)
};
GLfloat mat3_rotate_y[3*3] =
{
cos(this->rotate_rad_y), 0.0f, sin(this->rotate_rad_y),
0.0f, 1.0f, 0.0f,
-sin(this->rotate_rad_y), 0.0f, cos(this->rotate_rad_y)
};
GLfloat mat3_rotate_z[3*3] =
{
cos(this->rotate_rad_z), -sin(this->rotate_rad_z), 0.0f,
sin(this->rotate_rad_z), cos(this->rotate_rad_z), 0.0f,
0.0f, 0.0f, 1.0f
};
pthread_mutex_unlock(&this->rotate_mutex);
int id_mat3_rotate_x = glGetUniformLocation(this->ShaderHandle, "mat3_rotate_x");
int id_mat3_rotate_y = glGetUniformLocation(this->ShaderHandle, "mat3_rotate_y");
int id_mat3_rotate_z = glGetUniformLocation(this->ShaderHandle, "mat3_rotate_z");
glUniformMatrix3fv(id_mat3_rotate_x, 1, GL_FALSE, mat3_rotate_x);
glUniformMatrix3fv(id_mat3_rotate_y, 1, GL_FALSE, mat3_rotate_y);
glUniformMatrix3fv(id_mat3_rotate_z, 1, GL_FALSE, mat3_rotate_z);
// 设置缩放参数
pthread_mutex_lock(&this->scale_mutex);
GLfloat mat3_scale[3*3] =
{
this->scale_x, 0.0f, 0.0f,
0.0f, this->scale_y, 0.0f,
0.0f, 0.0f, this->scale_z
};
pthread_mutex_unlock(&this->scale_mutex);
int id_mat3_scale = glGetUniformLocation(this->ShaderHandle, "mat3_scale");
glUniformMatrix3fv(id_mat3_scale, 1, GL_FALSE, mat3_scale);
// 设置混合颜色
int id_color = glGetUniformLocation(this->ShaderHandle, "in_blend_color");
pthread_mutex_lock(&this->blendcolor_mutex);
glUniform4f(id_color,
this->blendcolor_r,
this->blendcolor_g,
this->blendcolor_b,
this->blendcolor_a
);
pthread_mutex_unlock(&this->blendcolor_mutex);
// 渲染纹理
int texture_localtion = glGetUniformLocation(this->ShaderHandle, "texture1");
glEnable(GL_TEXTURE_2D);
glCullFace(GL_FRONT);
glEnable(GL_CULL_FACE); // 采用正面剔除法
glEnable(GL_DEPTH_TEST);
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
glDepthFunc(GL_LESS);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
pthread_mutex_lock(&this->texture_mutex);
// 当纹理图片信息合法
if((this->TextureBuff.width > 0) &&
(this->TextureBuff.height > 0) &&
(this->TextureBuff.pdata != 0)
)
{
glTexImage2D(GL_TEXTURE_2D,
0,
this->TextureBuff.format,
this->TextureBuff.width,
this->TextureBuff.height,
0,
this->TextureBuff.format,
GL_UNSIGNED_BYTE,
this->TextureBuff.pdata
);
}
pthread_mutex_unlock(&this->texture_mutex);
glGenerateMipmap(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->texture_handle);
glUniform1i(texture_localtion, 0);
// 渲染操作
GLint id_vertex = glGetAttribLocation(this->ShaderHandle, "in_pos");
GLint id_texture_color = glGetAttribLocation(this->ShaderHandle, "in_color");
// 检查顶点信息可用
pthread_mutex_lock(&this->vertex_mutex);
if((this->pvertex != 0) &&
(this->vertex_len > 0)
)
{
// 设置顶点数据
glVertexAttribPointer(id_vertex,
3,
GL_FLOAT,
GL_FALSE,
5*sizeof(GLfloat),
this->pvertex
);
// 使能当前顶点数据
glEnableVertexAttribArray(id_vertex);
// 设置纹理数据
glVertexAttribPointer(id_texture_color,
2,
GL_FLOAT,
GL_FALSE,
5*sizeof(GLfloat),
this->pvertex + 3
);
// 使能当前纹理坐标数据
glEnableVertexAttribArray(id_texture_color);
// 执行绘图
glDrawArrays(GL_TRIANGLES,
0,
this->vertex_len/5 // 这里的单位是点的数量
// 除以5表示5个float数据为1个点信息
);
}
pthread_mutex_unlock(&this->vertex_mutex);
}
// 指定纹理
void CEasyGL_3DTextureShader::SetTexture2D(GLint imageformat, // 纹理图片格式
int width, // 宽度
int height, // 高度
void* pdata // 纹理数据首地址
)
{
pthread_mutex_lock(&this->texture_mutex);
this->TextureBuff.format = imageformat;
this->TextureBuff.width = width;
this->TextureBuff.height = height;
this->TextureBuff.pdata = pdata;
pthread_mutex_unlock(&this->texture_mutex);
}
// 当前类的名字
std::string CEasyGL_3DTextureShader::ClassName(void)
{
std::string re_str = "CEasyGL_3DTextureShader";
return re_str;
}
//---------------------------------------------------------------------
// 无光照效果的2D纹理填充着色器
// 绘制数据为x y z的点信息 和 u v纹理坐标信息
// 提供平移、三维旋转、缩放、整体混合纯色的API
// 着色器渲染,由操作对象调用,不可用由框架直接调用
void CEasyGL_2DTextureShader::ShaderRender(void)
{
// 检查着色器
if(!this->shader_valid) return;
// 应用着色器
glUseProgram(this->ShaderHandle);
// 设置平移参数
int id_vec3_move = glGetUniformLocation(this->ShaderHandle, "vec3_move");
pthread_mutex_lock(&this->move_mutex);
glUniform3f(id_vec3_move, this->move_x, this->move_y, this->move_z);
pthread_mutex_unlock(&this->move_mutex);
// 设置移动中心参数
int id_vec3_move_center = glGetUniformLocation(this->ShaderHandle, "vec3_move_center");
pthread_mutex_lock(&this->move_center_mutex);
glUniform3f(id_vec3_move_center, this->move_center_x, this->move_center_y, this->move_center_z);
pthread_mutex_unlock(&this->move_center_mutex);
// 设置旋转参数
pthread_mutex_lock(&this->rotate_mutex);
GLfloat mat3_rotate_x[3*3] =
{
1.0, 0.0f, 0.0f,
0.0f, cos(this->rotate_rad_x), -sin(this->rotate_rad_x),
0.0f, sin(this->rotate_rad_x), cos(this->rotate_rad_x)
};
GLfloat mat3_rotate_y[3*3] =
{
cos(this->rotate_rad_y), 0.0f, sin(this->rotate_rad_y),
0.0f, 1.0f, 0.0f,
-sin(this->rotate_rad_y), 0.0f, cos(this->rotate_rad_y)
};
GLfloat mat3_rotate_z[3*3] =
{
cos(this->rotate_rad_z), -sin(this->rotate_rad_z), 0.0f,
sin(this->rotate_rad_z), cos(this->rotate_rad_z), 0.0f,
0.0f, 0.0f, 1.0f
};
pthread_mutex_unlock(&this->rotate_mutex);
int id_mat3_rotate_x = glGetUniformLocation(this->ShaderHandle, "mat3_rotate_x");
int id_mat3_rotate_y = glGetUniformLocation(this->ShaderHandle, "mat3_rotate_y");
int id_mat3_rotate_z = glGetUniformLocation(this->ShaderHandle, "mat3_rotate_z");
glUniformMatrix3fv(id_mat3_rotate_x, 1, GL_FALSE, mat3_rotate_x);
glUniformMatrix3fv(id_mat3_rotate_y, 1, GL_FALSE, mat3_rotate_y);
glUniformMatrix3fv(id_mat3_rotate_z, 1, GL_FALSE, mat3_rotate_z);
// 设置缩放参数
pthread_mutex_lock(&this->scale_mutex);
GLfloat mat3_scale[3*3] =
{
this->scale_x, 0.0f, 0.0f,
0.0f, this->scale_y, 0.0f,
0.0f, 0.0f, this->scale_z
};
pthread_mutex_unlock(&this->scale_mutex);
int id_mat3_scale = glGetUniformLocation(this->ShaderHandle, "mat3_scale");
glUniformMatrix3fv(id_mat3_scale, 1, GL_FALSE, mat3_scale);
// 设置混合颜色
int id_color = glGetUniformLocation(this->ShaderHandle, "in_blend_color");
pthread_mutex_lock(&this->blendcolor_mutex);
glUniform4f(id_color,
this->blendcolor_r,
this->blendcolor_g,
this->blendcolor_b,
this->blendcolor_a
);
pthread_mutex_unlock(&this->blendcolor_mutex);
// 渲染纹理
int texture_localtion = glGetUniformLocation(this->ShaderHandle, "texture1");
glEnable(GL_TEXTURE_2D);
glDisable(GL_CULL_FACE); // 关闭面剔除法
glEnable(GL_DEPTH_TEST);
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
glDepthFunc(GL_LESS);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
pthread_mutex_lock(&this->texture_mutex);
// 当纹理图片信息合法
if((this->TextureBuff.width > 0) &&
(this->TextureBuff.height > 0) &&
(this->TextureBuff.pdata != 0)
)
{
glTexImage2D(GL_TEXTURE_2D,
0,
this->TextureBuff.format,
this->TextureBuff.width,
this->TextureBuff.height,
0,
this->TextureBuff.format,
GL_UNSIGNED_BYTE,
this->TextureBuff.pdata
);
}
pthread_mutex_unlock(&this->texture_mutex);
glGenerateMipmap(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->texture_handle);
glUniform1i(texture_localtion, 0);
// 渲染操作
GLint id_vertex = glGetAttribLocation(this->ShaderHandle, "in_pos");
GLint id_texture_color = glGetAttribLocation(this->ShaderHandle, "in_color");
// 检查顶点信息可用
pthread_mutex_lock(&this->vertex_mutex);
if((this->pvertex != 0) &&
(this->vertex_len > 0)
)
{
// 设置顶点数据
glVertexAttribPointer(id_vertex,
3,
GL_FLOAT,
GL_FALSE,
5*sizeof(GLfloat),
this->pvertex
);
// 使能当前顶点数据
glEnableVertexAttribArray(id_vertex);
// 设置纹理数据
glVertexAttribPointer(id_texture_color,
2,
GL_FLOAT,
GL_FALSE,
5*sizeof(GLfloat),
this->pvertex + 3
);
// 使能当前纹理坐标数据
glEnableVertexAttribArray(id_texture_color);
// 执行绘图
glDrawArrays(GL_TRIANGLES,
0,
this->vertex_len/5 // 这里的单位是点的数量
// 除以5表示5个float数据为1个点信息
);
}