#include <windows.h> //此头文件一定要放在最前
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <conio.h>
using namespace std;
//添加这3条语句
#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "glu32.lib")
#pragma comment (lib, "glaux.lib")
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
//这句是不让控制台窗体出现,如果想要出现,去掉即可。
float root2 = sqrt(2);
float pi = 3.14159;
void drawline()使用OpenGL的划线函数,与中点算法对比
{
glColor3f(1,1,0);
glBegin(GL_LINES);
glVertex2f(310,260);
glVertex2f(230,230);
glEnd();
glColor3f(0,0,1);
glBegin(GL_LINES);
glVertex2f(310,360);
glVertex2f(230,230);
glEnd();
glColor3f(1,0,0);
glBegin(GL_LINES);
glVertex2f(310,170);
glVertex2f(230,230);
glEnd();
glColor3f(1,0,1);
glBegin(GL_LINES);
glVertex2f(310,40);
glVertex2f(230,230);
glEnd();
}
void MidpointLine1(int x0,int y0,int x1,int y1, float rc,float gc,float bc)
{//0<=m<=1
int d = 0;
int d1 = d;
int d2 = d;
int a = y0-y1;
int b = x1-x0;
int c = x0*y1-x1*y0;
glColor3f(rc,gc,bc);
d = a + a + b;
d1 = a+a;
d2 = (a + b)+(a + b);
int x = x0;
int y = y0;
for(int i = x0; i< x1; i++)
{
if(d<0)
{
y++;
d+=d2;
}
else d+=d1;
x++;
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
}
void MidpointLine2(int x0,int y0,int x1,int y1, float rc,float gc,float bc)
{//m >1(cuowu)
int d = 0;
int d1 = d;
int d2 = d;
int a = y0-y1;
int b = x1-x0;
int c = x0*y1-x1*y0;
glColor3f(rc,gc,bc);
d = a + b + b;
d1 = (a + b)+(a + b);
d2 = b + b;
int x = x0;
int y = y0;
for(int i = y0; i< y1; i++)
{
if(d>0)
{
x++;
d+=d1;
}
else d+=d2;
y++;
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
}
void MidpointLine3(int x0,int y0,int x1,int y1, float rc,float gc,float bc)
{// -1 <= m <= 0
int d = 0;
int d1 = d;
int d2 = d;
int a = y0-y1;
int b = x1-x0;
int c = x0*y1-x1*y0;
glColor3f(rc,gc,bc);
d = a + a - b;
d1 = (a - b)+(a - b);
d2 = a + a;
int x = x0;
int y = y0;
for(int i = x0; i< x1; i++)
{
if(d>0)
{
y--;
d+=d1;
}
else d+=d2;
x++;
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
}
void MidpointLine4(int x0,int y0,int x1,int y1, float rc,float gc,float bc)
{//m<-1
int d = 0;
int d1 = d;
int d2 = d;
int a = y0-y1;
int b = x1-x0;
int c = x0*y1-x1*y0;
glColor3f(rc,gc,bc);
d = a - b - b;
d1 = - b - b;
d2 = (a - b)+(a - b);
int x = x0;
int y = y0;
for(int i = y1; i< y0; i++)
{
if(d<=0)
{
x++;
d+=d2;
}
else d+=d1;
y--;
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
}
void MidpointLine(int x0,int y0,int x1,int y1, float rc,float gc,float bc)
{
float dx = x1-x0,dy=y1-y0;
float m = 0;
if(dx != 0) m = dy/dx;
else return;
if(m>=0 && m <=1){
if(x0 > x1)
{
int t = x1; x1 = x0; x0 = t;
t = y1;y1 = y0; y0 = t;
}
MidpointLine1(x0,y0,x1,y1,rc,gc,bc);
}
if(m>1){
if(x0 > x1)
{
int t = x1; x1 = x0; x0 = t;
t = y1;y1 = y0; y0 = t;
}
MidpointLine2(x0,y0,x1,y1,rc,gc,bc);
}
if(m<=0 && m >=-1){
if(x0 > x1)
{
int t = x1; x1 = x0; x0 = t;
t = y1;y1 = y0; y0 = t;
}
MidpointLine3(x0,y0,x1,y1,rc,gc,bc);
}
if(m <-1){
if(x0 > x1)
{
int t = x1; x1 = x0; x0 = t;
t = y1;y1 = y0; y0 = t;
}
MidpointLine4(x0,y0,x1,y1,rc,gc,bc);
}
}
void init()
{
glClearColor(0.0,0.0,0.0,0.0);//黑色背景
}
void middlecircle(int x0,int y0,int r,float R,float G,float B)
{
glColor3f(R,G,B);
glBegin(GL_POINTS);
int h = 5 - 4*r;
int x = 0;
int y = r;
glVertex2i(x+x0,y+y0);
glVertex2i(-x+x0,-y+y0);
glVertex2i(-x+x0,y+y0);
glVertex2i(x+x0,-y+y0);
int maxX = root2/2.0*r;
while(x<maxX)
{
if(h>0)
{
h = h + 8*(x-y) + 20;
y--;
}
else h = h + 8*x +12;
x++;
glVertex2i(x+x0,y+y0);
glVertex2i(-x+x0,-y+y0);
glVertex2i(-x+x0,y+y0);
glVertex2i(x+x0,-y+y0);
glVertex2i(y+x0,x+y0);
glVertex2i(-y+x0,-x+y0);
glVertex2i(-y+x0,x+y0);
glVertex2i(y+x0,-x+y0);
}
glEnd();
}
void PloyAppCircle(int x0,int y0,int R,int r,int g,int b,int err)
{//误差控制的多边形逼近圆弧
//float angle = 0.1;
float angle = 2*acos((R-err)*1.0/R);
printf("%f \n",angle);
int n = 2*pi/angle;
angle = 2*pi/n;
printf("%d\n",n);
int i;
glColor3f(r,g,b);
glBegin(GL_LINE_LOOP);
int x,y;
for(i=0;i<n;i++)
{
x = x0 + R*cos(i*angle);
y = y0 + R*sin(i*angle);
glVertex2i(x,y);
printf("%d %d\n",x,y);
}
glEnd();
}
void CALLBACK draw()
{
glClear(GL_COLOR_BUFFER_BIT);
drawline();
MidpointLine(210,260,130,230,1,1,0);
MidpointLine(210,360,130,230,0,0,1);
MidpointLine(210,170,130,230,1,0,0);
MidpointLine(210,40,130,230,1,0,1);
middlecircle(500,500,150,0.0,1.0,1.0);
PloyAppCircle(500,500,150,0.0,0.0,1.0,10);
glFinish();
}
int main(int argc, char* argv[])
{
auxInitDisplayMode(AUX_SINGLE|AUX_RGBA);
auxInitPosition(0,0,1000,1000);
auxInitWindow("Project 2");
init();
auxMainLoop(draw);
return 0;
}
图形学第二次作业
最新推荐文章于 2022-03-01 19:20:57 发布