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

运行效果

相关推荐
郝学胜-神的一滴6 小时前
中级OpenGL教程 004:为几何体注入法线灵魂
c++·unity·游戏引擎·godot·图形渲染·opengl·unreal
小三金7 小时前
免费的国外模型资源网站整理
3d
AI_Auto1 天前
【智能制造】- 工业制造中的3D视觉四大核应用场景
3d·制造
ZC跨境爬虫1 天前
跟着 MDN 学 HTML day_16:(音频与视频处理——从画布滤镜到3D沉浸音频的进阶指南)
前端·javascript·ui·3d·html·音视频
爱看书的小沐2 天前
【小沐学WebGIS】基于Cesium.JS与jsbsim联动三维飞行仿真(OpenGL、Cesium.js、Three.js)
c++·qt·three.js·opengl·cesium·jsbsim
♡すぎ♡2 天前
ShaderLab:可互动水面(基于RenderTexture,实时生成动态扰动)
计算机图形学·贴图·opengl·着色器
爱看书的小沐3 天前
【小沐杂货铺】基于Three.js绘制三维艺术画廊3DArtGallery (Three.js,WebGL)
javascript·3d·webgl·three.js·babylon.js·三维画廊
格林威3 天前
3D相机视觉检测:环境光太强,结构光点云全是噪点怎么办?
开发语言·人工智能·数码相机·计算机视觉·3d·视觉检测·工业相机
爱看书的小沐3 天前
【小沐学GIS】基于C++渲染三维飞行仿真Flight Simulation(OpenGL )第十三期
c++·qt·webgl·opengl·飞行仿真·flight
threelab3 天前
Three.js 3D 饼图效果 | 三维可视化 / AI 提示词
javascript·人工智能·3d