HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Accelerate OpenGL 2D on Python3

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
python3accelerateopengl

Problem

I'm using OpenGL to draw about 20 circles. Each circle has 2 lines, ~10 segments, and all of them have different colors and lengths. Frames per Second are around 4.

How can I optimize this to run faster, with higher frame-rates? I am using Python 3 on Ubuntu.

```
class ScreenOpenGL(Screen):
def __init__(self,layers,layers_lock):
""" Инициализирует экран и запускает его, потом всю программу """
Screen.__init__(self,"OpenGL",layers,layers_lock)

self.window = 0
self.quad = None
self.keypress = []

print("Fuck")
# self.infoScreen = ScreenCursesInfo()
self.infoScreen = ScreenStandartInfo()
GLUT.glutInit(sys.argv)
GLUT.glutInitDisplayMode(GLUT.GLUT_RGBA | GLUT.GLUT_DOUBLE | GLUT.GLUT_ALPHA | GLUT.GLUT_DEPTH)
GLUT.glutInitWindowSize(640, 480)
GLUT.glutInitWindowPosition(400, 400)
self.window = GLUT.glutCreateWindow(b"Project Evoluo alpha")
GLUT.glutDisplayFunc(self._loop) # Функция, отвечающая за рисование
GLUT.glutIdleFunc(self._loop) # При простое перерисовывать
GLUT.glutReshapeFunc(self._resize) # изменяет размеры окна
GLUT.glutKeyboardFunc(self._keyPressed) # Обрабатывает нажатия
self._initGL(640, 480)
field_params(640, 480)
print("Fuck")

def run(self):
# для threading
GLUT.glutMainLoop()

def _initGL(self,Width,Height):
GL.glShadeModel(GL.GL_SMOOTH);
GL.glClearColor(0.0, 0.0, 0.0, 0.0) # This Will Clear The Background Color To Black
GL.glClearDepth(1.0) # Enables Clearing Of The Depth Buffer
GL.glDepthFunc(GL.GL_LESS) # The Type Of Depth Test To Do
GL.glHint(GL.GL_LINE_SMOOTH_HINT, GL.GL_NICEST)
GL.glEnable(GL.GL_BLEND); # Enable Blending
GL.glLineWidth(1.);
GL.glDisable(GL.GL_LINE_SMOOTH)
GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);

self.width = Width
self.height = Height
self.quad = GLU.gluNewQuadric()
GLU.gluQuadricNormals(self.quad, GLU.GLU_SMOOTH

Solution

Not really sure why this line is neccesary.

print("Fuck")


Unless "Fuck" is Russian for something, this should be removed.

In addition, using % for string formatting is deprecated. One should be using str.format instead. str.format supports normal, positional, and named parameters. Here's an example:

# str.format without positional or named parameters
print("{} {}".format("Hello", "world"))

# str.format with positional parameters
print("{1} {0}".format("world", "Hello"))

# str.format with named parameters
print("{word1} {word2}".format(word1="Hello", word2="world"))


I also noticed that you're mixing English and Russian comments. I'd recommend sticking to one language or the other.

In the class ScreenOpenGL you have a "private" method named _initGL. Is there any reason why the contents of this method can't just be part of __init__?

You also don't need parentheses around Boolean conditions. For example, (self.last_tick != tick) would become self.last_tick != tick.

Finally, I noticed that you're mixing a lot of naming styles. Variables and functions should be snake_case, and classes should be PascalCase. If a variable is a constant (un-changing value), it should be in UPPER_SNAKE_CASE.

Code Snippets

print("Fuck")
# str.format without positional or named parameters
print("{} {}".format("Hello", "world"))

# str.format with positional parameters
print("{1} {0}".format("world", "Hello"))

# str.format with named parameters
print("{word1} {word2}".format(word1="Hello", word2="world"))

Context

StackExchange Code Review Q#25138, answer score: 7

Revisions (0)

No revisions yet.