【OpenGL】绘制柱状图

1.绘制目标

  1. 理解物体坐标系、世界坐标系以及两者之间的关系,尝试使用glTranslate*()函数进行模型变换。
  2. 学会使用GL_TRIANGLE_STRIP绘制二维实体。
  3. 【OpenGL】绘制柱状图_#include

2.核心代码

2.1 坐标轴

/// <summary>  
/// 坐标轴
/// </summary>
/// <param name="x">向左平移</param>
/// <param name="y">向下平移</param>
void Draw_cross(double x, double y)
{
glColor3f(0.f, 0.f, 0.f); //设置RGB
glBegin(GL_LINES); //画直线

//画十字
glVertex2f(-8.0f - x, 0.0f - y);
glVertex2f(8.0f - x, 0.0f - y);
glVertex2f(0.0f - x, 8.0f - y);
glVertex2f(0.0f - x, 0.0f - y);

//x轴的箭头
glVertex2f(7.7f - x, 0.3f - y);
glVertex2f(8.0f - x, 0.0f - y);
glVertex2f(7.7f - x, -0.3f - y);
glVertex2f(8.0f - x, 0.0f - y);

//y轴的箭头
glVertex2f(-0.2f - x, 7.5f - y);
glVertex2f(0.0f - x, 8.0f - y);
glVertex2f(0.2f - x, 7.5f - y);
glVertex2f(0.0f - x, 8.0f - y);

glEnd();
}

2.2 彩色矩形

/// <summary>  
/// 彩色矩形
/// </summary>
/// <param name="x">左上角点的X坐标</param>
/// <param name="y">左上角点的Y坐标</param>
/// <param name="width">矩形宽</param>
/// <param name="height">矩形高</param>
/// <param name="R">红色</param>
/// <param name="G">绿色</param>
/// <param name="B">蓝色</param>
void Draw_block(double x, double y, double width, double height, double R, double G, double B)
{
glColor3f(R, G, B); //设置RGB
glPointSize(1.f);
glBegin(GL_TRIANGLE_STRIP); //画矩形
glVertex2f(x, y);
glVertex2f(x, y - height);
glVertex2f(x + width, y);
glVertex2f(x + width, y - height);
glEnd();
}

3.全部代码

#define GLEW_STATIC
#define FREEGLUT_STATIC

#include <GL/glew.h>
#include <GL/freeglut.h>
#include <GL/glext.h>
#include <iostream>
#include "main.h"

void init() {
//全1未白色,全0为黑色
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}

/// <summary>
/// 画十字
/// </summary>
/// <param name="x">向左平移</param>
/// <param name="y">向下平移</param>
void Draw_cross(double x , double y)
{
glColor3f(0.f, 0.f, 0.f); //设置RGB
glBegin(GL_LINES); //画直线

//画十字
glVertex2f(-8.0f - x, 0.0f - y);
glVertex2f(8.0f - x, 0.0f - y);
glVertex2f(0.0f - x, 8.0f - y);
glVertex2f(0.0f - x, 0.0f - y);

//x轴的箭头
glVertex2f(7.7f - x, 0.3f - y);
glVertex2f(8.0f - x, 0.0f - y);
glVertex2f(7.7f - x, -0.3f - y);
glVertex2f(8.0f - x, 0.0f - y);

//y轴的箭头
glVertex2f(-0.2f - x, 7.5f - y);
glVertex2f(0.0f - x, 8.0f - y);
glVertex2f(0.2f - x, 7.5f - y);
glVertex2f(0.0f - x, 8.0f - y);

glEnd();
}

/// <summary>
/// 绘制矩形
/// </summary>
/// <param name="x">左上角点的X坐标</param>
/// <param name="y">左上角点的Y坐标</param>
/// <param name="width">矩形宽</param>
/// <param name="height">矩形高</param>
/// <param name="R">红色</param>
/// <param name="G">绿色</param>
/// <param name="B">蓝色</param>
void Draw_block(double x, double y, double width, double height , double R, double G, double B)
{
glColor3f(R, G, B); //设置RGB
glPointSize(1.f);
glBegin(GL_TRIANGLE_STRIP); //画矩形
glVertex2f(x, y);
glVertex2f(x, y - height);
glVertex2f(x + width, y);
glVertex2f(x + width, y - height);
glEnd();
}

void display() {
//当前可写的颜色缓冲
glClear(GL_COLOR_BUFFER_BIT);

// 画十字
Draw_cross(0.5, 0);

// 画方块
Draw_block(-5.0, 1.2, 1.0, 1.2, 0.5, 0.5, 0.5);
Draw_block(-3.0, 3.0, 1.0, 3.0, 1, 0, 1);
Draw_block(-1.0, 5.6, 1.0, 5.6, 1, 0, 0);
Draw_block(1.0, 6.7, 1.0, 6.7, 0, 1, 0);
Draw_block(3.0, 4.7, 1.0, 4.7, 0, 0, 1);
Draw_block(5.0, -1.5, 1.0, -1.5, 0.4, 0.8, 0.2);

glutSwapBuffers();
}

void reshape(int w, int h) {
//x[0,w],y[0,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.运行结果

【OpenGL】绘制柱状图_#include_02