0

I am trying to output a square, and am getting a rather distorted rhombus. Like so,

Not the sort of cube that was expected...

And though I can tell that this is in fact the cube I had intended, the cube is strangely distorted. In my own workings to create a simple 3D projection program, I found a similar problem when I lacked the offset of 2D points to the middle of the screen, however I know of no such way to inform OpenGL of this offset...

For anyone who may be wondering, my current camera object looks like [in python]:

class Camera:
    def __init__(self,x,y,z,fov=45,zNear=0.1,zFar=50):
        self.x,self.y,self.z = x,y,z
        self.pitch,self.yaw,self.roll = 0,0,0
        glMatrixMode(GL_PROJECTION)
        gluPerspective(fov, 1, zNear, zFar)
    def __goto__(self,x,y,z,pitch,yaw,roll):
        print "loc:",x,y,z
        print "ang:",pitch,yaw,roll

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        glRotatef(pitch,1,0,0)
        glRotatef(yaw,0,1,0)
        glRotatef(roll,0,0,1)
        glTranslatef(-x,-y,-z)
    def __flushloc__(self):
        self.__goto__(self.x,self.y,self.z,self.pitch,self.yaw,self.roll)

with a cube being rendered in the following manner:

class Cube:
    def __init__(self,x,y,z,width):
        self.vertices=[]
        for x in [x,x+width]:
            for y in [y,y+width]:
                for z in [z,z+width]:
                    self.vertices.append((x,y,z))
        self.faces = [
            [0,1,3,2],
            [4,5,7,6],
            [0,2,6,4],
            [1,3,7,5],
            [0,1,5,4],
            [2,3,7,6]]
    def __render__(self):
        glBegin(GL_QUADS)

        for face in self.faces:
            for vertex in face:
                glVertex3fv(self.vertices[vertex])

        glEnd()

Perhaps I should also mention that the given window is 400px by 400px, thence the aspect ratio is 1.

1 Answers1

0

Two things:

I suspect the "distortion" you're seeing is likely a normal effect of the perspective projection matrix; a square in 3D space can render as a weird rhombus shape in 2D when perspective is applied. It's hard to tell in this case what the expected output should be, because you haven't included the coordinates of the camera and the cube. However, if you used an orthogonal projection (via gluOrtho2D), my guess is that it would come out to be a (rotated, translated, and squashed) parallelogram.

Second, I think you're only seeing a single face of your cube displayed. The picture might make more sense if you could see the other visible faces of the cube, but your vertices list is getting trashed because you've reused the x,y,z variables in Cube.__init__.

If you fix this name collision, does your render get better? You might try renaming one set of x,y,z to something else, like this:

def __init__(self,x,y,z,width):
    self.vertices=[]
    for xv in [x,x+width]:
        for yv in [y,y+width]:
            for zv in [z,z+width]:
                self.vertices.append((xv,yv,zv))
wildwilhelm
  • 4,809
  • 1
  • 19
  • 24
  • Might I just say that it is my own stupidity that generally turns out to be the source of my problems. And as you just proved, I probably shouldn't be on stack overflow. There was in fact no distortion, simply an extremely peculiar set of vertices created by the overlap you pointed out. –  Dec 14 '16 at 16:49
  • Not stupid, just learning : ) Aliasing of variable names is easy enough to do in Python, so no worries! I think most linters wouldn't have caught that issue; putting it on SO is a quick way to get some eyeballs on the source and find your problem. – wildwilhelm Dec 14 '16 at 16:51