GLFW+GLEW搭建opengl环境(备忘)
2024-08-28 10:38:47
使用VS2017社区版本(免费版)
下载GLFW和GLEW源码。
使用CMAKE生成工程文件
打开右击GLFW和GLEW项目编译
GLFW默认是静态库
编译GLEW时调整为静态库。将生成的lib和源码中的include文件夹放好,新建空的C++项目。在项目属性设置好路径。
opengl32.lib包含在window sdk 10中了。不需要单独编译。
为了支持中文需要使用UTF-8无bom编码。测试代码如下。
#include <iostream>
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
#include <Windows.h>
// GLFW
#include <GLFW/glfw3.h>
#pragma comment(lib,"winmm.lib") // 告诉连接器与这个库连接,因为我们要播放多媒体声音
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
//#define DEBUG
// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode); // Window dimensions
const GLuint WIDTH = , HEIGHT = ; // The MAIN function, from here we start the application and run the game loop
//#ifdef DEBUG
//int main()
//#else
//int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
//#endif
int main()
{
std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
// Init GLFW
glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, );
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, );
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT,"墨迹",nullptr, nullptr);
if (window == nullptr)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -;
}
glfwMakeContextCurrent(window);
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;
return -;
} // Define the viewport dimensions
glViewport(, , WIDTH, HEIGHT); // Game loop
while (!glfwWindowShouldClose(window))
{
// Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents(); // Render
// Clear the colorbuffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); // Swap the screen buffers
glfwSwapBuffers(window);
Sleep();
} // Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return ;
} // Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
std::cout << key << std::endl;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
效果如下
程序400kb,无需动态库。
单独一个程序的话会比动态库要小。
最新文章
- webpack react基础配置一
- ecshop 订单-》订单状态 2
- Save vtkImageData to BMP Image 保存vtkImageData为图片
- delphi的webBrowser操作HTML研究
- 海量数据处理利器之Hash——在线邮件地址过滤
- hdu1507二分匹配
- 【转】android中Uri.parse()用法
- c#中匿名函数lamb表达式
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(2)
- URL重写案例
- HDU 2473 Junk-Mail Filter(并查集+删点,设立虚父节点/找个代理)
- 无法建立到http://localhost:6080/arcgis/manager/的连接
- shell 中 2>;&;1 的使用
- struts1:Struts配置文件初解
- 翻煎饼 Stacks of Flapjacks
- 最优化算法:BFGS算法全称和L-BFGS算法全称
- git查日志命令
- 用clumsy模拟丢包测试socket库的失败重传
- 基于aws api gateway的asp.net core验证
- 微信小程序--家庭记账本开发--06
热门文章
- 读取注册表获取Windows系统XP/7/8/10类型(使用wcscmp比较wchar[]内容)
- 主要C++流派,看看你是哪一流
- Qt+VS编译器:默认库“library”与其他库的使用冲突;使用 /NODEFAULTLIB:library(我曾经碰到过,修改qmake.conf,但我修改的是VS的IDE配置)good
- 深入理解Java G1垃圾收集器
- 获取原生DOM,diy脚手架,vue-clide使用,element-ui的使用
- python的实用函数
- Python连载15-高阶函数&;map映射
- Tido c++线段树知识讲解(转载)
- spring 5.x 系列第19篇 ——spring简单邮件、附件邮件、内嵌资源邮件、模板邮件发送 (xml配置方式)
- 曹工说Tomcat3:深入理解 Tomcat Digester