Allink
v0.1
|
00001 #include <Draw.h> 00002 00003 #ifdef __glut_h__ 00004 #include <vector> 00005 int Detail = 20; 00006 GLfloat luceAmbiente[4] = { .1, .1, .1,1.}; 00007 GLfloat luceDiffusa[4] = { 1., 1., 1.,1.}; 00008 GLfloat luceSpeculare[] = { 1.0, 1.0, 1.0,1.}; 00009 GLfloat direzione[4] = { 1.0, 1.0, 0.0,1.}; 00010 GLfloat posizioneLight0[4] = { 1., 1., 1.0,0.}; 00011 GLfloat posizioneLight1[4] = { 1., 0., 1.0,0.}; 00012 //int width=600,height=600; 00013 GLint MaterialShininess = 50; 00014 00015 void Draw::DMinimal(void){ 00016 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00017 glClearColor(Rback,Gback,Bback,Aback);//colore sfondo 00018 glPushMatrix();//Whell 00019 glTranslatef(0,0,zw); 00020 glPushMatrix();//Rotation 00021 glRotatef(xa,1.,0.,0.); 00022 glRotatef(ya,0.,1.,0.); 00023 glRotatef(za,0.,0.,1.); 00024 glPushMatrix();//Translation 00025 //glTranslatef(xp,yp,zp); 00026 //glTranslatef(-.5*Edge[0]*InvScaleUn,-.5*Edge[1]*InvScaleUn,-.5*Edge[2]*InvScaleUn); 00027 glCallList(Particles); 00028 ParticleRealTime(); 00029 glPopMatrix();//Translation 00030 glPopMatrix();//Rotation 00031 glPopMatrix();//Wheel 00032 //glFlush(); //Finish rendering 00033 glutSwapBuffers(); 00034 } 00035 void Draw::DFigure(void){ 00036 glMatrixMode( GL_MODELVIEW ); 00037 // pixel = (GLuint *)realloc(pixel,4*width*height*sizeof(*pixel)); 00038 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00039 glClearColor(Rback,Gback,Bback,Aback);//colore sfondo 00040 glPushMatrix();//Whell 00041 glTranslatef(0,0,zw); 00042 glPushMatrix();//Rotation 00043 glRotatef(xa,1.,0.,0.); 00044 glRotatef(ya,0.,1.,0.); 00045 glRotatef(za,0.,0.,1.); 00046 glPushMatrix();//Translation 00047 glTranslatef(xp,yp,zp); 00048 glTranslatef(-.5*Edge[0]*InvScaleUn,-.5*Edge[1]*InvScaleUn,-.5*Edge[2]*InvScaleUn); 00049 if(IfBlend) glEnable(GL_DEPTH_TEST); 00050 else glDisable(GL_DEPTH_TEST); 00051 glBlendFunc(BlendSource,BlendDest); 00052 if(lu != 0){ 00053 glPushMatrix();//Light 00054 glRotatef(xf,1.,0.,0.); 00055 glRotatef(yf,0.,1.,0.); 00056 glRotatef(zf,0.,0.,1.); 00057 glTranslatef(1.5*Edge[0]*InvScaleUn,1.5*Edge[1]*InvScaleUn,1.5*Edge[2]*InvScaleUn); 00058 GLfloat PosLight[4] = {xl0,yl0,zl0,1.}; 00059 glLightfv(GL_LIGHT0,GL_POSITION,PosLight); 00060 //glLightfv(GL_LIGHT1,GL_POSITION,posizioneLight1); 00061 //glutSolidSphere(0.05,20,20); 00062 glPopMatrix();//Light 00063 } 00064 int PreChain = 0; 00065 glCallList(Particles); 00066 if(IfScript) 00067 glCallList(ScriptList); 00068 ParticleRealTime(); 00069 if(IfImage){ 00070 glPushMatrix(); 00071 // glTranslatef(.5,.7,1.6); 00072 glDrawPixels(ImWidth,ImHeight,GL_RGBA,GL_UNSIGNED_BYTE,pixel); 00073 glPopMatrix(); 00074 } 00075 if(la!=0){ 00076 glPushAttrib(GL_LIGHTING_BIT); 00077 glDisable(GL_LIGHTING); 00078 glColor4f(1.-Rback,1.-Gback,1.-Bback,1.0); 00079 glPushMatrix();//Cube 00080 glTranslated(.5*Edge[0]*InvScaleUn,.5*Edge[1]*InvScaleUn,.5*Edge[2]*InvScaleUn); 00081 glScalef((GLfloat)Edge[0]*InvScaleUn, 00082 (GLfloat)Edge[1]*InvScaleUn, 00083 (GLfloat)(Edge[2])*InvScaleUn); 00084 glutWireCube((GLfloat)1.); 00085 glPopMatrix();//Cube 00086 glPopAttrib(); 00087 } 00088 if(gr != 0){ 00089 glColor4f(1.-Rback,1.-Gback,1.-Bback,1.0); 00090 glPushMatrix();//Griglia 00091 //glTranslated(0.,0.,Edge[2]*InvScaleUn); 00092 glPopMatrix();//Griglia 00093 glCallList(Griglia); 00094 } 00095 glPopMatrix();//Translation 00096 glPopMatrix();//Rotation 00097 glPopMatrix();//Wheel 00098 //Text 00099 // glDisable(GL_TEXTURE_2D); 00100 // glDisable(GL_LIGHTING); 00101 // glDisable(GL_DEPTH_TEST); 00102 // glLoadIdentity(); 00103 // glMatrixMode(GL_PROJECTION); 00104 // glPushMatrix(); 00105 // glLoadIdentity(); 00106 // glOrtho(0,WinWidth,0,WinHeight,-1.0,1.0); 00107 if(IfInfo){ 00108 glPushMatrix();//Info 00109 glColor4f(1.-Rback,1.-Gback,1.-Bback,1.0); 00110 glRasterPos3d(xi, yi, zi); 00111 int len = (int)strlen(info); 00112 for (int i = 0; i < len; i++) { 00113 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, info[i]); 00114 //glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, info[i]); 00115 } 00116 glPopMatrix();//Info 00117 } 00118 glCallList(DrLegend); 00119 glFlush(); //Finish rendering 00120 glutSwapBuffers(); 00121 } 00122 void Draw::Draw1(){ 00123 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00124 glPushMatrix();//Translation 00125 glTranslatef(xp,yp,zp); 00126 glPushMatrix();//Rotation 00127 glRotatef(xa,1.,0.,0.); 00128 glRotatef(ya,0.,1.,0.); 00129 glRotatef(za,0.,0.,1.); 00130 glTranslatef(-.5*Edge[0]*InvScaleUn,-.5*Edge[1]*InvScaleUn,-.5*Edge[2]*InvScaleUn); 00131 if(lu != 0){ 00132 glPushMatrix();//Ligth 00133 glRotatef(xf,1.,0.,0.); 00134 glRotatef(yf,0.,1.,0.); 00135 glRotatef(zf,0.,0.,1.); 00136 glTranslatef(1.,1.,Edge[2]*InvScaleUn); 00137 glLightfv(GL_LIGHT0,GL_AMBIENT,luceAmbiente); 00138 glLightfv(GL_LIGHT0,GL_DIFFUSE,luceDiffusa); 00139 glLightfv(GL_LIGHT0,GL_SPECULAR,luceSpeculare); 00140 glLightfv(GL_LIGHT0,GL_POSITION,posizioneLight0); 00141 glutSolidSphere(0.05,20,20); 00142 glPopMatrix();//Light 00143 } 00144 glPushMatrix();//Cube 00145 glTranslatef(.5*Edge[0]*InvScaleUn,.5*Edge[1]*InvScaleUn,.5*Edge[2]*InvScaleUn); 00146 glScalef((GLfloat)Edge[0]*InvScaleUn, 00147 (GLfloat)Edge[1]*InvScaleUn, 00148 (GLfloat)(Edge[2])*InvScaleUn); 00149 glColor4f(1.,1.,1.,1.); 00150 glutWireCube((GLfloat)1); 00151 glPopMatrix();//Cube 00152 // glPushMatrix(); 00153 // glTranslatef(.5,.7,8.); 00154 // glDrawPixels(width,height,GL_RGBA,GL_UNSIGNED_BYTE,pixel); 00155 // glPopMatrix(); 00156 if(1==0) 00157 { 00158 GLfloat Normal[3]={.1,.1,.1}; 00159 glNormalPointer(GL_FLOAT,sizeof(GLfloat),&Normal[0]); //Pointer to the first color*/ 00160 int NPoint = 20; 00161 GLfloat Deltax = 1./(GLfloat) NPoint; 00162 double *Surface = new double [3*NPoint*NPoint]; 00163 glColor4f(1.0,.0,1.,1.); 00164 for(int i=0;i<NPoint;i++){ 00165 for(int j=0;j<NPoint;j++){ 00166 Surface[(i*NPoint+j)*3+0] = Deltax*i+drand48()*.01; 00167 Surface[(i*NPoint+j)*3+1] = Deltax*j+drand48()*.01; 00168 Surface[(i*NPoint+j)*3+2] = drand48()*.05; 00169 } 00170 } 00171 glVertexPointer(3,GL_DOUBLE,3*sizeof(double),Surface); 00172 DrTriangles(NPoint); 00173 } 00174 { 00175 //ShowTexture(); 00176 } 00177 glPushMatrix(); 00178 glTranslatef(.5,.3,.4); 00179 glColor4f(.0,.0,1.,1.); 00180 glutSolidSphere(.2,Detail,Detail); 00181 glPopMatrix(); 00182 glPushMatrix(); 00183 glTranslatef(.5,.7,.6); 00184 glColor4f(.0,1.0,.0,1.); 00185 glutSolidSphere(.2,Detail,Detail); 00186 glPopMatrix(); 00187 glPopMatrix();//Rotation 00188 glPopMatrix();//Translation 00189 glutSwapBuffers(); 00190 } 00191 void Draw::DrTriangles(int NPoint){ 00192 vector <GLuint> VecIndices; 00193 for(int i=0;i<NPoint-1;i++){ 00194 for(int j=0;j<NPoint-1;j++){ 00195 VecIndices.push_back(i*NPoint + j); 00196 VecIndices.push_back((i+1)*NPoint + j); 00197 VecIndices.push_back((i+1)*NPoint + (j+1)); 00198 VecIndices.push_back(i*NPoint + j); 00199 VecIndices.push_back((i+1)*NPoint + (j+1)); 00200 VecIndices.push_back((i)*NPoint + (j+1)); 00201 } 00202 } 00203 int NIndex = VecIndices.size(); 00204 GLuint *Indices = new GLuint [NIndex]; 00205 for(int i=0;i<NIndex;i++)Indices[i] = VecIndices[i]; 00206 // GLuint *Indices; Indices = &VecIndices[0]; 00207 VecIndices.clear(); 00208 glDrawElements(GL_TRIANGLES,NIndex,GL_UNSIGNED_INT,Indices); 00209 free(Indices); 00210 } 00211 void Draw::ShowImage(){ 00212 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00213 glDrawPixels(ImWidth,ImHeight,GL_RGBA,GL_UNSIGNED_BYTE,pixel); 00214 glutSwapBuffers(); 00215 } 00216 00217 int Draw::ApplyTexture(){ 00218 glEnable(GL_TEXTURE_2D); 00219 printf("Texture %d\n",glIsEnabled(GL_TEXTURE_2D)); 00220 if(OpenImage("Texture.tif")==0) {/*printf("Ciccia!\n")*/;return 0;} 00221 glGenTextures(1,&Texture); 00222 glBindTexture(1,Texture); 00223 glTexImage2D(GL_TEXTURE_2D, 0, 4, ImWidth, ImHeight , 0, GL_RGBA, GL_UNSIGNED_BYTE, pixel); 00224 //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 00225 //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 00226 //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 00227 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 00228 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); 00229 //gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture); 00230 return 1; 00231 } 00232 int Draw::ShowTexture(){ 00233 glBindTexture(GL_TEXTURE_2D,Texture); 00234 glBegin(GL_QUADS); 00235 glTexCoord2f(0.,0.); 00236 glTexCoord2f(0.,0.);glVertex3f(0.,0.,1.0); 00237 glTexCoord2f(1.,0.);glVertex3f(1.,0.,1.0); 00238 glTexCoord2f(1.,1.);glVertex3f(1.,1.,1.0); 00239 glTexCoord2f(0.,1.);glVertex3f(0.,1.,1.0); 00240 glEnd(); 00241 glRasterPos3f(0.,0.,0.); 00242 glDrawPixels(ImWidth,ImHeight,GL_RGBA,GL_UNSIGNED_BYTE,pixel); 00243 } 00244 void Draw::Transform(){ 00245 glLineWidth(2); 00246 glDisable(GL_LIGHTING); 00247 Vettore u(.3,.1,.6); 00248 Vettore v(.1,.5,.4); 00249 Vettore w(u.x[0]-v.x[0],u.x[1]-v.x[1],u.x[2]-v.x[2]); 00250 Vettore o(.5,.5,.5); 00251 //Vettore o(.0,.0,.0); 00252 Vettore n(3); 00253 Vettore Norm1(3); 00254 Vettore Norm2(3); 00255 Vettore PS(3); 00256 double Angle = 90.; 00257 Quadri qx(u.x,Angle*DEG_RAD); 00258 Quadri qy(v.x,Angle*DEG_RAD); 00259 Quadri qz(w.x,Angle*DEG_RAD); 00260 //glGetDoublev(GL_MODELVIEW_MATRIX,MatrOld); 00261 qx.Prod(qy); 00262 qx.Prod(qz); 00263 Matrice Mx(qx,4); 00264 Matrice My(qy,4); 00265 Matrice Mz(qz,4); 00266 //Mx.Mult(My); 00267 //Mx.Mult(Mz); 00268 la = 0; 00269 IfScript = 0; 00270 n.Copy(&v); 00271 n.ProjOnAxis(&u); 00272 //Mx.Print(); 00273 //glMatrixMode(GL_MODELVIEW); 00274 glMatrixMode(GL_PROJECTION); 00275 glDeleteLists(Particles,1); 00276 Particles = glGenLists(1); 00277 glNewList(Particles,GL_COMPILE); 00278 glColor4f( .1,.1,.1,1.); 00279 glPushMatrix();//x 00280 glBegin(GL_LINES); 00281 glVertex3d(0.,0.,0.); 00282 glVertex3d(1.,0.,0.); 00283 glEnd(); 00284 glPopMatrix();//Nano 00285 glPushMatrix();//y 00286 glBegin(GL_LINES); 00287 glVertex3d(0.,0.,0.); 00288 glVertex3d(0.,1.,0.); 00289 glEnd(); 00290 glPopMatrix();//Nano 00291 glPushMatrix();//z 00292 glBegin(GL_LINES); 00293 glVertex3d(0.,0.,0.); 00294 glVertex3d(0.,0.,1.); 00295 glEnd(); 00296 glPopMatrix();//Nano 00297 //origin 00298 glPushMatrix();//Nano 00299 glBegin(GL_LINES); 00300 glVertex3d(0.,0.,0.); 00301 glVertex3d(o[0],o[1],o[2]); 00302 glEnd(); 00303 glPopMatrix();//Nano 00304 glColor4f( 1.,.1,.1,1.); 00305 //glMultMatrixd(Mx.data); 00306 PutString(o[0]-.05,o[1]-.05,o[2]-.05,"o"); 00307 //axis 00308 glPushMatrix();//Nano 00309 glBegin(GL_LINES); 00310 glVertex3d(o[0],o[1],o[2]); 00311 glVertex3d(o[0]+u[0],o[1]+u[1],o[2]+u[2]); 00312 glEnd(); 00313 glPopMatrix();//Nano 00314 PutString(o[0]+u[0],o[1]+u[1],o[2]+u[2],"axis"); 00315 glPushMatrix();//Nano 00316 //v 00317 glColor4f( .1,.1,1.,1.); 00318 glPushMatrix();//Nano 00319 glBegin(GL_LINES); 00320 glVertex3d(o[0],o[1],o[2]); 00321 glVertex3d(o[0]+v[0],o[1]+v[1],o[2]+v[2]); 00322 glEnd(); 00323 glPopMatrix();//Nano 00324 PutString(o[0]+v[0],o[1]+v[1],o[2]+v[2],"v"); 00325 glPushMatrix();//Nano 00326 //n 00327 glColor4f( 1.,1.,.1,1.); 00328 glPopMatrix();//Nano 00329 glBegin(GL_LINES); 00330 glVertex3d(o[0]+.1,o[1],o[2]); 00331 glVertex3d(o[0]+n[0]+.1,o[1]+n[1],o[2]+n[2]); 00332 glEnd(); 00333 glPopMatrix();//Nano 00334 PutString(o[0]+n[0]+.1,o[1]+n[1],o[2]+n[2],"n"); 00335 //glCallList(Cylinder); 00336 n.PerpTo(&v,&u); 00337 //glPopMatrix();//Nano 00338 glPushMatrix();//Nano 00339 //v-n 00340 glColor4f( .1,1.,.1,1.); 00341 //glPopMatrix();//Nano 00342 glBegin(GL_LINES); 00343 glVertex3d(o[0]+v[0],o[1]+v[1],o[2]+v[2]); 00344 glVertex3d(o[0]+v[0]+n[0],o[1]+v[1]+n[1],o[2]+v[2]+n[2]); 00345 glEnd(); 00346 glPopMatrix();//Nano 00347 PutString(o[0]+v[0]+n[0],o[1]+v[1]+n[1],o[2]+v[2]+n[2],"v-n"); 00348 //v' 00349 glColor4f(.5,.5,1.,1.); 00350 //glPopMatrix();//Nano 00351 glPushMatrix();//Nano 00352 glBegin(GL_LINES); 00353 glVertex3d(o[0],o[1],o[2]); 00354 glVertex3d(o[0]+v[0]+2.*n[0],o[1]+v[1]+2.*n[1],o[2]+v[2]+2.*n[2]); 00355 glEnd(); 00356 glPopMatrix();//Nano 00357 PutString(o[0]+v[0]+2.*n[0],o[1]+v[1]+2.*n[1],o[2]+v[2]+2.*n[2],"v'"); 00358 //normal 00359 Norm1.Normal(&v,&u); 00360 glColor4f(1.,.1,1.,1.); 00361 //glPopMatrix();//Nano 00362 glPushMatrix();//Nano 00363 glBegin(GL_LINES); 00364 glVertex3d(o[0],o[1],o[2]); 00365 glVertex3d(o[0]+Norm1[0],o[1]+Norm1[1],o[2]+Norm1[2]); 00366 glEnd(); 00367 glPopMatrix();//Nano 00368 PutString(o[0]+Norm1[0],o[1]+Norm1[1],o[2]+Norm1[2],"normal"); 00369 //normal 00370 Norm2.Normal(&u,&Norm1); 00371 glColor4f(1.,.1,1.,1.); 00372 glPushMatrix();//Nano 00373 glBegin(GL_LINES); 00374 glVertex3d(o[0],o[1],o[2]); 00375 glVertex3d(o[0]+Norm2[0],o[1]+Norm2[1],o[2]+Norm2[2]); 00376 glEnd(); 00377 glPopMatrix();//Nano 00378 PutString(o[0]+Norm2[0],o[1]+Norm2[1],o[2]+Norm2[2],"normal"); 00379 //plane 00380 Vettore v1(.3,.6,.8); 00381 //Vettore v2(.2,.7,.3);//out 00382 Vettore v2(.4,.2,.7);//in 00383 Vettore v3(.5,.7,.9); 00384 Piano p1(&v1,&v2,&v3); 00385 glColor4f(.5,.1,1.,1.); 00386 Vettore PSurf = p1.ProjOnSurf(&o); 00387 glPushMatrix();//Nano 00388 glBegin(GL_LINES); 00389 glVertex3d(PSurf[0],PSurf[1],PSurf[2]); 00390 glVertex3d(o[0],o[1],o[2]); 00391 glEnd(); 00392 glColor4f(.8,.1,1.,1.); 00393 glBegin(GL_POLYGON); 00394 glVertex3d(p1.P1[0],p1.P1[1],p1.P1[2]); 00395 glVertex3d(p1.P2[0],p1.P2[1],p1.P2[2]); 00396 glVertex3d(p1.P3[0],p1.P3[1],p1.P3[2]); 00397 glEnd(); 00398 glPopMatrix();//Nano 00399 glPushMatrix();//Nano 00400 if(p1.IsOnSurf(&PSurf)){ 00401 PutString(PSurf[0],PSurf[1],PSurf[2],"in"); 00402 } 00403 else{ 00404 PutString(PSurf[0],PSurf[1],PSurf[2],"out"); 00405 } 00406 glPopMatrix();//Nano 00407 Vettore Vel(.1,.2,.2); 00408 //Vettore Vel1 = p1.Reflect(&Vel); 00409 glPushMatrix();//Nano 00410 glBegin(GL_LINES); 00411 glVertex3d(PSurf[0],PSurf[1],PSurf[2]); 00412 glVertex3d(PSurf[0]+Vel[0],PSurf[1]+Vel[1],PSurf[2]+Vel[2]); 00413 glEnd(); 00414 glPopMatrix();//Nano 00415 glPushMatrix();//Nano 00416 PutString(PSurf[0]+Vel[0],PSurf[1]+Vel[1],PSurf[2]+Vel[2],"vel"); 00417 glPopMatrix();//Nano 00418 p1.Impact(&PSurf,&Vel); 00419 glPushMatrix();//Nano 00420 glBegin(GL_LINES); 00421 glVertex3d(PSurf[0],PSurf[1],PSurf[2]); 00422 glVertex3d(PSurf[0]+Vel[0],PSurf[1]+Vel[1],PSurf[2]+Vel[2]); 00423 glEnd(); 00424 glPopMatrix();//Nano 00425 glPushMatrix();//Nano 00426 PutString(PSurf[0]+Vel[0],PSurf[1]+Vel[1],PSurf[2]+Vel[2],"vel'"); 00427 glPopMatrix();//Nano 00428 // 00429 glEndList(); 00430 } 00431 void Draw::DrCube(){ 00432 glLineWidth(3); 00433 glDeleteLists(Particles,1); 00434 Particles = glGenLists(1); 00435 glNewList(Particles,GL_COMPILE); 00436 glColor4f( 1.,.1,.1,1.); 00437 glEndList(); 00438 } 00439 void Draw::PutString(double *Pos,char *String){ 00440 glRasterPos3f((Pos[0]*InvScaleUn), 00441 (Pos[1]*InvScaleUn), 00442 (Pos[2]*InvScaleUn)); 00443 for(int l=0; l < strlen(String); l++) { 00444 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, String[l]); 00445 //glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,String[l]); 00446 } 00447 glCallList(Point); 00448 } 00449 void Draw::PutString(double Posx,double Posy,double Posz,char *String){ 00450 glRasterPos3f((Posx*InvScaleUn), 00451 (Posy*InvScaleUn), 00452 (Posz*InvScaleUn)); 00453 for(int l=0; l < strlen(String); l++) { 00454 //glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, String[l]); 00455 glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,String[l]); 00456 } 00457 glCallList(Point); 00458 } 00459 #endif// __glut_h__