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

运行效果

相关推荐
Struart_R12 小时前
Spann3R、MUSt3R、CUT3R、TTT3R论文解读
人工智能·计算机视觉·3d·三维重建·前馈模型
( ⩌ - ⩌ )13 小时前
OpenCV实现视频采集
opencv·计算机视觉·opengl
alvin_200514 小时前
python之OpenGL应用(五)变换
python·opengl
twe775825814 小时前
电镀液应用中的技术支持体系:如何优化工艺并解决常见问题
科技·3d·制造·动画
老李的森林21 小时前
机械--3D打印切片软件Cura与Simplify3D
3d
zhooyu1 天前
二维坐标转三维坐标的实现原理
c++·3d·opengl
twe77582581 天前
3D IC封装的崭新视角:如何用3D动画揭示技术奥秘
科技·3d·制造·动画
( ⩌ - ⩌ )2 天前
4.OpenGL纹理贴图
qt·opengl·纹理
syncon122 天前
激光轻松修复OLED手机屏幕绿线故障
科技·3d·制造
3DVisionary2 天前
捕捉亚毫米级裂纹演化!DIC技术为裂纹扩展与抗裂研究带来全新方案
人工智能·python·3d·应变测量·金属3d打印·dic精度检验方法·各向异性