-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpreview3d.c
1989 lines (1727 loc) · 61.7 KB
/
preview3d.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
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
/*
normalmap GIMP plugin
Copyright (C) 2002-2012 Shawn Kirst <skirst@gmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA.
*/
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <gtk/gtk.h>
#include <gtk/gtkgl.h>
#include <GL/glew.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include "scale.h"
#include "objects/quad.h"
#include "objects/cube.h"
#include "objects/sphere.h"
#include "objects/torus.h"
#include "objects/teapot.h"
#include "pixmaps/object.xpm"
#include "pixmaps/light.xpm"
#include "pixmaps/scene.xpm"
#include "pixmaps/full.xpm"
#define IS_POT(x) (((x) & ((x) - 1)) == 0)
typedef float matrix[16];
typedef float vec4[4];
typedef float vec3[3];
typedef float vec2[2];
typedef enum
{
BUMPMAP_NORMAL = 0, BUMPMAP_PARALLAX, BUMPMAP_POM, BUMPMAP_RELIEF,
BUMPMAP_MAX
} BUMPMAP_TYPE;
typedef enum
{
ROTATE_OBJECT = 0, ROTATE_LIGHT, ROTATE_SCENE,
ROTATE_MAX
} ROTATE_TYPE;
typedef enum
{
OBJECT_QUAD = 0, OBJECT_CUBE, OBJECT_SPHERE, OBJECT_TORUS, OBJECT_TEAPOT,
OBJECT_MAX
} OBJECT_TYPE;
static int _active = 0;
static int _gl_error = 0;
static gint32 normalmap_drawable_id = -1;
static GtkWidget *window = 0;
static GtkWidget *glarea = 0;
static GtkWidget *rotate_obj_btn = 0;
static GtkWidget *object_opt = 0;
static GtkWidget *controls_table = 0;
static GtkWidget *bumpmapping_opt = 0;
static GtkWidget *specular_check = 0;
static GtkWidget *gloss_opt = 0;
static GtkWidget *specular_exp_range = 0;
static GtkWidget *ambient_color_btn = 0;
static GtkWidget *diffuse_color_btn = 0;
static GtkWidget *specular_color_btn = 0;
static GtkWidget *uvscale_spin1 = 0;
static GtkWidget *uvscale_spin2 = 0;
static int fullscreen = 0;
static GLuint diffuse_tex = 0;
static GLuint gloss_tex = 0;
static GLuint normal_tex = 0;
static GLuint white_tex = 0;
static struct
{
float *verts;
unsigned short *indices;
unsigned int num_verts;
unsigned int num_indices;
GLuint vbo;
} object_info[OBJECT_MAX] =
{
{quad_verts, quad_indices, QUAD_NUM_VERTS, QUAD_NUM_INDICES, 0},
{cube_verts, cube_indices, CUBE_NUM_VERTS, CUBE_NUM_INDICES, 0},
{sphere_verts, sphere_indices, SPHERE_NUM_VERTS, SPHERE_NUM_INDICES, 0},
{torus_verts, torus_indices, TORUS_NUM_VERTS, TORUS_NUM_INDICES, 0},
{teapot_verts, teapot_indices, TEAPOT_NUM_VERTS, TEAPOT_NUM_INDICES, 0}
};
static const float anisotropy = 4.0f;
static int has_glsl = 0;
static int has_npot = 0;
static int has_generate_mipmap = 0;
static int has_aniso = 0;
static int num_mtus = 0;
static int max_instructions = 0;
static int max_indirections = 0;
static GLhandleARB programs[BUMPMAP_MAX];
static const char *vert_source =
"varying vec2 tex;\n"
"varying vec3 vpos;\n"
"varying vec3 normal;\n"
"varying vec3 tangent;\n"
"varying vec3 binormal;\n"
"\n"
"uniform vec2 uvscale;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = ftransform();\n"
" tex = gl_MultiTexCoord0.xy * uvscale;\n"
" vpos = (gl_ModelViewMatrix * gl_Vertex).xyz;\n"
" tangent = gl_NormalMatrix * gl_MultiTexCoord3.xyz;\n"
" binormal = gl_NormalMatrix * gl_MultiTexCoord4.xyz;\n"
" normal = gl_NormalMatrix * gl_Normal;\n"
"}\n";
static const char *normal_frag_source =
"varying vec2 tex;\n"
"varying vec3 vpos;\n"
"varying vec3 normal;\n"
"varying vec3 tangent;\n"
"varying vec3 binormal;\n"
"uniform sampler2D sNormal;\n"
"uniform sampler2D sDiffuse;\n"
"uniform sampler2D sGloss;\n\n"
"uniform vec3 lightDir;\n"
"uniform bool specular;\n"
"uniform float specular_exp;\n"
"uniform vec3 ambient_color;\n"
"uniform vec3 diffuse_color;\n"
"uniform vec3 specular_color;\n\n"
"void main()\n"
"{\n"
" vec3 V = normalize(vpos);\n"
" vec3 N = texture2D(sNormal, tex).rgb * 2.0 - 1.0;\n"
" N = normalize(N.x * tangent + N.y * binormal + N.z * normal);\n"
" vec3 diffuse = texture2D(sDiffuse, tex).rgb;\n"
" float NdotL = clamp(dot(N, lightDir), 0.0, 1.0);\n"
" vec3 color = diffuse * diffuse_color * NdotL;\n"
" if(specular)\n"
" {\n"
" vec3 gloss = texture2D(sGloss, tex).rgb;\n"
" vec3 R = reflect(V, N);\n"
" float RdotL = clamp(dot(R, lightDir), 0.0, 1.0);\n"
" color += gloss * specular_color * pow(RdotL, specular_exp);\n"
" }\n"
" gl_FragColor.rgb = ambient_color * diffuse + color;\n"
"}\n";
static const char *parallax_frag_source =
"varying vec2 tex;\n"
"varying vec3 vpos;\n"
"varying vec3 normal;\n"
"varying vec3 tangent;\n"
"varying vec3 binormal;\n"
"uniform sampler2D sNormal;\n"
"uniform sampler2D sDiffuse;\n"
"uniform sampler2D sGloss;\n\n"
"uniform vec3 lightDir;\n"
"uniform bool specular;\n"
"uniform float specular_exp;\n"
"uniform vec3 ambient_color;\n"
"uniform vec3 diffuse_color;\n"
"uniform vec3 specular_color;\n\n"
"void main()\n"
"{\n"
" mat3 TBN = mat3(tangent, binormal, normal);\n"
" vec3 V = normalize(vpos);\n"
" vec3 V_ts = V * TBN;\n"
" float height = texture2D(sNormal, tex).a;\n"
" float offset = height * 0.025 - 0.0125;\n"
" vec2 tc = tex + offset * V_ts.xy;\n"
" height += texture2D(sNormal, tc).a;\n"
" offset = 0.025 * (height - 1.0);\n"
" tc = tex + offset * V_ts.xy;\n"
" vec3 N = texture2D(sNormal, tc).rgb * 2.0 - 1.0;\n"
" N = normalize(N.x * tangent + N.y * binormal + N.z * normal);\n"
" vec3 diffuse = texture2D(sDiffuse, tc).rgb;\n"
" float NdotL = clamp(dot(N, lightDir), 0.0, 1.0);\n"
" vec3 color = diffuse * diffuse_color * NdotL;\n"
" if(specular)\n"
" {\n"
" vec3 gloss = texture2D(sGloss, tc).rgb;\n"
" vec3 R = reflect(V, N);\n"
" float RdotL = clamp(dot(R, lightDir), 0.0, 1.0);\n"
" color += gloss * specular_color * pow(RdotL, specular_exp);\n"
" }\n"
" gl_FragColor.rgb = ambient_color * diffuse + color;\n"
"}\n";
static const char *pom_frag_source =
"varying vec2 tex;\n"
"varying vec3 vpos;\n"
"varying vec3 normal;\n"
"varying vec3 tangent;\n"
"varying vec3 binormal;\n"
"\n"
"uniform sampler2D sNormal;\n"
"uniform sampler2D sDiffuse;\n"
"uniform sampler2D sGloss;\n"
"\n"
"uniform vec3 lightDir;\n"
"uniform bool specular;\n"
"uniform vec3 ambient_color;\n"
"uniform vec3 diffuse_color;\n"
"uniform vec3 specular_color;\n"
"uniform float specular_exp;\n"
"uniform vec2 planes;\n"
"uniform float depth_factor;\n"
"\n"
"void ray_intersect(sampler2D reliefMap, inout vec4 p, inout vec3 v)\n"
"{\n"
" const int search_steps = 20;\n"
"\n"
" v /= float(search_steps);\n"
"\n"
" vec4 pp = p;\n"
" for(int i = 0; i < search_steps - 1; ++i)\n"
" {\n"
" p.w = texture2D(reliefMap, p.xy).w;\n"
" if(p.w > p.z)\n"
" {\n"
" pp = p;\n"
" p.xyz += v;\n"
" }\n"
" }\n"
"\n"
" float f = (pp.w - pp.z) / (p.z - pp.z - p.w + pp.w);\n"
" p = mix(pp, p, f);\n"
"}\n"
"\n"
"void ray_intersect_ATI(sampler2D reliefMap, inout vec4 p, inout vec3 v)"
"{\n"
" float h0 = 1.0 - texture2D(reliefMap, p.xy + v.xy * 1.000).a;\n"
" float h1 = 1.0 - texture2D(reliefMap, p.xy + v.xy * 0.875).a;\n"
" float h2 = 1.0 - texture2D(reliefMap, p.xy + v.xy * 0.750).a;\n"
" float h3 = 1.0 - texture2D(reliefMap, p.xy + v.xy * 0.625).a;\n"
" float h4 = 1.0 - texture2D(reliefMap, p.xy + v.xy * 0.500).a;\n"
" float h5 = 1.0 - texture2D(reliefMap, p.xy + v.xy * 0.375).a;\n"
" float h6 = 1.0 - texture2D(reliefMap, p.xy + v.xy * 0.250).a;\n"
" float h7 = 1.0 - texture2D(reliefMap, p.xy + v.xy * 0.125).a;\n"
"\n"
" float x, y, xh, yh;\n"
" if (h7 > 0.875) { x = 0.937; y = 0.938; xh = h7; yh = h7; }\n"
" else if(h6 > 0.750) { x = 0.750; y = 0.875; xh = h6; yh = h7; }\n"
" else if(h5 > 0.625) { x = 0.625; y = 0.750; xh = h5; yh = h6; }\n"
" else if(h4 > 0.500) { x = 0.500; y = 0.625; xh = h4; yh = h5; }\n"
" else if(h3 > 0.375) { x = 0.375; y = 0.500; xh = h3; yh = h4; }\n"
" else if(h2 > 0.250) { x = 0.250; y = 0.375; xh = h2; yh = h3; }\n"
" else if(h1 > 0.125) { x = 0.125; y = 0.250; xh = h1; yh = h2; }\n"
" else { x = 0.000; y = 0.125; xh = h0; yh = h1; }\n"
"\n"
" float parallax = (x * (y - yh) - y * (x - xh)) / ((y - yh) - (x - xh));\n"
" p.xyz += v * (1.0 - parallax);\n"
"}\n"
"\n"
"void main()\n"
"{\n"
"\n"
" vec3 V = normalize(vpos);\n"
" float a = dot(normal, -V);\n"
" vec3 v = vec3(dot(V, tangent), dot(V, binormal), a);\n"
" vec3 scale = vec3(1.0, 1.0, depth_factor);\n"
" v *= scale.z / (scale * v.z);\n"
" vec4 p = vec4(tex, vec2(0.0, 1.0));\n"
"#ifdef ATI\n"
" ray_intersect_ATI(sNormal, p, v);\n"
"#else\n"
" ray_intersect(sNormal, p, v);\n"
"#endif\n"
"\n"
" vec2 uv = p.xy;\n"
" vec3 N = texture2D(sNormal, uv).xyz * 2.0 - 1.0;\n"
" vec3 diffuse = texture2D(sDiffuse, uv).rgb;\n"
"\n"
" N.z = sqrt(1.0 - dot(N.xy, N.xy));\n"
" N = normalize(N.x * tangent + N.y * binormal + N.z * normal);\n"
"\n"
" float NdotL = clamp(dot(N, lightDir), 0.0, 1.0);\n"
"\n"
" vec3 color = diffuse * diffuse_color * NdotL;\n"
"\n"
" if(specular)\n"
" {\n"
" vec3 gloss = texture2D(sGloss, uv).rgb;\n"
" vec3 R = reflect(V, N);\n"
" float RdotL = clamp(dot(R, lightDir), 0.0, 1.0);\n"
" color += gloss * specular_color * pow(RdotL, specular_exp);\n"
" }\n"
"\n"
" gl_FragColor.rgb = ambient_color * diffuse + color;\n"
"}\n";
static const char *relief_frag_source =
"varying vec2 tex;\n"
"varying vec3 vpos;\n"
"varying vec3 normal;\n"
"varying vec3 tangent;\n"
"varying vec3 binormal;\n"
"\n"
"uniform sampler2D sNormal;\n"
"uniform sampler2D sDiffuse;\n"
"uniform sampler2D sGloss;\n"
"\n"
"uniform vec3 lightDir;\n"
"uniform bool specular;\n"
"uniform vec3 ambient_color;\n"
"uniform vec3 diffuse_color;\n"
"uniform vec3 specular_color;\n"
"uniform float specular_exp;\n"
"uniform vec2 planes;\n"
"uniform float depth_factor;\n"
"\n"
"float ray_intersect(sampler2D reliefMap, vec2 dp, vec2 ds)\n"
"{\n"
" const int linear_search_steps = 20;\n"
"\n"
" float size = 1.0 / float(linear_search_steps);\n"
" float depth = 0.0;\n"
" float best_depth = 1.0;\n"
"\n"
" for(int i = 0; i < linear_search_steps - 1; ++i)\n"
" {\n"
" depth += size;\n"
" float t = texture2D(reliefMap, dp + ds * depth).a;\n"
" if(best_depth > 0.996)\n"
" if(depth >= t)\n"
" best_depth = depth;\n"
" }\n"
" depth = best_depth;\n"
"\n"
" const int binary_search_steps = 5;\n"
"\n"
" for(int i = 0; i < binary_search_steps; ++i)\n"
" {\n"
" size *= 0.5;\n"
" float t = texture2D(reliefMap, dp + ds * depth).a;\n"
" if(depth >= t)\n"
" {\n"
" best_depth = depth;\n"
" depth -= 2.0 * size;\n"
" }\n"
" depth += size;\n"
" }\n"
"\n"
" return(best_depth);\n"
"}\n"
"\n"
"void main()\n"
"{\n"
"\n"
" vec3 V = normalize(vpos);\n"
" float a = dot(normal, -V);\n"
" vec2 s = vec2(dot(V, tangent), dot(V, binormal));\n"
" s *= depth_factor / a;\n"
" vec2 ds = s;\n"
" vec2 dp = tex;\n"
" float d = ray_intersect(sNormal, dp, ds);\n"
"\n"
" vec2 uv = dp + ds * d;\n"
" vec3 N = texture2D(sNormal, uv).xyz * 2.0 - 1.0;\n"
" vec3 diffuse = texture2D(sDiffuse, uv).rgb;\n"
"\n"
" N.z = sqrt(1.0 - dot(N.xy, N.xy));\n"
" N = normalize(N.x * tangent + N.y * binormal + N.z * normal);\n"
"\n"
" float NdotL = clamp(dot(N, lightDir), 0.0, 1.0);\n"
"\n"
" vec3 color = diffuse * diffuse_color * NdotL;\n"
"\n"
" if(specular)\n"
" {\n"
" vec3 gloss = texture2D(sGloss, uv).rgb;\n"
" vec3 R = reflect(V, N);\n"
" float RdotL = clamp(dot(R, lightDir), 0.0, 1.0);\n"
" color += gloss * specular_color * pow(RdotL, specular_exp);\n"
" }\n"
"\n"
" gl_FragColor.rgb = ambient_color * diffuse + color;\n"
"}\n";
static int bumpmapping = BUMPMAP_NORMAL;
static int specular = 0;
static int rotate_type = ROTATE_OBJECT;
static int object_type = OBJECT_QUAD;
static vec3 ambient_color = {0.2f, 0.2f, 0.2f};
static vec3 diffuse_color = {1, 1, 1};
static vec3 specular_color = {1, 1, 1};
static float specular_exp = 32.0f;
static vec3 uvscale = {1, 1};
static const float depth_factor = 0.05f;
static int mx;
static int my;
static vec3 object_rot;
static vec3 light_rot;
static vec3 scene_rot;
static float zoom;
#define M(r,c) m[(c << 2) + r]
#define T(r,c) t[(c << 2) + r]
static void mat_invert(matrix m)
{
float invdet;
matrix t;
invdet = (float)1.0 / (M(0, 0) * (M(1, 1) * M(2, 2) - M(1, 2) * M(2, 1)) -
M(0, 1) * (M(1, 0) * M(2, 2) - M(1, 2) * M(2, 0)) +
M(0, 2) * (M(1, 0) * M(2, 1) - M(1, 1) * M(2, 0)));
T(0,0) = invdet * (M(1, 1) * M(2, 2) - M(1, 2) * M(2, 1));
T(0,1) = -invdet * (M(0, 1) * M(2, 2) - M(0, 2) * M(2, 1));
T(0,2) = invdet * (M(0, 1) * M(1, 2) - M(0, 2) * M(1, 1));
T(0,3) = 0;
T(1,0) = -invdet * (M(1, 0) * M(2, 2) - M(1, 2) * M(2, 0));
T(1,1) = invdet * (M(0, 0) * M(2, 2) - M(0, 2) * M(2, 0));
T(1,2) = -invdet * (M(0, 0) * M(1, 2) - M(0, 2) * M(1, 0));
T(1,3) = 0;
T(2,0) = invdet * (M(1, 0) * M(2, 1) - M(1, 1) * M(2, 0));
T(2,1) = -invdet * (M(0, 0) * M(2, 1) - M(0, 1) * M(2, 0));
T(2,2) = invdet * (M(0, 0) * M(1, 1) - M(0, 1) * M(1, 0));
T(2,3) = 0;
T(3,0) = -(M(3, 0) * T(0, 0) + M(3, 1) * T(1, 0) + M(3, 2) * T(2, 0));
T(3,1) = -(M(3, 0) * T(0, 1) + M(3, 1) * T(1, 1) + M(3, 2) * T(2, 1));
T(3,2) = -(M(3, 0) * T(0, 2) + M(3, 1) * T(1, 2) + M(3, 2) * T(2, 2));
T(3,3) = 1;
memcpy(m, t, 16 * sizeof(float));
}
static void mat_transpose(matrix m)
{
matrix t;
t[0 ] = m[0 ]; t[1 ] = m[4 ]; t[2 ] = m[8 ]; t[3 ] = m[12];
t[4 ] = m[1 ]; t[5 ] = m[5 ]; t[6 ] = m[9 ]; t[7 ] = m[13];
t[8 ] = m[2 ]; t[9 ] = m[6 ]; t[10] = m[10]; t[11] = m[14];
t[12] = m[3 ]; t[13] = m[7 ]; t[14] = m[11]; t[15] = m[15];
memcpy(m, t, sizeof(matrix));
}
static void mat_mult_vec(vec3 v, matrix m)
{
vec3 t;
t[0] = M(0, 0) * v[0] + M(0, 1) * v[1] + M(0, 2) * v[2];
t[1] = M(1, 0) * v[0] + M(1, 1) * v[1] + M(1, 2) * v[2];
t[2] = M(2, 0) * v[0] + M(2, 1) * v[1] + M(2, 2) * v[2];
v[0] = t[0];
v[1] = t[1];
v[2] = t[2];
}
static inline void vec3_set(vec3 v, float x, float y, float z)
{
v[0] = x;
v[1] = y;
v[2] = z;
}
static inline void vec3_copy(vec3 r, vec3 v)
{
r[0] = v[0];
r[1] = v[1];
r[2] = v[2];
}
static void vec4_normalize(vec4 r, vec4 v)
{
float len = sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]);
if(len != 0)
{
float ilen = 1.0f / len;
r[0] = v[0] * ilen;
r[1] = v[1] * ilen;
r[2] = v[2] * ilen;
r[3] = v[3] * ilen;
}
else
r[0] = r[1] = r[2] = r[3] = 0;
}
static void vec3_normalize(vec3 r, vec3 v)
{
float len = sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
if(len != 0.0f)
{
float ilen = 1.0f / len;
r[0] = v[0] * ilen;
r[1] = v[1] * ilen;
r[2] = v[2] * ilen;
}
else
r[0] = r[1] = r[2] = 0;
}
static inline void quat_ident(vec4 q)
{
q[0] = q[1] = q[2] = 0;
q[3] = 1;
}
static void quat_mul(vec4 r, vec4 a, vec4 b)
{
r[0] = a[0] * b[3] + b[0] * a[3] + a[1] * b[2] - a[2] * b[1];
r[1] = a[1] * b[3] + b[1] * a[3] + a[2] * b[0] - a[0] * b[2];
r[2] = a[2] * b[3] + b[2] * a[3] + a[0] * b[1] - a[1] * b[0];
r[3] = a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2];
}
static void quat_rotate(vec4 q, float a, float x, float y, float z)
{
float hs, len, ilen;
len = sqrtf(x * x + y * y + z * z);
if(len == 0) return;
ilen = 1.0f / len;
x *= ilen;
y *= ilen;
z *= ilen;
a = (a * (M_PI / 180.0f)) * 0.5f;
hs = sinf(a);
q[0] = x * hs;
q[1] = y * hs;
q[2] = z * hs;
q[3] = cosf(a);
}
static void quat_get_direction(vec3 v, vec4 q)
{
v[0] = 2.0f * (q[0] * q[2] - q[3] * q[1]);
v[1] = 2.0f * (q[1] * q[2] + q[3] * q[0]);
v[2] = 1.0f - 2.0f * (q[0] * q[0] + q[1] * q[1]);
}
#undef M
#undef T
static void init(GtkWidget *widget, gpointer data)
{
int i, err;
unsigned char white[16] = {0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff};
GdkGLContext *glcontext = gtk_widget_get_gl_context(widget);
GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable(widget);
GtkWidget *menu;
GList *curr;
if(!gdk_gl_drawable_gl_begin(gldrawable, glcontext))
return;
err = glewInit();
if(err != GLEW_OK)
{
g_message("%s", (char *)glewGetErrorString(err));
_gl_error = 1;
}
glClearColor(0, 0, 0.4f, 0);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glLineWidth(3);
_gl_error = 0;
if(!GLEW_ARB_multitexture)
{
g_message("GL_ARB_multitexture is required for the 3D preview");
_gl_error = 1;
}
if(!GLEW_ARB_texture_env_combine)
{
g_message("GL_ARB_texture_env_combine is required for the 3D preview");
_gl_error = 1;
}
if(!GLEW_ARB_texture_env_dot3)
{
g_message("GL_ARB_texture_env_dot3 is required for the 3D preview");
_gl_error = 1;
}
if(_gl_error) return;
glGenTextures(1, &diffuse_tex);
glGenTextures(1, &gloss_tex);
glGenTextures(1, &normal_tex);
glGenTextures(1, &white_tex);
glGetIntegerv(GL_MAX_TEXTURE_UNITS, &num_mtus);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_DOT3_RGB);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
glBindTexture(GL_TEXTURE_2D, white_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 4, 4, 0,
GL_LUMINANCE, GL_UNSIGNED_BYTE, white);
if(num_mtus > 2)
{
glActiveTexture(GL_TEXTURE2);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, white_tex);
}
has_glsl = GLEW_ARB_shader_objects && GLEW_ARB_vertex_shader &&
GLEW_ARB_fragment_shader;
has_npot = GLEW_ARB_texture_non_power_of_two;
has_generate_mipmap = GLEW_SGIS_generate_mipmap;
has_aniso = GLEW_EXT_texture_filter_anisotropic;
if(has_glsl)
{
GLhandleARB prog, vert_shader, frag_shader;
int res, len, loc;
const char *sources[2];
char *info;
/* Get max # of instructions and indirections supported by the hardware.
* Used to determine if parallax occlusion and relief mapping should be
* enabled and if the "ATI" version of parallax occlusion mapping should
* be used.
*/
if(GLEW_ARB_fragment_program)
{
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 1);
glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB,
GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB,
&max_instructions);
glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB,
GL_MAX_PROGRAM_NATIVE_TEX_INDIRECTIONS_ARB,
&max_indirections);
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
}
vert_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
glShaderSourceARB(vert_shader, 1, &vert_source, 0);
glCompileShaderARB(vert_shader);
glGetObjectParameterivARB(vert_shader, GL_OBJECT_COMPILE_STATUS_ARB, &res);
if(!res)
{
glGetObjectParameterivARB(vert_shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
info = g_malloc(len + 1);
glGetInfoLogARB(vert_shader, len, 0, info);
g_message("Vertex shader failed to compile:\n%s\n", info);
g_free(info);
}
prog = glCreateProgramObjectARB();
glAttachObjectARB(prog, vert_shader);
frag_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
glShaderSourceARB(frag_shader, 1, &normal_frag_source, 0);
glCompileShaderARB(frag_shader);
glGetObjectParameterivARB(frag_shader, GL_OBJECT_COMPILE_STATUS_ARB, &res);
if(res)
glAttachObjectARB(prog, frag_shader);
else
{
glGetObjectParameterivARB(frag_shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
info = g_malloc(len + 1);
glGetInfoLogARB(frag_shader, len, 0, info);
g_message("Normal mapping fragment shader failed to compile:\n%s\n",
info);
g_free(info);
glDeleteObjectARB(prog);
prog = 0;
}
glDeleteObjectARB(frag_shader);
if(prog)
{
glLinkProgramARB(prog);
glGetObjectParameterivARB(prog, GL_OBJECT_LINK_STATUS_ARB, &res);
if(!res)
{
glGetObjectParameterivARB(prog, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
info = g_malloc(len + 1);
glGetInfoLogARB(prog, len, 0, info);
g_message("Normal mapping program failed to link:\n%s\n", info);
g_free(info);
glDeleteObjectARB(prog);
prog = 0;
}
}
programs[BUMPMAP_NORMAL] = prog;
prog = glCreateProgramObjectARB();
glAttachObjectARB(prog, vert_shader);
frag_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
glShaderSourceARB(frag_shader, 1, ¶llax_frag_source, 0);
glCompileShaderARB(frag_shader);
glGetObjectParameterivARB(frag_shader, GL_OBJECT_COMPILE_STATUS_ARB, &res);
if(res)
glAttachObjectARB(prog, frag_shader);
else
{
glGetObjectParameterivARB(frag_shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
info = g_malloc(len + 1);
glGetInfoLogARB(frag_shader, len, 0, info);
g_message("Parallax mapping fragment shader failed to compile:\n%s\n",
info);
g_free(info);
glDeleteObjectARB(prog);
prog = 0;
}
glDeleteObjectARB(frag_shader);
if(prog)
{
glLinkProgramARB(prog);
glGetObjectParameterivARB(prog, GL_OBJECT_LINK_STATUS_ARB, &res);
if(!res)
{
glGetObjectParameterivARB(prog, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
info = g_malloc(len + 1);
glGetInfoLogARB(prog, len, 0, info);
g_message("Parallax mapping program failed to link:\n%s\n", info);
g_free(info);
glDeleteObjectARB(prog);
prog = 0;
}
}
programs[BUMPMAP_PARALLAX] = prog;
if(max_instructions >= 200)
{
prog = glCreateProgramObjectARB();
glAttachObjectARB(prog, vert_shader);
if(max_indirections < 100)
sources[0] = "#define ATI 1\n";
else
sources[0] = "";
sources[1] = pom_frag_source;
frag_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
glShaderSourceARB(frag_shader, 2, sources, 0);
glCompileShaderARB(frag_shader);
glGetObjectParameterivARB(frag_shader, GL_OBJECT_COMPILE_STATUS_ARB, &res);
if(res)
glAttachObjectARB(prog, frag_shader);
else
{
glGetObjectParameterivARB(frag_shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
info = g_malloc(len + 1);
glGetInfoLogARB(frag_shader, len, 0, info);
g_message("Parallax Occlusion mapping fragment shader failed to compile:\n%s\n",
info);
g_free(info);
glDeleteObjectARB(prog);
prog = 0;
}
glDeleteObjectARB(frag_shader);
if(prog)
{
glLinkProgramARB(prog);
glGetObjectParameterivARB(prog, GL_OBJECT_LINK_STATUS_ARB, &res);
if(!res)
{
glGetObjectParameterivARB(prog, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
info = g_malloc(len + 1);
glGetInfoLogARB(prog, len, 0, info);
g_message("Parallax Occlusion mapping program failed to link:\n%s\n",
info);
g_free(info);
glDeleteObjectARB(prog);
prog = 0;
}
}
programs[BUMPMAP_POM] = prog;
}
else
programs[BUMPMAP_POM] = 0;
if(max_instructions >= 200 && max_indirections >= 100)
{
prog = glCreateProgramObjectARB();
glAttachObjectARB(prog, vert_shader);
frag_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
glShaderSourceARB(frag_shader, 1, &relief_frag_source, 0);
glCompileShaderARB(frag_shader);
glGetObjectParameterivARB(frag_shader, GL_OBJECT_COMPILE_STATUS_ARB, &res);
if(res)
glAttachObjectARB(prog, frag_shader);
else
{
glGetObjectParameterivARB(frag_shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
info = g_malloc(len + 1);
glGetInfoLogARB(frag_shader, len, 0, info);
g_message("Relief mapping fragment shader failed to compile:\n%s\n",
info);
g_free(info);
glDeleteObjectARB(prog);
prog = 0;
}
glDeleteObjectARB(frag_shader);
if(prog)
{
glLinkProgramARB(prog);
glGetObjectParameterivARB(prog, GL_OBJECT_LINK_STATUS_ARB, &res);
if(!res)
{
glGetObjectParameterivARB(prog, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
info = g_malloc(len + 1);
glGetInfoLogARB(prog, len, 0, info);
g_message("Relief mapping program failed to link:\n%s\n", info);
g_free(info);
glDeleteObjectARB(prog);
prog = 0;
}
}
programs[BUMPMAP_RELIEF] = prog;
}
else
programs[BUMPMAP_RELIEF] = 0;
glDeleteObjectARB(vert_shader);
if(programs[BUMPMAP_NORMAL])
{
glUseProgramObjectARB(programs[BUMPMAP_NORMAL]);
loc = glGetUniformLocationARB(programs[BUMPMAP_NORMAL], "sNormal");
glUniform1iARB(loc, 0);
loc = glGetUniformLocationARB(programs[BUMPMAP_NORMAL], "sDiffuse");
glUniform1iARB(loc, 1);
loc = glGetUniformLocationARB(programs[BUMPMAP_NORMAL], "sGloss");
glUniform1iARB(loc, 2);
}
if(programs[BUMPMAP_PARALLAX])
{
glUseProgramObjectARB(programs[BUMPMAP_PARALLAX]);
loc = glGetUniformLocationARB(programs[BUMPMAP_PARALLAX], "sNormal");
glUniform1iARB(loc, 0);
loc = glGetUniformLocationARB(programs[BUMPMAP_PARALLAX], "sDiffuse");
glUniform1iARB(loc, 1);
loc = glGetUniformLocationARB(programs[BUMPMAP_PARALLAX], "sGloss");
glUniform1iARB(loc, 2);
}
if(programs[BUMPMAP_POM])
{
glUseProgramObjectARB(programs[BUMPMAP_POM]);
loc = glGetUniformLocationARB(programs[BUMPMAP_POM], "sNormal");
glUniform1iARB(loc, 0);
loc = glGetUniformLocationARB(programs[BUMPMAP_POM], "sDiffuse");
glUniform1iARB(loc, 1);
loc = glGetUniformLocationARB(programs[BUMPMAP_POM], "sGloss");
glUniform1iARB(loc, 2);
loc = glGetUniformLocationARB(programs[BUMPMAP_POM], "depth_factor");
glUniform1fARB(loc, depth_factor);
}
if(programs[BUMPMAP_RELIEF])
{
glUseProgramObjectARB(programs[BUMPMAP_RELIEF]);
loc = glGetUniformLocationARB(programs[BUMPMAP_RELIEF], "sNormal");
glUniform1iARB(loc, 0);
loc = glGetUniformLocationARB(programs[BUMPMAP_RELIEF], "sDiffuse");
glUniform1iARB(loc, 1);
loc = glGetUniformLocationARB(programs[BUMPMAP_RELIEF], "sGloss");
glUniform1iARB(loc, 2);
loc = glGetUniformLocationARB(programs[BUMPMAP_RELIEF], "depth_factor");
glUniform1fARB(loc, depth_factor);
}
glUseProgramObjectARB(0);
for(i = 0; i < OBJECT_MAX; ++i)
{
glGenBuffersARB(1, &object_info[i].vbo);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, object_info[i].vbo);
glBufferDataARB(GL_ARRAY_BUFFER_ARB,
object_info[i].num_verts * 16 * sizeof(float),
object_info[i].verts, GL_STATIC_DRAW_ARB);
}
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(bumpmapping_opt));
curr = gtk_container_get_children(GTK_CONTAINER(menu));
for(i = 0; i < BUMPMAP_MAX && curr; ++i)
{
if(programs[i] == 0)
gtk_widget_set_sensitive(GTK_WIDGET(curr->data), 0);
curr = curr->next;
}
}
else
{
gtk_widget_set_sensitive(gloss_opt, 0);
gtk_widget_set_sensitive(bumpmapping_opt, 0);
gtk_widget_set_sensitive(specular_check, 0);
gtk_widget_set_sensitive(specular_exp_range, 0);
gtk_widget_set_sensitive(ambient_color_btn, 0);
gtk_widget_set_sensitive(diffuse_color_btn, 0);
gtk_widget_set_sensitive(specular_color_btn, 0);
}
object_rot[0] = object_rot[1] = object_rot[2] = 0;
light_rot[0] = light_rot[1] = light_rot[2] = 0;
scene_rot[0] = scene_rot[1] = scene_rot[2] = 0;
zoom = 2;
gdk_gl_drawable_gl_end(gldrawable);
}
static void draw_object(int obj, vec3 l, matrix m)
{
const int vsize = 16 * sizeof(float);
int i;
vec3 c, t, b, n;
vec2 uv;
float *verts;
unsigned short *indices;
if(obj < 0 || obj >= OBJECT_MAX) return;
if(has_glsl)
{
glBindBufferARB(GL_ARRAY_BUFFER_ARB, object_info[obj].vbo);
#define OFFSET(x) ((void*)((x) * sizeof(float)))
glVertexPointer(4, GL_FLOAT, vsize, OFFSET(0));