OpenGL 3.2 核心配置文件指南
谁能推荐一个学习 OpenGL 3.2 核心配置文件的指南?
SDK很难读懂,而且我见过的大部分指南都只教授老方法。
Can anyone suggest a guide for learning the OpenGL 3.2 core profile?
The SDK is hard to read, and most of the guides I have seen only taught the old method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道任何好的指南,但我可以为您做一个快速总结,
我假设您已经熟悉着色器、顶点缓冲区等的基础知识。如果您不熟悉,我建议您阅读有关着色器的指南首先,因为所有 OpenGL 3 都基于着色器的使用
初始化时:
使用
glGenBuffers
、glBindBuffer(GL_ARRAY_BUFFER, bufferID)
和 <创建并填充顶点缓冲区code>glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW)与索引缓冲区相同,只是使用
GL_ELEMENT_ARRAY_BUFFER
而不是GL_ARRAY_BUFFER
创建纹理,就像在以前版本的 OpenGL (
glGenTextures
) 中所做的那样,glBindTexture
,glTexImage2D
)使用
glCreateShader
创建着色器,使用glShaderSource
设置其 GLSL 源代码,并使用glCompileShader 编译它们
;您可以使用glGetShaderiv(shader, GL_COMPILE_STATUS, &out)
检查它是否成功,并使用glGetShaderInfoLog
检索错误消息使用
glCreateShader
创建程序(即一组绑定的着色器) (通常是一个顶点着色器和一个片段着色器)与
glCreateProgram
一起使用,然后使用glAttachShader
绑定所需的着色器,然后使用 链接 程序代码>glLinkProgram;就像着色器一样,您可以使用glGetProgram
和glGetProgramInfoLog
检查链接是否成功
当您想要绘制时:
使用
glBindBuffer
绑定顶点和索引缓冲区分别带有参数GL_ARRAY_BUFFER
和GL_ELEMENT_ARRAY_BUFFER
绑定程序与
glUseProgram
现在,对于着色器中的每个变化变量,您必须调用
glVertexAttribPointer
,其语法类似于旧版glVertexPointer
、glColorPointer
code> 等函数,但其第一个参数除外,它是变量的标识符;可以通过在链接程序上调用glGetAttribLocation
来检索此标识符对于着色器中的每个统一变量,您必须调用
glUniform
;它的第一个参数是统一变量的位置(也是一种标识符),您可以通过调用glGetUniformLocation来检索该变量(警告:如果您有一个名为“a”的数组,则必须调用function with "a[0]")对于要绑定的每个纹理,您必须使用
GL_TEXTUREi
调用glActiveTexture
(i对于每个纹理都不同) ,然后glBindTexture
,然后将uniform的值设置为iCall
glDrawElements
这是你必须做的基本事情。当然还有其他东西,如顶点数组对象、统一缓冲区等,但它们仅用于优化目的
其他领域如剔除、混合、视口等与旧版本的 OpenGL 基本保持相同
我也建议您学习此演示程序(使用顶点数组对象)。最有趣的文件是 main.cpp
希望这可以帮助
I don't know any good guide but I can make you a quick summary
I'll assume that you are already familiar with the basics of shaders, vertex buffers, etc. If you don't, I suggest you read a guide about shaders first instead, because all OpenGL 3 is based on the usage of shaders
On initialisation:
Create and fill vertex buffers using
glGenBuffers
,glBindBuffer(GL_ARRAY_BUFFER, bufferID)
andglBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW)
Same for index buffers, except that you use
GL_ELEMENT_ARRAY_BUFFER
instead ofGL_ARRAY_BUFFER
Create textures exactly like you did in previous versions of OpenGL (
glGenTextures
,glBindTexture
,glTexImage2D
)Create shaders using
glCreateShader
, set their GLSL source code usingglShaderSource
, and compile them withglCompileShader
; you can check if it succeeded withglGetShaderiv(shader, GL_COMPILE_STATUS, &out)
and retrieve error messages usingglGetShaderInfoLog
Create programs (ie. a group of shaders bound together, usually one vertex shader and one fragment shader) with
glCreateProgram
, then bind the shaders you want usingglAttachShader
, then link the program usingglLinkProgram
; just like shaders you can check linking success withglGetProgram
andglGetProgramInfoLog
When you want to draw:
Bind the vertex and index buffers using
glBindBuffer
with argumentsGL_ARRAY_BUFFER
andGL_ELEMENT_ARRAY_BUFFER
respectivelyBind the program with
glUseProgram
Now for each varying variable in your shaders, you have to call
glVertexAttribPointer
whose syntax is similar to the legacyglVertexPointer
,glColorPointer
, etc. functions, except for its first parameter which is an identifier for the varying ; this identifier can be retrieved by callingglGetAttribLocation
on a linked programFor each uniform variable in your shaders, you have to call
glUniform
; its first parameter is the location (also a kind of identifier) of the uniform variable, which you can retrieve by callingglGetUniformLocation
(warning: if you have an array named "a", you have to call the function with "a[0]")For each texture that you want to bind, you have to call
glActiveTexture
withGL_TEXTUREi
(i being different for each texture), thenglBindTexture
and then set the value of the uniform to iCall
glDrawElements
This is the basic things you have to do. Of course there are other stuff, like vertex array objects, uniform buffers, etc. but they are merely for optimization purposes
Other domains like culling, blending, viewports, etc. remained mostly the same as in older versions of OpenGL
I also suggest you study this demo program (which uses vertex array objects). The most interesting file is main.cpp
Hope this can help