图形学:Opengl圆和多边形的绘制

1、采用中点Bresenham算法实现带颜色的圆绘制,同时采用OpenGL库函数实现带颜色的圆的绘制。

2、采用X-扫描线算法/改进的x-扫描线算法/边缘填充算法/教材或文献中其他算法实现六边形绘制,同时采用OpenGL库函数实现带颜色的六边形绘制。

图形学:Opengl圆和多边形的绘制_第1张图片

// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include "stdio.h"
#include 


void putpixel(int x, int y)
{


 glBegin(GL_POINTS);
 glVertex2f(876 + x, 876 + y);
 glEnd();
 glFlush();
}

void Circle(int x, int y)
{
 putpixel(x, y);
 glColor3f(1.0, 0.0, 0.0);
 putpixel(y, x);
 glColor3f(1.0, 0.0, 0.0);
 putpixel(-y, x);
 glColor3f(1.0, 0.0, 0.0);
 putpixel(-x, y);
 glColor3f(1.0, 0.0, 0.0);
 putpixel(-x, -y);
 glColor3f(1.0, 0.0, 0.0);
 putpixel(-y, -x);
 glColor3f(1.0, 0.0, 0.0);
 putpixel(y, -x);
 glColor3f(1.0, 0.0, 0.0);
 putpixel(x, -y);
 glColor3f(1.0, 0.0, 0.0);
}

void MidBresenhamCircle(int r)
{
 int x, y, d;
 x = 0;
 y = r;
 d = 1 - r;
 while (x <= y)
 {
  Circle(x, y);//⚪点定义
  
  if (d < 0)
   d += 2 * x + 3;
  else
  {
   d += 2 * (x - y) + 5;
   y--;
  }
  x++;
 }
}


void display()
{
 
 glClearColor(1.0, 1.0, 1.0, 1.0);
 gluOrtho2D(0.0, 1500.0, 0.0, 1500.0);
 glClear(GL_COLOR_BUFFER_BIT);
 MidBresenhamCircle(600);
}

int main(int argc, char** argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
 glutInitWindowSize(500, 500);
 glutInitWindowPosition(100, 100);
 glutCreateWindow("圆形");
 glutDisplayFunc(display);
 glutMainLoop();
 return 0;
}





#include "stdafx.h"
#include
#include "stdio.h"
#include 

#include 

using namespace std;

int n = 3600;  //圆绘制次数
float PI = 3.1415926f;
float R = 0.8f;  //半径

void myDisplay(void)
{
 //画圆
 
 glClearColor(1, 1, 1, 1);
 glColor4f(0, 0, 1, 0);
 glBegin(GL_LINE_LOOP);
 glClear(GL_COLOR_BUFFER_BIT);
 for (int i = 0; i < n; i++)
 {
  glVertex2f(R*sin(2 * PI*i / n), R*cos(2 * PI*i / n));   //定义顶点
 }
 glEnd();
 glFlush();
}
 int main(int argc, char *argv[])
 {
  glutInit(&argc, argv);   //初始化GLUT
  glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  glutInitWindowPosition(100, 100);
  glutInitWindowSize(400, 400);
  glutCreateWindow("My first OpenGL program");
  glutDisplayFunc(&myDisplay);   //回调函数
  glutMainLoop();    //持续显示,当窗口改变会重新绘制图形
  return 0;
 }

图形学:Opengl圆和多边形的绘制_第2张图片

#include"stdafx.h"
#include 
#include 
const int POINTNUM = 6;      //多边形点数. 

        /******定义结构体用于活性边表AET和新边表NET***********************************/
typedef struct XET
{
 float x;
 float dx, ymax;
 XET* next;
} AET, NET;

/******定义点结构体point******************************************************/
struct point
{
 float x;
 float y;
} polypoint[POINTNUM] = { { 250,10 },{ 550,150 },{ 550,400 },{ 250,250 },{ 100,350 },{ 120,200 } }; //多边形顶点 

