OpenGL入门:窗口开启、改变窗口背景颜色

开启一个窗口:


// Include for GLFW headers - library handling the window
#include <GLFW/glfw3.h>
#include <iostream>

int main()
{
    glfwInit(); // 初始化GLFW,在创建窗口前一定要做
    
    // 创建一个名为My Window的500*500的窗口
    // 第一个参数用于指定是否是全屏,第二个参数用于指向一个窗口用于共享资源。
    // 返回一个创建好的窗口的指针,如果失败则返回nullptr
    GLFWwindow* window = glfwCreateWindow(500, 500, "My Window", nullptr, nullptr);

    if( window==nullptr ) {
        std::cerr<<"GLFW Failed to create a Window"<<std::endl;
        glfwTerminate();
        exit(1);
    }

	// 如果窗口没有关闭
    while( !glfwWindowShouldClose(window) ) {
        glfwSwapBuffers(window); // Double buffering (will be used to avoid flickering when animating a scene)
        glfwPollEvents();        // Handle GLFW events (ex. mouse clicks, etc)
    }

    glfwTerminate(); // Close the GLFW Window

    return 0;
}

关键代码:

glfwInit();
GLFWwindow* window = glfwCreateWindow(500,500,"My Window",nullptr,nullptr);
glfwWindowShouldClose(window); //True or False
glfwSwapBuffers(window); //减少闪烁
glfwPollEvents(); //用于检测鼠标点击
glfwTerminate();

Rq:

  • OpenGL 会直接和GPU对话。
  • OpenGL 是一个库。

运用OpenGL在窗口中绘制图形

显示一个窗口

#include "../external/glad/include/glad/glad.hpp" // glad.h should be included before glfw or any OpenGL related include

#include <GLFW/glfw3.h>
#include <iostream>
#include <cmath>

int main()
{
    glfwInit();

    // 告诉GLFW OpenGL的环境,这里是OpenGL 3.3 core profile
    glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, 1);
    glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
    glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
	
	// =====跟之前一样,初始化一个窗口=====
    GLFWwindow* window = glfwCreateWindow(500, 500, "My Window", nullptr, nullptr);

    if( window==nullptr ) {
        std::cerr<<"GLFW Failed to create a Window"<<std::endl;
        glfwTerminate();
        exit(1);
    }
	// ===============================
	
    // 使得窗口能接受OpenGL的指令
    glfwMakeContextCurrent(window);

    // Load OpenGL Functions
    gladLoadGL();

    // Print OpenGL Information,与具体的显卡有关
    std::cout << "OpenGL information: VENDOR      : " << glGetString(GL_VENDOR)   <<std::endl;
    std::cout << "                    RENDERDER   : " << glGetString(GL_RENDERER) <<std::endl;
    std::cout << "                    VERSION     : " << glGetString(GL_VERSION)  <<std::endl;
    std::cout << "                    GLSL VERSION: " << glGetString(GL_SHADING_LANGUAGE_VERSION)<<std::endl;

	// =====跟之前一样,对一个窗口的关闭进行判断=====
    while( !glfwWindowShouldClose(window) ) {
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
	// ========================================
    
    return 0;
}

关键代码:

#include "../external/glad/include/glad/glad.hpp" // glad.h should be included before glfw or any OpenGL related include
// 告诉GLFW OpenGL的环境,这里是OpenGL 3.3 core profile
    glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, 1);
    glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
    glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
// 使得窗口能接受OpenGL的指令
    glfwMakeContextCurrent(window);
// Load OpenGL Functions
    gladLoadGL();

Rq:

  • 为了得到一个良好的动画的体验,至少要有25fps。

利用OpenGL修改窗口背景颜色

while( !glfwWindowShouldClose(window) )的循环中加入

// Set the (R,G,B,A) color to clear the screen
// RGBA (Red-Green-Blue-Alpha), A表示透明度,每一个值在0和1之间。
glClearColor(1.0f, 1.0f, 0.5f, 1.0f);
// Clear the screen (designated by the color buffer),如果不删除不能改变颜色
glClear(GL_COLOR_BUFFER_BIT);

改变颜色的闪烁

我们用余弦函数的形式改变图片中红色的组成多少

int counter = 0;
while( !glfwWindowShouldClose(window) ) {
    counter = (counter+1)%100;
    float u = counter/99.0f; // 不能写成/99,因为需要一个float的除法!
    // Set the (R,G,B,A) color to clear the screen
    glClearColor(0.5+std::cos(2*3.14f*u)/2.0f, 1.0f, 0.5f, 1.0f);
    // Clear the screen (designated by the color buffer)
    glClear(GL_COLOR_BUFFER_BIT);
    glfwSwapBuffers(window);
    glfwPollEvents();
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值