I want to change the color of the sinusoyda. enter image description here

For this, I made the loading of masterpieces (fragmentary and vertex) in the function where I draw the sine wave, and change the color in the fragment shader.

ShaderProgram shaderProgram; shaderProgram.loadShaders("basic.vert", "basic.frag"); shaderProgram.use(); shaderProgram.setUniform("vertColor", glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); // изменил цвет 

basic.vert

 #version 330 core layout (location = 0) in vec3 pos; void main() { gl_Position = vec4(pos.x, pos.y, pos.z, 1.0); }; 

basic.frag

 #version 330 core out vec4 frag_color; void main() { frag_color = vec4(0.35f, 0.96f, 0.3f, 1.0f); }; 

But for some reason it does not work.

  • So in fact you don’t have uniform ' vertColor in the shader. - HolyBlackCat
  • @HolyBlackCat did so shaderProgram.setUniform("frag_color", glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); but earned. How do i fix the shader ?? Just from the examples I have only this option: #version 330 core uniform vec4 vertColor; out vec4 frag_color; void main() {frag_color = vertColor;} uniform vec4 vertColor; out vec4 frag_color; void main() {frag_color = vertColor;} uniform vec4 vertColor; out vec4 frag_color; void main() {frag_color = vertColor;} I don’t understand what to write in the shader. - timob256
  • I apologize, but after this and the previous questions I have a feeling that you are studying GL by typing. setUniform("frag_color" will not work, because frag_color is not uniform. "I just have the following example from examples: ..." In theory, it should work. Did you try it? - HolyBlackCat
  • @HolyBlackCat I have two books Open GL 4. Shader Language. Recipebook and OpenGL ES 3.0 Developer Guide. in print. BUT they are very complex (in perception for me) and there are few examples in them. That version of the examples works, but I don’t know how to transfer it to mine - timob256
  • one
    "how to transfer it to mine I do not know" What exactly is the complexity? Replace the code in the basic.frag with the one given in the comments. Do shaderProgram.setUniform("vertColor", glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); . - HolyBlackCat

1 answer 1

Changed basic.frag . Thank you HolyBlackCat: 3

 #version 330 core uniform vec4 vertColor; out vec4 frag_color; void main() { if(vertColor == 0) frag_color = vec4(0.35f, 0.96f, 0.3f, 1.0f); else frag_color = vertColor; }; 

Or you can make a call

  shaderProgram.setUniform("vertColor", glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); 

enter image description here