I'm importing .obj and .stl files using OpenGL and attempting to color each face. As a proof of concept that I can color the object, I assigned each vertex an integer in the order it was imported (1 for the first vertex, 2 for the second vertex, 3 for the third and etc.) Next, I created a function that maps the index of the vertex to an RGB value. When I drew all of the faces, the faces only had a few different colors - and there were no color transitions. In other words, the colors I saw did not smoothly transition - rather, there were a few defined colors with fairly defined lines in between.
That gave me a hunch that OpenGL might not be displaying the colors how I want them, so I set all of the vertices of all the faces to orange in my createVertexArray
method - which initializes the glBufferData
. The output showed the model yellow instead of orange. What could be causing this?
RGB 255,162,0 is orange
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;
}
}
part of main where I do the drawing:
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
}
OpenGL is probably clamping those floating-point color values down to the [0.0-1.0] range before drawing them. So RGB(255, 162, 0) -> RGBF(1.0, 1.0, 0.0) == RGB(255, 255, 0), which is yellow.
Use floating-point colors in the [0.0-1.0] range or use an array of GLubyte
and GL_UNSIGNED_BYTE
for the type
parameter of (I assume) your glColorPointer()
call.