openGL-輸入點並顯示

綜述

沒有任何技術含量,只是做一個備份。方便自己查閱。

代碼


#include <GLUT/GLUT.h>
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <fstream>
using namespace std;
ofstream outfile;
string path = "/Users/frankdura/Desktop/myfile.txt";
struct Point{
    //定義點的結構體,主要用於編碼
    float x,y;
    int code;
};
vector<float> con_x,con_y;
vector<float> con_x2,con_y2;
int window_size=600;           //這是我們顯示界面的大小

void InitEnvironment()
{
    //一些初始化操作
    glClearColor(0.0,0.0,0.0,0);
    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(4);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluOrtho2D(0,window_size,0,window_size);
}

void drawpoints(float x, float y,int red,int blue,int green){
    glColor3f(red,blue,green);
    glBegin(GL_POINTS);
    glVertex2f(x, window_size-y);
    cout << x << "  " << y<< endl;
    glEnd();
    glFlush();
}
void init_Display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    // 首先繪製裁剪區域
    glColor3f(0.98f, 0.625f, 0.12f);
    //刷新隊列
    //在內部,openGL的命令和語句常常等待在隊列裏
    //直到openGL驅動程序同時處理幾個“命令”
    glFlush();
    for (int i = 0; i < con_x.size() ; ++i) {
//        cout << con_x[i] <<"  "<<con_y[i] << endl;
        drawpoints(con_x2[i],con_y2[i],1,0,1);
    }
    for (int i = 0; i < con_x.size() ; ++i) {
//        cout << con_x[i] <<"  "<<con_y[i] << endl;
        drawpoints(con_x[i],con_y[i],1,1,0);
    }


}


void OnMouse(int button,int state,int x,int y)
{
//    if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
//    {
//            glColor3f(1,0,0);
//            glBegin(GL_POINTS);
//            glVertex2f(x, window_size-y);
//            cout << x << "  " << y<< endl;
//            glEnd();
//            glFlush();
//    }
}

int main(int argc, char *argv[])
{
    cout << "begin!"<<endl;
    float x,y;
    while(cin>>x>> y && x!=0){

        con_x.push_back(x);
        con_y.push_back(y);
    }
    cout << "read!" << con_x.size() << endl;
    while(cin>>x >> y&&x!=0){
        con_x2.push_back(x);
        con_y2.push_back(y);
    }
    cout << "read2!over" << endl;
    glutInit(&argc, argv);   //初始化GLUT
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(300, 100);
    glutInitWindowSize(window_size, window_size);
    glutCreateWindow("show_point");
    InitEnvironment();   //初始化
    glutMouseFunc(&OnMouse);  //註冊鼠標事件
    glutDisplayFunc(&init_Display);   //回調函數
    glutMainLoop();    //持續顯示,當窗口改變會重新繪製圖形
    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章