Is it possible to set the color of a single vertex using a GLSL vertex shader program, in the same way that gl_Position changes the position of a vertex ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For versions of GLSL prior version 1.30, you want to write to the
gl_FrontColororgl_BackColorbuilt-ins, which are varyings accessible in the vertex shader. Read about varyings in the GLSL 1.10 specification (http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.10.59.pdf) to learn more about them, or the GL_ARB_vertex_shader extension specification.gl_FrontColorandgl_BackColorare 4D RGBA vectors which take normalized floating point scalars.But this will set all the vertices to red, not just one vertex. This is because the same vertex shader is run for all the vertices. If you want to set individual colours, use
glColorPointertogether withglDrawArrays,glDrawElements,glDrawRangeElementsorglMultiDrawElements. The vertex color set byglColorPointercan be read asgl_Colorin the vertex shader.gl_Colorin the vertex shader is a per-vertex attribute.To read the color you wrote in the vertex shader, in the fragment shader, read the built-in varying
gl_Color. Finished fragments should be written togl_FragColor.Vertex shader example:
Fragment shader example:
Also, to make the vertex shader set the varyings just like the OpenGL fixed-function pipeline, call the function ftransform().