Advertisement
plantbae

Test OpenGL Line (Works only from 0° to -45°)

Sep 14th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <windows.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <GL/glut.h>
  6.  
  7. int Xo, Xf;
  8. int Yo, Yf;
  9.  
  10. int H, W;
  11. int yL = 10;
  12. void ConfigureWindow(int newW, int newH);
  13. void OnResizeWindow(int width, int height);
  14. void OnRender(void);
  15. void init();
  16.  
  17. void init(void)
  18. {
  19.     Xo = 0;
  20.     Xf = 0;
  21.     Yo = 0;
  22.     Yf = 0;
  23.     glClearColor(0.0, 0.0, 0.0, 0.0); //parametros: rojo, amarillo y azul, el cuarto es el parametro alpha
  24.     glShadeModel(GL_FLAT);
  25.     glColor3f(1.0, 1.0, 1.0);
  26. }
  27. void OnMouse(int button, int state, int x, int y)
  28. {
  29.     if (button == GLUT_LEFT_BUTTON)
  30.     {
  31.         if (state == GLUT_DOWN)
  32.         {
  33.             Xo = x;
  34.             Yo = y;
  35.         }
  36.     }
  37. }
  38.  
  39. void OnMouseMove(int x, int y)
  40. {
  41.     Xf = x;
  42.     Yf = y;
  43.  
  44.     glutPostRedisplay();
  45. }
  46.  
  47. void pantalla(){
  48.  
  49.     glMatrixMode(GL_MODELVIEW);
  50.     glLoadIdentity();
  51.  
  52.     glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
  53.     glBegin(GL_POINTS);
  54.     int dx, dy, dE, dNE, d, x, y;
  55.     dx = Xf - Xo;
  56.     dy = Yf - Yo;
  57.    
  58.     x = Xo;
  59.     y = Yo;
  60. /////////////////  
  61.     if(x < Xf)
  62.     {
  63.             d = -2 * dy + dx;
  64.             dE = 2 * dy - dx;
  65.             dNE = 2 * (dy - dx);
  66.             //dE < 0 && dNE >0
  67.             while (x < Xf)
  68.    
  69.         {
  70.             if (d <= 0){
  71.                 d = d + dE;
  72.                 x++;
  73.             }
  74.             else{
  75.                 d = d + dNE;
  76.                 x++;
  77.                 y++;
  78.             }
  79.             glVertex2i(x, y);
  80.    
  81.         }
  82.         glEnd();
  83.     }
  84. //////////
  85.  else if (y < Yf)
  86.  {
  87.             d = -dy + 2*dx;
  88.             dE = (3/2)*dy + 1;
  89.             dNE = (3/2)*dy;
  90.             //dE < 0 && dNE >0
  91.             while (x < Xf)
  92.    
  93.         {
  94.             if (d <= 0){
  95.                 d = d + dE;
  96.                 y++;
  97.             }
  98.             else{
  99.                 d = d + dNE;
  100.                 x++;
  101.                 y++;
  102.             }
  103.             glVertex2i(x, y);
  104.         }
  105.         glEnd();
  106.     }
  107.  ////////////////
  108. }
  109. void ConfigureWindow(int newW, int newH)
  110. {
  111.     glViewport(0, 0, newW, newH);
  112.  
  113.     glMatrixMode(GL_PROJECTION);
  114.     glLoadIdentity();
  115.     gluOrtho2D(0, newW, newH, 0);
  116. }
  117.  
  118. void OnResizeWindow(int width, int height)
  119. {
  120.     ConfigureWindow(width, height);
  121.  
  122.     W = width;
  123.     H = height;
  124. }
  125.  
  126. void OnRender(void)
  127. {
  128.     glClear(GL_COLOR_BUFFER_BIT);
  129.     ConfigureWindow(W, H);
  130.     pantalla();
  131.  
  132.     glutSwapBuffers();
  133. }
  134. void reshape(int w, int h)
  135.  
  136. {
  137.     glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  138.     glMatrixMode(GL_PROJECTION);
  139.     glLoadIdentity();
  140.     glOrtho(-10.0, 10.0, -10.0, 10, -10.0, 10.0);
  141.     glMatrixMode(GL_MODELVIEW);
  142.     glLoadIdentity();
  143.  
  144. }
  145.  
  146. int main(int argc, char **argv){
  147.     H = 500;
  148.     W = 500;
  149.     glutInit(&argc, argv);
  150.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  151.     glutCreateWindow("Línea");
  152.     glutReshapeWindow(W, H);
  153.     glutDisplayFunc(OnRender);
  154.     glutReshapeFunc(OnResizeWindow);
  155.     glutMouseFunc(OnMouse);
  156.     glutMotionFunc(OnMouseMove);
  157.     glutMainLoop();
  158.     return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement