I have a 3D terrain, and I edit it with a rectangular brush defined by a position, width, and length. I’m trying to get this brush to show up as a green rectangle under the mouse. I do this by creating two std::vectors containing all the X coordinates and Z coordinates of the terrain covered by the brush, then every time the brush moves I recalculate all of the terrain vertices’ positions and colors, and if they fall within the brush I set their colors to green, and if not then to clear. Then I upload all the terrain data to the GPU with glBufferData.
It works okay, but the trouble is that it’s really inefficient since I’m sending data about every single vertex to the GPU every time the mouse’s position changes even by one pixel, which could be multiple times a second. As a result, other code is not being run unless I hold the mouse motionless.
This is the code for changing a vertex’s color:
// Check if this vertex's X and Z coordinates are in the lists of X and Z coordinates within the brush:
if ((std::find(brushXCoords.begin(), brushXCoords.end(), (float)xCoor) != brushXCoords.end()) && (std::find(brushZCoords.begin(), brushZCoords.end(), (float)zCoor) != brushZCoords.end())){
this->backgroundSolidVertices[iterBase].RGBAdata = glm::vec4(0.0f, 1.0f, 0.0f, 0.7f);
}
else {
this->backgroundSolidVertices[iterBase].RGBAdata = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f);
}
This code executes every time the mouse moves, for every single vertex. brushXCoords and brushZCoords are std::vectors containing the xCoords and zCoords within the brush. backgroundSolidVertices is an std::vector of all the vertices.
What I hope to do is move this into the vertex shader. If I provide the shader with the brush’s current position and dimensions, I could then write code to determine whether a particular vertex is in the brush or not, and if so set its’ color to green before passing it to the fragment shader. The trouble is that brushXCoords and brushZCoords are std::vectors and I don’t know if GLSL has any data type that acts like an std::vector. I know it has arrays, but I can’t use those to hold brush coordinates since the dimensions aren’t guaranteed to be constant and the array might be overrun.
I’m using GLSL 1.3 (the most recent my machine can support) with OpenGL 3.1. Is there some way I could pull off std::vector-like functionality in this version of GLSL? Or is there some other way to compute this, without using std::vectors to hold the brush coordinates?