openGL 第章节 例子

cpp 复制代码
#include "stdafx.h"
#include <GL/glut.h>
#include <iostream>
#include <iostream>

using namespace std;



GLsizei winWidth = 600, winHeight = 500;
GLint xRaster = 25, yRaster = 150 , xRastar1=25;


GLubyte label[36] = { 'J', 'a', 'n', 'F', 'e', 'b', 'M', 'a', 'r',
'A', 'p', 'r', 'M', 'a', 'y', 'J', 'u', 'n',
'J', 'u', 'l', 'A', 'u', 'g', 'S', 'e', 'p',
'O', 'c', 't', 'N', 'o', 'v', 'D', 'e', 'c' };
GLint dataValue[12] = { 420, 342, 324, 310, 262, 185,
190, 196, 217, 240, 312, 438 };


void init(void)
{
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0, 600.0, 0.0, 500.0);
}


void lineGraph(void)
{
    GLint month, k;
    GLint x = 30;
    

    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0, 0.0, 1.0);
    glBegin(GL_LINE_STRIP); /*划线 不闭合折线*/
    for (k = 0; k < 12; k++)
        glVertex2i(x + k * 50, dataValue[k]); /*30,420  80,342  130,324 180,310 230,262 280,185  330,190  380,196  430,217  480,240  530,312  580,438*/
    glEnd();


    glColor3f(1.0, 0.0, 0.0);
    for (k = 0; k < 12; k++)
    {
        glRasterPos2i(xRaster + k * 50, dataValue[k] - 4); /*更改光栅点的位置 第一个参数由第31行减5,第二个参数由第31行减4*/
        glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '*'); /*在每一个折点上画 '*' ps:不能有空格 GLUT_BITMAP_9_BY_15:一种固定宽度字体,每个字符都放在一个9x15像素的矩形框内*/
    }


    glColor3f(1.0, 0.0, 0.0); /*设置字体颜色*/
    xRastar1 = 20;
    for (month = 0; month < 12; month++)
    {
        glRasterPos2i(xRastar1, yRaster); /*x=20,y=150*/
        for (k = 3 * month; k < 3 * month + 3; k++) /*每一个内循环写一个月份*/ {
            glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, label[k]);/*GLUT_BITMAP_HELVETICA_12 一种12点均匀间距的Helvetica字体  */
        }
        xRastar1 += 50; /*x坐标向前推进50个单位*/
    }
    glFlush();
}


void barChart(void)
{
    GLint month, k;

    cout << "222"<< "\n";
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 0.0);
    for (k = 0; k < 12; k++)
        glRecti(20 + k * 50, 165, 40 + k * 50, dataValue[k]); /*画矩形 x左下,y左下,x右上,y右上 每个条形图间隔20个单位*/
    glColor3f(0.0, 0.0, 0.0);
    xRaster = 20;
    for (month = 0; month < 12; month++)
    {
        glRasterPos2i(xRaster, yRaster);
        for (k = 3 * month; k < 3 * month + 3; k++) {
            glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, label[k]);
        }
        xRaster += 50;
    }
    glFlush();
}


void winReshapeFcn(GLint newWidth, GLint newHeight)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight));


    glClear(GL_COLOR_BUFFER_BIT);
}


void main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("多点画线");


    init();
    glutDisplayFunc(lineGraph);
    glutReshapeFunc(winReshapeFcn);
    glutMainLoop();


} 
相关推荐
♡すぎ♡9 天前
Shader Graph: 能量护盾
计算机图形学·shadergraph·能量护盾
♡すぎ♡11 天前
镜面 IBL 预过滤贴图的计算
算法·计算机图形学·贴图·pbr
♡すぎ♡13 天前
二维 uv 与单位球面坐标的相互转化公式
计算机图形学·uv
♡すぎ♡20 天前
ShaderLab:PBR+IBL(ShaderToy Translation)
算法·计算机图形学·着色器·pbr·ibl
♡すぎ♡22 天前
现代实时渲染管线
计算机图形学·opengl·着色器·渲染管线
♡すぎ♡1 个月前
ShaderLab:可互动水面(基于RenderTexture,实时生成动态扰动)
计算机图形学·贴图·opengl·着色器
♡すぎ♡1 个月前
ShaderLab:海面——顶点变换,程序化生成无需贴图
计算机图形学·opengl·着色器
♡すぎ♡2 个月前
ShaderLab:线条几何体旋转
unity·计算机图形学·着色器·shaderlab
做cv的小昊2 个月前
结合代码读3DGS论文(12)——NeurIPS 2024 Spotlight 3DGS经典Backbone工作3DGS-MCMC论文及代码解读
论文阅读·计算机视觉·3d·图形渲染·游戏开发·计算机图形学·3dgs