glShaderSource的参数的含义是什么? [英] What is the meaning of the parameters to glShaderSource?

查看:1696
本文介绍了glShaderSource的参数的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着深入了解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 ++中意味着什么?这是否意味着我给它一个字符串,一个const字符串或指针的字符串,如果这是可能的话?为什么?

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 需要两个相关序列数组)的值。

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规范需要谈

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类型的别名类似于C char 的类型,用于表示一个窄字符。
GLint 是一个有符号整数类型,通常用于表示对象句柄和偏移量。
还有其他人喜欢 GLenum GLvoid

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拼写。除了用于指向单个字符,它通常用于表示一个字符串。当以这个意思使用时,它应该指向一个字符序列,以一个哨兵值'\0'结束,以知道字符串结束。

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 '\0' to know that the string ends.

使得 glShaderSource 采用多个字符串的原因是因为OpenGL的着色器编译器暴露了一个文件的概念。这些字符串中的每一个表示一个文件的内容,并且它将被编译,就像这些文件被连接在一起。请注意,此文件与文件系统名称相同。 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 语句或一些常见的前缀,或者自己实现某种类型的include指令,那么就可以将它们整合到完整的着色器源中。

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.

有关如何使用它的示例:

As for an example of how to use it:

std::string v = "#version 150\n";
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的参数的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