本文介绍了OpenGL的:如何保持物体在窗口时,它的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用布氏中点算法功课OpenGL的一个MS油漆一样的应用。到目前为止,我可以画直线和椭圆。我失去了所有这些调整窗口大小时。我怎样才能让他们得出?

全部code:

 的#includeGL / glut.h#包括LT&;&stdio.h中GT;
#包括LT&;&math.h中GT;
INT I;// INT的主窗口,子窗口;
INT X1,Y1,X2,Y2;INT modoDeDibujo;INT W = 1000,H = 1000;
/ *无效menuApp(int值)
{
    如果(价值== 1)输出(凌特\\ n);
    如果(价值== 2)输出(Circulo \\ n);
    如果(价值== 3)的printf(省略号\\ n);
    如果(值== 4)出口(0);}无效crearMenu()
{
    // INICIO Creando埃尔菜单
    INT子菜单;
    子菜单= glutCreateMenu(menu​​App);
    glutAddMenuEntry(凌特,1);    glutAddMenuEntry(Elipse,3);
    glutAddMenuEntry(Salir,4);
    glutCreateMenu(menu​​App);
    glutAddSubMenu(子子菜单);
    glutAttachMenu(GLUT_RIGHT_BUTTON);    //鳍Creando埃尔菜单
} * /无效renderPoint(无效)/ * REVISAR ESTO * /
{
    glClear(GL_COLOR_BUFFER_BIT);
    在glBegin(GL_POINTS);
        glVertex2f(-0.98,0.98);
    glEnd();
    glFlush();
}无效renderPoint(双X,双Y)
{    //输出(变换之前%F \\ t%F \\ T#renderPoint \\ n,X,Y);    W = glutGet(GLUT_WINDOW_WIDTH);
    H = glutGet(GLUT_WINDOW_HEIGHT);    浮X;
    浮ÿ;
    在glBegin(GL_POINTS);
        X =(2 * X / W) - 1;
        Y =(-2 * Y / H)+ 1;
        glVertex2f(X,Y);
        //输出(变换点%F \\ t%F \\ T#renderPoint \\ n,X,Y);    glEnd();
    glFlush();}/ *维基伪:功能线(X0,X1,Y0,Y1)// X1
     布尔陡:= ABS(Y1 - Y0)GT; ABS(X1 - X0)// X2
     如果陡峭的话// X3
         掉期(X0,Y0)// X4
         交换(X1,Y1)// X5的
     如果X0> X1则// 5233
         掉期(X0,X1)// X7
         掉期(Y0,Y1)// X8
     INT DELTAX:= X1 - X0 // X9
     INT DELTAY:= ABS(Y1 - Y0)// X10
     INT错误:= DELTAX / 2 // X11
     INT ystep // X12
     INT Y:= Y0 // X13
     如果Y0< Y1然后ystep:= 1否则ystep:= -1 // X14
     从X0到X1点¯x// X15
         如果陡峭的话积(Y,X)其他情节(X,Y)// X16
         错误:=错误 - DELTAY // X17
         如果错误< 0,则// X18
             Y:= Y + ystep // X19
             错误:错误= + DELTAX // X20
* /
空隙bresenham1(闪烁X0,闪烁X1,闪烁Y0,闪烁Y1)//函数线(X0,X1,Y0,Y1)
{
    //双RESULT1 =晶圆厂((双)Y1 - Y0); // ABS(Y1 - Y0)
    //双RESULT2 =晶圆厂((双)×1 - X0); // ABS(X1 - X0)
    INT RESULT1 = ABS(Y1-Y0);
    INT结果2 = ABS(X1-X0);布尔陡=(RESULT1>结果2); //布尔陡:= ABS(Y1 - Y0)GT; ABS(X1 - X0)如果(陡){//如果陡峭的话        闪光辅助1 = X0; //掉期(X0,Y0)
        X0 = Y0;
        Y0 = AUX1;        闪烁AUX2 = X1; //交换(X1,Y1)
        X1 = Y1;
        Y1 = AUX2;}如果(X0> X1){//如果(X0> X1)
        闪烁AUX3 = X0; //掉期(X0,X1)
        X0 = X1;
        X1 = AUX3;        闪烁AUX4 = Y0; //交换(Y0,Y1)
        Y0 = Y1;
        Y1 = AUX4;}INT DELTAX = X1-X0; // DELTAX = X1-X0
