I am using pyopenGL in developing a FEM application
Why am I getting this result when rendering some quads
Values and colors are symmetric
enter image description here
Some ideas for obtaining better result
here some lines of the code:
Shaders:
GL_VERT_SHADER = '''
#version 330 core
layout (location = 0) in vec3 vertPos; // Input position
layout (location = 1) in vec4 vertCol; // Input color
smooth out vec4 color;
uniform mat4 finalMatrix; // The final matrix
void main() {
gl_Position = finalMatrix * vec4(vertPos.x, vertPos.y, vertPos.z, 1.0);
color = vertCol;
}
'''
GL_FRAG_SHADER = '''
#version 330
smooth in vec4 color;
void main() {
gl_FragColor = color;
}
'''
VAO and VBO
glBindVertexArray(self.vaomquad)
glBindBuffer(GL_ARRAY_BUFFER,self.vbomquad)
glBufferData(GL_ARRAY_BUFFER,data=vertquads,usage=GL_STATIC_DRAW,size=sizeof(vertquads))
glEnableVertexAttribArray(0)
glVertexAttribPointer(0,3,GL_FLOAT,False,28,None)
glEnableVertexAttribArray(1)
glVertexAttribPointer(1,4,GL_FLOAT,False,28,ctypes.c_void_p(12))
glBindBuffer(GL_ARRAY_BUFFER,0)
glBindVertexArray(0)
Drawing:
glBindVertexArray(self.vaomquad)
glDrawArrays(GL_QUADS,0,self.nquads*4)
glBindVertexArray(0)
For the colors interpolation I used this function:
def interpolatecolor(self,v,vmin,vmax):
anum = 1020/(vmax-vmin)
bnum = -anum*vmin
numcol = v*anum+bnum
r,g,b = 0,0,0
if (numcol>=0 and numcol<255):
r = 255
g = numcol
elif (numcol>=255 and numcol<510):
r = 255-(numcol-255)
g = 255
elif (numcol>=510 and numcol<765):
b = numcol-510
g = 255
elif (numcol>=765 and numcol<=1020):
g = 255-(numcol-765)
b = 255
return (r,g,b,1)
I resolve the problem by adding the barycenter of the quad so that I had to render a quad primitive as a group of four triangles (12 vertices per quad). Interpolation is now based on the four vertices of the quad.
enter image description here
Be aware that
GL_QUADS
aren’t supported in core profile 3.2 onwards – but your GL implementation is apparently allowing a compatibility profile. I don’t know how the triangles ‘dispatched’ to 3.3 core shaders will be split / interpolated from quad data.Thank you for the answer. I tried to render with triangles and using six vertices per primitive but I get the same result
glDrawArrays(GL_TRIANGLES,0,self.nquads*6)