首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于OpenGL的dda算法

基于OpenGL的dda算法
EN

Stack Overflow用户
提问于 2017-02-13 19:53:30
回答 1查看 1.4K关注 0票数 0

编译时出错:

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

代码:

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

发布于 2017-02-13 20:34:09

如果您在POSIX上编译它,您将以<math.h> (和cmath)声明yn结束,这将与任何同名的全局变量完全冲突。

由于您将这个问题解释为c++14,所以还应该告诉您的编译使用该标准,方法是

代码语言:javascript
复制
 g++ -std=c++14

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42212413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档