【OpenGL】绘制圆、正弦曲线
- 1.绘制目标
- 2.核心代码
-
- 3.全部代码
- 4.运行结果
1.绘制目标
- 学会使用GL_POINTS、GL_LINES、GL_LINE_LOOP、GL_LINE_STRIP绘制基本二维形状。
- 理解OpenGL状态机概念
a) 使用glPointSize()设置点大小
b) 使用glLineWidth()设置线的粗细
c) 使用glColor3f()设置颜色
2.核心代码
2.1 正弦函数
void Draw_sine(int down)
{
glColor3f(0.f, 0.f, 1.f);
GLfloat x = -1.0;
glBegin(GL_LINE_STRIP);
for (float x = -5 * PI; x < 5 * PI; x += 0.1f)
{
glVertex2f(x / (5 * PI), sin(x) - down);
}
glEnd();
}
2.2 圆
void Draw_circle(int x, int y)
{
glColor3f(1.f, 0.f, 0.f);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < n; i++)
{
glVertex2f(R * cos(2 * PI * i / n) - x, R * sin(2 * PI * i / n) - y);
}
int i = 0;
glVertex2f(R * cos(2 * PI * i / n) - x, R * sin(2 * PI * i / n) - y);
glEnd();
glColor3f(0.f, 0.f, 0.f);
glBegin(GL_LINES);
glVertex2f(0.0f, 0.0f);
glVertex2f(0.0f, 2.5f);
glEnd();
glColor3f(0.f, 0.f, 0.f);
glBegin(GL_LINES);
glVertex2f(0.0f, 0.0f);
glVertex2f(2.5f, 0.0f);
glEnd();
glColor3f(0.f, 1.f, 0.f);
glPointSize(10);
glBegin(GL_POINTS);
glVertex2f(0.f, 0.f);
glEnd();
glColor3f(0.f, 0.f, 1.f);
glPointSize(10);
glBegin(GL_POINTS);
glVertex2f(2.5f, 0.f);
glEnd();
glColor3f(0.f, 0.f, 1.f);
glPointSize(10);
glBegin(GL_POINTS);
glVertex2f(0.f, 2.5f);
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
Draw_circle(0, 0);
Draw_sine(5);
glutSwapBuffers();
}
3.全部代码
#define GLEW_STATIC
#define FREEGLUT_STATIC
#include
#include
#include
#include "math.h"
#include
#include "main.h"
int n = 3600;
float PI = 3.1415926f;
float R = 2.5f;
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
void Draw_sine(int down)
{
glColor3f(0.f, 0.f, 1.f);
GLfloat x = -1.0;
glBegin(GL_LINE_STRIP);
for (float x = -5 * PI; x < 5 * PI; x += 0.1f)
{
glVertex2f(x / (5 * PI), sin(x) - down);
}
glEnd();
}
void Draw_circle(int x, int y)
{
glColor3f(1.f, 0.f, 0.f);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < n; i++)
{
glVertex2f(R * cos(2 * PI * i / n) - x, R * sin(2 * PI * i / n) - y);
}
int i = 0;
glVertex2f(R * cos(2 * PI * i / n) - x, R * sin(2 * PI * i / n) - y);
glEnd();
glColor3f(0.f, 0.f, 0.f);
glBegin(GL_LINES);
glVertex2f(0.0f, 0.0f);
glVertex2f(0.0f, 2.5f);
glEnd();
glColor3f(0.f, 0.f, 0.f);
glBegin(GL_LINES);
glVertex2f(0.0f, 0.0f);
glVertex2f(2.5f, 0.0f);
glEnd();
glColor3f(0.f, 1.f, 0.f);
glPointSize(10);
glBegin(GL_POINTS);
glVertex2f(0.f, 0.f);
glEnd();
glColor3f(0.f, 0.f, 1.f);
glPointSize(10);
glBegin(GL_POINTS);
glVertex2f(2.5f, 0.f);
glEnd();
glColor3f(0.f, 0.f, 1.f);
glPointSize(10);
glBegin(GL_POINTS);
glVertex2f(0.f, 2.5f);
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
Draw_circle(0, 0);
Draw_sine(5);
glutSwapBuffers();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-8.f, 8.f, -8.f, 8.f);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitContextVersion(3, 3);
glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(800, 600);
glutCreateWindow("HellGL");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glewExperimental = GL_TRUE;
glewInit();
init();
glutMainLoop();
return 0;
}
4.运行结果