目录
1. OpenGL初识 - 点的绘制
2. OpenGL初识 - 简单图形绘制
3. 直线光栅化 - 使用OpenGL绘制直线
4. 直线光栅化 - DDA画线算法
5. 直线光栅化 - 中点画线算法
1. OpenGL初识 - 点的绘制
在这一部分,我们将学习如何使用OpenGL绘制点。以下是具体的代码示例:
#include
#include
void myDisplay(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glPointSize(3.0);
glBegin(GL_POINTS);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.4f, -0.4f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.4f, 0.4f);
glEnd();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("Hello Point!");
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
2. OpenGL初识 - 简单图形绘制
接下来,我们扩展到绘制简单的几何图形,如矩形和三角形。以下是代码示例:
#include
#include
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.8f, -0.5f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(-0.8f, -0.5f);
glEnd();
glPointSize(3);
glBegin(GL_POINTS);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.4f, -0.4f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.4f, 0.4f);
glEnd();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("Hello Point!");
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}
3. 直线光栅化 - 使用OpenGL绘制直线
在这里,我们将学习如何使用OpenGL绘制直线。以下是代码示例:
#include
#include
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex2f(0.0f, 0.0f);
glVertex2f(100.0f, 240.0f);
glEnd();
glFlush();
}
void Init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
}
void myReshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("Hello Point!");
Init();
glutDisplayFunc(myDisplay);
glutReshapeFunc(myReshape);
glutMainLoop();
return 0;
}
4. 直线光栅化 - DDA画线算法
DDA(数字微分分析器)算法是一种常用的直线光栅化算法。以下是实现该算法的代码示例:
#include
#include
void LineDDA(int x0, int y0, int x1, int y1)
{
int dx = x1 - x0, dy = y1 - y0;
float k = dy / (float)dx;
int y = y0;
glColor3f(1.0f, 1.0f, 0.0f);
glPointSize(1);
for (int x = x0; x <= x1; x++) {
glBegin(GL_POINTS);
glVertex2i(x, (int)(y + 0.5));
glEnd();
y += k;
}
glFlush();
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
LineDDA(0, 0, 200, 300);
glFlush();
}
void Init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
}
void myReshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("Hello Point!");
Init();
glutDisplayFunc(myDisplay);
glutReshapeFunc(myReshape);
glutMainLoop();
return 0;
}
5. 直线光栅化 - 中点画线算法
中点画线算法是另一种高效的直线光栅化方法。以下是其实现代码:
#include
#include
void MidPLine(int x0, int y0, int x1, int y1)
{
int dx = x1 - x0, dy = y1 - y0;
int d = dx - 2 * dy;
int dt = 2 * dx - 2 * dy;
int db = -2 * dy;
int x = x0, y = y0;
glColor3f(0.0f, 1.0f, 0.0f);
glPointSize(1);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
while (x if (d <0) {
x++;
y++;
d += dt;
} else {
x++;
d += db;
}
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
glFlush();
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
MidPLine(10, 50, 300, 260);
glFlush();
}
void Init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
}
void myReshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("Hello Point!");
Init();
glutDisplayFunc(myDisplay);
glutReshapeFunc(myReshape);
glutMainLoop();
return 0;
}