opengl学习篇之 画直线 圆 和矩形

// opengl画直线程序框架.cpp : 定义应用程序的入口点。
//

//鼠标画线小程序
#include "stdafx.h"
#include "opengl画直线程序框架.h"
#include <glut.h>
#include <math.h>


#define N 1000       //maximum line numbers
#define Pi 3.14159
int ww, hh;     // for display window width and height
int line[N][4], k = 0;  //for line's endpoint coordinates and line number

void Myinit(void);
void Reshape(int w, int h);
void myMouse(int button, int state, int x, int y);
void myMotion(int x, int y);
void Display(void);
void  drawlines();
void drawRect();
void drawCircular();

int APIENTRY _tWinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPTSTR    lpCmdLine,
    int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    char *argv[] = { "hello ", " " };
    int argc = 2; // must/should match the number of strings in argv

    glutInit(&argc, argv);  //初始化GLUT库;
    glutInitWindowSize(800, 600);  //设置显示窗口大小
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);  //设置显示模式;(注意双缓冲)
    glutCreateWindow("鼠标画线小程序演示"); // 创建显示窗口
    Myinit();
    glutDisplayFunc(Display);  //注册显示回调函数
    glutMouseFunc(myMouse);    //注册鼠标按钮回调函数
    glutMotionFunc(myMotion);  //注册鼠标移动回调函数
    glutReshapeFunc(Reshape);  //注册窗口改变回调函数
    glutMainLoop();  //进入事件处理循环
    return 0;
}

void Myinit(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glLineWidth(3.0);
}

//渲染绘制子程序--------------------------------------------------------------------------
void Display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);   //刷新颜色缓冲区;
    //drawlines();  //画线子程序;
    //drawRect();    //画矩形
    drawCircular();    //画圆
    glutSwapBuffers();  //双缓冲的刷新模式;
}

//-----------------------------------------------
void Reshape(int w, int h)  //窗口改变时自动获取显示窗口的宽w和高h
{
    glMatrixMode(GL_PROJECTION);  //投影矩阵模式
    glLoadIdentity();     //矩阵堆栈清空

    //这两个是一一对应的关系
    glViewport(0, 0, w, h);  //设置视区大小
    gluOrtho2D(0, w, 0, h);   //设置裁剪窗口大小
    ww = w;
    hh = h;
}


//鼠标按钮响应事件..
void myMouse(int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON&&state == GLUT_DOWN)
    {
        line[k][0] = x;   //线段起点x坐标
        line[k][1] = hh - y;  //线段终点y坐标
    }

    if (button == GLUT_LEFT_BUTTON&&state == GLUT_UP)
    {
        line[k][2] = x;   //线段起点x坐标
        line[k][3] = hh - y;   //线段终点y坐标
        k++;
        glutPostRedisplay();
    }
}

//鼠标移动时获得鼠标移动中的坐标-----------------------------------------------------
void myMotion(int x, int y)
{
    //get the line's motion point
    line[k][2] = x;   //动态终点的x坐标
    line[k][3] = hh - y;  //动态终点的y坐标
    glutPostRedisplay();
}

//画线子程序
void  drawlines()
{
    for (int i = 0; i <= k; i++) //********
    {
        glBegin(GL_LINES);
        glVertex2f(line[i][0], line[i][1]);
        glVertex2f(line[i][2], line[i][3]);
        glEnd();
    }
}

//画矩形
void drawRect()
{
    for (int i = 0; i <= k; i++) //********
    {
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);//这个语句的作用告诉opengl只画出边框,但不填充内部。运行效果如下:
        glRectf(line[i][0], line[i][1], line[i][2], line[i][3]);  //绘制矩形
    }
}

//圆形
void drawCircular()
{
    //int n = 100;
    //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);//这个语句的作用告诉opengl只画出边框,但不填充内部。运行效果如下:
    //glBegin(GL_POLYGON);
    //for (int i = 0; i<n; ++i)
    //    glVertex2f(((line[i][0]+ line[i][2])/2.0) + sqrt((line[i][0] - line[i][2])*(line[i][0] - line[i][2])+(line[i][1] - line[i][3])*(line[i][1] - line[i][3]))/2 * cos(2 * Pi / n*i), ((line[i][1] + line[i][3]) / 2.0) + sqrt((line[i][0] - line[i][2])*(line[i][0] - line[i][2])+(line[i][1] - line[i][3])*(line[i][1] - line[i][3]))/2 * sin(2 * Pi / n*i));
    //glEnd();

    int n = 100;
    float R;
    
    for (int i = 0; i <= k; i++)
    {
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);//这个语句的作用告诉opengl只画出边框,但不填充内部。运行效果如下:
        glBegin(GL_POLYGON);
        R = sqrt((line[i][0] - line[i][2])*(line[i][0] - line[i][2]) + (line[i][1] - line[i][3])*(line[i][1] - line[i][3]));
        for (int j = 0; j < n; j++)
        {
            glVertex2f(line[i][0] + R * cos(2 * Pi / n*j), line[i][1] + R * sin(2 * Pi / n*j));
        }
        glEnd();
    }

    
}

猜你喜欢

转载自blog.csdn.net/guanshanyue96/article/details/89040327