OpenGL如何基于glfw库 进行 点线面 已解决

GLFW是现在较流行、使用广泛的OpenGL的界面库,而glut库已经比较老了。GLEW是和管理OpenGL函数指针有关的库,因为OpenGL只是一个标准/规范,具体的实现是由驱动开发商针对特定显卡实现的。由于OpenGL驱动版本众多,它大多数函数的位置都无法在编译时确定下来,需要在运行时查询,而GLEW可以解决这些问题。下面就来看下OpenGL是如何基于glfw库实现画点、画线、画三角的。

1、glVertex函数

2、坐标系

代码如下:

复制代码
#include <GLFW/glfw3.h>
void drawPoint()
{
    /* Draw a point */
    glClearColor (0.0, 0.0, 0.0, 0.0); 
    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(2.0f);
    glBegin(GL_POINTS);
    glColor3f(1.0, 0.0, 0.0);    // Red
    glVertex2f(0.0f,0.0f);
    glVertex2f(0.5f,0.8f);
    glEnd();
}
void drawLint()
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glLineWidth(2);//设置线段宽度
    glBegin(GL_LINES);
    glColor3f(1.0,0.0,0.0);
    glVertex2f(0.8,1); //定点坐标范围
    glVertex2f(0,-1);
    glEnd();
}
void drawTriangle()
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    glColor3f(1.0, 0.0, 0.0);    // Red
    glVertex3f(0.0, 1.0, 0.0);
    glColor3f(0.0, 1.0, 0.0);    // Green
    glVertex3f(-1.0, -1.0, 0.0);
    glColor3f(0.0, 0.0, 1.0);    // Blue
    glVertex3f(1.0, -1.0, 0.0);
    glEnd();
}
int main(void)
{
    GLFWwindow* window;
    /* Initialize the library */
    if (!glfwInit())
        return -1;
    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(480, 320, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    /* Make the window's context current */
    glfwMakeContextCurrent(window);
    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /*your draw*/
        // drawPoint();
        // drawLint();
        drawTriangle();
        /* Swap front and back buffers */
        glfwSwapBuffers(window);
        /* Poll for and process events */
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

运行效果

相关推荐
Tian_Hang18 小时前
eclipse ditto 学习笔记
运维·服务器·开发语言·javascript·3d
AI视觉网奇21 小时前
BambuStudio 编译实战 2026
3d
AI前沿资讯21 小时前
AI3D角色生产如何减少返工?用 V2Fun 前移建模与动画流程
人工智能·3d
蓝速科技1 天前
蓝速科技视觉 3D 全息舱 AI 数字人一体机带灯与无灯款深度评测
人工智能·科技·3d
尘中远1 天前
【Qwt 7.0 系列】3D 数据可视化 —— OpenGL 高性能三维绘图
qt·3d·qcustomplot·qwt·科学绘图·高性能绘图
林恒smileZAZ2 天前
Three.js 3D 地图特效与材质实现指南
javascript·3d·材质
蓝速科技2 天前
蓝速科技 3D 全息舱 AI 数字人博物馆导览效果实录
人工智能·科技·3d
lauo2 天前
【案例】每天省5小时:ibbot手机文章中心-采集到自动发布-工作流(agent)
人工智能·microsoft·3d·智能手机
chaoyuanl2 天前
沉浸式飞行影院进场安装前期筹备事项
大数据·科技·3d·xr·娱乐
探物 AI19 天前
【3D·感知】从PointNet到PointPillars:如何让自动驾驶汽车“实时“看见3D世界?
3d·自动驾驶·汽车