37a0ec9ae5a621b09fed352449636fc09deae623
[neverball] / share / back.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <SDL.h>
16 #include <math.h>
17 #include <string.h>
18
19 #include "glext.h"
20 #include "vec3.h"
21 #include "back.h"
22 #include "image.h"
23
24 /*---------------------------------------------------------------------------*/
25
26 #define PI 3.1415926535897932
27
28 static GLuint back_list;
29 static GLuint back_text;
30
31 /*---------------------------------------------------------------------------*/
32
33 void back_init(const char *s)
34 {
35     int i, slices = 32;
36     int j, stacks = 16;
37
38     back_free();
39     back_text = make_image_from_file(s);
40
41     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
42
43     back_list = glGenLists(1);
44
45     glNewList(back_list, GL_COMPILE);
46     {
47         glBindTexture(GL_TEXTURE_2D, back_text);
48
49         glColor3f(1.0f, 1.0f, 1.0f);
50
51         for (i = 0; i < stacks; i++)
52         {
53             float k0 = (float)  i      / stacks;
54             float k1 = (float) (i + 1) / stacks;
55
56             float s0 = fsinf(V_PI * (k0 - 0.5));
57             float c0 = fcosf(V_PI * (k0 - 0.5));
58             float s1 = fsinf(V_PI * (k1 - 0.5));
59             float c1 = fcosf(V_PI * (k1 - 0.5));
60
61             glBegin(GL_QUAD_STRIP);
62             {
63                 for (j = 0; j <= slices; j++)
64                 {
65                     float k = (float) j / slices;
66                     float s = fsinf(V_PI * k * 2.0);
67                     float c = fcosf(V_PI * k * 2.0);
68
69                     glTexCoord2f(k, 1.0f - k1);
70                     glVertex3f(s * c1, c * c1, s1);
71
72                     glTexCoord2f(k, 1.0f - k0);
73                     glVertex3f(s * c0, c * c0, s0);
74                 }
75             }
76             glEnd();
77         }
78     }
79     glEndList();
80 }
81
82 void back_free(void)
83 {
84     if (glIsList(back_list))
85         glDeleteLists(back_list, 1);
86
87     if (glIsTexture(back_text))
88         glDeleteTextures(1, &back_text);
89
90     back_list = 0;
91     back_text = 0;
92 }
93
94 void back_draw(float t)
95 {
96     glPushMatrix();
97     {
98         GLfloat dx =  60.f * fsinf(t / 10.f) + 90.f;
99         GLfloat dz = 180.f * fsinf(t / 12.f);
100
101         glDisable(GL_LIGHTING);
102         glDepthMask(GL_FALSE);
103         {
104             glScalef(BACK_DIST, BACK_DIST, BACK_DIST);
105             glRotatef(dz, 0.f, 0.f, 1.f);
106             glRotatef(dx, 1.f, 0.f, 0.f);
107
108             glCallList(back_list);
109         }
110         glDepthMask(GL_TRUE);
111         glEnable(GL_LIGHTING);
112     }
113     glPopMatrix();
114 }
115
116 /*---------------------------------------------------------------------------*/