你好窗口学完,做个按键改变窗口颜色
#include <iostream>
#include <windows.h>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <random>
#include <time.h>
void processInput(GLFWwindow * window);
void processInput(GLFWwindow* window, int key, int scancode, int action, int mods);
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
std::default_random_engine engine(time(NULL));
int main(int argc,char * argv[]) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "test window", NULL, NULL);
if(window == nullptr)
{
std::cout << "open window fail" << std::endl;
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if(glewInit() != GLEW_OK)
{
std::cout << "glew init fail" << std::endl;
glfwTerminate();
return EXIT_FAILURE;
}
glViewport(0, 0, 800, 600);
glfwSetKeyCallback(window, processInput); // 注册按键回调
while(!glfwWindowShouldClose(window))
{
//processInput(window);
//glClearColor(1.0, 0, 0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void processInput(GLFWwindow* window, int key, int scancode, int action, int mods)
{
std::uniform_real_distribution<float> dis(0, 1.0);
if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
else if(key == GLFW_KEY_1 && action == GLFW_RELEASE)
{
std::vector<float> randVec;
for (int index = 0; index < 4; ++index)
{
float randNum = dis(engine);
randVec.push_back(randNum);
}
printf("%f:%f:%f:%f\n", randVec.at(0), randVec.at(1), randVec.at(2), randVec.at(3));
glClearColor(randVec.at(0), randVec.at(1), randVec.at(2), randVec.at(3));
}
}
void processInput(GLFWwindow * window)
{
if(glfwGetKey(window,GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window,true);
}
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}