OpenGL着色器 GLSL语言让三角形颜色随着时间改变

本文探讨了如何在使用GLUT库进行OpenGL编程时,实现一个基于键盘输入而非默认的glutDisplayFunc无限循环,并解释了为何仅通过glutDisplayFunc不能实现无限循环,以及如何通过键盘事件触发自定义的display函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Github Link

glutDisplayFunc(display);
glutMainLoop();

glut库中的这两行代码无法无限循环,display函数只执行了一次。glutMainLoop()函数,只有改变窗体大小才能触发glutDisplayFunc(display);
我运行程序之后,任意敲击键盘,开启无限循环
在这里插入图片描述

#include <stdlib.h>
#include<iostream>
#include<GL/glew.h>
#include<GL/glut.h>
void userInit();  //自定义初始化
void display(void);

GLuint VAO; 
GLuint VBO;
unsigned int vertexShader;
unsigned int fragmentShader;
unsigned int shaderProgram;
float vertices[] = {
   
		 0.5f, -0.5f, 0.0f,  // bottom right
		-0.5f, -0.5f, 0.0f,  // bottom left
		 0.0f,  0.5f, 0.0f   // top 
};
const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
"   gl_Position = vec4(aPos, 1.0);\n"
"}\0";

const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"uniform vec4 ourColor;\n"
"void main()\n"
"{\n"
"   FragColor = ourColor;\n"
/*here we have the input Color. We just write it out to FragColor. FragColor is our own output variable which GL automatically bind to output 0, therefore we don't need to set it up from our C++ side of the code.*/
"}\n\0";


void keyboard
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值