我使用OpenGL导入.obj和.stl文件,并尝试为每个脸部着色。作为我可以为对象着色的概念证明,我按照导入的顺序为每个顶点分配了一个整数(第一个顶点为1,第二个顶点为2,第三个顶点为3等)接下来,我创建了一个将顶点索引映射到RGB值的函数。当我绘制所有面部时,面部只有几种不同的颜色 - 并且没有颜色过渡。换句话说,我看到的颜色没有平滑过渡 - 相反,有一些定义的颜色,其间有相当明确的线条。
这给了我一个预感,OpenGL可能没有显示我想要的颜色,所以我在我的createVertexArray
方法中将所有面的所有顶点都设置为橙色 - 初始化glBufferData
。输出显示模型为黄色而不是橙色。可能导致这种情况的原因是什么?
RGB 255,162,0是橙色
createVertexArray:
void createVertexArray(std::vector<triangle> & facet, std::map<vec3d, GLfloat> & vecMagMap, float* vertices, std::vector<GLfloat> mags, GLfloat j_min, GLfloat j_max)
{
const int NUM_FACETS = facet.size();
std::cout << "\nNUMFACETS: " << NUM_FACETS << "\n";
int count = 0;
hsv temp;
rgb tempRGB;
int tempH = 0;
for (int facet_index = 0; facet_index < NUM_FACETS; facet_index++){
tempRGB = getColor(vecMagMap[facet[facet_index].point[0]], 0, vecMagMap.size());
temp = rgb2hsv(tempRGB);
//if (tempH != temp.h){ std::cout << tempH << "\n"; tempH = temp.h; }
vertices[count + 0] = facet[facet_index].point[0].x;
vertices[count + 1] = facet[facet_index].point[0].y;
vertices[count + 2] = facet[facet_index].point[0].z;
vertices[count + 3] = facet[facet_index].normal.x;
vertices[count + 4] = facet[facet_index].normal.y;
vertices[count + 5] = facet[facet_index].normal.z;
vertices[count + 6] = 255;
vertices[count + 7] = 162;
vertices[count + 8] = 0;
vertices[count + 9] = 1.0;
vertices[count + 10] = facet[facet_index].point[1].x;
vertices[count + 11] = facet[facet_index].point[1].y;
vertices[count + 12] = facet[facet_index].point[1].z;
vertices[count + 13] = facet[facet_index].normal.x;
vertices[count + 14] = facet[facet_index].normal.y;
vertices[count + 15] = facet[facet_index].normal.z;
vertices[count + 16] = 255;
vertices[count + 17] = 162;
vertices[count + 18] = 0;
vertices[count + 19] = 1.0;
vertices[count + 20] = facet[facet_index].point[2].x;
vertices[count + 21] = facet[facet_index].point[2].y;
vertices[count + 22] = facet[facet_index].point[2].z;
vertices[count + 23] = facet[facet_index].normal.x;
vertices[count + 24] = facet[facet_index].normal.y;
vertices[count + 25] = facet[facet_index].normal.z;
vertices[count + 26] = 255;
vertices[count + 27] = 162;
vertices[count + 28] = 0;
vertices[count + 29] = 1.0;
count += 30;
}
}
我做绘图的主要部分:
glGenBuffers(1, &bufferID);
glBindBuffer(GL_ARRAY_BUFFER, bufferID);
glBufferData(GL_ARRAY_BUFFER,facet.size()*30*sizeof(GLfloat),vertices,GL_STATIC_DRAW);
glPolygonMode(GL_FRONT, // options: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK
GL_FILL); // options: GL_POINT, GL_LINE, GL_FILL (default)
glShadeModel(GL_SMOOTH); // shading model
scale = 5.0*scale_0; // initial scale
delta = 0.010*scale_0; // change in scale
int frame = 0;
while(running){
drawGeometry(argv, window, scale, frame,vertices, 3*facet.size(),move_x, move_y, move_z,rotate_x, rotate_y, rotate_z);// render objects in the window
if(!pause){rotate_x += drot_x;rotate_y += drot_y;rotate_z += drot_z;}
glfwSwapBuffers(window);// swap front and back buffers
glfwPollEvents();// poll for and processs events
}
答案 0 :(得分:3)
OpenGL可能会在绘制之前将这些浮点颜色值限制在[0.0-1.0]范围内。所以RGB(255,162,0) - &gt; RGBF(1.0,1.0,0.0)== RGB(255,255,0),为黄色。
使用[0.0-1.0]范围内的浮点颜色或使用GLubyte
和GL_UNSIGNED_BYTE
数组作为(我假设)您type
的{{1}}参数打电话。