Skip to content

Instantly share code, notes, and snippets.

@dirumahrafif
Last active November 28, 2025 05:44
Show Gist options
  • Select an option

  • Save dirumahrafif/e3488af12e1d30ece5c3552631700ce5 to your computer and use it in GitHub Desktop.

Select an option

Save dirumahrafif/e3488af12e1d30ece5c3552631700ce5 to your computer and use it in GitHub Desktop.
Membuat Bentuk
from core.base import Base
from core.openGLUtils import OpenGLUtils
from core.attribute import Attribute
from OpenGL.GL import *
class Test(Base):
def initialize(self):
print("init...")
# position merujuk dari referensi associateVariable
vsCode = """
in vec3 position;
void main(){
gl_Position = vec4(position.x, position.y, position.z, 1.0);
}
"""
fsCode = """
out vec4 fragColor;
void main(){
fragColor = vec4(1.0, 1.0, 0.0, 1.0);
}
"""
self.programRef = OpenGLUtils.initializeProgram(vsCode, fsCode)
glLineWidth(4)
# membuat tempat menyimpan konfigurasi buffer
# vertex array object
vaoRef = glGenVertexArrays(1)
glBindVertexArray(vaoRef)
positionData = [
[ 0.8, 0.0, 0.0], [ 0.4, 0.6, 0.0],
[-0.4, 0.6, 0.0], [-0.8, 0.0,0.0],
[-0.4, -0.6, 0.0], [0.4,-0.6, 0.0]
]
self.vertexCount = len(positionData)
# Attribute mengupload data ke GPU
positionAttribute = Attribute("vec3", positionData)
# associateVariable Menghubungkan data di GPU dengan variabel `position` di Vertex Shader
positionAttribute.associateVariable(self.programRef, "position")
def update(self):
# mengaktifkan shader program
glUseProgram(self.programRef)
# perintah menggambar, bisa diisi
# GL_LINES : putus-putus
# GL_LINE_STRIP : garis terakhir terpisah
# GL_LINE_LOOP : bersambung
# GL_TRIANGLES : segitiga per tiga titik
# GL_TRIANGLE_FAN : segitiga menyebar
glDrawArrays(GL_LINE_LOOP, 0, self.vertexCount)
Test().run()
from OpenGL.GL import *
import numpy as np
class Attribute(object):
def __init__(self, dataType, data):
self.dataType = dataType
self.data = data
# membuat id buffer
self.bufferRef = glGenBuffers(1)
self.uploadData()
def uploadData(self):
# ubah list menjadi numpy array
# ubah ke bentuk 32 bit
data = np.array(self.data).astype(np.float32)
# aktifkan buffer
glBindBuffer(GL_ARRAY_BUFFER, self.bufferRef)
# kirim data ke GPU
# meratakan array menjadi 1 dimensi
# GL_STATIC_DRAW memberi informasi data tidak sering berubah
glBufferData(GL_ARRAY_BUFFER, data.ravel(), GL_STATIC_DRAW)
def associateVariable(self, programRef, variabelName):
# mencari lokasi variabel di shader berdasar namanya
variabelRef = glGetAttribLocation(programRef, variabelName)
if variabelRef == -1:
return
# mengaktifkan buffer
glBindBuffer(GL_ARRAY_BUFFER, self.bufferRef)
if self.dataType == 'int':
glVertexAttribPointer(variabelRef, 1, GL_INT, False, 0, None)
elif self.dataType == 'float':
glVertexAttribPointer(variabelRef, 1, GL_FLOAT, False, 0, None)
elif self.dataType == 'vec2':
glVertexAttribPointer(variabelRef, 2, GL_FLOAT, False, 0, None)
elif self.dataType == 'vec3':
glVertexAttribPointer(variabelRef, 3, GL_FLOAT, False, 0, None)
elif self.dataType == 'vec4':
glVertexAttribPointer(variabelRef, 4, GL_FLOAT, False, 0, None)
else:
raise Exception("Attribute "+variabelName+" has unknown type "+self.dataType)
# mengaktifkan tempat di GPU agar data bisa mengalir ke shader saat proses rendering
glEnableVertexAttribArray(variabelRef)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment