OpenGL 应用程序可以使用以下代码来声明和存储 uvec4 变量:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
int main()
{
// Initialize GLFW
if (!glfwInit())
{
std::cerr << "Failed to initialize GLFW" << std::endl;
return -1;
}
// Create a windowed mode window and its OpenGL context
GLFWwindow* window = glfwCreateWindow(640, 480, "My Window", NULL, NULL);
if (!window)
{
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Make the window's context current
glfwMakeContextCurrent(window);
// Initialize GLEW
if (glewInit() != GLEW_OK)
{
std::cerr << "Failed to initialize GLEW" << std::endl;
glfwTerminate();
return -1;
}
// Declare and store a uvec4 variable
GLuint myUVec4 = glGetUniformLocation(shaderProgram, "myUVec4");
glUniform4ui(myUVec4, 255, 0, 0, 255);
// Main loop
while (!glfwWindowShouldClose(window))
{
// Render here
// Swap front and back buffers
glfwSwapBuffers(window);
// Poll for and process events
glfwPollEvents();
}
glfwTerminate();
return 0;
}
在上面的代码中,我们声明了一个名为 myUVec4
的变量,并将其存储在着色器程序中。我们使用 glUniform4ui()
函数将值 (255, 0, 0, 255)
分别存储到该变量的四个分量中。这个值代表了一个红色不透明颜色,因为前三个分量的值是最大的,而最后一个分量代表了完全不透明的 alpha 值。
在主循环中,我们可以使用这个变量来设置渲染状态或者在着色器程序中进行计算。