window默认支持到OpenGL1.3,后续也没有更新,opengl并不是以API更新或者新的开发包方式更新功能,它使用了扩展方式获取高版本新的功能。
如执行扩展功能函数glArrayElementEXT:代码如下
char* ext = (char*)glGetString(GL_EXTENSIONS); //获取该电脑显卡支持的扩展函数结合
PFNGLARRAYELEMENTEXTPROC glArrayElementEXT = (PFNGLARRAYELEMENTEXTPROC)wglGetProcAddress("glArrayElementEXT"); //获取扩展函数glArrayElementEXT 指针
glArrayElementEXT(0); //通过函数glArrayElementEXT 指针 调用扩展函数glArrayElementEXT
上面这样获取扩展功能的方式很麻烦,所以就诞生了glew,是一个跨平台的C++扩展库,方便开发者使用opengl扩展功能。
安装及使用
环境:win7 VS2013
1. 下载glew:
地址:https://github.com/Perlmint/glew-cmake
2. 打开生成glew VS工程:
在build目录下选择对应版本的VS工程打开,我使用的是VS2013打开 VC12目录下glew.sln
glew_shared :是编译动态链接库
glew_static : 是编译静态库
根据自己情况选择一种方式,我这里选择glew_static 。
编译之后得到glew32sd.lib
3. opengl项目配置:
a. 项目属性 ----> C/C++ —> 附加包含目录 —> your_path\glew-master\include
b. 项目属性 ----> 链接器 —> 常规 —> 附加库目录 —> your_path\lib
c. 项目属性 ----> 链接器 —> 输入 —> 附加依赖项 —> glew32sd.lib
4. 代码:
注意: glew.h要放在glfw3.h glut.h 之前,并定义宏 GLEW_STATIC
#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 = 800, HEIGHT = 600;
// 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, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
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, "win view", nullptr, nullptr);
if(window == nullptr) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
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 -1;
}
// Define the viewport dimensions
glViewport(0, 0, 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(1.0f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
/* Draw a triangle */
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();
/* Swap front and back buffers */
glfwSwapBuffers(window);
//Sleep(20);
}
// Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return 0;
}
// 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);
}
5. 运行结果:
来源:oschina
链接:https://my.oschina.net/u/4313085/blog/3274378