/docs/MyDocs

To get this branch, use:
bzr branch http://darksoft.org/webbzr/docs/MyDocs

« back to all changes in this revision

Viewing changes to Development/libraries/opengl/example/objects.h

  • Committer: Suren A. Chilingaryan
  • Date: 2009-04-09 03:21:08 UTC
  • Revision ID: csa@dside.dyndns.org-20090409032108-w4edamdh4adrgdu3
import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
int DrawSimple() {
 
2
    glPolygonMode(GL_FRONT, GL_FILL);
 
3
    glBegin(GL_TRIANGLES);
 
4
    glColor3f(1,0,0);
 
5
    glVertex3f(-1,0,0);
 
6
    glVertex3f(1,0,0);
 
7
    glColor3f(0,0,1); 
 
8
    glVertex3f(0,1,0);
 
9
    glEnd();
 
10
 
 
11
    glPointSize(3.0);
 
12
    glBegin(GL_POINTS);
 
13
    glColor3f(0,1,0);
 
14
    glVertex3f(1,1,1);
 
15
    glEnd();
 
16
    
 
17
    
 
18
    glEnable(GL_LINE_STIPPLE);
 
19
    glLineStipple(1, 0x3F07);
 
20
 
 
21
    glBegin(GL_LINES);
 
22
    glColor3f(0,1,0);
 
23
    glVertex3f(-1,0,0);
 
24
    glVertex3f(1,1,1);
 
25
    glEnd();
 
26
}
 
27
 
 
28
 
 
29
void drawtriangle(float *v1, float *v2, float *v3, long i, int skel) { 
 
30
 float color[] = {((i+2)%3)/2,((i+1)%3)/2,(i%3)/2,1};
 
31
 
 
32
 if (ENABLE_ANTIALIASING) {
 
33
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
34
 }
 
35
 
 
36
 glBegin(GL_POLYGON); 
 
37
      glColor4fv(color);
 
38
      glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
 
39
      glNormal3fv(v1); glVertex3fv(v1);    
 
40
      glNormal3fv(v2); glVertex3fv(v2);    
 
41
      glNormal3fv(v3); glVertex3fv(v3);    
 
42
 glEnd(); 
 
43
 
 
44
 if (skel) {
 
45
    if (ENABLE_ANTIALIASING) {
 
46
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
47
    }
 
48
 
 
49
    glLineWidth(4);
 
50
    glBegin(GL_LINE_LOOP); 
 
51
     glColor3f(0,0,1);
 
52
     glVertex3fv(v1);    
 
53
     glVertex3fv(v2);    
 
54
     glVertex3fv(v3);    
 
55
    glEnd(); 
 
56
 }
 
57
}
 
58
 
 
59
void subdivide(float *v1, float *v2, float *v3, long depth, long idx, int skel) {
 
60
   GLfloat v12[3], v23[3], v31[3];
 
61
   GLint i;
 
62
 
 
63
   if (depth == 0) {
 
64
        drawtriangle(v1, v2, v3, idx, skel);
 
65
        return;
 
66
   }
 
67
   
 
68
   for (i = 0; i < 3; i++) {
 
69
        v12[i] = v1[i]+v2[i];
 
70
        v23[i] = v2[i]+v3[i];
 
71
        v31[i] = v3[i]+v1[i];
 
72
   }
 
73
 
 
74
//   printf("%f %f %f\n %f %f %f\n %f %f %f\n\n", v1[0],v1[1],v1[2], v2[0], v2[1], v2[2], v3[0], v3[1], v3[2]);
 
75
//   printf("%f %f %f\n\n", v12[0], v12[1], v12[2]);
 
76
   normalize(v12);
 
77
   normalize(v23);
 
78
   normalize(v31);
 
79
 
 
80
   subdivide(v1, v12, v31, depth-1, idx, skel);
 
81
   subdivide(v2, v23, v12, depth-1, idx+(1<<(2*(depth-1))), skel);
 
82
   subdivide(v3, v31, v23, depth-1, idx+(2<<(2*(depth-1))), skel);
 
83
   subdivide(v12, v23, v31, depth-1, idx+(3<<(2*(depth-1))), skel);
 
84
}
 
85
 
 
86
 
 
87
void DrawPolyhedron(long depth, int skel) {
 
88
 int i;
 
89
 GLfloat d1[3], d2[3], norm[3];
 
90
 
 
91
#define X .525731112119133606 
 
92
#define Z .850650808352039932
 
93
 
 
94
 static GLfloat vdata[12][3] = {    
 
95
   {-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},    
 
96
   {0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},    
 
97
   {Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0} 
 
98
 };
 
99
 
 
100
 static GLint tindices[20][3] = { 
 
101
   {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},    
 
102
   {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},    
 
103
   {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6}, 
 
104
   {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} 
 
105
 };
 
106
    
 
107
 for (i = 0; i < 20; i++) { 
 
108
      subdivide(
 
109
            &vdata[tindices[i][0]][0],       
 
110
            &vdata[tindices[i][1]][0],       
 
111
            &vdata[tindices[i][2]][0],
 
112
            depth,
 
113
            i<<(2*(depth)),
 
114
            skel
 
115
        ); 
 
116
 }
 
117
}
 
118
 
 
119
void generateTexture(GLubyte *checkImage, long checkImageWidth, long checkImageHeight) {
 
120
    int i, j, r, c;
 
121
 
 
122
       for (i = 0; i < checkImageWidth; i++) {
 
123
        for (j = 0; j < checkImageHeight; j++) {
 
124
            c = ((((i&0x8)==0)^((j&0x8))==0))*255;
 
125
            checkImage[(i*checkImageHeight + j)*3 + 0] = (GLubyte) c;
 
126
            checkImage[(i*checkImageHeight + j)*3 + 1] = (GLubyte) c;
 
127
            checkImage[(i*checkImageHeight + j)*3 + 2] = (GLubyte) c;
 
128
        }
 
129
    }
 
130
}