Graphical artifacts with OpenGL?
I'm not sure if its called a graphical artifact but see this: http://img835.imageshack.us/img835/9785/anomoly.png
You will notice 6 pixels on the black outline that are out of place.
My application allows zooming in and when I render I do this:
RENDER:
// Set an orthogonal projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(GetCameraX() - ((float)controls.MainGlFrame.Dimensions.x) * scene[current.currentScene].camera.ScaleFactor
/ 2, GetCameraX() + ((float)controls.MainGlFrame.Dimensions.x) * scene[current.currentScene].camera.ScaleFactor / 2,
GetCameraY() - ((float)controls.MainGlFrame.Dimensions.y) * scene[current.currentScene].camera.ScaleFactor / 2,
GetCameraY() + ((float)controls.MainGlFrame.Dimensions.y) * scene[current.currentScene].camera.ScaleFactor / 2,
-1.0f, 1.0f);
// Set the model matrix as the current matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
hdc = BeginPaint(controls.MainGlContext.mhWnd,&ps);
//start OGL code
Camera struct looks like this:
struct SceneCam {
double x;
double y;
double tempX;
double tempY;
double ScaleFactor;
};
I suspect this might be the issue since what I use to do is just set a top left coorinate system and GlTranslate all my geometry, but I thought doing it this way was more proper. The artifacts are random, and rarely happen but I still want to get rid of them.
Thanks
Render:
void CGlEngine::RenderShapes()
{
// Set an orthogonal projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(GetCameraX() - ((float)controls.MainGlFrame.Dimensions.x) * scene[current.currentScene].camera.ScaleFactor
/ 2, GetCameraX() + ((float)controls.MainGlFrame.Dimensions.x) * scene[current.currentScene].camera.ScaleFactor / 2,
GetCameraY() - ((float)controls.MainGlFrame.Dimensions.y) * scene[current.currentScene].camera.ScaleFactor / 2,
GetCameraY() + ((float)controls.MainGlFrame.Dimensions.y) * scene[current.currentScene].camera.ScaleFactor / 2,
-1.0f, 1.0f);
// Set the model matrix as the current matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
hdc = BeginPaint(controls.MainGlContext.mhWnd,&ps);
//start OGL code
glClear( GL_COLOR_BUFFER_BIT);
for(unsigned int currentlayer = 0; currentlayer < scene[current.currentScene].layer.size(); ++currentlayer)
{
for(unsigned int i = 0; i < scene[current.currentScene].layer[currentlayer].Shapes.size(); i++)
{
//render shape here
scene[current.currentScene].layer[currentlayer].Shapes[i].Render();
}
}
for(int i = 0; i < TempBuffers.size(); ++i)
{
if(scene[current.currentScene].layer[TempBuffers[i].layer].Shapes[TempBuffers[i].shape].Contour[0].DrawingPoints.size() > 1)
{
glColor3f(0,0,0);
glBindBufferARB(GL_ARRAY_BUFFER_ARB,controls.MainGlFrame.BufferVBO);
glBufferDataARB(GL_ARRAY_BUFFER_ARB,sizeof(GLdouble) * scene[current.currentScene].layer[TempBuffers[i].layer].Shapes[TempBuffers[i].shape].Contour[0].DrawingPoints.size() * 2
,(GLdouble*)(&scene[current.currentScene].layer[TempBuffers[i].layer].Shapes[TempBuffers[i].shape].Contour[0].DrawingPoints[0]),GL_STATIC_COPY);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB( GL_ARRAY_BUFFER_ARB, controls.MainGlFrame.BufferVBO);
glVertexPointer( 2, GL_DOUBLE, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
if(scene[current.currentScene].layer[TempBuffers[i].layer].Shapes[TempBuffers[i].shape].connected)
{
glDrawArrays(GL_LINE_LOOP, 0, scene[current.currentScene].layer[TempBuffers[i].layer].Shapes[TempBuffers[i].shape].Contour[0].DrawingPoints.size());
}
else
{
glDrawArrays(GL_LINE_STRIP, 0, scene[current.currentScene].layer[TempBuffers[i].layer].Shapes[TempBuffers[i].shape].Contour[0].DrawingPoints.size());
}
glColor3f(1,0.5,0.5);
//glDrawArrays(GL_POINTS, 0, layer[TempBuffers[i].layer].Shapes[TempBuffers[i].shape].Contour[0].DrawingPoints.size());
glDisableClientState(GL_VERTEX_ARRAY);
glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
}
}
/*
glColor3f(0,0,0);
glLineWidth(2);
*/
/*
开发者_如何学JAVA if (ObjectBuffer.Contour[0].UserPoints.size() > 0)
{
glBegin(GL_LINES);
for(unsigned int i = 0; i < ObjectBuffer.Contour[0].DrawingPoints.size() - 1; ++i)
{
glVertex2d(ObjectBuffer.Contour[0].UserPoints[i].UserPoint.x,ObjectBuffer.Contour[0].UserPoints[i].UserPoint.y);
glVertex2d(ObjectBuffer.Contour[0].UserPoints[i + 1].UserPoint.x,ObjectBuffer.Contour[0].UserPoints[i + 1].UserPoint.y);
}
glEnd();
}
*/
//end OGL code
glFlush();
Sleep(1);
SwapBuffers(hdc);
EndPaint(controls.MainGlContext.mhWnd,&ps);
}
Resize:
void CGlFrame::resize(HWND mainWindow,CGlContext *context, float rightRemainder)
{
RECT clientRect;
GetClientRect(mainWindow,&clientRect);
if(!hasstarted)
{
context->killOGL();
context->init(framehWnd,
context->format);
hasstarted = true;
//Generate a VBO
glGenBuffersARB(1,&BufferVBO);
}
SetWindowPos(framehWnd,NULL,toolWidth,tabHeight,
(static_cast<int>(((clientRect.right - clientRect.left) - toolWidth) - rightRemainder) ) + toolWidth,
(static_cast<int>((clientRect.bottom - clientRect.top) - tabHeight - paramHeight) ) ,NULL);
int width;
int height;
RECT newwin;
GetClientRect(framehWnd,&newwin);
width = newwin.right - newwin.left;
height = newwin.bottom - newwin.top;
Dimensions.x = width;
Dimensions.y = height;
glViewport(0, 0, width, height);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glEnable(GL_MULTISAMPLE_ARB);
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor( 1.0f, 1.0f, 1.0f, 0.0f );
glLineWidth(2);
glPointSize(5);
}
Shape render:
void CGlShape::Render()
{
if(!render)
{
return;
}
glColor4f(MainShapeColor.r,MainShapeColor.g,MainShapeColor.b,MainShapeColor.a);
if(Gradient.active)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,Gradient.TextureId);
}
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBufferARB( GL_ARRAY_BUFFER_ARB, ObjectVBOInt);
glVertexPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
glBindBufferARB( GL_ARRAY_BUFFER_ARB, TextureCoordsVBOInt);
glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
glDrawArrays(GL_TRIANGLES, 0, ObjectVBOCount);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
glDisable(GL_TEXTURE_2D);
for(int i = 0; i < Contour.size(); ++i)
{
glColor4f(Contour[i].Outline.OutlineColor.r,
Contour[i].Outline.OutlineColor.g,
Contour[i].Outline.OutlineColor.b,
Contour[i].Outline.OutlineColor.a);
glBindBufferARB( GL_ARRAY_BUFFER_ARB, Contour[i].Outline.OutlineVBO);
glVertexPointer( 2, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
glDrawArrays(GL_TRIANGLES, 0, Contour[i].Outline.OutlineSize);
glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
}
glDisableClientState(GL_VERTEX_ARRAY);
}
Edit
I managed to solve it by rendering the outlines twice with a gltranslation of 0.00001. This works but causes overdraw, I wish I had a better solution.
Given their placement (right where two triangles butt up against each other), I'd guess that it's a simple matter of floating point rounding so the two objects don't "hit" against each other exactly.
At a guess, drawing the black section as a couple of GL_TRIANGLE_STRIP
s instead of individual GL_TRIANGLES
would stand a decent chance of fixing the problem (also stands a decent chance of improving performance a bit since it reduces the number of vertices you need to give it).
精彩评论