ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

openGL API glShaderSource详解

2021-12-15 16:30:52  阅读:203  来源: 互联网

标签:string openGL shader length API glShaderSource GL 着色器


openGL API glShaderSource详解

官方文档

 [函数说明](https://www.khronos.org/registry/OpenGL-Refpages/gl4/)
 Name

glShaderSource — Replaces the source code in a shader object

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

Parameters
shader
Specifies the handle of the shader object whose source code is to be replaced.

count
Specifies the number of elements in the string and length arrays.

string
Specifies an array of pointers to strings containing the source code to be loaded into the shader.

length
Specifies an array of string lengths.

Description
glShaderSource sets the source code in shader to the source code in the array of strings specified by string. Any source code previously stored in the shader object is completely replaced. The number of strings in the array is specified by count. If length is NULL, each string is assumed to be null terminated. If length is a value other than NULL, it points to an array containing a string length for each of the corresponding elements of string. Each element in the length array may contain the length of the corresponding string (the null character is not counted as part of the string length) or a value less than 0 to indicate that the string is null terminated. The source code strings are not scanned or parsed at this time; they are simply copied into the specified shader object.

Notes
OpenGL copies the shader source code strings when glShaderSource is called, so an application may free its copy of the source code strings immediately after the function returns.

Errors
GL_INVALID_VALUE is generated if shader is not a value generated by OpenGL.

GL_INVALID_OPERATION is generated if shader is not a shader object.

GL_INVALID_VALUE is generated if count is less than 0.

Associated Gets
glGetShader with arguments shader and GL_SHADER_SOURCE_LENGTH

glGetShaderSource with argument shader

glIsShader

Version Support
OpenGL Version
Function / Feature Name 2.0 2.1 3.0 3.1 3.2 3.3 4.0 4.1 4.2 4.3 4.4 4.5
glShaderSource ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
See Also
glCompileShader, glCreateShader, glDeleteShader

Copyright
Copyright © 2003-2005 3Dlabs Inc. Ltd. Copyright © 2010-2014 Khronos Group. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. http://opencontent.org/openpub/.

翻译

名称
glShaderSource - 替换着色器对象中的源代码

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

参数
shader

要被替换源代码的着色器对象的句柄(ID)。

count

指定字符串和长度数组中的元素数。

string

指定指向包含要加载到着色器的源代码的字符串的指针数组。

length

指定字符串长度的数组。

描述
对于支持着色器编译器的实现,glShaderSource将着色器中的源代码设置为string指定的字符串数组中的源代码。先前存储在着色器对象中的任何源代码都将被完全替换。数组中的字符串数由count指定。 如果length为NULL,则认为每个字符串都以null结尾。如果length不是NULL,则它指向包含字符串的每个相应元素的字符串长度的数组。length数组中的每个元素可以包含相应字符串的长度(空字符不计为字符串长度的一部分)或小于0的值以表示该字符串为空终止。此时不扫描或解析源代码字符串; 它们只是复制到指定的着色器对象中。

注意
着色器编译器支持是可选的,因此必须在使用之前通过使用参数GL_SHADER_COMPILER调用glGet来查询。glShaderSource,glCompileShader,glGetShaderPrecisionFormat,glReleaseShaderCompiler等在不支持着色器编译器的实现上都将生成GL_INVALID_OPERATION。这样的实现提供了glShaderBinary替代方案,用于提供预编译的着色器二进制文件。

调用glShaderSource时,OpenGL会复制着色器源代码字符串,因此应用程序可以在函数返回后立即释放源代码字符串的副本。

错误
GL_INVALID_OPERATION:不支持着色器编译器

GL_INVALID_VALUE:shader不是OpenGL生成的值

GL_INVALID_OPERATION:shader不是着色器对象

GL_INVALID_VALUE:count比0小

相关Gets
glGet 参数GL_SHADER_COMPILER

glGetShaderiv 参数shader和GL_SHADER_SOURCE_LENGTH

glGetShaderSource 参数shader

glIsShader

另见
glCompileShader,glGetShaderPrecisionFormat,glCreateShader,glDeleteShader

版权
https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glShaderSource.xml

https://blog.csdn.net/flycatdeng

Copyright © 1991-2006 Silicon Graphics, Inc.本文档的许可是根据SGI Free Software B License.详见http://oss.sgi.com/projects/FreeB/.

例子

GLuint createShaderProgram()
{
	GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
	GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
	GLuint vfProagram = glCreateProgram();
	
	string vertexShaderStr = readFile("vertShader.glsl");
	string fragShaderStr = readFile("fragShader.glsl");
	const char* vertShaderSrc = vertexShaderStr.c_str();
	const char* fragShaderSrc = fragShaderStr.c_str();

	glShaderSource(vShader, 1, &vertShaderSrc, nullptr);
	glShaderSource(fShader, 1, &fragShaderSrc, nullptr);
	glCompileShader(vShader);
	glCompileShader(fShader);
	
	glAttachShader(vfProagram, vShader);
	glAttachShader(vfProagram, fShader);
	glLinkProgram(vfProagram);

	return vfProagram;
}

运行效果

在这里插入图片描述

工程下载

工程源码下载

标签:string,openGL,shader,length,API,glShaderSource,GL,着色器
来源: https://blog.csdn.net/aoxuestudy/article/details/121945058

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有