Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

No shape display pyopengl

So i’m trying to make a function for displaying a circle, here’s the function :

def drawCircle(x,y,size):
    glColor3f(1,1,1)
    glBegin(GL_TRIANGLE_STRIP)
    glVertex2f(0,0)
    glVertex2f(100,0)
    glVertex2f(100,100)
    glVertex2f(0,100)

But when i try it, nothing display. If you have the solution, please explain me in detail, I’m not a profesional in opengl or in pyopengl.

Here’s the code :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import glfw
from OpenGL.GL import *
from OpenGL.GLUT import *
import numpy as np
import pyrr
from random import *
from math import *

buffer = bytearray(800 * 600 * 3)
display = (800,600)

if not glfw.init():
    raise Exception("glfw can not be initialized!")

def CreateWindow(title="Baguette game",width=800,height=600):
    display = (width,height)
    window = glfw.create_window(width,height,title, None, None)
    glfw.set_window_pos(window,400,200)
    glfw.make_context_current(window)

    return window

def DrawTriangle(pointA=[-0.5, -0.5, 0.0],pointB=[0.5, -0.5,0.0],
                 pointC=[-0.5, 0, 0.0],color=[1.0,1.0,1.0]):
    vertices = [pointA[0], pointA[1], pointA[2],
                pointB[0], pointB[1], pointB[2],
                pointC[0], pointC[1], pointC[2]]

    colors = [color[0], color[1], color[2],
              color[0], color[1], color[2],
              color[0], color[1], color[2] ]
    
    v = np.array(vertices,dtype=np.float32)
    c = np.array(colors, dtype=np.float32)
    
    glEnableClientState(GL_VERTEX_ARRAY)
    glVertexPointer(3, GL_FLOAT,0,v)
    glEnableClientState(GL_COLOR_ARRAY)
    glColorPointer(3, GL_FLOAT,0,c)
    glDrawArrays(GL_TRIANGLES,0,3)

def DrawRectangle(size=[0.5,0.5],position=[0,0],color=[1.0,1.0,1.0]):
    DrawTriangle([position[0]-size[0]/2,position[1]+size[1]/2,0],
                  [position[0]+size[0]/2,position[1]+size[1]/2,0],
                  [position[0]+size[0]/2,position[1]-size[1]/2,0],
                  color)
    DrawTriangle([position[0]-size[0]/2,position[1]-size[1]/2,0],
                  [position[0]+size[0]/2,position[1]-size[1]/2,0],
                  [position[0]-size[0]/2,position[1]+size[1]/2,0],
                  color)

def Pixel(x,y):
    buffer_data = [randint(0,255), 0, 0] * (x * y)
    buffer = (GLubyte * (x * y * 3))(*buffer_data)
    glDrawPixels(x, y, GL_RGB, GL_UNSIGNED_BYTE, buffer)

def drawCircle(x,y,size):
    glColor3f(1,1,1)
    glBegin(GL_TRIANGLE_STRIP)
    glVertex2f(0,0)
    glVertex2f(100,0)
    glVertex2f(100,100)
    glVertex2f(0,100)
    
if __name__=="__main__":
    window = CreateWindow()
    initialPosition = (0,0,0)
    z=1
    while not glfw.window_should_close(window):
        glfw.poll_events()
        #glClear(GL_COLOR_BUFFER_BIT)

        DrawRectangle([1,1],[-0.5,0],[1,0,0])
        DrawRectangle([1,1],[0.5,0],[0,1,0])
        
        glfw.swap_buffers(window)
    glfw.terminate()

>Solution :

You don’t use the x, y and size arguments at all. A glBegin/glEnd sequence must be delimited with glEnd. e.g.:

def drawCircle(cx, cy, radius):
    glColor3f(1,1,1)
    glBegin(GL_TRIANGLE_FAN)
    glVertex2f(cx, cy)
    for a in range(361):
        x = cx + radius * cos(radians(a))
        y = cy + radius * sin(radians(a))
        glVertex2f(x, y)
    glEnd()

For example draw a circle in the center of the viewport:

drawCircle(0, 0, 0.5)

I recommend reading a good OpenGL tutorial. e.g.: LearnOpenGL

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading