// OGL1.cpp : Defines the entry point for the console application.
//
#include <stdlib.h>
#include <stdio.h>
#include "stdafx.h"
#include "windows.h"
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library
#include <gl\glut.h> // Header File For The OpenGL32 tools Library
#include<time.h>
int postRedisplay = 0;
int x = 0;
int y = 0;
int z = 1;
int counter = 0;
int sefDx0;
int sefDy0;
int sefDx1;
int sefDy1;
void init (void)
{
glClearColor (0.0, 1.0, 1.0, 0.0); // Set display-window color to white.
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D (0.0, 500.0, 0.0, 500.0);
}
void DDA(int x0, int y0, int x1, int y1){//普通画线方法
int x;
float dx, dy ,y ,k;
clock_t start,finish;
double duration;
dx = x1 - x0;
dy = y1 - y0;
k = dx / dy;
y = y0;
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f (1.0, 0.0, 0.0); // Set line segment color to red.
glPointSize( 10.0f );
start = clock();
for(x = x0; x <= x1; x++){
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
y += k;
finish = clock();
}
duration = (double) (finish-start) / CLOCKS_PER_SEC;
printf("普通画线法所用时间为%lf\n",duration);
}
void callBack(void){
DDA(0, 0 , 500, 500);
}
void MidlePoint_Line(int x0, int y0, int x1, int y1){//中点画线法
int a, b, d1, d2, d, x, y;
clock_t start,finish;
double duration;
a = y0 - y1;
b = x1 - x0;
d = 2 * a + b;
d1 = 2 * a;
d2 = 2 * (a + b);
x = x0;
y = y0;
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f (1.0, 0.0, 0.0); // Set line segment color to red.
glPointSize( 5.0f );
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
start = clock();
while(x < x1){
if ( d < 0 ){
x++;
y++;
d += d2;
}
else{
x++;
d += d1;
}
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
finish = clock();
}
duration = (double) (finish-start) / CLOCKS_PER_SEC;
printf("中点画线法所用时间为%lf\n",duration);
}
void Bresenham(int x0, int y0, int x1, int y1){//bresenham画线方法
int x, y, dx, dy, e, i;
dx = x1 - x0;
dy = y1 - y0;
e = -dx;
x = x0;
y = y0;
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f (1.0, 0.0, 0.0); // Set line segment color to red.
glPointSize( 5.0f );
for( i = 0; i < dx; i++){
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
x++;
e = e + 2 * dx;
if(e >= 0){
y++;
e = e - 2* dx;
}
}
}
void selfDefine(int button , int state, int x, int y){//自定义画线方法
if(state == GLUT_DOWN){
printf("%d,%d\n",x,500 - y);
glBegin(GL_POINTS);
glVertex2i(x,500 - y);
glEnd();
glFlush();
counter++;
}
if(counter%2 == 0){
sefDx1 = x;
sefDy1 = 500 - y;
MidlePoint_Line(sefDx0,sefDy0,sefDx1,sefDy1);
}
if(counter%2 == 1){
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f (1.0, 0.0, 0.0); // Set line segment color to red.
sefDx0 = x;
sefDy0 = 500 - y;
}
}
void MidPointCircle(int r){
int x, y;
float d;
x = 0;
y = r;
d = 6 - 4*r;
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f (1.0, 0.0, 0.0); // Set line segment color to red.
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
glFlush();
while(x<=y){
if(d<0)
d += 8*x + 12;
else{
d += 8*(x-y)+20;
y--;
}
x++;
glBegin(GL_POINTS);
glVertex2i(x,y);
glVertex2i(y,x);
glEnd();
glFlush();
}
}
void Menu(int val){
switch(val){
case 1: DDA(0, 0, 500, 500); break;
case 2: MidlePoint_Line(0, 0, 500, 500); break;
case 3: Bresenham(0, 0, 500, 500);break;
case 4: glutMouseFunc( selfDefine );break;
case 5: MidPointCircle(250);break;
case 6: exit(0);break;
}
}
void showMenu(int button , int state, int x, int y){
glutCreateMenu(Menu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutAddMenuEntry("DDA法", 1);
glutAddMenuEntry("中点画线法", 2);
glutAddMenuEntry("Bresenham算法", 3);
glutAddMenuEntry("自定义起点终点",4);
glutAddMenuEntry("中点画圆法",5);
glutAddMenuEntry("Exit", 6);
}
void main (int argc, char** argv)
{
glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition (0, 0); // Set top-left display-window position.
glutInitWindowSize (500, 500); // Set display-window width and height.
glutCreateWindow ("An Example OpenGL Program"); // Create display window.
init ( ); // Execute initialization procedure.
glutDisplayFunc ( callBack ); // Send graphics to display window.
glutMouseFunc( showMenu );
glutMainLoop ( ); // Display everything and wait.
}
- 1
- 2
前往页