[OpenGL]创建一个窗口

代码

#include <stdio.h>
#include <stdlib.h>

//GLEW  的库
#include <GL/glew.h> 
 //GLFW 的库,处理窗口和键盘的消息
#include <glfw3.h>  
GLFWwindow* window;

三维数学的库
#include <glm/glm.hpp>  
using namespace glm;

int main( void )
{
    //初始化 GLFW
    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        getchar();
        return -1;
    }

    //设置一些 hint
    //glfwWindowHint(int hint, int mode);
    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //set Vision to 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // Open a window and create its OpenGL context
    window = glfwCreateWindow( 1024, 768, "HelloWorld", NULL, NULL);
    if( window == NULL ){
        fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
        getchar();
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    // Initialize GLEW
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        getchar();
        glfwTerminate();    //destory all remain
        return -1;
    }

    // Ensure we can capture the escape key being pressed below
    // 确保当某些键按下的时候,后面 glfwGetKey 可以获得 GLFW_PRESS
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    // Dark blue background
    // glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
    // glClearColor(Red, Green, Blue, 0.0f);
    // 为色彩缓冲区指定用于清除的值
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    do{
        //执行清除色彩缓冲区的任务
        glClear( GL_COLOR_BUFFER_BIT );

        // Swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents();

    } // Check if the ESC key was pressed or the window was closed
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
           glfwWindowShouldClose(window) == 0 );

    // Close OpenGL window and terminate GLFW
    glfwTerminate();

    return 0;
}

API 分析

glfwSetInputMode

设置特定窗口的输入模式

void glfwSetInputMode   (   GLFWwindow *    window, int     mode, int   value ) 

mode 可以为 GLFW_CURSOR, GLFW_STICKY_KEYS or GLFW_STICKY_MOUSE_BUTTONS.
当 mode = GLFW_CURSOR
value 为
GLFW_CURSOR_NORMAL makes the cursor visible and behaving normally. 让光标可见,且正常
GLFW_CURSOR_HIDDEN makes the cursor invisible when it is over the client area of the window but does not restrict the cursor from leaving. 当光标到窗口区域的时候,光标隐藏
GLFW_CURSOR_DISABLED hides and grabs the cursor, providing virtual and unlimited cursor movement. This is useful for implementing for example 3D camera controls. 强行夺取光标,光标消失
当 mode = GLFW_STICKY_KEYS
value 为
GLFW_TRUE 使能粘滞键。确保按键将会使 glfwGetKey returns GLFW_PRESS。它常常被用在你只关注键是否被按下,而不关注键什么时候按下或者以什么顺序按下时。
GLFW_FALSE 禁能粘滞键。
当 mode = GLFW_STICKY_MOUSE_BUTTONS
value 为
GLFW_TRUE 使能鼠标粘滞键。确保鼠标按钮将会使 glfwGetMouseButton returns GLFW_PRESS
GLFW_FALSE 禁能鼠标粘滞键。

glClearColor & glClear
void glClearColor(GLclampf red,GLclampf green,GLclampf blue,GLclampf alpha)

设置颜色缓冲区颜色为(红,绿,蓝,透明)

void glClear(GLbitfield mask);

执行清理命令
GLbitfield mask:可以使用 | 运算符组合不同的缓冲标志位,表明需要清除的缓冲
GL_COLOR_BUFFER_BIT: 当前可写的颜色缓冲 glClearColor
GL_DEPTH_BUFFER_BIT: 深度缓冲 glClearDepth
GL_ACCUM_BUFFER_BIT: 累积缓冲 glClearAccum
GL_STENCIL_BUFFER_BIT: 模板缓冲 glClearStencil

glfwSwapBuffers & glfwPollEvents

glfwSwapBuffers
This function swaps the front and back buffers of the specified window when rendering with OpenGL or OpenGL ES. 交换前后端缓冲区?不懂——>[OpenGL中的缓冲区] OpenGL 在绘制图元时,先是在一个缓冲区中完成渲染,然后再把渲染结果交换到屏幕上。我们把这两个缓冲区称为前颜色缓冲区(屏幕)和后颜色缓冲区。
glfwPollEvents
This function processes only those events that are already in the event queue and then returns immediately. Processing events will cause the window and input callbacks associated with those events to be called.
作用是 Processes all pending events.

glfwGetKey
int glfwGetKey(GLFWwindow *     window, int     key  )  

返回上一次特定窗口中某个按键的状态(按下GLFW_PRESS 或者 释放GLFW_RELEASE)

    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
           glfwWindowShouldClose(window) == 0 );
  1. 获取 windows 窗口 ESC 键的状态
  2. 如果没有按下返回 GLFW_RELEASE 不会进入 && 后面的判断
  3. 如果有被按下返回 GLFW_PRESS 就进入后面的判断
  4. 执行 glfwWindowShouldClose 指令,返回 close flag
  5. 0 && 0 ,跳出 while 循环

参考

glfw 若干函数列表:http://blog.csdn.net/ccsdu2004/article/details/3838184
OpenGL 中的缓冲区:http://blog.csdn.net/Haohan_Meng/article/details/25246519

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值