summaryrefslogtreecommitdiff
path: root/trunk/glagen/3d/triangle_gl.cc
blob: 173db4f656de535e449f2d60a326e30d4930f207 (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
//=============================================================================
//
// Glagen : a planet sized landscape generator
// Copyright (C) 2002  Julien Guertault, Hugues Hiegel, Meng-Tih Lam
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//=============================================================================
//
// Glagen : GPL LAndscape GENerator
//
// triangle.cc for Glagen : made by Zavie (Julien Guertault)
//
// www.glagen.org
//
//=============================================================================

#ifndef		DARWIN
# include	<GL/gl.h>
# include	<GL/glu.h>
#else
# include	<gl.h>
# include	<glu.h>
#endif

#include	"data_glagen.hh"
#include	"dot.hh"
#include	"triangle.hh"

//
// Manage the T-case (when the has a 4th point)
//
bool		Triangle :: T_case (const unsigned int id)
{
  Dot		*middle;
  Triangle	*child1;
  Triangle	*child2;

  // Is there a triangle on this side ?
  if (_neighbor_ab != NULL)

    // Then is it split ?
    if (_neighbor_ab->Is_split ())
      {
	// Find the dangling vertex
	child1 = _neighbor_ab->Child (_a);
	child2 = _neighbor_ab->Child (_b);
	if (child1->B () == child2->C ())
	  middle = child1->B ();
	else
	  middle = child1->C ();

	// Display two triangles instead of just one
	glBegin (GL_TRIANGLES);
	_b->Display (id);
	_c->Display (id);
	middle->Display (id);
	_c->Display (id);
	_a->Display (id);
	middle->Display (id);
	glEnd ();

	// The T case is fixed
	return true;
      }
  if (_neighbor_bc != NULL)
    if (_neighbor_bc->Is_split ())
      {
	child1 = _neighbor_bc->Child (_b);
	child2 = _neighbor_bc->Child (_c);
	if (child1->B () == child2->C ())
	  middle = child1->B ();
	else
	  middle = child1->C ();
	glBegin (GL_TRIANGLES);
	_a->Display (id);
	_b->Display (id);
	middle->Display (id);
	_c->Display (id);
	_a->Display (id);
	middle->Display (id);
	glEnd ();
	return true;
      }
  if (_neighbor_ca != NULL)
    if (_neighbor_ca->Is_split ())
      {
	child1 = _neighbor_ca->Child (_c);
	child2 = _neighbor_ca->Child (_a);
	if (child1->B () == child2->C ())
	  middle = child1->B ();
	else
	  middle = child1->C ();
	glBegin (GL_TRIANGLES);
	_b->Display (id);
	_c->Display (id);
	middle->Display (id);
	_a->Display (id);
	_b->Display (id);
	middle->Display (id);
	glEnd ();
	return true;
      }
  // In fact there's no T-case for this triangle
  return false;
}

//
// Display the faces included in the triangle (general case)
//
void		Triangle :: Display (const unsigned int id)
{
  // Test if this triangle has been visited yet
  if (step == glagen.step)
    return;
  step = glagen.step;

  if (glagen.display_all == true || _visible == true)
    {
      // If the triangle has childs, we visit them instead
      if (_child_center != NULL)
	{
	  _child_a->Display (id);
	  _child_b->Display (id);
	  _child_c->Display (id);
	  _child_center->Display (id);
	}
      else
	{
	  // T-case managment
	  if (false == T_case (id))
	    {
	      if (glagen.display_normal == true)
		{
		  float x = (_a->x () + _b->x () + _c->x ()) / 3;
		  float y = (_a->y () + _b->y () + _c->y ()) / 3;
		  float z = (_a->z () + _b->z () + _c->z ()) / 3;

		  // Display the normal vector
		  if (glagen.light)
		    glDisable(GL_LIGHTING);
		  glBegin (GL_LINES);
		  glColor3f(1.0, 0.2, 0.0);
		  glVertex3f(x, y, z);
		  glVertex3f(x + _normal_real.X () / 100,
			     y + _normal_real.Y () / 100,
			     z + _normal_real.Z () / 100);
		  glEnd ();
		  if (glagen.light)
		    glEnable(GL_LIGHTING);
		}
	      // Could it be more simple ?
	      glBegin (GL_TRIANGLES);
	      _a->Display (id);
	      _b->Display (id);
	      _c->Display (id);
	      glEnd ();
	    }
	}
    }
  // Recursive call at the top of the three
  if (NULL == Father ())
    {
      _neighbor_ab->Display (id);
      _neighbor_bc->Display (id);
      _neighbor_ca->Display (id);
    }
}