Advertisement

OpenGL with APIs

Started by November 22, 2020 03:30 AM
4 comments, last by David_Barel 3 years, 11 months ago

Hello,

I am working on a prototype for a game now that relies heavily on machine learning. I am developing it using OpenGL. I am not familiar with AI libraries for C++; therefore, I was thinking of using APIs to provide behaviours to my entities. The downside of this method is that I must rely on continues internet connection and take the round trip to the server into consideration.

I would love to hear suggestions about possible implementation.

Thank you.

PyQt5 allows to work with OpenGL. PyQt5 has a lot of helper classes to simplified using of OpenGL. I think you are familiar with Python because I see Python in your tags and machine learning are often in Python. Install PyQt5 using this command:

pip install PyQt5

Copy and run this code that draws a triangle: https://rextester.com/LWYAU14361

import sys
import numpy as np
from OpenGL import GL as gl
from PyQt5.QtWidgets import QOpenGLWidget, QApplication
from PyQt5.QtGui import (QOpenGLBuffer, QOpenGLShaderProgram,
    QOpenGLShader)
from PyQt5.QtCore import Qt

class OpenGLWidget(QOpenGLWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Triangle, PyQt5, OpenGL ES 2.0")
        self.resize(300, 300)
    def initializeGL(self):
        gl.glClearColor(0.5, 0.8, 0.7, 1.0)
        vertShaderSrc = """
            attribute vec3 aPosition;
            void main()
            {
                gl_Position = vec4(aPosition, 1.0);
            }
        """
        fragShaderSrc = """
            void main()
            {
                gl_FragColor = vec4(0.5, 0.2, 0.9, 1.0);
            }
        """
        program = QOpenGLShaderProgram(self)
        program.addShaderFromSourceCode(QOpenGLShader.Vertex, vertShaderSrc)
        program.addShaderFromSourceCode(QOpenGLShader.Fragment, fragShaderSrc)
        program.link()
        program.bind()
        vertPositions = np.array([
            -0.5, -0.5, 0.0,
            0.5, -0.5, 0.0,
            0.0, 0.5, 0.0], dtype=np.float32)
        self.vertPosBuffer = QOpenGLBuffer()
        self.vertPosBuffer.create()
        self.vertPosBuffer.bind()
        self.vertPosBuffer.allocate(vertPositions, len(vertPositions) * 4)
        program.bindAttributeLocation("aPosition", 0)
        program.setAttributeBuffer(0, gl.GL_FLOAT, 0, 3)
        program.enableAttributeArray(0)

    def paintGL(self):
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
        gl.glDrawArrays(gl.GL_TRIANGLES, 0, 3)

def main():
    QApplication.setAttribute(Qt.AA_UseDesktopOpenGL)
    a = QApplication(sys.argv)
    w = OpenGLWidget()
    w.show()
    sys.exit(a.exec_())

if __name__ == "__main__":
    main()

Advertisement

Please visit opencv.org. It's an “AI” library, a Computer Vision library. Works good.

What kind of AI are you looking for?

@8Observer8 Thanks a lot. I will try that.

@taby Thank you for your reply, Taby. I am building a game where the players drive along with “self-driving” cars. However, the purpose is not to mimic the behavior of real-life self driving cars, instead, I would like to mimic the behavior of human drivers. So the AI Cars need to learn how to drive and behave from humans (including irrational changes of lanes, honking the horns, getting distracted by interesting things etc. )

This topic is closed to new replies.

Advertisement