   Colorpicking over VBO grid
  I'm having some problems with my VBO grid and giving each cell/node a unique color, and hoped anyone on here might share some ideas/insight.I have the grid set up using degenerated triangles in triangle strip mode:void Grid::buildIndices(unsigned int x, unsigned int y){// 0123// 4567// 8910c// n1314u// 16171819//convert the (x,y) to a single index value which represents the "current" index in the triangle strip//represented by "c" in the figure aboveunsigned int current_index = y*cellCountX+x;//find the vertex index in the grid directly under the current index//represented by "u" in the figure aboveunsigned int under_index = 0;if(y < cellCountY-1)under_index = (y+1)*cellCountX+x;elsereturn; //This is the last row, which has already been covered in the previous row with triangle strips in mindindices.push_back(current_index);indices.push_back(under_index);//degenerate triangle technique at end of each row, so that we only have one dip call for the entire grid//otherwise we'd need one triangle strip per row. We only want one triangle strip for the entire grid!if(x < cellCountX-1)return;if(y >= cellCountY-2)return;//find the next vertex index in the grid from the current index//represented by "n" in the figure above//We already know that the current index is the last in the current row and that it's not the last row in the grid,//so no need to make any if-checks here.unsigned int next_index = (y+1)*cellCountX;//Add an invisible degeneration here to bind with next row in triangle stripindices.push_back(under_index);indices.push_back(next_index);}My VBO code then reads in the data stored in my indices vector, in addition to other data like vertices, normals, texcoords and colors.const std::vector<float> &vertices = node->getVertexVector();const std::vector<float> &normals = node->getNormalVector();const std::vector<float> &colors = node->getColorVector();const std::vector<float> &texCoords = node->getTexCoordVector();unsigned int vboType = 0;if(node->isDynamic())vboType = VBO_DYNAMIC;elsevboType = VBO_STATIC;unsigned int *vboIds = new unsigned int[5];glGenBuffers(5, vboIds);vboNodeIds[node->getUID()] = vboIds;createVBO(VBO_INDICES, vboIds, &indices[0], sizeof(unsigned int)*indices.size(), GL_ELEMENT_ARRAY_BUFFER, VBO_STATIC); //The indices should always be staticcreateVBO(VBO_VERTICES, vboIds, &vertices[0], sizeof(float)*vertices.size(), GL_ARRAY_BUFFER, vboType);createVBO(VBO_NORMALS, vboIds, &normals[0], sizeof(float)*normals.size(), GL_ARRAY_BUFFER, vboType);createVBO(VBO_TEXCOORDS, vboIds, &colors[0], sizeof(float)*colors.size(), GL_ARRAY_BUFFER, vboType);createVBO(VBO_COLORS, vboIds, &texCoords[0], sizeof(float)*texCoords.size(), GL_ARRAY_BUFFER, vboType);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);glBindBuffer(GL_ARRAY_BUFFER, 0);}Each vertex has a unique color associated with it, starting at rgb(255,255,254), handled through this function:void Grid::updateColors(unsigned int &r, unsigned int &g, unsigned int &b){b--;if(b==0){b = 255;g--;if(g==0){g = 255;r--;if(r==0){//Quite impressive to manage ++16million cells in the grid!r = 255;}}}}Here's my color-picking code:glPushMatrix();{glMatrixMode(GL_MODELVIEW);glLoadIdentity();user->getCamera()->view();unsigned char pixel[3];GLint viewport[4];glGetIntegerv(GL_VIEWPORT, viewport);glPushAttrib(GL_ALL_ATTRIB_BITS);{// turn off texturing, lighting and fogglDisable(GL_TEXTURE_2D);glDisable(GL_FOG);glDisable(GL_LIGHTING);glDisable(GL_DITHER);battlefieldGrid->togglePickingMode(true);glPushMatrix();{battlefieldGrid->multParentsGlobalMatrix();battlefieldGrid->multMatrix();sceneManager->displayNode(battlefieldGrid);//battlefieldGrid->displayCells();}glPopMatrix();battlefieldGrid->togglePickingMode(false);// get color information from frame bufferglReadPixels(x, viewport[3] - y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);colorText->setText(cl_format("%1 %2 %3 %4", "COLOR:", pixel[0], pixel[1], pixel[2]));battlefieldGrid->selectCell(pixel[0], pixel[1], pixel[2]);}glPopAttrib();}glPopMatrix();}When I then want to display this for picking mode, I use glShadeModel(GL_FLAT), to prevent interpolation between vertex points per cell. This works to some degree, but the colors I get are not that which I had "hoped" for.So, is there another way to do color coding for cells in a grid? I know I could go about adding four vertices to each point in the grid and give each one a different color, but that sounds quite suboptimal to me, so I've been wanting a different solution. If no other solution is there but the one I've been pursuing, using GL_FLAT shading, then how is the final color per cell calculated internally? I couldn't quite figure that out from the colors I attach to my vertices.For code completeness, look at my repository here:Tower DefenseThanks to anyone who can offer their help and insight on this matter!   
  Ok, so I solved the picking by using gluUnProject. This is quite a bit slower than my previous method from what I can see, but at least it works :) So, if someone still has some tips on improvements, I'm eager to learn!Here's the code for completeness:void System::doPicking(float x, float y){GLdouble posX, posY, posZ;glMatrixMode(GL_MODELVIEW);glLoadIdentity();user->getCamera()->view();glGetIntegerv(GL_VIEWPORT, viewport);glGetDoublev(GL_MODELVIEW_MATRIX, modelview); glGetDoublev(GL_PROJECTION_MATRIX, projection);float z;glReadPixels((GLint)x, viewport[3] - (GLint)y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);GLint result = gluUnProject(x, (float)viewport[3] - y, z, modelview, projection, viewport, &posX, &posY, &posZ);battlefieldGrid->selectCell((float)posX,(float)posY,(float)posZ);};  Risking to ask something very stupid: Have you tried putting an appropriate texture on a single quad?You can set GL_MAG_FILTER to GL_NEAREST, that should disable interpolation between the texel colors.I'm still somewhat trying to grasp what you are intending to render there.   ;  I'me trying to render a terrain (z uniformaly 0.0 for now) where the system will track the mouse posistion over the terrain, so that the player can place towers. I wanted it to be grid-based placing of towers, so the tower will be placed inside the bounds of each cell in the grid.Here's an example using quads with 4 vertices per point in the grid starting at color 255,255,254 for the first quad.During my optimalization of the grid though, I have changed this to a VBO approach using triangle strip, so that I have a minial DIP calls. I also render a minimal of vertices for the grid, so I don't want to use 4 vertices per point to achieve the correct coloring. This causes interpolation, but doing GL_FLAT shading, I'm able to somewhat get a flat color, though it's not quite how I want it, because it's not displaying the same colors as in the screen above, and the coloring is not equal on the two triangles that makes up each quad/cell.</src>If I could somehow manualy calculate the colors at each vertex point, so that the GL_FLAT shaded color becomes the correct one for each quad, that would be the prefered approach the way I see it now. But I haven't found any information on how this could be done, and as it's a school project, I don't have time to experiment for many days to get it right :/   ;  Ah ok, now i get it :)Well the texture approach using GL_NEAREST as min and mag filters seems like the ideal approach to me. Although you may have to use FBOs if the terrain changes frequently.Otherwise you will have to render each quad using four individual vertices as you mentioned. It's not so bad. I render geometries using millions of triangles, I'm sure you can render your terrain with a bit of vertex overhead :DPut the geometry in a VBO (no IBO needed if vertices aren't shared) or a display list, although that may cause some loading overhead if the grid is very large.Hmm, you might also be able to put the up to 4 colors per vertex into multi-texture coordinates 0-3 and somehow tag the quads during rendering so that the correct color is used. It would require a shader to pick the correct color but it would work with just one vertex per, well, vertex. Perhaps render in four passes with different uniform setting?It might also work using geometry shaders. Process each quad and replace it with a new quad. The new quad will pretty much be the old quad with the exception of color, where the same vertex color is copied to all four out vertices (perhaps from the top left one or something). You could even go so far as to render the map as point data and convert each into a quad on the fly.I'm reasonably certain i could come up with more possible ways but I'm equally certain they'll get ever stranger. As i said, I'd stick with the texture approach or if needed the 4-vertices-per-quad approach ;)   ;  Thanks for your input IronFox, I'll look into these options you mentioned if I find that the glUnProject becomes too heavy.The first project here focuses on fixed-pipeline, so I'm trying to do everything without shaders. In the next project we'll add a shader pipeline to this project, which is why I'm really trying to keep it simple now. Thus, I might not go with another approach for picking until we're at the second project, where I can use shaders.   ;  Agh, small code mistake caused the wrong coloring :P I should've guessed something like this was the cause of the strange colors I was seeing! switched the texcoord array with the color array... strange I didn't get any opengl errors doing this, as texcoords has been set up with 2 coords in the buffer, and colors with 4... shouldn't that cause an out of bounds for colors accessing the texcoord array??createVBO(VBO_TEXCOORDS, vboIds, &colors[0], sizeof(float)*colors.size(), GL_ARRAY_BUFFER, vboType);createVBO(VBO_COLORS, vboIds, &texCoords[0], sizeof(float)*texCoords.size(), GL_ARRAY_BUFFER, vboType);