INT DELTAY = ABS(Y1-Y0); // INT DELTAY:= ABS(Y1 - Y0) - revisar
INT误差=(DELTAX / 2); // INT错误:= DELTAX / 2
INT ystep; // INT ystepINT Y = Y0; // INT Y:Y0 =    如果(Y0< Y1){//如果Y0< Y1然后ystep:= 1否则ystep:= -1        ystep = 1;    }    其他{ystep = -1;}对于(INT X = X0; X< = X1,X ++){//对于x从X0到X1
  如果(陡){//如果陡峭的话积(Y,X)其他情节(X,Y)       renderPoint(Y,X);
   }
   其他{    renderPoint(X,Y​​);
   }错误=错误 - DELTAY; //错误:=错误 - DELTAY如果(误差和小于0){//如果错误< 0,则
  Y = Y + ystep; // Y:= Y + ystep
  误差=误差+ DELTAX; //错误:错误= + DELTAX} // end如果(误差℃下)
从X0到X1} //结束对
} //结束布氏
无效Plot4EllipsePoints(INT X,诠释Y,INT CX,CY诠释){      renderPoint(CX + X,CY + Y); //象限1分
      renderPoint(CX-X,CY + Y); //象限2点
      renderPoint(CX-X,CY-Y); //象限3点
      renderPoint(CX + X,CY-Y); //象限4点}无效PlotEllipse(INT CX,CY INT,INT XRadius,诠释YRadius为){
        INT X,Y;
        INT享有极高,YChange;
        INT EllipseError;
        INT TwoASquare,TwoBSquare;
        INT StoppingX,StoppingY;
    TwoASquare = 2 * XRadius * XRadius;
    TwoBSquare = 2 * YRadius为* YRadius为;    X = XRadius;
    Y = 0;    享有极高= YRadius为* YRadius为*(1-(2 * XRadius));
    YChange = XRadius * XRadius;    EllipseError = 0;
    StoppingX = TwoBSquare * XRadius;
    StoppingY = 0;    而(StoppingX> = StoppingY){       Plot4EllipsePoints(X,Y,CX,CY);
       ÿ++;
       StoppingY = StoppingY + TwoASquare;       EllipseError = EllipseError + YChange;       YChange = YChange + TwoASquare;       如果(((2 * EllipseError)+ Xchange的)大于0)
       {
            X - ;
            StoppingX = StoppingX - TwoBSquare;
                EllipseError = EllipseError + Xchange的;
                Xchange的= Xchange的+ TwoBSquare;    }
      }      //设置1点完成,开始第二组
     X = 0;
     Y = YRadius为;     Xchange的= YRadius为* YRadius为;
     YChange = XRadius * XRadius *(1-2 * YRadius为);     EllipseError = 0;
     StoppingX = 0;
     StoppingY = TwoASquare * YRadius为;
     而(StoppingX&下; = StoppingY){//第二组点,Y'&所述-1    Plot4EllipsePoints(X,Y,CX,CY);
        X ++;
        StoppingX = StoppingX + TwoBSquare;
        EllipseError = EllipseError + Xchange的;
        Xchange的= Xchange的+ TwoBSquare;       如果(((2 * EllipseError)+ YChange)大于0){           Y--;
           StoppingY = StoppingY - TwoASquare;
           EllipseError = EllipseError + YChange;
           YChange = YChange + TwoASquare;
    }     }}无效renderAll(无效)
{
    / * glutSetWindow(的主窗口);
    glutPostRedisplay();
    glutSetWindow(子窗口);
    glutPostRedisplay(); * /
}无效movimiento(INT BOTON,诠释埃斯塔,诠释的x,int y)对
{
    如果((埃斯塔== GLUT_DOWN)及及(BOTON == GLUT_LEFT_BUTTON))//按下鼠标
    {
        X1 = X; Y1 = Y;
        PlotEllipse(X,Y,200,100);
        renderPoint(X,Y​​);
    }
    如果((埃斯塔== GLUT_UP)及及(BOTON == GLUT_LEFT_BUTTON))//鼠标向上
    {
        //的printf(向上| X:%D,Y数:%d \\ n,X,Y);
        X2 = X; Y2 = Y;
        // renderLine();
        bresenham1(X1,X2,Y1,Y2);
       // PRUEBA山岛LA PRIMITIVA DE OPENGL         在glBegin(GL_LINES);         glEnd();
        // renderPoint(X,Y​​);
    }
}无效的MouseMove(INT X,int y)对
{
    //的printf(X:%d个| Y数:%d \\ n,X,Y);
    X2 = X; Y2 = Y;
    // renderLine();
    // bresenham1(X1,Y1,X2,Y2);
}无效teclado(无符号字符键,诠释的x,int y)对
{
    如果(键== 1){
      modoDeDibujo = 1; // dibuja lineas
      的printf(摩多去dibujo:LINEA);
    }    如果(键== 2){
       modoDeDibujo = 2; // dibuja省略号
    }
    如果(键== 27)出口(0);
}无效ESPECIALES(INT键,诠释的x,int y)对
{
    如果(键== GLUT_KEY_F1)出口(0);
}静态无效
键(unsigned char型K,INT的x,int y)对
{
  开关(K){
  案例27:/ * *逃生/
    出口(0);
    打破;
  默认:
    返回;
  }
  glutPostRedisplay();
}INT主(INT ARGC,CHAR *的argv [])
{
    I = 0;
    // inicializa拉斯operaciones德的OpenGL /葡萄糖转运,DB CR底注德城市搜救funciones转运蛋白
    glutInit(安培; ARGC,ARGV);    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(W,H);
    //克雷亚UNA塔纳德的Opengl
    glutCreateWindow(他利亚);
    glutDisplayFunc(renderPoint);
    glutMouseFunc(movimiento);
    glutKeyboardFunc(teclado); // teclas ASCII
    glutSpecialFunc(ESPECIALES); //图像绘制拉斯teclas [f1..f12]    // glutPassiveMotionFunc(pasivo);
    glutKeyboardFunc(键);
    glutMotionFunc(的MouseMove);
    // crearMenu();
    glutMainLoop();
}


