2012-06-04 19 views
0

我想改变一个C++项目,当我点击视图端口时,它正在绘制一些行。这个功能非常好,但是我想改变的是当我点击“UP”或“Down”键时,下一行的颜色会改变。目前,如果我点击这些键,所有行的颜色都会改变,包括旧的(已绘制)。OpenGL项目与drawPrimitive()

请给我一个做什么的想法。这里是一些代码:

void drawPrimitive() { 
Vertex *temp; 

// Set the primitive color 
glColor3fv(primitiveColor); 

// Set the point size in case we are drawing a point 
if (type == POINT) 
    glPointSize(pointSize); 

// Display results depending on the mode 
glBegin(mode); 
    for(temp = head; temp != NULL; temp = temp->np) 
    { 
     if (smoothShading) 
      glColor3f(temp->r, temp->g, temp->b); 

     glVertex2f(temp->x, temp->y); 
    } 
glEnd(); } 


void mouse(int button, int state, int x, int y) { 
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) 
{ 
    float pointX, pointY; 

    pointX = (float)x/window_width * world_width; 
    pointY = (float)(window_height - y)/window_height * world_height; 

    // Add a vertex to the list of vertices... 
    addVertex(&head, &tail, pointX, pointY, 0.0f, primitiveColor[0], primitiveColor[1], primitiveColor[2]); 

    // automatically calls the display function 
    glutPostRedisplay(); 
} 
else if(button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) 
{ 
    deleteVertex(&head, &tail); 
    glutPostRedisplay(); 
} } 


void special(int key, int x, int y) { 
switch (key) 
{ 
    // change primitive color 
    case GLUT_KEY_UP : 
     changePrimitiveColor(1); 
     break; 
    case GLUT_KEY_DOWN : 
     changePrimitiveColor(-1); 
     break; 
} 

glutPostRedisplay(); } 


void changePrimitiveColor(int step) { 
primitiveColorId += step; 

if (primitiveColorId < 0) 
    primitiveColorId = COLOR_COUNT - 1; 

if (primitiveColorId >= COLOR_COUNT) 
    primitiveColorId = 0; 

setColor(primitiveColor, primitiveColorId); } 

回答

0

你的代码有点不清楚;是一个全局变量primitiveColor?

假设您为每个重绘的所有行调用drawPrimitive(),则相同的primitiveColor将用于所有行。只要按上或下时改变颜色,就会调用重新显示功能,并且所有行将使用相同的颜色重新绘制。

你可能想要做的是有一个包含基元和它们各自颜色的列表。当你遍历这个列表时,你可以为每一行设置一个颜色。

+0

我发现了一个类似于此的解决方案。当调用drawPrimitive()修改特征线的颜色时,我改变了这一点。 –

0

请记住,OpenGL的行为像一个状态机。如果您设置了颜色,那么在绘制颜色后绘制的所有内容都将被绘制。 OpenGL不会记得你的其他元素有不同的颜色,你想保持它的样子。你必须做记账。

因此,每次绘制内容时,都必须明确说明哪些元素具有哪种颜色。