Reputation: 57162
I'm trying to draw a 5 pointed star shape in openGL (Java) that is both filled and outlined. The code i'm using to draw it
gl.glPushAttrib(GL.GL_ALL_ATTRIB_BITS);
try {
// set attributes for filling the shape
Color fillColor = Color.GREEN;
float[] rgb = fill.getRGBColorComponents(null);
gl.glColor4f(rgb[0], rgb[1], rgb[2], 1f);
gl.glEnable(GL.GL_POLYGON_SMOOTH);
gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL);
gl.glEnable(GL.GL_POLYGON_OFFSET_FILL);
gl.glCallList(this.glListId);
// set attributes for outlining the shape
gl.glEnable(GL.GL_LINE_SMOOTH);
Color outline = Color.RED;
float[] rgb = outline.getRGBColorComponents(null);
gl.glColor4f(rgb[0], rgb[1], rgb[2], 1f);
gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
gl.glLineWidth(5);
gl.glCallList(this.glListId);
}
finally {
gl.glPopAttrib();
}
The creation of the call list just looks like:
gl.glNewList(this.glListId, GL.GL_COMPILE);
gl.glBegin(GL.GL_POLYGON);
for (int j = 0; j < xCoords.length; j++) {
// 2d polygon
gl.glVertex2d(xCoords[j], yCoords[j]);
}
gl.glEnd();
When I do this, the outline of the star draws perfectly, but the fill color bleeds outside the edges of the outline (if I draw it without the outline, the polygon looks does not look crisp either). Any ideas why?
EDIT: I added a screenshot showing the problem
thanks, Jeff
Upvotes: 2
Views: 3068
Reputation: 52084
Depending on what you mean by "star shape", remember that GL_POLYGON
can only render convex polygons correctly:
GL_POLYGON
:Draws a single, convex polygon. Vertices 1 through N define this polygon.
Upvotes: 8