解决方案

首先,你需要安排的code。你必须有且只有一个显示功能,即清除缓冲区,调用其他的平局,函数,并将它们刷新到屏幕(或交换,如果您使用的是双缓冲缓冲)。

在调整窗口大小,GLUT会调用显示功能是renderPoint()如你所知:

  glutDisplayFunc(renderPoint);

renderPoint的清除重绘点,例如前的缓冲区:

 无效renderPoint(无效)/ * REVISAR ESTO * /
{
    glClear(GL_COLOR_BUFFER_BIT);
    在glBegin(GL_POINTS);
        glVertex2f(-0.98,0.98);
    glEnd();
    glFlush();
}

由于缓冲区已经被清除,那些函数renderPoint(圆形的点,线的点..)绘制外已经没有任何意义,因为你没有从主显示功能打电话给他们的所有要点,是renderPoint。

如何保持屏幕上的点?

您必须在缓冲区中的点(你想画的任何点)存储如数组,动态数组,性病::矢量或其他任何东西。在显示功能,写一个循环语句来访问每个点并提取x和y ..然后绘制它们。

例如,代替上述功能,其替换为:

 类MyPoint {
    上市:
    浮X;
    浮ÿ;
    MyPoint(浮法X,浮法Y)
    {
        这 - > X = X;
        这 - > Y = Y;
    }};#包括LT&;矢量>
的std ::矢量<&MyPoint GT;测试点;无效renderPoint(无效)/ * REVISAR ESTO * / {
    testPoints.push_back(MyPoint(-0.58,0.58));
    testPoints.push_back(MyPoint(0.58,0.58));
    testPoints.push_back(MyPoint(0.58,-0.58));    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(2);    在glBegin(GL_POINTS);
        的for(int i = 0; I< testPoints.size();我++)
        {
          glVertex2f(测试点[I] .X,测试点[I] .Y);
        }
    glEnd();    glFlush();
}

你可以看到,通过使用动态数组,如(性病::向量)来存储点,并通过使用一个循环语句中,我们能够保持三点可见。

什么呢?


  • 做同样的方法,用其他形状,这样对于每一个鼠标点击的事件,你可能会添加或的push_back 两点重新$ p $在数组或std :: vector的名为 lineArray psents线终点。在显示功能,做一个for循环语句中提取两行后点绘制每一行。


  • 你应该使用 glutReshapeFunc glViewport ,以确保视口有相同的dimenstions作为调整大小事件发生后窗口。我认为 gluOrtho2d 比试图从Windows映射更优雅的坐标空间的OpenGL的坐标空间


  • 安排您的code,让你只使用一个显示功能。


