OPENGL实现DDA算法。麻烦高手帮忙查错。谢谢
我觉得没错啊。但是就是无法显示。图像。
谢谢高手们了。:)
// LINEDRAW.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include "GL\glut.h"
#include "math.h"
#define DDA 1
#define Bresenham 2
#define MAX 100
static int m_type=1;
struct m_point
{
int x;
int y;
} pt[MAX];
void init()
{//初始化窗口
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(255,0,0);
glPointSize(5.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,640,0,480);
}
void CreateMenu(int value)
{
switch(value)
{
case 1:
m_type=1;
break;
case 2:
m_type=2;
break;
}
}
int n_click=0;
void Mouse(int button,int state,int x,int y)
{
if(m_type == 1)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
if(n_click == 0)
{
pt[n_click].x=x;
pt[n_click].y=y;
}
else
{
int i=0;
pt[n_click].x=x;
pt[n_click].y=y;
double dx=pt[n_click].x-pt[n_click-1].x;
double dy=pt[n_click].y-pt[n_click-1].y;
double Length = fabs(dx)>=fabs(dy)?fabs(dx):fabs(dy);
double deltx=dx/Length;
double delty=dy/Length;
int m_x=int(pt[n_click].x);
int m_y=int(pt[n_click].y);
glBegin(GL_POINTS);
while(i<=Length)
{
glVertex2i(m_x,m_y);
m_x+=deltx;
m_y+=delty;
i++;
}
glEnd();
glFlush();
}
n_click++;
}
}
if(m_type == 2)
{
}
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
glutSwapBuffers();
}
void main(int argc,char* argv[])
{
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("直线画法实现");
init();
glutMouseFunc(Mouse); //响应鼠标
//右键建立菜单
glutCreateMenu(&CreateMenu);
glutAddMenuEntry("DDA算法",1);
glutAddMenuEntry("Bresenham算法",2);
glutAttachMenu(GLUT_RIGHT_BUTTON);
// glutMotionFunc(Motion);
glutDisplayFunc(Display);
glutMainLoop();
}