| 1 | /**************************************************************************** |
| 2 | * VCGLib o o * |
| 3 | * Visual and Computer Graphics Library o o * |
| 4 | * _ O _ * |
| 5 | * Copyright(C) 2004 \/)\/ * |
| 6 | * Visual Computing Lab /\/| * |
| 7 | * ISTI - Italian National Research Council | * |
| 8 | * \ * |
| 9 | * All rights reserved. * |
| 10 | * * |
| 11 | * This program is free software; you can redistribute it and/or modify * |
| 12 | * it under the terms of the GNU General Public License as published by * |
| 13 | * the Free Software Foundation; either version 2 of the License, or * |
| 14 | * (at your option) any later version. * |
| 15 | * * |
| 16 | * This program is distributed in the hope that it will be useful, * |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 19 | * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * |
| 20 | * for more details. * |
| 21 | * * |
| 22 | ****************************************************************************/ |
| 23 | /**************************************************************************** |
| 24 | History |
| 25 | |
| 26 | $Log: space.h,v $ |
| 27 | Revision 1.11 2007/08/17 07:00:00 cignoni |
| 28 | added a missing typename |
| 29 | |
| 30 | Revision 1.10 2007/07/31 12:21:50 ganovelli |
| 31 | added gltetra, added normal gltriangle |
| 32 | |
| 33 | Revision 1.9 2007/05/08 18:55:38 ganovelli |
| 34 | glTriangle added |
| 35 | |
| 36 | Revision 1.8 2007/01/18 01:26:23 cignoni |
| 37 | Added cast for mac compiling |
| 38 | |
| 39 | Revision 1.7 2005/10/13 08:32:26 cignoni |
| 40 | Added glscale(scalar) and corrected bug in glscale(point2) |
| 41 | |
| 42 | Revision 1.6 2005/06/30 10:17:04 ganovelli |
| 43 | added draw plane |
| 44 | |
| 45 | Revision 1.5 2005/05/05 12:28:13 cignoni |
| 46 | added glboxwire |
| 47 | |
| 48 | Revision 1.4 2004/07/07 23:30:28 cignoni |
| 49 | Added box3 drawing functions |
| 50 | |
| 51 | Revision 1.3 2004/05/26 15:13:01 cignoni |
| 52 | Removed inclusion of gl extension stuff and added glcolor stuff |
| 53 | |
| 54 | Revision 1.2 2004/05/13 23:44:47 ponchio |
| 55 | <GL/GL.h> --> <GL/gl.h> |
| 56 | |
| 57 | Revision 1.1 2004/04/05 11:56:14 cignoni |
| 58 | First working version! |
| 59 | |
| 60 | |
| 61 | ****************************************************************************/ |
| 62 | |
| 63 | #ifndef VCG_GL_SPACE_H |
| 64 | #define VCG_GL_SPACE_H |
| 65 | |
| 66 | // Please note that this file assume that you have already included your |
| 67 | // gl-extension wrapping utility, and that therefore all the extension symbol are already defined. |
| 68 | |
| 69 | #include <vcg/space/triangle3.h> |
| 70 | #include <vcg/space/plane3.h> |
| 71 | #include <vcg/space/point2.h> |
| 72 | #include <vcg/space/point3.h> |
| 73 | #include <vcg/space/color4.h> |
| 74 | #include <vcg/space/box2.h> |
| 75 | #include <vcg/space/box3.h> |
| 76 | |
| 77 | namespace vcg { |
| 78 | |
| 79 | inline void glScale(float const & p){ glScalef(p,p,p);} |
| 80 | inline void glScale(double const & p){ glScaled(p,p,p);} |
| 81 | |
| 82 | inline void glVertex(Point2<int> const & p) { glVertex2iv((const GLint*)p.V());} |
| 83 | inline void glVertex(Point2<short> const & p) { glVertex2sv(p.V());} |
| 84 | inline void glVertex(Point2<float> const & p) { glVertex2fv(p.V());} |
| 85 | inline void glVertex(Point2<double> const & p){ glVertex2dv(p.V());} |
| 86 | inline void glTexCoord(Point2<int> const & p) { glTexCoord2iv((const GLint*)p.V());} |
| 87 | inline void glTexCoord(Point2<short> const & p) { glTexCoord2sv(p.V());} |
| 88 | inline void glTexCoord(Point2<float> const & p) { glTexCoord2fv(p.V());} |
| 89 | inline void glTexCoord(Point2<double> const & p){ glTexCoord2dv(p.V());} |
| 90 | inline void glTranslate(Point2<float> const & p) { glTranslatef(p[0],p[1],0);} |
| 91 | inline void glTranslate(Point2<double> const & p){ glTranslated(p[0],p[1],0);} |
| 92 | inline void glScale(Point2<float> const & p) { glScalef(p[0],p[1],1.0);} |
| 93 | inline void glScale(Point2<double> const & p){ glScaled(p[0],p[1],1.0);} |
| 94 | |
| 95 | inline void glVertex(Point3<int> const & p) { glVertex3iv((const GLint*)p.V());} |
| 96 | inline void glVertex(Point3<short> const & p) { glVertex3sv(p.V());} |
| 97 | inline void glVertex(Point3<float> const & p) { glVertex3fv(p.V());} |
| 98 | inline void glVertex(Point3<double> const & p){ glVertex3dv(p.V());} |
| 99 | inline void glNormal(Point3<int> const & p) { glNormal3iv((const GLint*)p.V());} |
| 100 | inline void glNormal(Point3<short> const & p) { glNormal3sv(p.V());} |
| 101 | inline void glNormal(Point3<float> const & p) { glNormal3fv(p.V());} |
| 102 | inline void glNormal(Point3<double> const & p){ glNormal3dv(p.V());} |
| 103 | inline void glTexCoord(Point3<int> const & p) { glTexCoord3iv((const GLint*)p.V());} |
| 104 | inline void glTexCoord(Point3<short> const & p) { glTexCoord3sv(p.V());} |
| 105 | inline void glTexCoord(Point3<float> const & p) { glTexCoord3fv(p.V());} |
| 106 | inline void glTexCoord(Point3<double> const & p){ glTexCoord3dv(p.V());} |
| 107 | inline void glTranslate(Point3<float> const & p) { glTranslatef(p[0],p[1],p[2]);} |
| 108 | inline void glTranslate(Point3<double> const & p){ glTranslated(p[0],p[1],p[2]);} |
| 109 | inline void glScale(Point3<float> const & p) { glScalef(p[0],p[1],p[2]);} |
| 110 | inline void glScale(Point3<double> const & p){ glScaled(p[0],p[1],p[2]);} |
| 111 | |
| 112 | inline void glColor(Color4b const & c) { glColor4ubv(c.V());} |
| 113 | inline void glClearColor(Color4b const &c) { ::glClearColor(float(c[0])/255.0f,float(c[1])/255.0f,float(c[2])/255.0f,1.0f);} |
| 114 | inline void glLight(GLenum light, GLenum pname, Color4b const & c) { |
| 115 | static float cf[4]; |
| 116 | cf[0]=float(cf[0]/255.0); cf[1]=float(c[1]/255.0); cf[2]=float(c[2]/255.0); cf[3]=float(c[3]/255.0); |
| 117 | glLightfv(light,pname,cf); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | template <class T> |
| 122 | inline void glBoxWire(Box3<T> const & b) |
| 123 | { |
| 124 | glPushAttrib(GL_ENABLE_BIT); |
| 125 | glDisable(GL_LIGHTING); |
| 126 | glBegin(GL_LINE_STRIP); |
| 127 | glVertex3f((float)b.min[0],(float)b.min[1],(float)b.min[2]); |
| 128 | glVertex3f((float)b.max[0],(float)b.min[1],(float)b.min[2]); |
| 129 | glVertex3f((float)b.max[0],(float)b.max[1],(float)b.min[2]); |
| 130 | glVertex3f((float)b.min[0],(float)b.max[1],(float)b.min[2]); |
| 131 | glVertex3f((float)b.min[0],(float)b.min[1],(float)b.min[2]); |
| 132 | glEnd(); |
| 133 | glBegin(GL_LINE_STRIP); |
| 134 | glVertex3f((float)b.min[0],(float)b.min[1],(float)b.max[2]); |
| 135 | glVertex3f((float)b.max[0],(float)b.min[1],(float)b.max[2]); |
| 136 | glVertex3f((float)b.max[0],(float)b.max[1],(float)b.max[2]); |
| 137 | glVertex3f((float)b.min[0],(float)b.max[1],(float)b.max[2]); |
| 138 | glVertex3f((float)b.min[0],(float)b.min[1],(float)b.max[2]); |
| 139 | glEnd(); |
| 140 | glBegin(GL_LINES); |
| 141 | glVertex3f((float)b.min[0],(float)b.min[1],(float)b.min[2]); |
| 142 | glVertex3f((float)b.min[0],(float)b.min[1],(float)b.max[2]); |
| 143 | |
| 144 | glVertex3f((float)b.max[0],(float)b.min[1],(float)b.min[2]); |
| 145 | glVertex3f((float)b.max[0],(float)b.min[1],(float)b.max[2]); |
| 146 | |
| 147 | glVertex3f((float)b.max[0],(float)b.max[1],(float)b.min[2]); |
| 148 | glVertex3f((float)b.max[0],(float)b.max[1],(float)b.max[2]); |
| 149 | |
| 150 | glVertex3f((float)b.min[0],(float)b.max[1],(float)b.min[2]); |
| 151 | glVertex3f((float)b.min[0],(float)b.max[1],(float)b.max[2]); |
| 152 | glEnd(); |
| 153 | glPopAttrib(); |
| 154 | }; |
| 155 | template <class T> |
| 156 | /// Funzione di utilita' per la visualizzazione in OpenGL (flat shaded) |
| 157 | inline void glBoxFlat(Box3<T> const & b) |
| 158 | { |
| 159 | glPushAttrib(GL_SHADE_MODEL); |
| 160 | glShadeModel(GL_FLAT); |
| 161 | glBegin(GL_QUAD_STRIP); |
| 162 | glNormal3f(.0f,.0f,1.0f); |
| 163 | glVertex3f(b.min[0], b.max[1], b.max[2]); |
| 164 | glVertex3f(b.min[0], b.min[1], b.max[2]); |
| 165 | glVertex3f(b.max[0], b.max[1], b.max[2]); |
| 166 | glVertex3f(b.max[0], b.min[1], b.max[2]); |
| 167 | glNormal3f(1.0f,.0f,.0f); |
| 168 | glVertex3f(b.max[0], b.max[1], b.min[2]); |
| 169 | glVertex3f(b.max[0], b.min[1], b.min[2]); |
| 170 | glNormal3f(.0f,.0f,-1.0f); |
| 171 | glVertex3f(b.min[0], b.max[1], b.min[2]); |
| 172 | glVertex3f(b.min[0], b.min[1], b.min[2]); |
| 173 | glNormal3f(-1.0f,.0f,.0f); |
| 174 | glVertex3f(b.min[0], b.max[1], b.max[2]); |
| 175 | glVertex3f(b.min[0], b.min[1], b.max[2]); |
| 176 | glEnd(); |
| 177 | |
| 178 | glBegin(GL_QUADS); |
| 179 | glNormal3f(.0f,1.0f,.0f); |
| 180 | glVertex3f(b.min[0], b.max[1], b.max[2]); |
| 181 | glVertex3f(b.max[0], b.max[1], b.max[2]); |
| 182 | glVertex3f(b.max[0], b.max[1], b.min[2]); |
| 183 | glVertex3f(b.min[0], b.max[1], b.min[2]); |
| 184 | |
| 185 | glNormal3f(.0f,-1.0f,.0f); |
| 186 | glVertex3f(b.min[0], b.min[1], b.min[2]); |
| 187 | glVertex3f(b.max[0], b.min[1], b.min[2]); |
| 188 | glVertex3f(b.max[0], b.min[1], b.max[2]); |
| 189 | glVertex3f(b.min[0], b.min[1], b.max[2]); |
| 190 | glEnd(); |
| 191 | glPopAttrib(); |
| 192 | }; |
| 193 | |
| 194 | |
| 195 | template <class T> |
| 196 | /// Setta i sei clip planes di opengl a far vedere solo l'interno del box |
| 197 | inline void glBoxClip(const Box3<T> & b) |
| 198 | { |
| 199 | double eq[4]; |
| 200 | eq[0]= 1; eq[1]= 0; eq[2]= 0; eq[3]=(double)-b.min[0]; |
| 201 | glClipPlane(GL_CLIP_PLANE0,eq); |
| 202 | eq[0]=-1; eq[1]= 0; eq[2]= 0; eq[3]=(double) b.max[0]; |
| 203 | glClipPlane(GL_CLIP_PLANE1,eq); |
| 204 | |
| 205 | eq[0]= 0; eq[1]= 1; eq[2]= 0; eq[3]=(double)-b.min[1]; |
| 206 | glClipPlane(GL_CLIP_PLANE2,eq); |
| 207 | eq[0]= 0; eq[1]=-1; eq[2]= 0; eq[3]=(double) b.max[1]; |
| 208 | glClipPlane(GL_CLIP_PLANE3,eq); |
| 209 | |
| 210 | |
| 211 | eq[0]= 0; eq[1]= 0; eq[2]= 1; eq[3]=(double)-b.min[2]; |
| 212 | glClipPlane(GL_CLIP_PLANE4,eq); |
| 213 | eq[0]= 0; eq[1]= 0; eq[2]=-1; eq[3]=(double) b.max[2]; |
| 214 | glClipPlane(GL_CLIP_PLANE5,eq); |
| 215 | } |
| 216 | template <class T> |
| 217 | inline void glBoxWire(const Box2<T> & b) |
| 218 | { |
| 219 | glPushAttrib(GL_ENABLE_BIT); |
| 220 | glDisable(GL_LIGHTING); |
| 221 | glBegin(GL_LINE_LOOP); |
| 222 | |
| 223 | glVertex2f((float)b.min[0],(float)b.min[1]); |
| 224 | glVertex2f((float)b.max[0],(float)b.min[1]); |
| 225 | glVertex2f((float)b.max[0],(float)b.max[1]); |
| 226 | glVertex2f((float)b.min[0],(float)b.max[1]); |
| 227 | glEnd(); |
| 228 | |
| 229 | glPopAttrib(); |
| 230 | }; |
| 231 | template <class T> |
| 232 | inline void glPlane3( Plane3<T> p, Point3<T> c, T size ) { |
| 233 | Point3<T> w = p.Direction(); |
| 234 | Point3<T> u,v,c1; |
| 235 | GetUV<T>(w,u,v); |
| 236 | |
| 237 | c1 = p.Projection(c); |
| 238 | |
| 239 | u.Normalize(); |
| 240 | w.Normalize(); |
| 241 | v.Normalize(); |
| 242 | |
| 243 | Matrix44<T> m; |
| 244 | *(Point3<T>*)&m[0][0] = *(Point3<T>*)&u[0];m[0][3]=0; |
| 245 | *(Point3<T>*)&m[1][0] = *(Point3<T>*)&w[0];m[1][3]=0; |
| 246 | *(Point3<T>*)&m[2][0] = *(Point3<T>*)&v[0];m[2][3]=0; |
| 247 | *(Point3<T>*)&m[3][0] = *(Point3<T>*)&c1[0];m[3][3]=1; |
| 248 | |
| 249 | vcg::Transpose(m); |
| 250 | glPushMatrix(); |
| 251 | glMultMatrix(m); |
| 252 | |
| 253 | glBegin(GL_QUADS); |
| 254 | glNormal(Point3<T>(0,1,0)); |
| 255 | glVertex(Point3<T>(-size,0,-size)); |
| 256 | glVertex(Point3<T>(size ,0,-size)); |
| 257 | glVertex(Point3<T>(size ,0, size)); |
| 258 | glVertex(Point3<T>(-size,0, size)); |
| 259 | glEnd(); |
| 260 | |
| 261 | |
| 262 | glPopMatrix(); |
| 263 | } |
| 264 | |
| 265 | |
| 266 | template <class TriangleType> |
| 267 | inline void glTriangle3( TriangleType & c ) { |
| 268 | vcg::Point3<typename TriangleType::ScalarType> n = vcg::Normal(c); |
| 269 | glBegin(GL_TRIANGLES); |
| 270 | glNormal(n); |
| 271 | glVertex(c.P(0)); |
| 272 | glVertex(c.P(1)); |
| 273 | glVertex(c.P(2)); |
| 274 | glEnd(); |
| 275 | } |
| 276 | |
| 277 | template <class TetraType> |
| 278 | inline void glTetra3( TetraType & c ) { |
| 279 | glTriangle3(Triangle3<typename TetraType::ScalarType>(c.P(0),c.P(1),c.P(2))); |
| 280 | glTriangle3(Triangle3<typename TetraType::ScalarType>(c.P(1),c.P(3),c.P(2))); |
| 281 | glTriangle3(Triangle3<typename TetraType::ScalarType>(c.P(0),c.P(2),c.P(3))); |
| 282 | glTriangle3(Triangle3<typename TetraType::ScalarType>(c.P(1),c.P(0),c.P(3))); |
| 283 | } |
| 284 | |
| 285 | }//namespace |
| 286 | #endif |