learn OpenGL 开始一个窗口!

本文介绍如何使用OpenGL ES和GLFW库在窗口应用程序中实现颜色的动态变化,通过按键触发随机颜色设置,包括初始化GLFW、设置OpenGL版本、创建窗口、注册按键回调函数等关键步骤。

你好窗口学完,做个按键改变窗口颜色

#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);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值