File: texture.c

package info (click to toggle)
glut 3.7-14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,556 kB
  • ctags: 45,170
  • sloc: ansic: 148,716; makefile: 35,208; ada: 2,062; yacc: 473; fortran: 290; lex: 131; csh: 51; sed: 49; sh: 33
file content (203 lines) | stat: -rw-r--r-- 4,404 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
/*
 * chess.c - part of the chess demo in the glut distribution.
 *
 * (C) Henk Kok (kok@wins.uva.nl)
 *
 * This file can be freely copied, changed, redistributed, etc. as long as
 * this copyright notice stays intact.
 */

/*
 * Marble texture - shamelessly ripped from siggraph92_C23.shar
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include "chess.h"

#define DOT(a,b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])

#define B 256

static int p[B + B + 2];
static GLfloat g[B + B + 2][3];
static int start = 1;

#define setup(i,b0,b1,r0,r1) \
        t = vec[i] + 10000.; \
        b0 = ((int)t) & (B-1); \
        b1 = (b0+1) & (B-1); \
        r0 = t - (int)t; \
        r1 = r0 - 1.;

GLfloat noise3(GLfloat vec[3])
{
        int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
        GLfloat rx0, rx1, ry0, ry1, rz0, rz1, *q, sx, sy, sz, a, b, c, d, t, u, v;
        register int i, j;

        if (start) {
                start = 0;
                init();
        }

        setup(0, bx0,bx1, rx0,rx1);
        setup(1, by0,by1, ry0,ry1);
        setup(2, bz0,bz1, rz0,rz1);

        i = p[ bx0 ];
        j = p[ bx1 ];

        b00 = p[ i + by0 ];
        b10 = p[ j + by0 ];
        b01 = p[ i + by1 ];
        b11 = p[ j + by1 ];
#define at(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )

#define surve(t) ( t * t * (3. - 2. * t) )

#define lerp(t, a, b) ( a + t * (b - a) )

        sx = surve(rx0);
        sy = surve(ry0);
        sz = surve(rz0);


        q = g[ b00 + bz0 ] ; u = at(rx0,ry0,rz0);
        q = g[ b10 + bz0 ] ; v = at(rx1,ry0,rz0);
        a = lerp(sx, u, v);

        q = g[ b01 + bz0 ] ; u = at(rx0,ry1,rz0);
        q = g[ b11 + bz0 ] ; v = at(rx1,ry1,rz0);
        b = lerp(sx, u, v);

        c = lerp(sy, a, b);          /* interpolate in y at lo x */

        q = g[ b00 + bz1 ] ; u = at(rx0,ry0,rz1);
        q = g[ b10 + bz1 ] ; v = at(rx1,ry0,rz1);
        a = lerp(sx, u, v);

        q = g[ b01 + bz1 ] ; u = at(rx0,ry1,rz1);
        q = g[ b11 + bz1 ] ; v = at(rx1,ry1,rz1);
        b = lerp(sx, u, v);

        d = lerp(sy, a, b);          /* interpolate in y at hi x */

        return 1.5 * lerp(sz, c, d); /* interpolate in z */
}

void
init(void)
{
    int i, j, k;
    GLfloat v[3], s;

/* Create an array of random gradient vectors uniformly on the unit sphere */

    srand(1);
    for (i = 0 ; i < B ; i++) {
	do {		/* Choose uniformly in a cube */
	    for (j=0 ; j<3 ; j++)
		v[j] = (GLfloat)((rand() % (B + B)) - B) / B;
	    s = DOT(v,v);
	} while (s > 1.0);	  /* If not in sphere try again */
	s = sqrt(s);
	for (j = 0 ; j < 3 ; j++)       /* Else normalize */
	    g[i][j] = v[j] / s;
    }

/* Create a pseudorandom permutation of [1..B] */

    for (i = 0 ; i < B ; i++)
	p[i] = i;
    for (i = B ; i > 0 ; i -= 2) {
	k = p[i];
	p[i] = p[j = rand() % B];
	p[j] = k;
    }

/* Extend g and p arrays to allow for faster indexing */

    for (i = 0 ; i < B + 2 ; i++) {
	p[B + i] = p[i];
	for (j = 0 ; j < 3 ; j++)
	    g[B + i][j] = g[i][j];
    }
}

GLfloat turbulence(GLfloat x, GLfloat y, GLfloat z, GLfloat lofreq, GLfloat hifreq)
{
    GLfloat freq, t, p[3];

    p[0] = x + 123.456;
    p[1] = y;
    p[2] = z;

    t = 0;
    for (freq = lofreq ; freq < hifreq ; freq *= 2.) {
	t += fabs(noise3(p)) / freq;
	p[0] *= 2.;
	p[1] *= 2.;
	p[2] *= 2.;
    }
    return t - 0.3; /* readjust to make mean value = 0.0 */
}

GLfloat marble(GLfloat x, GLfloat y, GLfloat z)
{
    GLfloat m;
    m = turbulence(x, y, z, 0.3, 400.0);
    if (m > 1.0)
	m = 1.0;
    if (m < 0.0)
	m = 0.0;
    return m;
}

extern GLubyte white_square[TXSX][TXSY][3];
extern GLubyte black_square[TXSX][TXSY][3];
extern GLubyte wood[TXSX][TXSY][3];

void GenerateTextures(void)
{
    int i,j,k;
    GLfloat x,y,t,w,b;
    for (i=0;i<TXSX;i++)
    {
	for (j=0;j<TXSY;j++)
	{
	    x = ((GLfloat) i)/20.0;
	    y = ((GLfloat) j)/20.0;
	    t = marble(x, y, 0.0);

	    t = 0.2 + t;
	    if (t > 1.0)
		t = 1.0;

	    wood[i][j][0] = (0.6*t)*255;
	    wood[i][j][1] = (0.4*t)*255;
	    wood[i][j][2] = (0.5-0.4*t)*255;

	    x = ((GLfloat) i)/20.0;
	    y = ((GLfloat) j)/20.0;
	    t = marble(x, y, 0.0);

	    t = 0.2 + t;
	    if (t > 1.0)
		t = 1.0;

	    w = t;
	    b = 0.8 -t;
	    if (b < 0.0 )
		b = 0.0;

	    for (k=0;k<3;k++)
	    {
		white_square[i][j][k] = w*255;
		black_square[i][TXSY-j][k] = b*255;
	    }
	}
    }
}