使用OpenGL的dda算法

时间:2017-02-13 19:53:31

标签: opengl c++14

编译时的错误:

  • 将'y1'重新定义为不同种类的符号float x1 = 0,y1 = 0,x2 = 0,y2 = 0;
  • 非对象类型'double(double)'不可分配                        y1 = 240-y;
  • 没有用于调用'drawPixel'的匹配函数                        drawPixel(x1,y1);
  • 没有用于调用'dda'的匹配函数                        dda(x1,y1,x2,y2);

代码:

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <iostream>
using namespace std;
#include <cstdlib>
#include <cmath>

//defining constants
#define MAX_WIDTH 640
#define MAX_HEIGHT 480

int flag = 1;
float x1 = 0, y1 = 0, x2 = 0, y2 = 0;
//method for drawing the pixel
void drawPixel( int x, int y ){

    glBegin( GL_POINTS );
        //setting the pointer color
        glColor3f( 1.0, 1.0, 1.0 );
        glVertex2i( x, y );

    glEnd();
    glFlush();
}

//display callback function
void display(){

    glClear( GL_COLOR_BUFFER_BIT );

    gluLookAt( 0.0, 0.0, 0.0, 0.0, 0.0, 5.0, 0.0, 1.0, 0.0 );
    /*
    glBegin( GL_POINTS );
        //setting the pointer color
        glColor3f( 1.0, 1.0, 1.0 );
        glVertex2i( 0, 0 );
        glVertex2i( 10, 0 );

    glEnd();
    */
    glFlush();
}

//catching keyboard events
void keyboardEvent( unsigned char c, int x, int y ){

    if( c == 27 ){
        exit(0);
    }
}

//dda implementation
void dda( float m1 , float n1 , float m2 , float n2 ){

    float slope , Dx , Dy;
    float dx , dy , length; 

    int i = 1;

    dx = dy = length = 0.0;
    slope = Dx = Dy = 0.0;

    dx = m2 - m1;
    dy = n2 - n1;
    slope = dy/dx;      //slope

    cout << m1 << "\t" << n1 << "\t" << m2 << "\t" << n2 << endl;

    length = (dx >= dy)?dx:dy ;
    cout << length<< endl;

    Dx = dx/length;
    Dy = dy/length;

    drawPixel( round(m1), round(n1) );
    cout << m1 << m2 << endl;

    for( ; i <= length ; i++ ){
        m1 = m1 + Dx;
        n1 = n1 + Dy;
        drawPixel( round(m1), round(n1) );
        cout << m1 << m2 << endl;
    }
}

//catching mouse click events
void mouseEvent( int button, int state, int x, int y ){

    //checking if the mouse button is clicked or not using state para.
    if( state == GLUT_DOWN ){

        if( button == GLUT_LEFT_BUTTON && flag == 1 ){
            x1 = 320-x;
            y1 = 240-y;
            drawPixel( x1, y1 );
            flag++;
        }
        else if( button == GLUT_LEFT_BUTTON && flag == 2 ){
            x2 = 320-x;
            y2 = 240-y;
            drawPixel( x2, y2 );
            flag++;
        }
        else if( button == GLUT_RIGHT_BUTTON && flag > 2 ){

            drawPixel( 320-x, 240-y );
            cout << x1 << "\t" << y1 << "\t" << x2 << "\t" << y2 << endl;
            dda( x1, y1, x2, y2 );
        }
    }
}

//adjusting window when it is moved or resized
void reshape( int w, int h ){

    glViewport( 0, 0, w, h );
}

//initialising the window at startup
void initialise(){

    glClearColor( 0.0, 0.0, 0.0, 1.0 );
    glPointSize( 5 );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();   
    gluOrtho2D( -320, 320, -240, 240 );

}

int main( int argc, char **argv ){

    //initialising the glut library
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
    glutInitWindowSize( MAX_WIDTH, MAX_HEIGHT );
    glutInitWindowPosition( 100, 100 );
    glutCreateWindow("DDA Assignment");

    //calling normal functions
    initialise();

    //registering callback functions
    glutDisplayFunc( display );
    glutKeyboardFunc( keyboardEvent );
    glutMouseFunc( mouseEvent );
    glutReshapeFunc( reshape );

    glutMainLoop();

    return 0;
}

1 个答案:

答案 0 :(得分:2)

如果你在POSIX上编译它,你最终将<math.h>(和cmath)声明functions with the name y0, y1 and yn,这将与任何同名的全局变量完全冲突。

由于您明确将此问题标记为c++14,因此您还应该告诉编译使用该标准

 g++ -std=c++14

(或标准的任何其他适当版本),这也将阻止libc标头声明不在该标准中的函数。