绘制一点、一线、一面

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34195441/article/details/79122213

#include<GL\glut.h>
#include<stdio.h>  
#include<math.h>  
#include<string.h>  
#include<stdlib.h>  
int color[3];  
int wideth=0;  
  
void LineDDA(int x0, int y0, int  x1,  int y1,int color[])  
{  
    int x, dy, dx, y;  
    float m;  
    dx=x1-x0;  
    dy=y1-y0;  
    m=dy;  
    y=y0;  
  
    glColor3f (color[2]*1.0f, color[1]*1.0f, color[0]*1.0f);  
    glPointSize(wideth);  
    for(x=x0;x<=x1;x++)  
    {  
        glBegin (GL_POINTS);  
        glVertex2i (x, (int) (y+0.5));  
        glEnd ();  
        y+=m;  
    }  
}  
  
void myDisplay(void)  
{  
    glClear(GL_COLOR_BUFFER_BIT);  
      
    glColor3f (color[2]*1.0f, color[1]*1.0f, color[0]*1.0f);  
    glRectf (25.0, 25.0, 75.0, 75.0);  
     
    glPointSize(5.0f);  
    glBegin (GL_POINTS);  
    glColor3f (color[2]*1.0f, color[1]*1.0f, color[0]*1.0f);   
    glVertex2f(100.0f,200.0f);  
    glEnd ();  
    
    LineDDA (0, 0, 200, 300,color);  
    
    glBegin (GL_LINES);  
    glColor3f (1.0f, 0.0f, 0.0f); glVertex2f (100.0f, 0.0f);  
    glColor3f (0.0f, 1.0f, 0.0f); glVertex2f (180.0f, 240.0f);  
    glEnd ();  
    
    glFlush ();  
  
}  
  
void  Init()  
{  
    glClearColor (0.0, 0.0, 0.0, 0.0);  
    glShadeModel (GL_FLAT);  
  
}   
  
  
void Reshape (int w, int h)  
{  
    glViewport(0, 0, (GLsizei)  w, (GLsizei)  h);  
    glMatrixMode (GL_PROJECTION);  
    glLoadIdentity();  
    gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);  
}  
  
void select()  
{  
    printf("选择1~7分别为如下颜色");  
    printf("画线颜色定义如下:\n");  
    printf("  蓝     绿     青      红    黄    玫瑰    白\n");  
    printf("1(001) 2(010) 3(011) 4(100) 5(101) 6(110) 7(111)\n");  
    printf("Select[1~7]:\n");  
    int cr;  
    scanf("%d",&cr);  
    int i=0;  
    memset(color,0,sizeof(color));  
    while(cr)  
    {  
      color[i++]=cr%2;  
      cr/=2;  
    }  
    printf("Plese select the line's wideth:\n");  
    scanf("%d",&wideth);  
    wideth*=1.0;  
      
}  
  
int main (int argc, char  *argv[])  
{  
    select();  
    glutInit (&argc, argv);  
    glutInitDisplayMode (GLUT_RGB  | GLUT_SINGLE);  
    glutInitWindowPosition (100,100);  
    glutInitWindowSize (400, 400);  
    glutCreateWindow("OpenGL 2!");  
    Init();  
    glutDisplayFunc (myDisplay);  
    glutReshapeFunc (Reshape);  
    glutMainLoop ();  
    return 0;  
}  


运行截图:


猜你喜欢

转载自blog.csdn.net/qq_34195441/article/details/79122213
今日推荐