This is my code
Here's the header file.
class ShaderManager {
private:
const GLchar** vertexShaderCode_color;
const GLchar** fragmentShaderCode_color;
const GLchar** vertexShaderCode_texture;
const GLchar** fragmentShaderCode_texture;
ShaderManager();
~ShaderManager();
cpp file. in constructor
vertexShaderCode_color = {
"uniform mat4 uMVPMatrix;\n"
"attribute vec4 vPosition;\n"
"void main() { \n"
" gl_Position = uMVPMatrix * vPosition; \n"
"}\n"
};
fragmentShaderCode_color = {
"precision mediump float;\n"
" uniform vec4 vColor;\n"
"void main() {\n"
"gl_FragColor = vColor;\n"
"}\n"
};
vertexShaderCode_texture = {
"uniform mat4 uMVPMatrix;\n"
"attribute vec4 vPosition;\n"
"attribute vec2 texCoord;\n"
" varying vec2 texCoordOut;\n"
"void main() {\n"
"gl_Position = uMVPMatrix * vPosition;\n"
"texCoordOut = texCoord;\n"
"}\n"
};
fragmentShaderCode_texture = {
"precision mediump float;\n"
"varying vec2 texCoordOut;\n"
"uniform sampler2D u_texture;\n"
"uniform vec4 vColor;\n"
" void main() {\n"
" vec4 texColor = texture2D(u_texture, texCoordOut);\n"
" gl_FragColor = texColor;\n"
"}\n"
};
It's not working. The error message is:
Error 3 error : cannot convert '' to 'const GLchar** {aka const char**}' in assignment D:\\workspace\\VS2013\\Projects\\bbEngine2\\bbEngine2\\src\\GLManager\\ShaderManager.cpp 10 2 bbEngine2
how to initialize const char** array
In the same way as every other array.
const GLchar** vertexShaderCode_color;
That's not a const char**
array. It's a single pointer to const GLchar*
.
vertexShaderCode_color = {
"uniform mat4 uMVPMatrix;\n"
"attribute vec4 vPosition;\n"
"void main() { \n"
" gl_Position = uMVPMatrix * vPosition; \n"
"}\n"
};
That is indeed how to initialize an array. But as I already pointed out, you didn't declare vertexShaderCode_color
as an array. It's a pointer. Also, if that's inside the constructor body, then the member is already initialized. You annot assign to an array.
If you indeed want vertexShaderCode_color
to be an array, then you need to declare it as an array. Remember that sizes of arrays must be known at runtime:
const char* vertexShaderCode_color[5];
And then initialize it in the member initializer list or in a default member initializer.
I think you need something like:
static const char* vertexShaderCode_color_value[] = {
"uniform mat4 uMVPMatrix;\n"
"attribute vec4 vPosition;\n"
"void main() { \n"
" gl_Position = uMVPMatrix * vPosition; \n"
"}\n"
};
vertexShaderCode_colour = vertexShadeCode_colour_value;
You are being hit by the subtle difference between arrays and pointers in C and C++.
Edit: I thought you needed to namespace qualify the variable, but I see this is in the constructor ... in which case if you can't use a static variable for the value, you need to do some memory allocation:
In class definition:
std::vector<const GLChar*> vertexShaderCode_color_value;
GLChar** vertexShaderCode_color; // Order is significant.
In initializer list:
vertexShaderCode_colour_value { ... },
vertexShaderCode_colour { &vertexShaderCode_colour_value.front() },
(or do it in the constructor body if you prefer)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.