This is the quote from a book:
"The first thing we need to do is write the vertex shader in the shader language GLSL and then compile this shader so we can use it in our application."
And then the source code provided:
#version 330 core
layout (location=0)in vec3 position;
void main()
{
gl_Position=vec4(position.x, position.y,position.z,1.0);
};
I was dazzled at first. This cannot be written inside my .cpp file, because it was nothing C-like. I know I should create a .glsl file or something, but there were no specific instructions in the book. Then I found an example code (This was written in the .cpp):
//Shaders
const GLchar* vertexShaderSource = "#version 330 core\n"
"layout (location=0)in vec3 position;\n"
"void main()\n"
"{\n"
"gl_Position=vec4(position.x, position.y,position.z,1.0);\n"
"}\0";
...And indeed it worked. But why? Was the string converted into a file and then somehow read by the method glShaderSource();
? All this wasn't explained in the book.
My question:
- How does this method use a string as an argument and read the code?
- This is indeed a lengthy way. For example, I need to type " " and '\n' for every line. Else, it would be very unorganized and ugly without the '\n's. Is there an alternative method?