OpenGL: Triangle(DR)

本文介绍了如何在Microsoft Visual Studio中创建一个C++控制台应用,利用OpenGL库生成一个简单的三角形图形。首先,确保安装了必要的桌面开发组件,然后删除默认代码并粘贴提供的代码。代码中通过初始化顶点缓冲区(VBO)来存储顶点数据,再通过OpenGL的渲染函数绘制三角形,并使用双缓存技术更新显示。整个过程涉及OpenGL的状态机、自定义渲染管线以及窗口管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PART ONE

Step1: Run the IDE “Microsoft Visual Studio”, then click the “创建新项目” to create a “控制台应用” program with the “name" you like. Making sure that your IDE had already downloaded the "使用C++的桌面开发" components by the "Visual Studio installer".

e7525eec6d7b4c49950ee952da1d1c7e.png

97af31581b4746a3a79a9408f27810a2.png

606678bb938447f1a1ef08cef2c20c39.png

2cab81b07d054a439e2a8be4fe3ce734.png

 Step2 : Delete the original codes that you can see and Copy&Paste the codes in the PART TWO, then right-click the program‘s name, here is "Triangle".  Selecting the "管理NuGet程序包(N)", then type the "nupengl" like showing in the Image below. Clicking the "安装" button to install the packpage to use OpenGL with initializing the "glut and glfw". Now run the program, a triangle will be in the window which created by the "glut".

                   a458c6c69b69457391cb96840c827313.png        2f856e0de33249dcb10bcf56d43231aa.png

08a72770748747c78d1c627e659dd285.png

f102434838714f0997a93830c73d07fb.png

 PART TWO

codes.cpp

#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>

struct Vector3f {
    float x;
    float y;
    float z;
};

int width = 222;
int height = 207;

GLuint VBO;
void initVBO()
{
    Vector3f Vertices[3];
    Vertices[0] = { -1.0f, -1.0f, 0.0f };   // bottom left
    Vertices[1] = { 1.0f, -1.0f, 0.0f };    // bottom right
    Vertices[2] = {0.0f, 1.0f, 0.0f};       // top

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void render()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(0.3f, 0.3f, 0.3f, 1.0f);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableVertexAttribArray(0);

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    // init GLUT
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(width, height);
    glutInitWindowPosition(200, 100);
    int id = glutCreateWindow("Triangle");
    //printf("window id: %d\n", win);

    // init GLEW after GLUT
    GLenum err = glewInit();
    if (err != GLEW_OK) {
        fprintf(stderr, "Error: '%s'\n", glewGetErrorString(err));
        return 1;
    }
    
    initVBO();
    glutDisplayFunc(render);
    glutMainLoop();

    return 0;
}

部分三

代码分析

1、基本符合C++编程习惯,包含头文件、函数、主函数。

2、OpenGL是一个State Machine(状态机),SM渲染用的是OpenGL默认的渲染管线(DRPL:Default Rendering Pipeline),简称为DR(默认渲染版本)。在DR下,很多功能的实现是封装在GPU端,需要构建OpenGL Centext(上下文),给用户提供接口函数进行使用,接口函数主要是OpenGL的核心库中以”gl“开头命名,如gl<······>形式的函数。与DR相对应的是用户自定义的渲染管线(CRLP:Custom Rendering Pipeline)或软渲染管线(SRPL:Soft Rendering Pipeline),具体实现什么功能由用户自己完成,可以更加灵活的实现一些计算机图形学中的算法,简称为CR(自定义渲染版本)或SR。

3、主函数中包含glut的初始化(创建窗口等),glew的初始化(调用opengl变量、函数等)。

4、将CPU端数据(Vector3f Vertices[3])加载到GPU端的buffer中,此处使用的是顶点缓冲区VBO,因为对应的绑定点为GL_ARRAY_BUFFER。

5、render()渲染函数中,首先清空颜色缓冲区并设置一个背景色(默认为黑色),之后通过相关函数调用与VBO中数据相对应的属性点(默认为"0"),最后使用OpenGL中的绘制函数进行绘制后释放属性点,再使用双缓存技术将后面绘制端缓冲区中的数据交换到前端可以显示到屏幕的缓冲区进行显示。

 


 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱书网

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值