OpenGL wont render a square properly

Viewed 88

I am trying to render a colored square in OpenGL using glBegin(GL_QUADS) but for some reason it renders the square with an extra vertex in the middle. Screenshot. Am I doing something wrong or do I have to use a different method for 2d rendering?

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

v = ((0, 0), (0, 1), (1, 0), (1, 1))
s = (0, 1, 2, 3)


def c():
    glBegin(GL_QUADS)
    for vertex in s:
        glColor3fv((1, 1, 1))
        glVertex2fv(v[vertex])
    glEnd()


def main():
    pygame.init()
    display = (1500, 1000)
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
    gluPerspective(50, (display[0] / display[1]), 0.1, 5000)
    glTranslatef(0, 0, -8.5)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        c()
        pygame.display.flip()


main()
1 Answers

The order of the indices is wrong:

v = ((0, 0), (0, 1), (1, 0), (1, 1))

s = (0, 1, 2, 3)

s = (0, 1, 3, 2)

You need to order the indices around the quad:

2          3
 +<-------+
 .        ^
 .        |
 .        |
 +------->+
0          1

Alternatively you can change the order of the vertices:

v = ((0, 0), (0, 1), (1, 1), (1, 0))
s = (0, 1, 2, 3)
Related