PDA

View Full Version : Set vertex position and color at the same time



guihazan
09-10-2012, 12:55 PM
Hi,

I would like to set the vertex position and color at the same time.

My opengl2 codes are:



// http://www.songho.ca/opengl/gl_projectionmatrix.html
static char* vertexShaderCode = "attribute vec4 vPosition;"
"mat4 projectionMatrix = mat4( 2.0/480.0, 0.0, 0.0, -1.0,"
"0.0, -2.0/800.0, 0.0, 1.0,"
"0.0, 0.0, -1.0, 0.0,"
"0.0, 0.0, 0.0, 1.0);"
"void main() {gl_Position = vPosition*projectionMatrix;}";

static char* fragmentShaderCode =
"precision mediump float;"
"uniform vec4 vColor;"
"void main() {gl_FragColor = vColor;}";

...

gColorHandle = glGetUniformLocation(gProgram, "vColor");
gPositionHandle = glGetAttribLocation(gProgram, "vPosition");
glEnableVertexAttribArray(gPositionHandle);

...
Then i use these two functions to set the color and the position:

void glSetColorA(GLfloat* color, int32 rgb, int32 a)
{
PixelConv pc;
pc.pixel = rgb;
color[0] = (GLfloat)pc.r / (GLfloat)255;
color[1] = (GLfloat)pc.g / (GLfloat)255;
color[2] = (GLfloat)pc.b / (GLfloat)255;
color[3] = (GLfloat)a / (GLfloat)255;
glUniform4fv(gColorHandle, 1, color); // Set color for drawing the line
}

void glDrawPixel(GLfloat* coords, int32 x, int32 y)
{
coords[0] = x;
coords[1] = y;
coords[2] = 0;

glVertexAttribPointer(gPositionHandle, COORDS_PER_VERTEX, GL_FLOAT, GL_FALSE, COORDS_PER_VERTEX * sizeof(float), coords); // Prepare the triangle coordinate data
glDrawArrays(GL_POINTS, 0,1);
}


However, i plan to set a bunch of pixels, like 100 at a time, and i belive that if i could set both the color and the position in the same instruction, it would be faster.

What has to be changed? I'm an opengl newbie.

thanks

guich

Kopelrativ
09-10-2012, 01:32 PM
There is a reference implementation, see https://github.com/progschj/OpenGL-Examples/blob/master/01shader_vbo1.cpp
It is a complete single file minimal OpenGL program that draws with a color.