Advertisement
Gireada

shadow

Apr 24th, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. #include <gl/glos.h>
  2. #include <gl/gl.h>
  3. #include <gl/glu.h>
  4. #include <gl/glaux.h>
  5. #include <conio.h>
  6. #include <math.h>
  7.  
  8. void MakeShadowMatrix(GLfloat points[3][3], GLfloat lightPos[4], GLfloat destMat[4][4])
  9. {
  10.     GLfloat planeCoeff[4];
  11.     GLfloat dot;
  12.  
  13.     calcNormal(points, planeCoeff);
  14.  
  15.     planeCoeff[3] = -(planeCoeff[0] * points[2][0] + planeCoeff[1] * points[2][1] + planeCoeff[2] * points[2][2]);
  16.  
  17.     dot = planeCoeff[0] * lightPos[0] +
  18.         planeCoeff[1] * lightPos[1] +
  19.         planeCoeff[2] * lightPos[2] +
  20.         planeCoeff[3] * lightPos[3];
  21.  
  22.     destMat[0][0] = dot - lightPos[0] * planeCoeff[0];
  23.     destMat[1][0] = 0.0f - lightPos[0] * planeCoeff[1];
  24.     destMat[2][0] = 0.0f - lightPos[0] * planeCoeff[2];
  25.     destMat[3][0] = 0.0f - lightPos[0] * planeCoeff[3];
  26.  
  27.     destMat[0][1] = dot - lightPos[1] * planeCoeff[0];
  28.     destMat[1][1] = 0.0f - lightPos[1] * planeCoeff[1];
  29.     destMat[2][1] = 0.0f - lightPos[1] * planeCoeff[2];
  30.     destMat[3][1] = 0.0f - lightPos[1] * planeCoeff[3];
  31.  
  32.     destMat[0][2] = dot - lightPos[2] * planeCoeff[0];
  33.     destMat[1][2] = 0.0f - lightPos[2] * planeCoeff[1];
  34.     destMat[2][2] = 0.0f - lightPos[2] * planeCoeff[2];
  35.     destMat[3][2] = 0.0f - lightPos[2] * planeCoeff[3];
  36.  
  37.     destMat[0][3] = dot - lightPos[3] * planeCoeff[0];
  38.     destMat[1][3] = 0.0f - lightPos[3] * planeCoeff[1];
  39.     destMat[2][3] = 0.0f - lightPos[3] * planeCoeff[2];
  40.     destMat[3][3] = 0.0f - lightPos[3] * planeCoeff[3];
  41.  
  42. }
  43.  
  44. void CALLBACK deseneaza(void)
  45. {
  46.     GLfloat ambietLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
  47.     GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  48.     GLfloat specuulatLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  49.     GLfloat lightPos[] = { 0, 50, -100, 1 };
  50.     GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  51.  
  52.     GLfloat mynormal[3];
  53.     GLfloat plan[4][3] = {
  54.         {-500, -50, 500},
  55.         {500, -50, 500},
  56.         {500, -50, -500},
  57.         {-500, -50, -500}
  58.     };
  59.     GLfloat plan_[4][3] = {
  60.         {-500, -49.9, 500},
  61.         {500, -49.9, 500},
  62.         {500, -49.9, -500},
  63.         {-500, -49.9, -500}
  64.     };
  65.  
  66.     GLfloat shadowMat[4][4];
  67.     GLfloat pi = 3.14159f;
  68.     GLint i, j, lx = 100, ly = 100;
  69.  
  70.     lightPos[0] = sin(arot[1] * pi * 2 / 360) * 40;
  71.     lightPos[1] = 50;
  72.     lightPos[2] = cos(arot[1] * pi * 2 / 360) * 40 - 150;
  73.  
  74.     MakeShadowMatrix(plan_, lightPos, shadowMat);
  75.  
  76.     glEnable(GL_DEPTH_TEST);
  77.     glFrontFace(GL_CCW);
  78.  
  79.     glEnable(GL_LIGHTING);
  80.  
  81.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
  82.     glLightfx(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
  83.     glLightfx(GL_LIGHT0, GL_SPECULAR, specularLight);
  84.     glLightfx(GL_LIGHT0, GL_POSITION, lightPos);
  85.  
  86.     glEnable(GL_LIGHT0);
  87.  
  88.     glEnable(GL_COLOR_MATERIAL);
  89.  
  90.     glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
  91.  
  92.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  93.  
  94.     glMaterialfx(GL_FRONT, GL_SPECULAR, specref);
  95.     glMateriali(GL_FRONT, GL_SHININESS, 128);
  96.  
  97.     glMatrixMode(GL_MODELVIEW);
  98.  
  99.     glPushAttrib(GL_LIGHTING_BIT);
  100.     glDisable(GL_LIGHTING);
  101.  
  102.     glPopMatrix();
  103.  
  104.     glPushMatrix();
  105.  
  106.     glMultMatrixf((GLfloat *)shadowMat);
  107.     glColor3f(0, 0.3, 0);
  108.     glTranslatef(0, 0, -150);
  109.     glRotatef(a, 0, 1, 0);
  110.     glRotatef(b, 1, 0, 0);
  111.     auxSolidCube(20.0);
  112.  
  113.     glPopMatrix();
  114.  
  115.     glPopAttrib();
  116.     glPushMatrix();
  117.  
  118.     glColor3f(1, 0, 0);
  119.     glTranslatef(0, 0, -150);
  120.     glRotatef(a, 0, 1, 0);
  121.     glRotatef(b, 1, 0, 0);
  122.     auxSolidCube(20.0);
  123.     glPopMatrix();
  124.  
  125.     glPushMatrix();
  126.     glColor3f(0, 1.0, 0);
  127.     glBegin(GL_QUADS);
  128.     glVertex3fv(plan[0]);
  129.     glVertex3fv(plan[1]);
  130.     glVertex3fv(plan[2]);
  131.     glVertex3fv(plan[3]);
  132.     glEnd();
  133.     glPopMatrix();
  134.  
  135.     glFluash();
  136.  
  137.  
  138. }
  139.  
  140. void main()
  141. {
  142.     //initializare modului de afisare –
  143.     //un singur buffer – afisare direct pe ecran –
  144.     //culori după standardul RGBA
  145.     auxInitDisplayMode(AUX_SINGLE | AUX_RGBA);
  146.     //poziţia si dimensiunile ferestrei de afisare
  147.     auxInitPosition(100, 100, 300, 300);
  148.     //initializam fereastra precizand titlul acesteia
  149.     auxInitWindow("Program OpenGL");
  150.  
  151.     // aici se scrie codul OpenGL pentru afişare
  152.     //stergem fereastra folosind culoarea albastru
  153.     glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
  154.     glClear(GL_COLOR_BUFFER_BIT);
  155.  
  156.     auxMainLoop(deseneaza);
  157.     glFlush();
  158.     // Nu inchide! Asteapta apasarea unei taste
  159.     _getch();
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement