码迷,mamicode.com
首页 > 其他好文 > 详细

计算机图形学第一次实验

时间:2018-10-27 00:10:19      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:utc   flush   creat   create   ini   har   功能   glcolor3f   matrix   

步骤一:查找相关函数,了解其功能和调用

https://blog.csdn.net/hebbely/article/details/69951068

https://blog.csdn.net/beibiannabian/article/details/77534803

https://blog.csdn.net/su20145104009/article/details/50216979中点画线法

简单图元代码:

#include<GL/glut.h>
#include<math.h>
void MidPointLine(int x0, int y0, int x1, int y1)
{
	float dx, dy,m,x,y,d,a,b;
	a = y0 - y1;
	b = x1 - x0;
	dx = x1 - x0;
	dy = y1 - y0;
	x = x0;
	y = y0;
	m = 1.0*dy / dx;
	glBegin(GL_POINTS);
	glVertex2f(x, y);
	glEnd();
	if (m >= 0 && m <= 1)
	{
		d = 2 * a + b;
		while (x <=x1)
		{
			if (d > 0)//取下面的点
			{
				x++;
				d += 2 * a;
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
			else
			{
				x++;
				y++;
				d = d + 2 * a + 2 * b;
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
		}
	}
	else if (m > 1)
	{
		d =a + 2*b;
		while (y<=y1)
		{
			if (d > 0)
			{
				d = d + 2 * a + 2 * b;
				y++;
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
			else
			{
				d += 2 * b;
				x++;
				y++;
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
		}
	}
	else if (m<= 0&&m>= -1)
	{
		d = 2 * a - b;
		while (x <=x1)
		{
			if (d > 0)
			{
				d = d + 2 * a - 2 * b;
				x++;
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
			else
			{
				d = d + 2 * a;
				x++;
				y--; 
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
		}
	}
	else
	{
		d = a - 2 * b;
		while (y >=y1)
		{
			if (d > 0)
			{
				d = d - 2 * a;
				x++;
				y--;
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
			else
			{
				d = d + 2 * a - 2 * b;
				y--;
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
		}
	}
}
void changeSize(GLsizei w, GLsizei h)
{
	if (h == 0)
		h = 1;
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	if (w <= h)
	{
		glOrtho(0.0f, 500.0f, 0.0f, 500.0f * h / w, 1.0f, -1.0f);
	}
	else
	{
		glOrtho(0.0f, 500.0f * w / h, 0.0f, 500.0f, 1.0f, -1.0f);
	}
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
void init()
{
	glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
}
void RenderScene()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0, 0.0, 0.0);
	MidPointLine(100,250,250,50);
	glFlush();
}
int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutCreateWindow("Line");
	init();
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(500, 500);
	glutDisplayFunc(RenderScene);
	glutReshapeFunc(changeSize);
	glutMainLoop();
	return 0;
}

  中点线算法代码:

#include<GL/glut.h>
#include<math.h>
void MidPointLine(int x0, int y0, int x1, int y1)
{
	float dx, dy,m,x,y,d,a,b;
	a = y0 - y1;
	b = x1 - x0;
	dx = x1 - x0;
	dy = y1 - y0;
	x = x0;
	y = y0;
	m = 1.0*dy / dx;
	glBegin(GL_POINTS);
	glVertex2f(x, y);
	glEnd();
	if (m >= 0 && m <= 1)
	{
		d = 2 * a + b;
		while (x <=x1)
		{
			if (d > 0)//取下面的点
			{
				x++;
				d += 2 * a;
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
			else
			{
				x++;
				y++;
				d = d + 2 * a + 2 * b;
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
		}
	}
	else if (m > 1)
	{
		d =a + 2*b;
		while (y<=y1)
		{
			if (d > 0)
			{
				d = d + 2 * a + 2 * b;
				y++;
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
			else
			{
				d += 2 * b;
				x++;
				y++;
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
		}
	}
	else if (m<= 0&&m>= -1)
	{
		d = 2 * a - b;
		while (x <=x1)
		{
			if (d > 0)
			{
				d = d + 2 * a - 2 * b;
				x++;
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
			else
			{
				d = d + 2 * a;
				x++;
				y--; 
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
		}
	}
	else
	{
		d = a - 2 * b;
		while (y >=y1)
		{
			if (d > 0)
			{
				d = d - 2 * a;
				x++;
				y--;
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
			else
			{
				d = d + 2 * a - 2 * b;
				y--;
				glBegin(GL_POINTS);
				glVertex2f(x, y);
				glEnd();
			}
		}
	}
}
void changeSize(GLsizei w, GLsizei h)
{
	if (h == 0)
		h = 1;
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	if (w <= h)
	{
		glOrtho(0.0f, 500.0f, 0.0f, 500.0f * h / w, 1.0f, -1.0f);
	}
	else
	{
		glOrtho(0.0f, 500.0f * w / h, 0.0f, 500.0f, 1.0f, -1.0f);
	}
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
void init()
{
	glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
}
void RenderScene()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0, 0.0, 0.0);
	MidPointLine(100,250,250,50);
	glFlush();
}
int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutCreateWindow("Line");
	init();
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(500, 500);
	glutDisplayFunc(RenderScene);
	glutReshapeFunc(changeSize);
	glutMainLoop();
	return 0;
}

  

计算机图形学第一次实验

标签:utc   flush   creat   create   ini   har   功能   glcolor3f   matrix   

原文地址:https://www.cnblogs.com/legendcong/p/9858873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!