I don't know how to make sphere in Python using OpenGl and pygame. Somebody can help me?
Asked
Active
Viewed 5,086 times
1 Answers
1
You can use sphere = gluNewQuadric()
and gluSphere(sphere, x, y, z)
when you apply it.
Remember to import OpenGL.GLU
I've asked a question at How create a camera on PyOpenGL that can do "perspective rotations" on mouse movements? and the example answer might help you.
Full code:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import math
pygame.init()
display = (400, 300)
scree = pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
glEnable(GL_DEPTH_TEST)
sphere = gluNewQuadric() #Create new sphere
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
gluLookAt(0, -8, 0, 0, 0, 0, 0, 0, 1)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
glLoadIdentity()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN:
run = False
keypress = pygame.key.get_pressed()
# init model view matrix
glLoadIdentity()
# init the view matrix
glPushMatrix()
glLoadIdentity()
# apply the movment
if keypress[pygame.K_w]:
glTranslatef(0,0,0.1)
if keypress[pygame.K_s]:
glTranslatef(0,0,-0.1)
if keypress[pygame.K_d]:
glTranslatef(-0.1,0,0)
if keypress[pygame.K_a]:
glTranslatef(0.1,0,0)
# multiply the current matrix by the get the new view matrix and store the final vie matrix
glMultMatrixf(viewMatrix)
viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)
# apply view matrix
glPopMatrix()
glMultMatrixf(viewMatrix)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) #Clear the screen
glPushMatrix()
glTranslatef(-1.5, 0, 0) #Move to the place
glColor4f(0.5, 0.2, 0.2, 1) #Put color
gluSphere(sphere, 1.0, 32, 16) #Draw sphere
glPopMatrix()
pygame.display.flip() #Update the screen
pygame.time.wait(10)
pygame.quit()

Noah
- 445
- 4
- 19
-
Another possibility id [`glutSolidSphere`](https://www.opengl.org/resources/libraries/glut/spec3/node81.html) - `from OpenGL.GLUT import *` – Rabbid76 Jun 18 '19 at 07:22
-
It's good, but can you give me a full code, because something doesn't work at my code? – MatthewV_xD Jun 18 '19 at 08:06
-
@Matei Tell us your full code if you have any other problems that cannot be solved. – Noah Jun 18 '19 at 10:06