2012-08-23 20 views
1

首先,我将给出opengl中出现问题的图像的屏幕截图。第四个表面图像是通过Matlab绘制的,它是Opengl应该看起来像的图像。使用顶点缓冲区对象在OpenGL中进行表面绘制时的不合需要的锯齿

the problem here

another angle

...

数据集的

Matlab的渲染: MATLAB rendering

(初3个图像从OpenGL的在不同角度有问题的锯齿图中,和第4之一是MATLAB绘制图像,其是正确的)

图像是一个1024 x 1024复杂矩阵。每个元素的想象部分是点的高度(在一个1024x1024高度图中),实部是点的颜色。

在matlab中,我们创建了一个小高斯形山。在OpenGL中,它使用碎布和锯齿渲染。 “粗糙”贯穿整个图像。而且,根据对象的视角,看起来有一个区域超出了一条线,不仅更加奇怪的锯齿版本发生,并且渲染的图形也会进行高度跳跃/更改。

这是什么原因造成的?为什么这种“粗糙”发生,这是什么?我们现在已经用完了所有的想法,并会提供任何帮助。 VBO代码的相关部分如下。我们基本上为顶点创建一个float4对象。结构中的第一,第二和第三个浮点数对应于该点的坐标。第4浮点数(被视为4个单字节数字)是RGBA颜色。

还注意到包含高度图和颜色信息的复杂矩阵存储在GPU中,因此代码中存在对CUDA的调用。当所有数据都被转储到一个文件中时,matlab成功绘制了地图,所以数据绝对正确。

#define BUFFER_OFFSET(i) ((char *)NULL + (i)) 

void initGL() 
{ 
... 
    glViewport(0, 0, window_width, window_height); 
    glEnable(GL_BLEND); 
glEnable(GL_COLOR_MATERIAL); 
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

// projection 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
gluPerspective(60.0, (GLfloat)window_width/(GLfloat) window_height, 0.1, 15.0); 
... 
} 


void display() 
{ 
camx += camx_v; 
camy += camy_v; 
camx_v=0; 
camy_v=0; 


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

// set view matrix 
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 


gluLookAt(0, 0, 1, /* look from camera XYZ */ 
      0, 0, 0, /* look at the origin */ 
      0, 1, 0); /* positive Y up vector */ 

drawGround(); 

glTranslatef(camx, camy, translate_z); 

glRotatef(rotate_x, 1.0, 0.0, 0.0); 
glRotatef(rotate_y, 0.0, 1.0, 0.0); 


glBindBuffer(GL_ARRAY_BUFFER, vbo); 
glEnableClientState(GL_VERTEX_ARRAY); 
glVertexPointer(3, GL_FLOAT, 16, BUFFER_OFFSET(0)); 
glEnableClientState(GL_COLOR_ARRAY); 
glColorPointer(4, GL_UNSIGNED_BYTE, 16, BUFFER_OFFSET(12)); 

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_i); 
glDrawElements(GL_TRIANGLES, (mesh_width-1) * (mesh_height-1) * 6, GL_UNSIGNED_INT, (GLvoid*)0); 


glDisableClientState(GL_VERTEX_ARRAY); 
glDisableClientState(GL_COLOR_ARRAY); 



glutSwapBuffers(); 

} 

void createVBO(GLuint* vbo, struct cudaGraphicsResource **vbo_res, 
     unsigned int vbo_res_flags) 
{ 

glGenBuffers(1, vbo); 
glBindBuffer(GL_ARRAY_BUFFER, *vbo); 

unsigned int size = mesh_width * mesh_height * 4 * sizeof(float); 
glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW); 

glBindBuffer(GL_ARRAY_BUFFER, 0); 

cutilSafeCall(cudaGraphicsGLRegisterBuffer(vbo_res, *vbo, vbo_res_flags)); 


} 


    void createIBO(GLuint* vbo, struct cudaGraphicsResource **vbo_res, 
     unsigned int vbo_res_flags, unsigned int numofindice) 
    { 

glGenBuffers(1, vbo); 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *vbo); 


unsigned int size = (mesh_width-1) * (mesh_height-1) * numofindice * sizeof(GLuint); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, 0, GL_STATIC_DRAW); 


glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 
cutilSafeCall(cudaGraphicsGLRegisterBuffer(vbo_res, *vbo, vbo_res_flags)); 

} 

    void main() 
{ 
initGL(); 
createVBO(&vbo, &cuda_vbo_resource, cudaGraphicsMapFlagsWriteDiscard); 
    createIBO(&vbo_i, &cuda_vbo_resource_i, cudaGraphicsMapFlagsWriteDiscard, 6); 
    glutMainLoop(); 
    } 

// KERNEL填充GPU中的INDEX BUFFER,在程序初始化时调用一次。

__global__ void fillIBO(unsigned int* pos_i, unsigned int M) 
{ 
    unsigned int x = blockIdx.x*blockDim.x + threadIdx.x; 
unsigned int y = blockIdx.y*blockDim.y + threadIdx.y; 

unsigned int bi; 

if(y<M-1 && x<M-1) 
{ 


    bi = ((M-1)*y +x)*6; 

    //TRI 
    pos_i[bi++] = x + y*M + 1; 
    pos_i[bi++] = x + y*M + M + 1; 
    pos_i[bi++] = x + y*M; 

    pos_i[bi++] = x + y*M; 
    pos_i[bi++] = x + y*M + M + 1; 
    pos_i[bi++] = x + y*M + M; 

}  
    } 
+0

尝试在一个非常小的网格,与IBO在CPU上运算。您将有调试器来查看值,并用纸和笔来检查它们。 – Calvin1602

回答

1

通过更换第二个三角形:

pos_i[bi++] = x + y*M + 1; 
pos_i[bi++] = x + y*M + M + 1; 
pos_i[bi++] = x + y*M + M; 

也,我敢肯定这应该是

bi = (M*y +x)*6; 
+0

我试过了,但问题仍然存在(锯齿和跳跃高度)。 –

相关问题