glShaderSource 的参数是什么意思?

时间:2023-05-10
本文介绍了glShaderSource 的参数是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我已尽最大努力深入了解 OpenGL 的功能,但我对 glShaderSource 的参数有疑问:

I've tried my hardest to understand the functions of OpenGL in-depth, but I have an issue with glShaderSource's parameters:

void glShaderSource(
  GLuint shader,
  GLsizei count,
  const GLchar * const * string,
  const GLint * length);

我对最后两个参数感到困惑.它们在 C++ 中实际上是什么意思?如果可能的话,这是否意味着我给它一个字符串、一个常量字符或一个字符串指针?为什么?

I'm confused about the last two parameters. What do they actually mean in C++? Does it mean I give it a string, a const char or pointer of a string if thats even possible? And why?

推荐答案

glShaderSource 需要两个相关的值序列(C 样式数组).

glShaderSource expects two related sequences (C-style arrays) of values.

第一个序列是一个 C 字符串数组,无论是否以零结尾.

The first sequence is an array of C-strings, either zero-terminated or not.

第二个序列是一个整数数组,表示第一个序列中每个字符串的长度.如果字符串以零结尾,则此序列是可选的,因为库会自行查找长度.

The second sequence is an array of integers, indicating the length of each of the strings in the first sequence. This sequence is optional if the strings are zero-terminated, as the library will find the lengths by itself.

GL 前缀类型是因为 OpenGL 规范需要谈论类型而不将自己绑定到特定语言,所以它引入了常见 C 类型的别名.

The GL-prefixed types are because the OpenGL specification needs to talk about types without tying itself into a particular language, so it introduces aliases of common C types.

GLchar 是一种类似于 C char 的类型,用于表示窄字符.GLint 是有符号整数类型,通常用于表示对象句柄和偏移量.还有其他的,比如 GLenumGLvoid.

GLchar is a type similiar to the C char, which serves to represent a narrow character. GLint is a signed integer type, commonly used to represent object handles and offsets. There's also others like GLenum and GLvoid.

GLchar const*char const* 类型的 OpenGL 拼写.除了用于指向单个字符外,它通常用于表示一串字符.当用于该含义时,它应指向一个字符序列,以标记值 '' 结尾,以知道字符串结束.

GLchar const* is the OpenGL spelling of the char const* type. Apart from being used to point to a single character, it's commonly used to represent a string of characters. When used in that meaning, it shall point to a sequence of characters, ending with a sentinel value '' to know that the string ends.

glShaderSource 取多个字符串的原因是因为OpenGL 的着色器编译器暴露了文件 的概念.这些字符串中的每一个都代表一个文件的内容,并且编译时就好像这些文件连接在一起一样.请注意,这个 file 与同名的文件系统事物在很大程度上无关.glShaderSource 只处理包含文本的字符串.

The reason for making glShaderSource take more than one string is because OpenGL's shader compiler has exposed the concept of a file. Each of these strings represents the contents of one file, and it will compile as if these files are concatenated together. Note that this file is largely unrelated to the filesystem thing of the same name. glShaderSource only deals with strings containing text.

当您想将一些片段组合到完整的着色器源中时,这很有用,例如,如果您想添加 #version 语句或一些常见的前导码,或者已经实现了一些自己的某种包含指令.

This is beneficial when you've got some fragments you want to assemble together into the full shader source, like if you want to prepend a #version statement or some common preamble, or have implemented some sort of include directive yourself.

举个例子:

std::string v = "#version 150
";
std::string c = ReadTextFile("common.glsl"); // read a string with the file contents
std::string s = ReadTextFile("mesh.vert");   // same here

GLchar const* files[] = { v.c_str(), c.c_str(), s.c_str() };
GLint lengths[]       = { v.size(),  c.size(),  s.size()  };

glShaderSource(id, 3, files, lengths);

这里我们将三个字符串组合在一起,以便 OpenGL 将其视为一大块源文本.请注意,我的便利函数 ReadTextFile 将文件系统文件的内容读入字符串,OpenGL 的任何部分都不会触及文件系统.如果要将文本文件、图像文件或网格文件的内容转为 OpenGL 结构,则需要提前自行阅读.

Here we're combining three strings for OpenGL to consider as one large chunk of source text. Note that my convenience function ReadTextFile reads the content of a filesystem file into a string, no part of OpenGL ever touches the filesystem. If you want to get the contents of a text file, image file or mesh file into OpenGL structures, you need to read it in for yourself ahead of time.

这篇关于glShaderSource 的参数是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:平移/旋转后如何重新计算轴对齐的边界框? 下一篇:是否有任何计划或现有项目将 OpenGL API 移植到 C++?

相关文章