OpenGL Tutorial 2
VAO:
OpenGL抛弃glEnable(),glColor(),glVertex(),glEnable()这一套流程的函数和管线以后,就需要一种新的方法来传递数据到Graphics Card来渲染几何体,我们可以用VBO, 在3+版本我们可以使用Vertex Array Object-VAO,VAO是一个对象,其中包含一个或者更多的Vertex Buffer Objects。而VBO是Graphics Card中的一个内存缓冲区,用来保存顶点信息,颜色信息,法线信息,纹理坐标信息和索引信息等等。
VAO在Graphics Card线性的存储几个对象信息,替代了以前发送我们需要的数据到Graphics Card上,这也是Direct3D没有立即模式情况下工作的方法,这就意味着应用程序不需要传输数据到Graphics Card上而得到较高的性能。
VBO其实就是显卡中的显存,为了提高渲染速度,可以将要绘制的顶点数据缓存在显存中,这样就不需要将要绘制的顶点数据重复从CPU发送到GPU, 浪费带宽资源。
而VAO则是一个容器,可以包括多个VBO, 它类似于以前的call list, 由于它进一步将VBO容于其中,所以绘制效率将在VBO的基础上更进一步。
A Vertex Array Object (VAO) is an object which contains one or more Vertex Buffer Objects and is designed to store the information for a complete rendered object.
A Vertex Buffer Object (VBO) is a memory buffer in the high speed memory of your video card designed to hold information about vertices.
A Vertex Shader in OpenGL is a piece of C like code written to the GLSL specification which influences the attributes of a vertex. Vertex shaders can be used to modify properties of the vertex such as position, color, and texture coordinates.
A Fragment Shader is similar to a Vertex Shader, but is used for calculating individual fragment colors. This is where lighting and bump-mapping effects are performed.
Right Hand Rule
X is your thumb
Y is your index
Z is your middle finger. If you put your thumb to the right and your index to the sky, it will point to your back, too.
遇到的问题:
把下面这段代码放错了地方。暂时还不理解为什么,也不知道何谓"Do this once your window is created (= after the OpenGL Context creation"。待细究~~~
// Create a Vertex Array Object and set it as the current one.
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
// ( Do this once your window is created (= after the OpenGL Context creation) and before any other OpenGL call.)
Shader:
Shader Compilation
In the simplest possible configuration, you will need two shaders : one called Vertex Shader, which will be executed for each vertex, and one called Fragment Shader, which will be executed for each sample. And since we use 4x antialising, we have 4 samples in each pixel.
Shaders are programmed in a language called GLSL : GL Shader Language, part of OpenGL. GLSL has to be compiled at run time, which means that every time you launch your application, all your shaders are recompiled.
The two shaders are usually in separate files.The extension is irrelevant, it could be .txt or .glsl .
Load shader:
rough steps:
Create shaders; Read Vertex Shader and
Fragment Shader
code from the file; Compile and check Shaders; Link and check the program.
Vertex Shader
The first line tells the compiler that we will use OpenGL 3′s syntax.
#version 330 core
The second line declares the input data :
layout(location = 0) in vec3 vertexPosition_modelspace;
√
“vec3″ is a vector of 3 components in GLSL. It is similar (but different) to the glm::vec3 we used to declare our triangle. The important thing is that if we use 3 components in C++, we use 3 components in GLSL too.
√“layout(location = 0)” refers to the buffer we use to feed the vertexPosition_modelspace attribute. Each vertex can have numerous attributes : A position, one or several colours, one or several texture coordinates, lots of other things. OpenGL doesn’t know what a colour is : it just sees a vec3. So we have to tell him which buffer corresponds to which input. We do that by setting the layout to the same value as the first parameter to glVertexAttribPointer. The value “0″ is not important, it could be 12 (but no more than glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &v) ), the important thing is that it’s the same number on both sides.
√
“vertexPosition_modelspace” could be anything else. It will contain the position of the vertex for each run of the vertex shader.
√
“in” means that this is some input data. Soon we’ll see the “out” keyword.
The function that is called for each vertex:
void main(){
gl_Position.xyz = vertexPosition_modelspace;
gl_Position.w = 1.0;
}
gl_Position is one of the few
built-in variables
: you have to assign some value to it. Everything else is optional; we’ll see what “everything else” means in
Tutorial 4
.
Fragment Shader
set the color of each fragment to red. (Remember, there are 4 fragment in a pixel because we use 4x AA):
#version 330 core
out vec3 color;
void main(){
color = vec3(1,0,0);
}