File: trail.c

package info (click to toggle)
gltron 0.70final-10
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,088 kB
  • sloc: ansic: 19,172; sh: 3,004; cpp: 973; makefile: 266
file content (222 lines) | stat: -rw-r--r-- 5,542 bytes parent folder | download | duplicates (5)
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
#include "video/video.h"
#include "game/game.h"

#define TEX_SPLIT (1.0 - BOW_DIST2) / (1 - BOW_DIST1)
#undef TEX_SPLIT

static float normal1[] = { 1.0, 0.0, 0.0 };
static float normal2[] = { 0.0, 1.0, 0.0 };

/* 
   getDists returns the minimum distance from (the wall) *line to the
   specified (eye) point
   the z component is ignored
 */
float getDist(segment2 *s, float* eye) {
  float n[2];
  float tmp[2];
  n[0] = s->vStart.v[0] + s->vDirection.v[1];
  n[1] = s->vStart.v[1] - s->vDirection.v[0];
  tmp[0] = eye[0] - s->vStart.v[0];
  tmp[1] = eye[1] - s->vStart.v[1];
  if(n[0] == 0 && n[1] == 0) return length(tmp);
  return abs(scalarprod2(n, tmp) / length(n));
}

/*
  getSegmentEnd[XY]() returns the end point of the
  last trail segment line (before the lightcycles bow starts)
*/

float dists[] = { BOW_DIST2, BOW_DIST3, BOW_DIST1, 0 };

float getSegmentEndX(Data *data, int dist) {
  float tlength, blength;
	segment2 *s = data->trails + data->trailOffset;
	
  if(dirsX[data->dir] == 0) 
		return s->vStart.v[0] + s->vDirection.v[0];

  tlength = segment2_Length(s);
  blength = (tlength < 2 * BOW_LENGTH) ? tlength / 2 : BOW_LENGTH;
  return 
		s->vStart.v[0] + s->vDirection.v[0] -
		dists[dist] * blength * dirsX[ data->dir ];
}

float getSegmentEndY(Data *data, int dist) {
  float tlength, blength;
	segment2 *s = data->trails + data->trailOffset;

  if(dirsY[data->dir] == 0)
		return s->vStart.v[1] + s->vDirection.v[1];

  tlength = segment2_Length(s);
  blength = (tlength < 2 * BOW_LENGTH) ? tlength / 2 : BOW_LENGTH;
  return 
		s->vStart.v[1] + s->vDirection.v[1] -
		dists[dist] * blength * dirsY[ data->dir ];
}

/* getSegmentEndUV() calculates the texture coordinates for the last segment */
float getSegmentEndUV(segment2 *s, Data *data) {
  float tlength, blength;
  tlength = segment2_Length(s);
  blength = (tlength < 2 * BOW_LENGTH) ? tlength / 2 : BOW_LENGTH;
  return (tlength - 2 * blength) / DECAL_WIDTH;
}

/* getSegmentUV gets UV coordinates for an ordinary segment */
float getSegmentUV(segment2 *s) {
  return segment2_Length(s) / DECAL_WIDTH;
}

/* 
   drawTrailLines() draws a white line on top of each trail segment
   the alpha value is reduced with increasing distance to the player
*/

void drawTrailLines(Player *p, PlayerVisual *pV) {
  segment2 *s;
  float height;

  float *normal;
  Data *data;
  Camera *cam;

  float trail_top[] = { 1.0, 1.0, 1.0, 1.0 };

  data = p->data;
  cam = p->camera;

  height = data->trail_height;
  if(height <= 0) return;

  /*
  glDepthMask(GL_FALSE);
  glDisable(GL_DEPTH_TEST);
  */

  if (gSettingsCache.antialias_lines) {
    glEnable(GL_LINE_SMOOTH); /* enable line antialiasing */
  }

  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glDisable(GL_LIGHTING);

  glBegin(GL_LINES);

  s = data->trails;
  while(s != data->trails + data->trailOffset) { 
		/* the current line is not drawn */
    /* compute distance from line to eye point */
    getDist(s, cam->cam);
//		alpha = (game2->rules.grid_size - dist / 2) / game2->rules.grid_size;
    // trail_top[3] = alpha;
    glColor4fv(trail_top);
    
    if(s->vDirection.v[1] == 0) normal = normal1;
    else normal = normal2;
    glNormal3fv(normal);
    glVertex3f(s->vStart.v[0],
							 s->vStart.v[1],
							 height);
    glVertex3f(s->vStart.v[0] + s->vDirection.v[0],
							 s->vStart.v[1] + s->vDirection.v[1],
							 height);
    s++;
    polycount++;
  }
  glEnd();

  /* compute distance from line to eye point */
  getDist(s, cam->cam);
  //alpha = (game2->rules.grid_size - dist / 2) / game2->rules.grid_size;
	// trail_top[3] = alpha;
  glColor4fv(trail_top);

  glBegin(GL_LINES);

	glVertex3f(s->vStart.v[0],
						 s->vStart.v[1],
						 height);
  glVertex3f( getSegmentEndX(data, 0),
	      getSegmentEndY(data, 0),
	      height );

  glEnd();

  /* glEnable(GL_LIGHTING); */
  glDisable(GL_BLEND);
  glDisable(GL_LINE_SMOOTH); /* disable line antialiasing */
  
  /*
  glEnable(GL_DEPTH_TEST);
  glDepthMask(GL_TRUE);
  */
}

/* 
   drawTrailShadow() draws a alpha-blended shadow on the floor for each
   trail segment.
   The light source source is (in homogenous coordinates)
   at (-1,-1,1,0) (I hope that's correct)
*/

void drawTrailShadow(Player* p, PlayerVisual *pV) {
  /* states */

  if(gSettingsCache.use_stencil) {
    glEnable(GL_STENCIL_TEST);
    glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
    glStencilFunc(GL_GREATER, 1, 1);
    glEnable(GL_BLEND);
    glColor4fv(shadow_color);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  } else {
    glColor3f(0, 0, 0);
    glDisable(GL_BLEND);
  }

  /* transformation */

  glPushMatrix();
  glMultMatrixf(shadow_matrix);

  /* geometry */
	{
		int vOffset = 0;
		int iOffset = 0;

		TrailMesh mesh;
		mesh.pVertices = (vec3*) malloc(1000 * sizeof(vec3));
		mesh.pNormals = (vec3*) malloc(1000 * sizeof(vec3));
		mesh.pColors = (unsigned char*) malloc(1000 * 4 * sizeof(float));
		mesh.pTexCoords = (vec2*) malloc(1000 * sizeof(vec2));
		mesh.pIndices = (unsigned short*) malloc(1000 * 2);
		mesh.iUsed = 0;
		
		trailGeometry(p, pV, &mesh, &vOffset, &iOffset);
		bowGeometry(p, pV, &mesh, &vOffset, &iOffset);
		trailStatesShadowed();
		trailRender(&mesh);
		// no states restore, because we're drawing shadowed geometry

		free(mesh.pVertices);
		free(mesh.pNormals);
		free(mesh.pColors);
		free(mesh.pTexCoords);
		free(mesh.pIndices);
	}

  /* restore */

  if(gSettingsCache.use_stencil)
    glDisable(GL_STENCIL_TEST);

  glDisable(GL_BLEND);

  glPopMatrix();
}