OpenGL入门:窗口开启、改变窗口背景颜色
程序员文章站
2024-03-14 14:12:28
...
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();
}
上一篇: GRPC中设置client的超时时间(golang)
下一篇: 字符串—压缩字符串