您的计划可能是这样的:

 无效drawTestPoints()
{
  的for(int i = 0; I<长度;我+ +)
  {
      renderPoint(pointsArray [I] .X,pointsArray [I] .Y);分//矢量/(MyPoint)  }}无效drawLines()
{
  的for(int i = 0; I<长度)
  {      MyPoint的startPoint = linesArray [I]
      MyPoint终点= linesArray [I + 1];
      bresenham1(startPoint.x,endPoint.x,startPoint.y,endPoint.y);
      I + = 2;
  }}无效drawAll()
{
  glClear(GL_COLOR_BUFFER_BIT);    drawTestPoints();
    drawLines();
    drawOtherShapes();  glFlush();
}。

。//主FUNC:
glutDisplayFunc(drawAll);

==

I'm working a MS paint-like application on OpenGL using Bresenham's midpoint algorithm as homework. So far I can draw lines and ellipses. I lose them all when resizing the window. How can I keep them drawn?

Full code:

#include "GL/glut.h"

#include <stdio.h>
#include <math.h>
int i;

//int mainWindow, subWindow;
int X1, Y1, X2, Y2;

int modoDeDibujo; 

int W = 1000, H = 1000;
/*void menuApp (int value)
{
    if (value == 1) printf("Linea\n");
    if (value == 2) printf("Circulo\n");
    if (value == 3) printf("Elipsis\n");
    if (value == 4) exit(0);

}

void crearMenu()
{
    //inicio Creando el menu
    int submenu;
    submenu = glutCreateMenu(menuApp);
    glutAddMenuEntry("Linea", 1);

    glutAddMenuEntry("Elipse",3);
    glutAddMenuEntry("Salir",4);
    glutCreateMenu(menuApp);
    glutAddSubMenu("SubMenu", submenu);
    glutAttachMenu(GLUT_RIGHT_BUTTON);

    //fin Creando el menu
}*/

void renderPoint(void) /*REVISAR ESTO*/
{
    glClear (GL_COLOR_BUFFER_BIT);
    glBegin (GL_POINTS);
        glVertex2f  (-0.98, 0.98);
    glEnd ();
    glFlush ();
}





void renderPoint(double x, double y)
{

    //printf("BEFORE TRANSFORM %f\t%f\t# renderPoint\n", x, y);

    W = glutGet(GLUT_WINDOW_WIDTH);
    H = glutGet(GLUT_WINDOW_HEIGHT);

    float X;
    float Y;
    glBegin (GL_POINTS);
        X = (2*x/W) - 1;
        Y = (-2*y/H) + 1;
        glVertex2f (X, Y);
        //printf("TRANSFORMED POINT %f\t%f\t# renderPoint\n", X, Y);

    glEnd ();
    glFlush ();

}

/*wiki pseudo:

function line(x0, x1, y0, y1) //x1
     boolean steep := abs(y1 - y0) > abs(x1 - x0)//x2
     if steep then//x3
         swap(x0, y0) //x4
         swap(x1, y1) //x5 
     if x0 > x1 then //x6
         swap(x0, x1) //x7
         swap(y0, y1) //x8
     int deltax := x1 - x0 //x9
     int deltay := abs(y1 - y0) //x10
     int error := deltax / 2 //x11
     int ystep //x12
     int y := y0 //x13
     if y0 < y1 then ystep := 1 else ystep := -1 //x14
     for x from x0 to x1 //x15
         if steep then plot(y,x) else plot(x,y) //x16
         error := error - deltay //x17
         if error < 0 then //x18
             y := y + ystep //x19
             error := error + deltax //x20
*/


void bresenham1(GLint x0, GLint x1, GLint y0, GLint y1) //function line(x0, x1, y0, y1)
{


    //double result1 = fabs((double)y1 - y0); //abs(y1 - y0)
    //double result2 = fabs((double)x1 - x0); //abs(x1 - x0)


    int result1 = abs(y1-y0);
    int result2 = abs(x1-x0);

bool steep = (result1 > result2); //boolean steep := abs(y1 - y0) > abs(x1 - x0)

if (steep){ //if steep then

        GLint aux1 = x0; //swap(x0, y0)
        x0=y0;
        y0 = aux1;

        GLint aux2 = x1; // swap (x1,y1)
        x1=y1;
        y1=aux2;

}

if(x0>x1){ // if (x0>x1)
        GLint aux3=x0; //swap(x0,x1)
        x0=x1;
        x1=aux3;

        GLint aux4=y0;//swap(y0,y1)
        y0=y1;
        y1=aux4;

}

int deltax = x1-x0; // deltax = x1-x0
int deltay = abs(y1-y0); //  int deltay := abs(y1 - y0) - revisar 
int error = (deltax / 2); //int error := deltax / 2 
int ystep; // int ystep

int y = y0;  //int y := y0

    if (y0<y1){  //if y0 < y1 then ystep := 1 else ystep := -1 

        ystep=1;

    }

    else {ystep=-1;}

for (int x=x0; x<=x1; x++){ //for x from x0 to x1
  if (steep){  // if steep then plot(y,x) else plot(x,y)

       renderPoint(y,x);
   }
   else {

    renderPoint(x,y);
   }

error = error - deltay; //error := error - deltay

if (error<0) {  //if error < 0 then 
  y = y + ystep; // y := y + ystep
  error = error + deltax; //error := error + deltax

} // end if (error<0)


}// end for from x0 to x1


}// end bresenham


void Plot4EllipsePoints(int X, int Y,int CX,int CY){

      renderPoint(CX+X, CY+Y); //point in quadrant 1
      renderPoint(CX-X, CY+Y); // point in quadrant 2
      renderPoint(CX-X, CY-Y); // point in quadrant 3
      renderPoint(CX+X, CY-Y); // point in quadrant 4

}



void PlotEllipse (int CX, int CY, int XRadius, int YRadius) {


        int X, Y;
        int XChange, YChange;
        int EllipseError;
        int  TwoASquare, TwoBSquare;
        int  StoppingX, StoppingY;


    TwoASquare = 2 * XRadius * XRadius;
    TwoBSquare = 2 * YRadius * YRadius;

    X = XRadius;
    Y =0;

    XChange = YRadius*YRadius*(1-(2*XRadius));
    YChange = XRadius * XRadius;

    EllipseError =0;
    StoppingX = TwoBSquare*XRadius;
    StoppingY = 0;

    while(StoppingX >= StoppingY){

       Plot4EllipsePoints(X,Y,CX,CY);
       Y++;
       StoppingY=StoppingY + TwoASquare; 

       EllipseError= EllipseError+ YChange;

       YChange= YChange+ TwoASquare;

       if( ((2*EllipseError)  + XChange)>0)
       {
            X--;
            StoppingX = StoppingX - TwoBSquare;
                EllipseError= EllipseError + XChange;
                XChange = XChange + TwoBSquare;

    }


      }    

      //1st set of points done, start second set


     X=0;
     Y= YRadius;

     XChange= YRadius*YRadius;
     YChange = XRadius*XRadius*(1-2*YRadius);

     EllipseError=0;
     StoppingX =0;
     StoppingY= TwoASquare * YRadius;
     while(StoppingX <= StoppingY){ // 2nd set of points, y'<-1

    Plot4EllipsePoints(X,Y, CX,CY);
        X++;
        StoppingX = StoppingX + TwoBSquare;
        EllipseError = EllipseError + XChange;
        XChange = XChange + TwoBSquare;

       if (((2*EllipseError) + YChange)>0){

           Y--;
           StoppingY = StoppingY - TwoASquare;
           EllipseError = EllipseError + YChange;
           YChange = YChange + TwoASquare;


    }

     } 

}











void renderAll (void)
{
    /*glutSetWindow(mainWindow);
    glutPostRedisplay();
    glutSetWindow(subWindow);
    glutPostRedisplay();*/
}

void movimiento(int boton, int estado, int x, int y)
{
    if((estado == GLUT_DOWN) && (boton == GLUT_LEFT_BUTTON))//mouse down
    {
        X1 = x; Y1 = y;


        PlotEllipse (x, y, 200, 100);
        renderPoint(x,y);
    }
    if((estado == GLUT_UP) && (boton == GLUT_LEFT_BUTTON))//mouse up
    {
        //printf("  Up|x:%d, y:%d\n",x,y);
        X2 = x; Y2 = y;
        //renderLine();
        bresenham1(X1,X2,Y1,Y2);


       //PRUEBA USANDO LA PRIMITIVA DE OPENGL

         glBegin( GL_LINES );



         glEnd();


        //renderPoint(x, y);
    }
}

void MouseMove(int x, int y)
{
    //printf("x:%d  | y:%d\n", x,y);
    X2 = x; Y2 = y;
    //renderLine();
    //bresenham1(X1, Y1, X2, Y2);
}

void teclado(unsigned char key, int x, int y)
{
    if(key==1){
      modoDeDibujo=1; // dibuja lineas  
      printf("Modo de dibujo: linea");
    }

    if (key==2){
       modoDeDibujo=2; //dibuja elipses 
    }


    if(key == 27)exit(0);
}

void especiales(int key, int x, int y)
{
    if(key == GLUT_KEY_F1) exit(0);
}

static void
key(unsigned char k, int x, int y)
{
  switch (k) {
  case 27:  /* Escape */
    exit(0);
    break;
  default:
    return;
  }
  glutPostRedisplay();
}

int main (int argc, char *argv []) 
{
    i = 0;
    //inicializa las operaciones de OpenGL/GLUT, db cr antes de usar funciones GLUT
    glutInit (&argc, argv);

    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowPosition (100, 100);
    glutInitWindowSize (W, H);
    //Crea una ventana  de Opengl
    glutCreateWindow ("tarea");
    glutDisplayFunc (renderPoint);
    glutMouseFunc(movimiento);
    glutKeyboardFunc(teclado);//teclas ASCII
    glutSpecialFunc(especiales);//captura las teclas [f1..f12]

    //glutPassiveMotionFunc(pasivo);
    glutKeyboardFunc(key);
    glutMotionFunc(MouseMove);
    //crearMenu();
    glutMainLoop ();


}
解决方案

First of all , you need to arrange the code. You must have one and only one display function, that clears the buffer,calls the other draw-functions and flushes them to the screen ( or swaps the buffer if you are using a double buffer).

On resizing the window,GLUT will call the Display Function which is renderPoint() as you know :

glutDisplayFunc (renderPoint);

renderPoint clears the buffer before redrawing the "points" , for example :

void renderPoint(void) /*REVISAR ESTO*/
{
    glClear (GL_COLOR_BUFFER_BIT);
    glBegin (GL_POINTS);
        glVertex2f  (-0.98, 0.98);
    glEnd ();
    glFlush ();
}

Since the buffer has been cleared , all the points that are drawn outside the function renderPoint ( the points of circles, the points of lines .. ) has no meaning , because you did not call them from the "main display function which is renderPoint.

How to keep the points on the screen ?

You must store the points ( any point you want to draw ) in a buffer such as an array , a dynamic array, std::vector or anything else. in the display function , write a loop statement to visit each point and extract the x and y .. then draw them.

for example , instead of the above function , replace it with :

class MyPoint {
    public:
    float x;
    float y;
    MyPoint(float x, float y)
    {
        this->x = x;
        this->y = y;
    } };

#include <vector> 
std::vector<MyPoint> testPoints;

void renderPoint(void) /*REVISAR ESTO*/ {
    testPoints.push_back(MyPoint(-0.58,0.58));
    testPoints.push_back(MyPoint(0.58,0.58));
    testPoints.push_back(MyPoint(0.58,-0.58));

    glClear (GL_COLOR_BUFFER_BIT);
    glPointSize(2);

    glBegin (GL_POINTS);
        for(int i=0;i<testPoints.size();i++)
        {
          glVertex2f  (testPoints[i].x, testPoints[i].y);
        }
    glEnd ();

    glFlush (); 
}

as you can see , by using a dynamic array such as (std::vector) to store the points and by using a for loop statement , we able to keep the three points visible.

what else ?

  • do the same method with other shapes , so that for each "mouse click " event , you may add or push_back two points represents a line end points in an array or std::vector called lineArray . in the display function , make a for loop statement to draw each line after extracting the two line points.

  • you should use glutReshapeFunc and glViewport to make sure that the viewport has the same dimenstions as the window after the resizing event. and I think that gluOrtho2d is more elegant than trying to mapping from Windows coordinates space to OpenGL coordinates space

  • arrange your code , so that you just use one display function.

Your program could be Something like this:

void drawTestPoints()
{
  for(int i=0;i<length;i++)
  {
      renderPoint(pointsArray[i].x,pointsArray[i].y);// vector of points/(MyPoint)

  }

}

void drawLines()
{
  for(int i=0;i<length;)
  {

      MyPoint startPoint = linesArray[i];
      MyPoint endPoint = linesArray[i+1];
      bresenham1(startPoint.x,endPoint.x,startPoint.y,endPoint.y);
      i+=2;
  }

}

void drawAll()
{
  glClear (GL_COLOR_BUFFER_BIT);

    drawTestPoints();
    drawLines();
    drawOtherShapes();

  glFlush();
}

.
.
.

// in the main func:
glutDisplayFunc (drawAll);

==

这篇关于OpenGL的:如何保持物体在窗口时,它的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 07:24