fshake3d
0.0.1
FreeformDensity3DSurfaceEditor
|
00001 #include "scene_ElevationGrid.hpp" 00002 #include "opengl.hpp" 00003 #include <float.h> 00004 #include <gmtl/Intersection.h> 00005 00006 void ElevationGrid::drawPointLists( GLint listid ) 00007 { 00008 for (int iy = 0 ; iy < mYSteps ; ++iy) 00009 { 00010 for (int ix = 0; ix < mXSteps ; ++ix) 00011 { 00012 glPushMatrix(); 00013 Point3d p = getPoint(ix,iy); 00014 glTranslated(p[0],p[1],p[2]); 00015 glCallList( listid ); 00016 glPopMatrix(); 00017 } 00018 } 00019 } 00020 00021 void ElevationGrid::drawPoints() 00022 { 00023 glBegin(GL_POINTS); 00024 for (int iy = 0 ; iy < mYSteps ; ++iy) 00025 { 00026 for (int ix = 0; ix < mXSteps ; ++ix) 00027 { 00028 Point3d p = getPoint(ix,iy); 00029 glVertex3d(p[0],p[1],p[2]); 00030 } 00031 } 00032 glEnd(); 00033 } 00034 00035 void ElevationGrid::drawQuadStrips() 00036 { 00037 for (int iy = 0 ; iy < mYSteps-1 ; ++iy) 00038 { 00039 glBegin(GL_QUAD_STRIP); 00040 for (int ix = 0; ix < mXSteps ; ++ix) 00041 { 00042 Point3d p = getPoint(ix,iy+1); 00043 glVertex3d(p[0],p[1],p[2]); 00044 p = getPoint(ix,iy); 00045 glVertex3d(p[0],p[1],p[2]); 00046 } 00047 glEnd(); 00048 } 00049 } 00050 00051 void ElevationGrid::computeNormals() 00052 { 00053 int i = 0; 00054 for (int iy = 0; iy < mYSteps ; ++iy) 00055 { 00056 for (int ix = 0; ix < mXSteps ; ++ix) 00057 { 00058 Point3d p = getPoint(ix,iy); 00059 Point3d l = (ix > 0)? getPoint(ix-1,iy) : p; 00060 Point3d r = (ix < mXSteps-1)? getPoint(ix+1,iy) : p; 00061 Point3d b = (iy > 0)? getPoint(ix,iy-1) : p; 00062 Point3d u = (iy < mYSteps-1)? getPoint(ix,iy+1) : p; 00063 mNormals[i++] = makeNormal( makeCross( Vec3d(r - l), Vec3d(u - b) ) ); 00064 } 00065 } 00066 } 00067 00068 void ElevationGrid::drawQuadStripNormals() 00069 { 00070 for (int iy = 0 ; iy < mYSteps-1 ; ++iy) 00071 { 00072 glBegin(GL_QUAD_STRIP); 00073 for (int ix = 0; ix < mXSteps ; ++ix) 00074 { 00075 00076 Point3d p = getPoint(ix,iy+1); 00077 { 00078 const Vec3d& n = getNormal(ix,iy+1); 00079 glNormal3d(n[0],n[1],n[2]); 00080 } 00081 glVertex3d(p[0],p[1],p[2]); 00082 00083 p = getPoint(ix,iy); 00084 { 00085 const Vec3d& n = getNormal(ix,iy); 00086 glNormal3d(n[0],n[1],n[2]); 00087 } 00088 glVertex3d(p[0],p[1],p[2]); 00089 00090 } 00091 glEnd(); 00092 } 00093 } 00094 00095 void ElevationGrid::drawQuadStripNormalT1D() 00096 { 00097 for (int iy = 0 ; iy < mYSteps-1 ; ++iy) 00098 { 00099 glBegin(GL_QUAD_STRIP); 00100 for (int ix = 0; ix < mXSteps ; ++ix) 00101 { 00102 00103 Point3d p = getPoint(ix,iy+1); 00104 { 00105 const Vec3d& n = getNormal(ix,iy+1); 00106 glNormal3d(n[0],n[1],n[2]); 00107 } 00108 glTexCoord1d(p[2]*0.5); 00109 glVertex3d(p[0],p[1],p[2]); 00110 00111 p = getPoint(ix,iy); 00112 { 00113 const Vec3d& n = getNormal(ix,iy); 00114 glNormal3d(n[0],n[1],n[2]); 00115 } 00116 glTexCoord1d(p[2]*0.5); 00117 glVertex3d(p[0],p[1],p[2]); 00118 00119 } 00120 glEnd(); 00121 } 00122 } 00123 00124 00125 00126 void ElevationGrid::drawGrid() 00127 { 00128 for (int iy = 0 ; iy < mYSteps ; ++iy) 00129 { 00130 glBegin(GL_LINE_STRIP); 00131 for (int ix = 0; ix < mXSteps ; ++ix) 00132 { 00133 Point3d p = getPoint(ix,iy); 00134 glVertex3d(p[0],p[1],p[2]); 00135 } 00136 glEnd(); 00137 } 00138 for (int ix = 0; ix < mXSteps ; ++ix) 00139 { 00140 glBegin(GL_LINE_STRIP); 00141 for (int iy = 0 ; iy < mYSteps ; ++iy) 00142 { 00143 Point3d p = getPoint(ix,iy); 00144 glVertex3d(p[0],p[1],p[2]); 00145 } 00146 glEnd(); 00147 } 00148 } 00149 00150 void ElevationGrid::drawNormalLines(double s) 00151 { 00152 glBegin(GL_LINES); 00153 for (int iy = 0 ; iy < mYSteps ; ++iy) 00154 { 00155 for (int ix = 0; ix < mXSteps ; ++ix) 00156 { 00157 Point3d p = getPoint(ix,iy); 00158 Vec3d n = getNormal(ix,iy); 00159 glVertex3d(p[0],p[1],p[2]); 00160 glVertex3d(p[0]+n[0]*s,p[1]+n[1]*s,p[2]+n[2]*s); 00161 } 00162 } 00163 glEnd(); 00164 00165 } 00166 00167 bool ElevationGrid::intersect(const Rayd& ray, int& hitix, int& hitiy, double hitRadius) 00168 { 00169 double tsmallest = DBL_MAX; 00170 // TODO: use plane/ray intersection test 00171 for (int iy = 0 ; iy < mYSteps ; ++iy) 00172 { 00173 for (int ix = 0; ix < mXSteps ; ++ix) 00174 { 00175 Sphered s( getPoint(ix,iy), hitRadius ); 00176 00177 int numHits; 00178 double t0,t1; 00179 if ( gmtl::intersect(s, ray, numHits, t0, t1) ) 00180 { 00181 double t = (t0+t1)*0.5; 00182 if ( t < tsmallest ) 00183 { 00184 tsmallest = t; 00185 hitix = ix; 00186 hitiy = iy; 00187 } 00188 } 00189 } 00190 } 00191 00192 if (tsmallest != DBL_MAX) 00193 return true; 00194 00195 return false; 00196 } 00197 00198 void ElevationGrid::initGrid() 00199 { 00200 int i = 0; 00201 for (int iy = 0 ; iy < mYSteps ; ++iy) 00202 { 00203 for (int ix = 0; ix < mXSteps ; ++ix) 00204 { 00205 mPositions[i++] = Point3d( 00206 mXLimits[0] + double(mSize[0] * ix) / double(mXSteps-1) 00207 , mYLimits[0] + double(mSize[1] * iy) / double(mYSteps-1) 00208 , 0 00209 ); 00210 } 00211 } 00212 } 00213 00214 /* 00215 void ElevationGrid::computeNormals2() 00216 { 00217 int i = 0; 00218 for (int iy = 0; iy < mYSteps ; ++iy) 00219 { 00220 for (int ix = 0; ix < mXSteps ; ++ix) 00221 { 00222 Point3d p = getPoint(ix,iy); 00223 Point3d l = (ix > 0)? getPoint(ix-1,iy) : p; 00224 Point3d r = (ix < mXSteps-1)? getPoint(ix+1,iy) : p; 00225 Point3d b = (iy > 0)? getPoint(ix,iy-1) : p; 00226 Point3d u = (iy < mYSteps-1)? getPoint(ix,iy+1) : p; 00227 00228 makeNormal(Vec3d(l - p),Vec3d( 00229 00230 mNormals[i++] = makeCross( makeNormal( Vec3d(r - l) ), makeNormal( Vec3d(u - b) ) ); 00231 } 00232 } 00233 } 00234 */ 00235 00236 00237 /* 00238 void ElevationGrid::drawQuadStripN3T1() 00239 { 00240 for (int iy = 0 ; iy < mYSteps-1 ; ++iy) 00241 { 00242 glBegin(GL_QUAD_STRIP); 00243 for (int ix = 0; ix < mXSteps ; ++ix) 00244 { 00245 { 00246 const Vec3d& n = getNormal(ix,iy+1); 00247 glNormal3d(n[0],n[1],n[2]); 00248 glTexCoord1d(p[2]); 00249 } 00250 Point3d p = getPoint(ix,iy+1); 00251 glVertex3d(p[0],p[1],p[2]); 00252 p = getPoint(ix,iy); 00253 glVertex3d(p[0],p[1],p[2]); 00254 { 00255 const Vec3d& n = getNormal(ix,iy); 00256 glNormal3d(n[0],n[1],n[2]); 00257 glTexCoord1d(p[2]); 00258 } 00259 } 00260 glEnd(); 00261 } 00262 } 00263 */