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;
}

运行效果

相关推荐
程序员极光35 分钟前
第六篇,如何用高德地图获取定位!
3d·高德
Thomas_YXQ4 小时前
Unity3D Addressable 深度优化热更性能消耗
开发语言·3d·unity·微信
七77.4 小时前
【3D 场景生成】NuiScene: Exploring Efficient Generation of Unbounded Outdoor Scenes
3d·世界模型
threelab5 小时前
Three.js 几何图形变换 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
xian_wwq6 小时前
【学习笔记】倾斜摄影、高斯泼溅(3DGS)、点云与数字孪生“族谱”全盘点
笔记·学习·3d
AI视觉网奇6 小时前
stl转glb glb缩放
开发语言·3d
七77.7 小时前
【3D 场景生成】WorldGen: From Text to Traversable and Interactive 3D Worlds
3d·世界模型
文创工作室7 小时前
2024年Adobe Substance 3D Designer
3d·adobe
远离UE47 小时前
3D SDF 多光源 阴影 的不同尝试
3d
人工智能培训7 小时前
用知识图谱重构搜索引擎
大数据·人工智能·3d·重构·知识图谱·agent