I guess a picture says it all.. Basically, I’m working on a GUI for an application that acts pretty much like a standard gnome2/windows OS. Currently there’s no text or anything, but that’s not an issue just yet. As you can see, the exit buttons all fail miserably to render (They are supposed to have Xs in them) and when I move the windows to the top of the screen, they too begin to massively distort (I would bet they distort when they are on the bottom too, it’s just not as pronounced). At first I thought the issue was that I was rendering things without any Z positioning, so they were having trouble rendering two things that occupy the same space, but when I removed the background I got the same results, so apparently that wasn’t the problem. So, is this a common problem and does anyone know what could possibly cause it?
Here’s how I render the objects (I’m using python by the way):
MainVertexData = numpy.array([self.x,self.y, self.x+self.w,self.y, self.x,self.y+self.h, self.x+self.w,self.y+self.h], numpy.float32) FullWindowVertices = numpy.array([0,1,2,3],numpy.ushort) self.MainVertexData = MakeBuffer(GL_ARRAY_BUFFER,MainVertexData,len(MainVertexData)*4) self.FullWindowVertices = MakeBuffer(GL_ELEMENT_ARRAY_BUFFER,FullWindowVertices,len(FullWindowVertices)*2) self.BaseProgram = compileProgram(compileShader(ReadFile("Shaders/Mainv.glsl"), GL_VERTEX_SHADER), compileShader(ReadFile("Shaders/Mainf.glsl"), GL_FRAGMENT_SHADER)) self.Tex = TexFromPNG("Themes/theme/texture.png")
That is run during the instantiation of each object and never run again. Moving objects update their “MainVertexData” (After deleting the old one), but that’s it. It uses these helper functions:
def TexFromPNG(filename): img = Image.open(filename) # .jpg, .bmp, etc. also work img = ImageOps.flip(img) img_data = numpy.array(list(img.getdata()), 'B') texture = glGenTextures(1) glPixelStorei(GL_UNPACK_ALIGNMENT,1) glBindTexture(GL_TEXTURE_2D, texture) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.size, img.size, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data) return texture def MakeBuffer(target, data, size): TempBuffer = glGenBuffers(1) glBindBuffer(target, TempBuffer) glBufferData(target, size, data, GL_STATIC_DRAW) return TempBuffer def ReadFile(filename): shaderfile = open(filename,'r') source = shaderfile.read() shaderfile.close() return source
The actual rendering code is as follows:
glViewport(0, 0, self.width, self.height) # glClearDepth(1)# glClearColor(0,0,0,0)# glClear(GL_COLOR_BUFFER_BIT)#|GL_DEPTH_BUFFER_BIT# glEnable(GL_TEXTURE_2D)#These lines only get done at the beginning of rendering #for each object, do this: glUseProgram(self.BaseProgram) pos = glGetAttribLocation(self.BaseProgram, "position") glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, self.Tex) glUniform1i(glGetUniformLocation(self.BaseProgram,"texture"), 0) glBindBuffer(GL_ARRAY_BUFFER,self.MainVertexData) glVertexAttribPointer(pos, 2, GL_FLOAT, GL_FALSE, 8, None) glEnableVertexAttribArray(pos) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,self.FullWindowVertices) glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, None) glDisableVertexAttribArray(pos) glDisable(GL_TEXTURE_2D)#Do this once after all the objects have been drawn glutSwapBuffers()#
So yeah, if you need anything else (shader codes etc) let me know. Thanks a lot.
(P.S. I’m almost certain there’s a better way to go about rendering than that, so any guidance in that regards would be much appreciated as well)