//Vertex Shader
#version 450 core
out vec3 vs_color;
layout(location=0) in vec3 position;
layout(location=1) in vec3 colors;
uniform float x_offset;
void main(void)
{
gl_Position = vec4(position,1.0);
gl_Position.x += x_offset;
//vs_color = colors;
vs_color = position;
}
//Fragment Shader
#version 450 core
out vec4 color;
in vec3 vs_color;
void main(void)
{
color = vec4(vs_color,1);
}
This only works if I use vs_color = colors in vertex shader, for any other value like: position(contains xyz coordinates of vertex) or vec3(0.1,0.1,0.1), it throws this exception at glDrawArrays():
Exception thrown at 0x048D1AD0 (nvoglv32.dll) in OpenGL Starting Out.exe:
0xC0000005: Access violation reading location 0x00000000.
Why does this happen, and how can I fix this? (I want to try to set position value as color value)
EDIT:
Also if I don't enable the second vertex attribute using glEnableVertexArrayAttrib(VAO,1) //VAO is my vertex array object
I am able to do what I want (pass position to fragment shader)
But if I enable it, I need to pass it to fragment shader and then output the color(if I don't do anything with it in the fragment shader it gives the same error)
Here is how the attributes are set up:
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesAndcolors), verticesAndcolors, GL_STATIC_DRAW);
glVertexAttribPointer(
glGetAttribLocation(rendering_program,"position"),
3,
GL_FLOAT,
GL_FALSE,
6*sizeof(float),
(void*)0
);
glVertexAttribPointer(
glGetAttribLocation(rendering_program,"colors"),
3,
GL_FLOAT,
GL_FALSE,
6*sizeof(float),
(void*)(3*sizeof(float))
);
glEnableVertexArrayAttrib(VAO, 0);
glEnableVertexArrayAttrib(VAO, 1);
Edit-2:
If I do:
gl_Position = vec4(colors,1.0);
vs_color = position;
it does not give the access violation and works,
I checked how I set up my vertex attributes and I am not able to get further than this.