-2

I've been trying to get shaders to work. They don't.

std::cout << "a " << glGetError() <<std::endl; //making sure things work
GLuint vertexShader = 0;
GLuint fragmentShader = 1;
glCreateShader(GL_VERTEX_SHADER);
std::cout <<"b " << glGetError() <<std::endl;
glCreateShader(GL_FRAGMENT_SHADER);
std::cout <<"c " << glGetError() <<std::endl;
glShaderSource(vertexShader, GLsizei(1), (const char**)&VertexShaderSource, NULL);//ERROR
std::cout <<"d " << glGetError() <<std::endl;
glShaderSource(fragmentShader, GLsizei(1), (const char**)&FragmentShaderSource, NULL);//is somehow fine
std::cout <<"e " << glGetError() <<std::endl;
glCompileShader(vertexShader);
std::cout <<"f " << glGetError() <<std::endl;
glCompileShader(fragmentShader);
std::cout <<"g " << glGetError() <<std::endl;'

Now, the output is:

a 0
b 0
c 0
d 1281
e 0
f 1281
g 0

This means that the first glShaderSource call is not working, but, for some reason the next glShaderSource works, while doing what I think is the exactly same thing. What is going on in here???

Shaders are:

const std::string VertexShaderSource ="#version 330 core"
                                  ""
                                  "layout (location = 0) in vec3 position;"
                                  ""
                                  "void main()"
                                  "{"
                                  "    gl_Position = vec4(position.x, position.y, position.z, 1.0);"
                                  "}";

const std::string FragmentShaderSource ="#version 330 core"
                                    ""
                                    "out vec4 color;"
                                    ""
                                    "void main()"
                                    "{"
                                    "    color = vec4(1.0f, 1.0f, 1.0f, 1.0f);"
                                    "}";

And if passing string this way ((const char**)&VertexShaderSource) is wrong, then why the second call works just fine?

t.niese
  • 39,256
  • 9
  • 74
  • 101
ImAlive
  • 53
  • 6

2 Answers2

3

You have to set vertexShader and fragmentShader to the result of glCreateShader instead of hardcoding it to 0 and 1.

GLuint vertexShader;
GLuint fragmentShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
std::cout <<"b " << glGetError() <<std::endl;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
std::cout <<"c " << glGetError() <<std::endl;

The function glCreateShader returns 0 if it the call was not successful.

In your code you set vertexShader hardcoded to 0, and opengl reports an error for glShaderSource(vertexShader, ... because you pass 0 as id to glShaderSource.

The 1281 is an GL_INVALID_VALUE error and is reported if shader is not a value generated by OpenGL. glShaderSource: Errors

Beside that you (const char**)&VertexShaderSource is wrong to get the memory address where a std::string stores the data you have to use .c_str(): How to pass an std::string to glShaderSource?

Community
  • 1
  • 1
t.niese
  • 39,256
  • 9
  • 74
  • 101
0

You're passing the string wrongly, the correct way is VertexShaderSource.c_str().

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87