OpenGL: 繪制圖形
- 認(rèn)識(shí)頭文件
#include <iostream>
標(biāo)準(zhǔn)的輸入輸出流頭文件
#include "math3d.h"
//3D數(shù)學(xué)庫(kù)
- 畫(huà)正方形
//-----------------畫(huà)正方形-------------------------
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
//設(shè)置多邊形的4個(gè)頂點(diǎn)
glVertex3f(0.25f, 0.25f, 0.0f);
glVertex3f(0.75f, 0.25f, 0.0f);
glVertex3f(0.75f, 0.75f, 0.0f);
glVertex3f(0.25f, 0.75f, 0.0f);
glEnd();
glFlush();
畫(huà)圓
//----------------------畫(huà)圓---------------------------------
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
//設(shè)置顏色
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
const int n = 55;
const GLfloat R = 0.5f;
const GLfloat pi = 3.1415926f;
for (int i = 0; i < n; i++)
{
glVertex2f(R*cos(2 * pi / n*i), R*sin(2 * pi / n*i));
}
glEnd();
glFlush();
畫(huà)五角星
// ------------------------五角形-----------------------------
const GLfloat Pi = 3.1415926536f;
GLfloat a = 1 / (2-2*cos(72*Pi/180));
GLfloat bx = a * cos(18 * Pi/180);
GLfloat by = a * sin(18 * Pi/180);
GLfloat cy = -a * cos(18 * Pi/180);
GLfloat
PointA[2] = { 0, a },
PointB[2] = { bx, by },
PointC[2] = { 0.5, cy },
PointD[2] = { -0.5, cy },
PointE[2] = { -bx, by };
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
glVertex2fv(PointA);
glVertex2fv(PointC);
glVertex2fv(PointE);
glVertex2fv(PointB);
glVertex2fv(PointD);
glEnd();
glFlush();
畫(huà)正炫函數(shù)
----------- 畫(huà)出正弦函數(shù)的圖形---------------
const GLfloat factor = 0.1f;
GLfloat x;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glVertex2f(-1.0f, 0.0f);
glVertex2f(1.0f, 0.0f);
glVertex2f(0.0f, -1.0f);
glVertex2f(0.0f, 1.0f);
glEnd();
glBegin(GL_LINE_STRIP);
for(x=-1.0f/factor; x<1.0f/factor; x+=0.01f)
{
glVertex2f(x*factor, sin(x)*factor);
}
glEnd();
glFlush();
這次就介紹到這里啦。 GitHub:https://github.com/fzdfy/OpenGL_graphing