root/trunk/dglut/dglut_test.d

Revision 172, 5.4 kB (checked in by FeepingCreature, 1 year ago)
  • Went back to the simple blob
Line 
1 module dglut.dglut_test;
2
3 import dglut.opengl, dglut.vector, std.stdio, std.math;
4
5 import std.c.stdlib: exit;
6 extern(System) void key(char key, int x, int y) {
7   if (key == 'q') exit(0);
8 }
9
10 extern(System) {
11   void glutInit(int *argc, char **argv);
12   void glutInitDisplayMode(uint mode);
13   void glutInitWindowSize(int, int);
14   int glutCreateWindow(char *title);
15   void gluOrtho2D(Repeat!(4, double) lrbt);
16   void glutKeyboardFunc(void function(char, int, int));
17   void glutDisplayFunc(void function());
18   void glutIdleFunc(void function());
19   void glutMainLoop();
20   void gluLookAt(Repeat!(3, double) eye, Repeat!(3, double) center, Repeat!(3, double) up);
21   void gluPerspective(double fovy, double aspect, double znear, double zfar);
22   void glutSwapBuffers();
23 }
24
25 mixin ConstEnum!(ushort, "*2", 1, Prepend!(
26   "GLUT_",
27     Expand!("{INDEX|DOUBLE|ACCUM|ALPHA|DEPTH|STENCIL|UNUSED|MULTISAMPLE|STEREO|LUMINANCE}")
28   )
29 );
30 const ushort GLUT_RGB=0, GLUT_RGBA=0, GLUT_SINGLE=0;
31
32 void setupLights() {
33   glEnable(GL_LIGHTING);
34   with (Light[0]) {
35     enable;
36     position(lightpos.parts, true);
37     ambient=[0, 0, 0, 1];
38     diffuse=[1, 1, 1, 1];
39     specular=[1, 1, 1, 1];
40   }
41   /*with (Light[1]) {
42     enable;
43     position([10, 50, 70], true);
44     ambient=[0f, 0f, 0.2f, 1f];
45     diffuse=[2, 1, 1, 1];
46     specular=[1, 1, 1, 1];
47   }*/
48 }
49
50 import std.string: toStringz;
51 import dglut.ext;
52 FrameBuffer test; Texture target;
53 float[32] cameraMatrix, lightMatrix; // projection, view 4x4
54
55 vec3f campos, lightpos;
56 void main(string[] args) {
57   int c_argc=args.length;
58   char*[] c_args;
59   foreach (arg; args) c_args~=toStringz(arg);
60   c_args~=null;
61   glutInit(&c_argc, c_args.ptr);
62   glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
63   glutInitWindowSize(400, 400);
64   auto win = glutCreateWindow("Triangle\0".ptr);
65  
66   campos=vec3f(0, 5, -10);
67   lightpos=vec3f(100, -100, 10);
68  
69   writefln(Extensions.igrep("framebuffer_object"));
70   target=new Texture(1024, 1024);
71   glChecked("CreateFramebuffer", test=new FrameBuffer, test.attach(GL_COLOR_ATTACHMENT0_EXT, GL_RGBA, target));
72   writefln(test, " created - ", test.width, ":", test.height);
73  
74   MatrixMode.Projection;
75   glLoadIdentity;
76   gluPerspective(45, 1.0, 0.1, 100);
77   MatrixMode.Modelview;
78   Texture.enable;
79   glEnable(GL_DEPTH_TEST);
80
81   glutDisplayFunc=&display;
82   glutIdleFunc=&display;
83   glutKeyboardFunc=&key;
84   glutMainLoop;
85   return 0;
86 }
87
88 const FONT=cast(void*) 0x804a018;
89 const CHAR_W=8, CHAR_H=13, CHAR_DESCENT=3, LINE_SEP=2;
90
91 extern(System) {
92   void glutBitmapCharacter(void *font, int ch);
93   void glLoadIdentity();
94 }
95
96 void times(size_t t, void delegate()[] dgs...) { while (t--) foreach (dg; dgs) dg(); }
97
98 extern(System) void display() { d_display_wrap(); }
99 void d_display_wrap() { try glChecked("d_display", d_display); catch (Exception e) { writefln("Error in display: ", e); throw e; } }
100
101 import std.c.time, std.random;
102
103 void d_display() {
104   static int fps;
105   static typeof(time(null)) last;
106   ++fps;
107   if (last!=time(null)) {
108     last=time(null);
109     writefln(fps, " fps");
110     fps=0;
111   }
112   glLoadIdentity;
113   static float f=0;
114   f+=1;
115   Translate(0, 0, -5);
116   Translate(sin(f*0.01), 0, 0);
117   Rotate(f*0.2, 0, 1, 0);
118   Rotate(f*0.1, 1, 1, 0);
119
120   glChecked("RenderToFramebuffer", MatrixScope(test.Using(renderScene(f))));
121   glChecked("RenderToScreen", renderScene(f));
122  
123   glFlush;
124   glutSwapBuffers();
125 }
126
127 void renderScene(float t) {
128   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
129   setupLights();
130   auto points=[
131     vec3f(-1, -1, -1), vec3f(1, -1, -1), vec3f(-1, 1, -1), vec3f(1, 1, -1),
132     vec3f(-1, -1, 1), vec3f(1, -1, 1), vec3f(-1, 1, 1), vec3f(1, 1, 1)
133   ];
134   alias Vector!(vec3f, 3) Triangle;
135  
136   Triangle[] subdivide(Triangle[] from) {
137     auto res=new Triangle[from.length*4];
138     foreach (index, tri; from) with (tri) {
139       //if ((index&127) == 0) writefln(index, "/", from.length);
140       auto xy=(x+y)/2, yz=(y+z)/2, zx=(z+x)/2;
141       res[index*4..index*4+4] = [Triangle(x, xy, zx), Triangle(y, yz, xy), Triangle(z, zx, yz), Triangle(xy, yz, zx)];
142     }
143     return res;
144   }
145  
146   void deform(ref vec3f vec) {
147     vec.normalize;
148     vec.length=sin(vec.x*sin(t*0.01)*16)*0.2 + cos(vec.y*vec.z*cos(t*0.01)*16)*0.2+1;
149   }
150   static Triangle[] cube;
151   Triangle[] face(vec3f a, vec3f b, vec3f c, vec3f d) { return [Triangle(a, b, c), Triangle(c, d, a)][]; }
152   if (!cube.length) {
153     cube=face(points[0], points[2], points[3], points[1])
154       ~ face(points[4], points[5], points[7], points[6])
155       ~ face(points[0], points[1], points[5], points[4])
156       ~ face(points[3], points[2], points[6], points[7])
157       ~ face(points[0], points[4], points[6], points[2])
158       ~ face(points[1], points[3], points[7], points[5])
159       ;
160     times(5, { cube=cube.subdivide(); });
161   }
162   auto normals=new vec3f[cube.length];
163   foreach (id, ref tri; cube) with (tri) {
164     foreach (ref vec; parts) deform(vec);
165     normals[id]=(y-x).cross(z-x).normalized;
166   }
167  
168   static Texture tex;
169   glChecked("DisplayGenerateTexture", {
170     if (!tex) tex=new Texture(64, 64, true, (float x, float y) { return vec3f(sin(x)*2+2, y, (x-0.5)*(y-0.5)); });
171   });
172   with (Material.FrontAndBack) {
173     //specular=[.3f, .3f, .3f, 1f];
174     emission=[0f, 0f, 0f, 1f];
175   }
176   glChecked("DisplayBindTextue", tex.bind);
177   glChecked("DisplayRenderShape", Triangles({
178     foreach (id, t; cube) {
179       Normal(normals[id]);
180       TexCoord(0, 0); Vertex(t.x);
181       TexCoord(0, 1); Vertex(t.y);
182       TexCoord(1, 0); Vertex(t.z);
183     }
184   }));
185   //writefln(cube.length, " triangles rendered");
186 }
Note: See TracBrowser for help on using the browser.