Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Const pointer to pointer to char problems

This thread is locked; no one can reply to it. rss feed Print
Const pointer to pointer to char problems
Ariesnl
Member #2,902
November 2002
avatar

I would like to manage OpenGL shaders in a more C++ and OO way
So I wanted to write a class for it..
However I run into trouble making the shader code a bit more managable..

I want to store the shader code into my class and then compile it

OpenGL expects a constant pointer to a pointer to a char "const **GLchar"
Right now I have an array of Glchar where the shadercode is stored..

Is there any good way to stuff one into the other ??

Maybe it's me but I don't see it....

#SelectExpand
1bool OpenGLShader::Compile() 2{ 3 GLchar source[m_vShaderSource.size()+2]; 4 vector<GLchar>::const_iterator P = m_vShaderSource.begin(); 5 int nIndex =0; 6 while (P!=m_vShaderSource.end()) 7 { 8 source[nIndex] = *P; 9 nIndex++; 10 P++; 11 } 12 13 source[nIndex] ='\0'; // <------- 14 15 const GLchar ** pSource = &source; // <-------- 16 glShaderSource(m_nShaderIndex, 1, pSource, NULL); // <--------- 17 18 19 glCompileShader(m_nShaderIndex); 20 GLint status; 21 glGetShaderiv(m_nShaderIndex, GL_COMPILE_STATUS, &status); 22 if (status!=GL_TRUE) 23 { 24 glGetShaderInfoLog(m_nShaderIndex, 512, NULL, m_StatusBuffer); 25 return false; 26 } 27 else 28 { 29 return true; 30 } 31}

Thanks...

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Does casting work? A const** to GLchar just means you cant change the GLchars in the array.

const ** GLChar psource = (const ** GLCchar)&source;

One thing to ask is do you need to make it a 2d array? Right now you just have an array, or a 2d array with one row. What does the shader expect?

Ariesnl
Member #2,902
November 2002
avatar

#SelectExpand
1const GLchar* vertexSource = 2 "#version 150 core\n " 3 "in vec2 position; " 4 "in vec3 ColorIn; " 5 "out vec3 ColorFragment; " 6 "void main()" 7 "{" 8 "ColorFragment = ColorIn;" 9 "gl_Position = vec4(position, 0.0, 1.0);" 10 "}";

This code compiles on the graphics card so I think I need a 2D array...

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Arthur Kalliokoski
Second in Command
February 2005
avatar

It compiles to one long string.

00001c60  69 6e 67 00 25 73 00 00  23 76 65 72 73 69 6f 6e  |ing.%s..#version|
00001c70  20 31 35 30 20 63 6f 72  65 0a 20 69 6e 20 76 65  | 150 core. in ve|
00001c80  63 32 20 70 6f 73 69 74  69 6f 6e 3b 20 69 6e 20  |c2 position; in |
00001c90  76 65 63 33 20 43 6f 6c  6f 72 49 6e 3b 20 6f 75  |vec3 ColorIn; ou|
00001ca0  74 20 76 65 63 33 20 43  6f 6c 6f 72 46 72 61 67  |t vec3 ColorFrag|
00001cb0  6d 65 6e 74 3b 20 76 6f  69 64 20 6d 61 69 6e 28  |ment; void main(|
00001cc0  29 7b 43 6f 6c 6f 72 46  72 61 67 6d 65 6e 74 20  |){ColorFragment |
00001cd0  3d 20 43 6f 6c 6f 72 49  6e 3b 67 6c 5f 50 6f 73  |= ColorIn;gl_Pos|
00001ce0  69 74 69 6f 6e 20 3d 20  76 65 63 34 28 70 6f 73  |ition = vec4(pos|
00001cf0  69 74 69 6f 6e 2c 20 30  2e 30 2c 20 31 2e 30 29  |ition, 0.0, 1.0)|
00001d00  3b 7d 00 00 00 00 00 00  00 00 50 c1 00 00 c0 3f  |;}........P....?|

They all watch too much MSNBC... they get ideas.

Thomas Fjellstrom
Member #476
June 2000
avatar

Multiple literal strings that are one after the other get concatenated.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Arvidsson
Member #4,603
May 2004
avatar

bamccaig
Member #7,536
July 2006
avatar

So basically it wants a pointer to a string? &source should be your "C string" (that is, a pointer to the beginning of the array of characters) so you may need to store that somewhere in order to take the address of that?

const GLchar * str = &source;
const GLchar ** ptr = &str;
//    Here, eat ^ that and leave us alone!

???

Append:

That said, a non-const pointer to a string suggests that they may wish to change it on you, which may or may not throw you for a loop... Do make sure that what you're trying is safe.

Go to: