OpenGL入门到入坟:Hello Window 背景色控制

操作系统: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值