File: Mesh.cpp

package info (click to toggle)
structure-synth 1.5.0-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,268 kB
  • ctags: 1,966
  • sloc: cpp: 10,209; python: 164; makefile: 71; sh: 15
file content (131 lines) | stat: -rw-r--r-- 4,294 bytes parent folder | download | duplicates (9)
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
#include "Mesh.h"

#include "SyntopiaCore/Math/Vector3.h"

#include "../Logging/Logging.h"

using namespace SyntopiaCore::Logging;


using namespace SyntopiaCore::Math;

namespace SyntopiaCore {
	namespace GLEngine {


		Mesh::Mesh(SyntopiaCore::Math::Vector3f startBase, 
				SyntopiaCore::Math::Vector3f startDir1 , 
				 SyntopiaCore::Math::Vector3f startDir2, 
				 SyntopiaCore::Math::Vector3f endBase, 
				SyntopiaCore::Math::Vector3f endDir1 , 
				 SyntopiaCore::Math::Vector3f endDir2) : 
				startBase(startBase), startDir1(startDir1), startDir2(startDir2),
				endBase(endBase), endDir1(endDir1), endDir2(endDir2)
		{
			/// Bounding Mesh (not really accurate)
			from = startBase;
			to = startBase;

			Expand(from,to, startBase+startDir1);
			Expand(from,to, startBase+startDir2);
			Expand(from,to, startBase+startDir2+startDir1);
			Expand(from,to, endBase);
			Expand(from,to, endBase+endDir1);
			Expand(from,to, endBase+endDir2);
			Expand(from,to, endBase+endDir1+endDir2);
	
		};

		Mesh::~Mesh() { };

		void Mesh::draw() const {

			// --- TODO: Rewrite - way to many state changes...

			glPushMatrix();
			glTranslatef( startBase.x(), startBase.y(), startBase.z() );
			

			GLfloat col[4] = {0.0f, 1.0f, 1.0f, 0.1f} ;
			glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, col );
			

			glPolygonMode(GL_FRONT, GL_FILL);
			glPolygonMode(GL_BACK, GL_FILL);
				
			Vector3f O(0,0,0);
			Vector3f end = endBase - startBase;
			
			glEnable (GL_LIGHTING);

			glDisable(GL_CULL_FACE); // TODO: do we need this?
			glEnable (GL_BLEND);
			glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
			GLfloat c3[4] = {0.3f, 0.3f, 0.3f, 0.5f};
			glMaterialfv( GL_FRONT, GL_SPECULAR, c3 );
			glMateriali(GL_FRONT, GL_SHININESS, 127);
			glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
			glEnable(GL_COLOR_MATERIAL);
			
					
			glBegin( GL_QUADS );
			glColor4fv(primaryColor);
			GLfloat secondaryColor[4] = {oldRgb[0], oldRgb[1], oldRgb[2], oldAlpha};
			
			//vertex4n(O, startDir1,startDir2+startDir1,startDir2);
			Vector3f c1(startDir1*0.5f+startDir2*0.5f);
			Vector3f c2(end+endDir1*0.5f+endDir2*0.5f);
			vertex4(primaryColor, c1, O, startDir1, secondaryColor,c2,  end+endDir1,end,false);
			vertex4(primaryColor,c1,  O, startDir2, secondaryColor,c2,  end+endDir2,end,false);
			vertex4(primaryColor,c1,  startDir1, startDir1+startDir2,secondaryColor,c2,   end+endDir1+endDir2, end+endDir1,false);
			vertex4(primaryColor,c1,  startDir2, startDir1+startDir2,secondaryColor,c2,  end+endDir1+endDir2, end+endDir2,false);
			//vertex4n(O+end, endDir1+end,endDir2+endDir1+end,endDir2+end);
			glEnd();
			
			glDisable(GL_COLOR_MATERIAL);
			

			glPopMatrix();			
		};

	

		bool Mesh::intersectsRay(RayInfo* ri) {
			if (triangles.count()==0) initTriangles();	

			for (int i = 0; i < triangles.count(); i++) {
				if (triangles[i].intersectsRay(ri)) return true;
			}
			return false;
		};

		void Mesh::initTriangles() {
			triangles.clear();
			
			
			RaytraceTriangle::Vertex4(startBase, startBase+startDir1,endBase+endDir1,endBase, true,triangles,primaryColor[0],primaryColor[1],primaryColor[2],primaryColor[3]);
			RaytraceTriangle::Vertex4(startBase, endBase,endBase+endDir2,startBase+startDir2, true,triangles,primaryColor[0],primaryColor[1],primaryColor[2],primaryColor[3]);
			RaytraceTriangle::Vertex4(startBase+startDir1, startBase+startDir1+startDir2, endBase+endDir1+endDir2, endBase+endDir1, true,triangles,primaryColor[0],primaryColor[1],primaryColor[2],primaryColor[3]);
			RaytraceTriangle::Vertex4(startBase+startDir2, endBase+endDir2, endBase+endDir1+endDir2, startBase+startDir1+startDir2, true,triangles,primaryColor[0],primaryColor[1],primaryColor[2],primaryColor[3]);			
			from = startBase;
			to = startBase;
			for (int i = 0; i < triangles.count(); i++) {
				triangles[i].expandBoundingBox(from,to);
			}
		}

		

		bool Mesh::intersectsAABB(Vector3f from2, Vector3f to2) {
			if (triangles.count()==0) initTriangles();
			return
					    (from.x() < to2.x()) && (to.x() > from2.x()) &&
						(from.y() < to2.y()) && (to.y() > from2.y()) &&
						(from.z() < to2.z()) && (to.z() > from2.z());
		};



	}
}