wayne7773
wayne7773
2017-03-06 15:43

绘制有限元中的六面体单元,使用opengl,遮挡关系出现问题

5
  • opengl

void polygon(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d)
{
glBegin(GL_POLYGON);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glVertex3fv(d);
glEnd();
}

void cube(struct Data_Link d_p)
{
double *pNodxy = d_p.pNodxy;
int *pEle=d_p.pEle;
int Ne = d_p.Ne;int Nn = d_p.Nn;
int i,j,k=0;

GLfloat (*pN)[3] = (GLfloat(*)[3])malloc(3 * Nn * sizeof(GLfloat));//将节点坐标导入二维数组
GLint(*pE)[8] = (GLint(*)[8])malloc( 8 * Ne * sizeof(GLint));

for (i = 0;i < Nn;i++)
{
    for (j = 0;j < 3;j++)
    {
        pN[i][j] =pNodxy[k];
        k++;
    }
}
k = 0;
for (i = 0;i < Ne;i++)
{
    for (j = 0;j < 8;j++)
    {
        if (k % 9 != 0)
        {
            pE[i][j] = pEle[k];printf("%d\t", pEle[k]);
            k++;
        }
        else
        { 
            k++;
            j--;
        }           
    }       
}
for(i=0;i<Ne;i++)
{
    polygon(pN[pE[i][0]], pN[pE[i][3]], pN[pE[i][2]], pN[pE[i][1]]);
    polygon(pN[pE[i][2]], pN[pE[i][3]], pN[pE[i][7]], pN[pE[i][6]]);
    polygon(pN[pE[i][3]], pN[pE[i][0]], pN[pE[i][4]], pN[pE[i][7]]);
    polygon(pN[pE[i][1]], pN[pE[i][2]], pN[pE[i][6]], pN[pE[i][5]]);
    polygon(pN[pE[i][4]], pN[pE[i][5]], pN[pE[i][6]], pN[pE[i][7]]);
    polygon(pN[pE[i][5]], pN[pE[i][4]], pN[pE[i][0]], pN[pE[i][1]]);
}

}

void display()
{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1, 1, 1, 0, 0, 0, 0, 1, 0);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
cube(m_data_link);
glutSwapBuffers();
}

void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4, 4, -4, 4, -4, 4);
}

void init()
{
glClearColor(1, 1, 1, 1);
glClearDepth(1);
glColor3f(1, 0, 0);
glEnable(GL_DEPTH_TEST);
}

int main(int argc, char**argv)
{

struct Data_Link_KgFg m_KF;
FEM_Link_Read("FEM_TEST.txt", &m_data_link);
FEM_Link_Print(m_data_link);
getchar();

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("FEM_TEST");

glutReshapeFunc(reshape);
glutDisplayFunc(display);
init();
glutMainLoop();

}

这段代码时为了绘制有限元中的六面体单元,已经省略了数据读取的代码。各端点的坐标存储在pN[]数组中,经过一个六面体接一个六面体的循环绘制,已经实现了所有六面体的绘制,并且设置了背面不显示。且已开启深度缓存和深度测试,但是并没有起到作用,不知道是什么状况,求大神指点迷津!!!图片说明

运行后如上图所示

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