Results 1 to 2 of 2

Thread: how do I pass color info to vertex shader?

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2011
    Posts
    1

    how do I pass color info to vertex shader?

    Ok, so I'm making fireworks and it works rather well.
    As a basis for this I am using sparks, which are rendered as follows:
    Code :
    void Fireworks::render()
    {
    	//set the size of the rendering area
    	glViewport(0, 0, width, height);
     
     
    	//modelview transform matrix
    	Matrix4 modelviewmatrix = Matrix4::lookAtMatrix(viewpoint,
    	                                                viewtarget,
    	                                                Vector3(0.0f, 0.0f, 1.0f));
    	//projection transform matrix
    	Matrix4 projectionmatrix = Matrix4::perspectiveMatrix((float)M_PI_4, (float)width / height, 1.0f, 10000.0f);
     
     
    	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    	//use the terrain shader
    	glUseProgram(terrainshader.getProgram());
    	//set our matrices as "uniform" variables
    	glUniformMatrix4fv(glGetUniformLocation(terrainshader.getProgram(), "matmodelview"), 1, GL_TRUE, modelviewmatrix.elements());
    	glUniformMatrix4fv(glGetUniformLocation(terrainshader.getProgram(), "matprojection"), 1, GL_TRUE, projectionmatrix.elements());
    	glBegin(GL_LINES);
    	spark* p = sparks;
    	while(p != NULL){
    	  for(int i = 0; i < 3; i++){
    	    glVertex3f(p->position.x(), p->position.y(), p->position.z());
    	    glVertex3f(20.0f*sin(rand())+p->position.x(), 20.0f*cos(rand())+p->position.y(), 20.0f*cos(rand())+p->position.z());
      	  }
     
     	  p = p->next;
    	}
     
     
    	glEnd();

    These go through the vertex shader which is

    Code :
    out vec3 _color;
     
    void main()
    {
    	// Get a copy of the normal vector
    	_color = ?
     
    	// Transform the vertex position to screencoordinates
    	gl_Position = matprojection * (matmodelview * in_position);
    }
    and the fragment shader which is

    Code :
    in vec3 _color; //normal vector
     
     
    void main()
    {
    	//severely simple lighting model
     	fragcolor = vec4(_color , 1.0);
    }

    now I think I got the passing from the vertex to the fragment shader right, but I want to know how I can pass the color information from my program to the vertex shader. I can't seem to find how to do that...

    I could post the rest of the code, but I don't think it'll matter (and it's probably bad anyway). I currently set fragcolor to a fixed color and it works.

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: how do I pass color info to vertex shader?

    Hi,
    From the snippet you have posted, it appears you want to assign the per-vertex normal as color. To do that, you would have to pass the per-vertex normals (glNormal3f) along with positions (glVertex3f). Assuming that the normal is passed in to vertex shader as in_normal, then you can simply assign it to the color output variable like this,
    Code :
    in vec3 in_normal;
    in vec4 in_position;
    out vec3 _color;
     
    uniform mat4 MVP; //combined modelview/projection matrix
     
    void main()
    {
       // Assign the normal vector
       _color = in_normal;
     
       // Transform the vertex position to clip space
       gl_Position = MVP * in_position;
    }
    See if this helps.
    Regards,
    Mobeen

Similar Threads

  1. How to pass the color data to vertex shader(GLSL)
    By sc20184 in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 06-13-2017, 02:54 PM
  2. Replies: 3
    Last Post: 05-03-2017, 08:37 AM
  3. Can't pass color to shader
    By redefrec in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 08-11-2014, 12:31 PM
  4. Replies: 5
    Last Post: 06-23-2013, 01:13 AM
  5. Cannot pass color value to shader vertex attribute
    By Phinehas in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 10-05-2010, 09:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean