操作系统:Win10
工具:VS2019
环境:GLFW 3.3.0.1、GLEW1.9.0.1
测试结果
测试代码
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
using namespace std;
//窗口背景颜色设置
float red, green, blue = 0.0f;
//窗口消息处理函数
void processInput(GLFWwindow* window)
{
//R按下
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
{
cout << "R按下:";
cout << "当前窗口颜色:Red" << endl;
green = 0.0f;
blue = 0.0f;
red = 1.0f;
}
//G按下
if (glfwGetKey(window, GLFW_KEY_G) == GLFW_PRESS)
{
cout << "G按下:";
cout << "当前窗口颜色:Green" << endl;
green = 1.0f;
blue = 0.0f;
red = 0.0f;
}
//B按下
if (glfwGetKey(window, GLFW_KEY_B) == GLFW_PRESS)
{
cout << "B按下:";
cout << "当前窗口颜色:Blue" << endl;
green = 0.0f;
blue = 1.0f;
red = 0.0f;
}
//escape按下关闭窗口
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
cout << "escape 按下:";
cout << "Window was closed..." << endl;
glfwSetWindowShouldClose(window, true);
}
}
int main(int argc, char* argv[])
{
//初始化glfw,使用3.3版本
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//开窗500*500,标题OpenGL
GLFWwindow* window = glfwCreateWindow(500, 500, "OpenGL", NULL, NULL);
//开窗失败
if (window == NULL)
{
cout << "Creating window fail" << endl;
glfwTerminate();
return EXIT_FAILURE;
}
cout << "Create Window successfully" << endl;
glfwMakeContextCurrent(window);
//初始化glew
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
cout << "glew init fail" << endl;
glfwTerminate();
return EXIT_FAILURE;
}
glViewport(0, 0, 500, 500);
//窗口未关闭,循环
while (!glfwWindowShouldClose(window))
{
processInput(window);
//设置背景颜色,清除上一次的缓冲
glClearColor(red, green, blue, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
//交换缓冲
glfwSwapBuffers(window);
glfwPollEvents();
}
//程序退出
glfwTerminate();
return EXIT_SUCCESS;
}