summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/opengles/examples/glutdemoes.pp
blob: 0e2125473d4b30678d923444d1c57d3c1a7e12f4 (plain)
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
{
  GL units for Free Pascal - GLUT demo
  1999 Sebastian Guenther, sguenther@gmx.de
  2008 Jonas Maebe (converted to use vertex arrays)

  You may use this source as starting point for your own programs; consider it
  as Public Domain.
}

{$mode objfpc}

library GLUTDemoES;
uses
  gles11, math;

const

  FPCImg: array[0..4, 0..10] of Byte =
    ((1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1),
     (1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0),
     (1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0),
     (1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0),
     (1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1));

var
  counter: Integer;

(*
    3 __ 2
    /|  |
  7/0|_ |1
   |/  /
   +--+
   4  5

back plane: 0-3-2-1
*)

const
  colors: array[0..7, 0..3] of Single =
    ((0, 0, 0, 1), (0, 0, 1, 1), (0, 1, 0, 1), (0, 1, 1, 1),
     (1, 0, 0, 1), (1, 0, 1, 1), (1, 1, 0, 1), (1, 1, 1, 1));
  corners: array[0..7, 0..2] of Single =
    ((-1, -1, -1), (+1, -1, -1), (+1, +1, -1), (-1, +1, -1),
     (-1, -1, +1), (+1, -1, +1), (+1, +1, +1), (-1, +1, +1));

  indices: array[0..5] of array[0..3] of GLubyte =
    (
     { all vertices must be in ccw order }
     (0,3,1,2), // back
     (4,5,7,6), // front
     (3,0,7,4), // left
     (1,2,5,6), // right
     (3,7,2,6), // top
     (5,4,1,0)  // bottom
    );

procedure DrawCube;
begin
  glEnableClientState(GL_VERTEX_ARRAY);
  if (glGetError() <> GL_NO_ERROR) then
    halt(10);
  glEnableClientState(GL_COLOR_ARRAY);
  if (glGetError() <> GL_NO_ERROR) then
    halt(11);

  glVertexPointer(3,GL_FLOAT,0,@corners);
  if (glGetError() <> GL_NO_ERROR) then
    halt(12);
  glColorPointer(4,GL_FLOAT,0,@colors);
  if (glGetError() <> GL_NO_ERROR) then
    halt(13);

  // this will also draw a bunch of triangles inside the cube, but
  // may still be faster than the commented-out sequence below due
  // to less calls into the renderer
  glDrawElements(GL_TRIANGLE_STRIP,24,GL_UNSIGNED_BYTE,@indices[0]);
  if (glGetError() <> GL_NO_ERROR) then
    halt(14);

(*
  glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[0]);
  glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[1]);
  glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[2]);
  glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[3]);
  glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[4]);
  glDrawElements(GL_TRIANGLE_STRIP,4,GL_UNSIGNED_BYTE,@indices[5]);
*)
  glDisableClientState(GL_VERTEX_ARRAY);
  if (glGetError() <> GL_NO_ERROR) then
    halt(15);
  glDisableClientState(GL_COLOR_ARRAY);
  if (glGetError() <> GL_NO_ERROR) then
    halt(16);
end;


procedure glDraw; cdecl; export;
var
  x, y: Integer;
begin
  Inc(counter);

//  counter:=145;
  glClearColor(0, 0, 0.2, 1);
  if (glGetError() <> GL_NO_ERROR) then
    halt(8);
  glClear(GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT);
  if (glGetError() <> GL_NO_ERROR) then
    halt(9);

  glPushMatrix;
  glTranslatef(0, 0, Sin(Single(counter) / 20.0) * 5.0 - 5.0);
  glRotatef(Sin(Single(counter) / 200.0) * 720.0, 0, 1, 0);
  glRotatef(counter, 0, 0, 1);

  for y := 0 to 4 do
    for x := 0 to 10 do
      if FPCImg[y, x] > 0 then begin
        glPushMatrix;
        glRotatef(x * Sin(Single(counter) / 5.0), 0, 1, 0);
        glRotatef(y * Sin(Single(counter) / 12.0) * 4.0, 0, 0, 1);
        glTranslatef((x - 5) * 1, (2 - y) * 1, 0);
        glScalef(0.4, 0.4, 0.4);
        glRotatef(counter, 0.5, 1, 0);
        DrawCube;
        glPopMatrix;
      end;

  glPopMatrix;
end;


{ gluperspective replacement, from http://iphonedevelopment.blogspot.com/2008/12/gluperspective.html }

procedure gluperspective(fovy, aspect, znear, zfar: GLfloat);
var
  xmin, xmax, ymin, ymax: GLfloat;
begin
// Start in projection mode.
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 ymax := zNear * tan(fovy * pi / 360.0);
 ymin := -ymax;
 xmin := ymin * aspect;
 xmax := ymax * aspect;
 glfrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
end;


procedure glInit; cdecl; export;
begin
  // Enable backface culling
  glEnable(GL_CULL_FACE);
  if (glGetError() <> GL_NO_ERROR) then
    halt(1);

  // Set up depth buffer
  glEnable(GL_DEPTH_TEST);
  if (glGetError() <> GL_NO_ERROR) then
    halt(2);
  glDepthFunc(GL_LESS);
  if (glGetError() <> GL_NO_ERROR) then
    halt(3);

  // Set up projection matrix
  glMatrixMode(GL_PROJECTION);
  if (glGetError() <> GL_NO_ERROR) then
    halt(4);
  glLoadIdentity;
  gluPerspective(90, 1.3, 0.1, 100);
  if (glGetError() <> GL_NO_ERROR) then
    halt(5);
  glMatrixMode(GL_MODELVIEW);
  if (glGetError() <> GL_NO_ERROR) then
    halt(6);
  glLoadIdentity;
  glTranslatef(0, 0, -5.5);
  if (glGetError() <> GL_NO_ERROR) then
    halt(7);
end;


end.