void PolyScan()
{
 /******计算最高点的y坐标(扫描到此结束)****************************************/
 int MaxY = 0;
 int i;
 for (i = 0; i<POINTNUM; i++)
  if (polypoint[i].y>MaxY)
   MaxY = polypoint[i].y;

 /*******初始化AET表***********************************************************/
 AET *pAET = new AET;
 pAET->next = NULL;

 /******初始化NET表************************************************************/
 NET *pNET[1024];
 for (i = 0; i <= MaxY; i++)
 {
  pNET[i] = new NET;
  pNET[i]->next = NULL;
 }
 glClear(GL_COLOR_BUFFER_BIT);        //赋值的窗口显示. 
 glColor3f(0.0, 0.0, 0.0);             //设置直线的颜色红色 
 glBegin(GL_POINTS);
 /******扫描并建立NET表*********************************************************/
 for (i = 0; i <= MaxY; i++)
 {
  for (int j = 0; j<POINTNUM; j++)
   if (polypoint[j].y == i)
   {
    //一个点跟前面的一个点形成一条线段,跟后面的点也形成线段 
    if (polypoint[(j - 1 + POINTNUM) % POINTNUM].y>polypoint[j].y)
    {
     NET *p = new NET;
     p->x = polypoint[j].x;
     p->ymax = polypoint[(j - 1 + POINTNUM) % POINTNUM].y;
     p->dx = (polypoint[(j - 1 + POINTNUM) % POINTNUM].x - polypoint[j].x) / (polypoint[(j - 1 + POINTNUM) % POINTNUM].y - polypoint[j].y);
     p->next = pNET[i]->next;
     pNET[i]->next = p;
    }
    if (polypoint[(j + 1 + POINTNUM) % POINTNUM].y>polypoint[j].y)
    {
     NET *p = new NET;
     p->x = polypoint[j].x;
     p->ymax = polypoint[(j + 1 + POINTNUM) % POINTNUM].y;
     p->dx = (polypoint[(j + 1 + POINTNUM) % POINTNUM].x - polypoint[j].x) / (polypoint[(j + 1 + POINTNUM) % POINTNUM].y - polypoint[j].y);
     p->next = pNET[i]->next;
     pNET[i]->next = p;
    }
   }
 }
 /******建立并更新活性边表AET*****************************************************/
 for (i = 0; i <= MaxY; i++)
 {
  //计算新的交点x,更新AET 
  NET *p = pAET->next;
  while (p)
  {
   p->x = p->x + p->dx;
   p = p->next;
  }
  //更新后新AET先排序*************************************************************/ 
  //断表排序,不再开辟空间 
  AET *tq = pAET;
  p = pAET->next;
  tq->next = NULL;
  while (p)
  {
   while (tq->next && p->x >= tq->next->x)
    tq = tq->next;
   NET *s = p->next;
   p->next = tq->next;
   tq->next = p;
   p = s;
   tq = pAET;
  }
  //(改进算法)先从AET表中删除ymax==i的结点****************************************/ 
  AET *q = pAET;
  p = q->next;
  while (p)
  {
   if (p->ymax == i)
   {
    q->next = p->next;
    delete p;
    p = q->next;
   }
   else
   {
    q = q->next;
    p = q->next;
   }
  }
  //将NET中的新点加入AET,并用插入法按X值递增排序**********************************/ 
  p = pNET[i]->next;
  q = pAET;
  while (p)
  {
   while (q->next && p->x >= q->next->x)
    q = q->next;
   NET *s = p->next;
   p->next = q->next;
   q->next = p;
   p = s;
   q = pAET;
  }
  /******配对填充颜色***************************************************************/

  p = pAET->next;
  while (p && p->next)
  {
   for (float j = p->x; j <= p->next->x; j++)
    glVertex2i(static_cast<int>(j), i);
   p = p->next->next;//考虑端点情况 
  }
 }
 glEnd();
 glFlush();
}

void init(int argc, char** argv)
{
 glutInit(&argc, argv);  //I初始化 GLUT. 
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  //设置显示模式:单个缓存和使用RGB模型 
 glutInitWindowPosition(50, 100);  //设置窗口的顶部和左边位置 
 glutInitWindowSize(400, 300);  //设置窗口的高度和宽度 
 glutCreateWindow("Scan Program");

 glClearColor(1.0, 1.0, 1.0, 0); //窗口背景颜色为白色 
 glMatrixMode(GL_PROJECTION);
 gluOrtho2D(0, 600, 0, 450);
}

void myDisplay(void)
{
 glClear(GL_COLOR_BUFFER_BIT);
 glColor3f(0.0, 0.4, 0.2);
 glPointSize(1);
 glBegin(GL_POINTS);
 PolyScan();
 glEnd();
 glFlush();
}

int main(int argc, char** argv)
{
 init(argc, argv);
 glutDisplayFunc(myDisplay);        //图形的定义传递给我window. 
 glutMainLoop();
 return 0;
}

图形学:Opengl圆和多边形的绘制_第3张图片

#include "stdafx.h"
#include
#include "stdio.h"
#include 

#include 
const int n = 6;

const GLfloat R = 0.5f;

const GLfloat pi = 3.1415926536f;

void display()

{

 int i = 0;

 glClear(GL_COLOR_BUFFER_BIT);
 glColor3f(1, 0, 1);
 glBegin(GL_POLYGON);

 for (i = 0; i < n; i++)

 {

  glVertex2f(R*cos(2 * pi / n*i), R*sin(2 * pi / n*i));

 }

 glEnd();

 glFlush();

}

int main(int argc, char **argv)

{

 glutInit(&argc, argv);

 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

 glutInitWindowPosition(100, 100);

 glutInitWindowSize(400, 400);

 glutCreateWindow("opengl 3d view");

 glutDisplayFunc(display);

 glutMainLoop();

 return 0;

}

你可能感兴趣的:(算法,图形渲染,c#)