javaopengljogl

Multi-colored triangle strip with JOGL


I need to draw red and yellow triangles in a strip, and each triangle should have different color. This is my code so far:

Position drawPos = begPos.copy().offsetX(A);

gl.glBegin(GL.GL_TRIANGLE_STRIP);
gl.glColor3f(1.0f, 0.0f, 0.0f); // red
gl.glVertex3f(drawPos.getX(), drawPos.getY(), 0f);
drawPos.offsetX(-A);
gl.glVertex3f(drawPos.getX(), drawPos.getY(), 0f);
drawPos.offset2DAngle(-60f, A);
gl.glVertex3f(drawPos.getX(), drawPos.getY(), 0f);
gl.glColor3f(1.0f, 1.0f, 0.0f); // yellow
drawPos.offsetX(-A);
gl.glVertex3f(drawPos.getX(), drawPos.getY(), 0f);
gl.glColor3f(1.0f, 0.0f, 0.0f); // red
drawPos.offset2DAngle(-60f, A);
gl.glVertex3f(drawPos.getX(), drawPos.getY(), 0f);
gl.glColor3f(1.0f, 1.0f, 0.0f); // yellow
drawPos.offsetX(-A);
gl.glVertex3f(drawPos.getX(), drawPos.getY(), 0f);
gl.glEnd();

Im expecting to see two yellow and two red triangles with sharp edges and no gradients, but im getting this

triangle strip, but with gradient

I tried gl.glDisable(GL2.GL_COLOR_MATERIAL);, and gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2.GL_FILL);, and even

gl.glEnable(GL.GL_LINE_SMOOTH);
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

But it all doesnt work for me

Edit: Position class i just my little thing to reduce and simplify code in drawing methods


Solution

  • In a triangle strip always 2 vertices of 2 triangles are shared (see Triangle primitives). Thus, the color assigned to these vertices is also shared. A vertex cannot have 2 different color attributes. Either draw 2 GL_TRIANGLES instead of one GL_TRIANGLE_STRIP or change the shading model to flat shading:

    gl.glShadeModel(GL.GL_FLAT);