持有唯一值集的向量,以便使用 C++ 和 OpenGL 进行颜色选择



我是一个项目的一部分,我正在处理管道系统3D渲染的选择方法。 为了能够选择管道,我的研究表明颜色选择将是最好的方法(由于管道的数量,光线选择可能更困难(。

//define color for pipe
int lowc=0;
int highc=9;
float cB = (rand()%(highc-lowc+1)+lowc)/10.0;
float cG = (rand()%(highc-lowc+1)+lowc)/10.0;
float cR = (rand()%(highc-lowc+1)+lowc)/10.0;
//some way of confirming the complete color combination is unique.
 // Create and insert new pipe in a new branch..
 Pipe* new_p = new Pipe(new_n1, new_n2, d, wf,cB,cG,cR);
 ElementList* new_branch = new ElementList();
 new_branch->branch->Append(new_n1);
 new_branch->branch->Append(new_p);
 new_branch->branch->Append(new_n2); 

目前,我正在努力找出最有效的方法来检查定义的颜色是否已经存在。存储所有 1000 种当前颜色组合的矢量似乎太耗时,引用每种颜色组合的所有其他现有节点颜色值也是如此。是否有更好的解决方案来存储现有颜色的矢量(例如<0.2、0.6、0.4>、<0.8、0.1、0.1>等(并将其与任何其他现有矢量进行比较?

我可以给你一个选择物体三角形的例子。应将此示例更改为呈现管索引而不是面索引。

PickingModeBegin(); // see below
glBegin(GL_TRIANGLES);
for (FaceIndex fi = 0; fi < GetFacesSize(); fi++) {
    const Face & face = faces[fi];
    glColor3ub((fi >> 16) & 255, (fi >> 8) & 255, (fi & 255)); // color coded index
    glVertex3fv(vertices[face.a].GetPointer());
    glVertex3fv(vertices[face.b].GetPointer());
    glVertex3fv(vertices[face.c].GetPointer());
}
glEnd();
PickingModeEnd(); // see below
glReadBuffer(GL_BACK);                          
GLint viewportInfo[4];
glGetIntegerv(GL_VIEWPORT, viewportInfo);             
GLubyte pixel[3];
// read the color
glReadPixels(mousePosX, viewportInfo[3] - mousePosY, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);             
// extract face index
int selectedFaceIdx = (pixel[0] << 16) + (pixel[1] << 8) + pixel[2]; 

void PickingModeBegin(void) {
  glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  glCullFace(GL_BACK);
  glEnable(GL_CULL_FACE);

  // turn off everything
  glDisable(GL_BLEND);
  glDisable(GL_DITHER);
  glDisable(GL_FOG);
  glDisable(GL_LIGHTING);
  glDisable(GL_TEXTURE_1D);
  glDisable(GL_TEXTURE_2D);
  glDisable(GL_TEXTURE_3D);
  glShadeModel(GL_FLAT);
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LESS);
  glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
void PickingModeEnd(void) {
  glPopAttrib();
}
struct ColourValue
{
    int red, blue, green;
    ColourValue() : red(0), blue(0), green(0) {}
    ColourValue(int red, int blue, int green) : red(red), blue(blue), green(green) {}
    bool operator < (const ColourValue& other) const
    {
        // magic trick to sort in set
        return pair<int, pair<int, int>>(this->red, pair<int, int>(this->blue, this->green))
        < pair<int, pair<int, int>>(other.red, pair<int, int>(other.blue, other.green));
    }
};
set<ColourValue> all_colours;

最新更新