Advertisement
thecplusplusguy

OpenGL (SDL,C++) - 3d text rendering - main.cpp

Oct 14th, 2011
1,628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 KB | None | 0 0
  1. /*3d Collision text rendering, I only upload the functions.cpp, the text.h text.cpp and third.cpp, the rest of the files are not changed. The third.cpp already contain the material of the next tutorial, because I recordedit after this one.
  2. http://www.youtube.com/user/thecplusplusguy
  3. */
  4. #include "functions.h"
  5. #include "objloader.h"
  6. #include "text.h"
  7. float angle=0.0;
  8.  
  9. int cube;
  10. objloader obj;
  11. bool mousein=false;
  12. coordinate spherecenter(0.0,0.0,0.0);
  13. coordinate raystart(0.0,0.0,0.0);
  14. text* tex;
  15. void init()
  16. {
  17.     glClearColor(0.5,0.5,0.5,1.0);
  18.     glMatrixMode(GL_PROJECTION);
  19.     glLoadIdentity();
  20.     gluPerspective(45,640.0/480.0,1.0,500.0);
  21.     glMatrixMode(GL_MODELVIEW);
  22.     glEnable(GL_DEPTH_TEST);
  23.     cube=obj.load("test10.obj");
  24.     std::vector<unsigned int> chars;
  25.     char tmp[40];
  26.     for(int i=0;i<26;i++)
  27.     {
  28.         sprintf(tmp,"font/%d.obj",i);
  29.         unsigned int tmp2=obj.load(tmp);
  30.         chars.push_back(tmp2);
  31.     }
  32.     tex=new text(0.9,0.8,chars);
  33. //  cube=loadObject("test.obj");
  34.     glEnable(GL_LIGHTING);
  35.     glEnable(GL_LIGHT0);
  36.     float col[]={1.0,1.0,1.0,1.0};
  37.     glLightfv(GL_LIGHT0,GL_DIFFUSE,col);
  38.     initskybox();
  39. }
  40.  
  41. coordinate p1(-5.0,5.0,-5.0);
  42. coordinate p2(5.0,5.0,-5.0);
  43. coordinate p3(5.0,-5.0,-5.0);
  44. coordinate p4(-5.0,-5.0,-5.0);
  45. /*
  46.  
  47. */
  48.  
  49. void display()
  50. {
  51.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  52.     glLoadIdentity();
  53.     Control(0.2,0.2,mousein);
  54.     drawSkybox(50.0);
  55.     UpdateCamera();
  56.     float pos[]={-1.0,1.0,-2.0,1.0};
  57.     glLightfv(GL_LIGHT0,GL_POSITION,pos);
  58. //  glTranslatef(0.0,0.0,-4.0);
  59. //  glRotatef(angle,1,1,1);
  60.     glDisable(GL_LIGHTING);
  61. //  if(raysphere(spherecenter.x,spherecenter.y,spherecenter.z,0.0,0.0,1.0,raystart.x, raystart.y,raystart.z,1.0))
  62.     if(rayplane(0.0,0.0,1.0,raystart.x,raystart.y,raystart.z,0.0,0.0,-1.0,p1,p2,p3,p4))
  63.         glColor3f(1.0,0.0,0.0);
  64.     else
  65.         glColor3f(1.0,1.0,1.0);
  66.     //xs+t*xd
  67.     glBegin(GL_LINES);
  68.         glVertex3f(raystart.x,raystart.y,raystart.z);
  69.         glVertex3f(raystart.x+100*0,raystart.y+100*0,raystart.z+100*-1);
  70.     glEnd();
  71.     glBegin(GL_QUADS);
  72.         glVertex3f(-5.0,5.0,-5.0);
  73.         glVertex3f(5.0,5.0,-5.0);
  74.         glVertex3f(5.0,-5.0,-5.0);
  75.         glVertex3f(-5.0,-5.0,-5.0);
  76.         glColor3f(0,0,0);
  77.         glVertex3f(-10.0,0.0,-10.0);
  78.         glVertex3f(-10.0,-5.0,10.0);
  79.         glVertex3f(10.0,-5.0,10.0);
  80.         glVertex3f(10.0,0.0,-10.0);
  81.     glEnd();
  82.     glEnable(GL_LIGHTING);
  83.     glCallList(cube);
  84.     tex->drawText(coordinate(10,0,0),coordinate(angle,0,0),"HELLO\nWORLD\nFROM\nOPENGL");
  85.     glColor3f(1.0,1.0,1.0);
  86. }
  87.  
  88.                 coordinate p5(-10.0,0,-10.0);
  89.                 coordinate p6(-10.0,-5.0,10.0);
  90.                 coordinate p7(10.0,-5.0,10.0);
  91.                 coordinate p8(10.0,0,-10.0);
  92.  
  93. int main()
  94. {
  95.     SDL_Init(SDL_INIT_EVERYTHING);
  96.     SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|SDL_OPENGL);
  97.     bool running=true;
  98.     Uint32 start;
  99.     SDL_Event event;
  100.     init();
  101.     bool b[4]={0,0,0,0};
  102.     while(running)
  103.     {
  104.         start=SDL_GetTicks();
  105.         while(SDL_PollEvent(&event))
  106.         {
  107.             switch(event.type)
  108.             {
  109.                 case SDL_QUIT:
  110.                     running=false;
  111.                     break;
  112.                 case SDL_MOUSEBUTTONDOWN:
  113.                     mousein=true;
  114.                     SDL_ShowCursor(SDL_DISABLE);
  115.                     break;
  116.                 case SDL_KEYDOWN:
  117.                     if(event.key.keysym.sym==SDLK_p)
  118.                     {
  119.                         mousein=false;
  120.                         SDL_ShowCursor(SDL_ENABLE);
  121.                         break;
  122.                     }
  123.                     if(event.key.keysym.sym==SDLK_ESCAPE)
  124.                     {
  125.                         running=false;
  126.                         break;
  127.                     }  
  128.                     switch(event.key.keysym.sym)
  129.                     {
  130.                         case SDLK_UP:
  131.                             b[0]=1;
  132.                             break;
  133.  
  134.                         case SDLK_LEFT:
  135.                             b[1]=1;
  136.                             break;
  137.  
  138.                         case SDLK_DOWN:
  139.                             b[2]=1;
  140.                             break;
  141.  
  142.                         case SDLK_RIGHT:
  143.                             b[3]=1;
  144.                             break;
  145.                     }
  146.                     break;             
  147.                 case SDL_KEYUP:
  148.                     switch(event.key.keysym.sym)
  149.                     {
  150.                         case SDLK_UP:
  151.                             b[0]=0;
  152.                             break;
  153.  
  154.                         case SDLK_LEFT:
  155.                             b[1]=0;
  156.                             break;
  157.  
  158.                         case SDLK_DOWN:
  159.                             b[2]=0;
  160.                             break;
  161.  
  162.                         case SDLK_RIGHT:
  163.                             b[3]=0;
  164.                             break;
  165.                     }
  166.                     break;                     
  167.             }
  168.         }
  169.         if(b[0])
  170.             raystart.y+=0.3;
  171.         if(b[1])
  172.             raystart.x-=0.3;
  173.         if(b[2])
  174.             raystart.y-=0.3;
  175.         if(b[3])
  176.             raystart.x+=0.3;
  177.         display();
  178.         SDL_GL_SwapBuffers();
  179.         angle+=0.5;
  180.         if(angle>360)
  181.             angle-=360;
  182.         coordinate cameraPos=camPos();
  183.         if(spheresphere(cameraPos,2.0,coordinate(0,0,0),1.0))
  184.             std::cout << "collision\n";
  185.         sphereplane(cameraPos,coordinate(0,0,1),p1,p2,p3,p4,2.0);
  186.         sphereplane(cameraPos,coordinate(0,0.9701425,0.242535625),p5,p6,p7,p8,2.0);
  187.         moveTo(cameraPos);
  188.         if(1000/30>(SDL_GetTicks()-start))
  189.             SDL_Delay(1000/30-(SDL_GetTicks()-start));
  190.     }
  191.     SDL_Quit();
  192.     killskybox();
  193.     delete tex;
  194.     return 0;  
  195. }
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement