Skip to content

Commit

Permalink
fixed mesh loading
Browse files Browse the repository at this point in the history
  • Loading branch information
mackst committed Jan 17, 2016
1 parent 2da0641 commit ee5988e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
7 changes: 5 additions & 2 deletions pysrc/3.model_loading/1.model_rendered.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ def initializeGL(self):
self.__shaderProgram = shaders.compileProgram(vertexShader, fragmentShader)

modelPath = os.path.join(abPath, '..', '..', 'resources', 'objects', 'nanosuit', 'nanosuit.obj')
#modelPath = os.path.join(abPath, '..', '..', 'resources', 'objects', 'rock', 'rock.obj')
#modelPath = r'F:\coding\test\cube.obj'
#modelPath = r'F:\coding\test\plane.obj'
self.model = Model(modelPath)

# Draw in wireframe
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
#glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)

def resizeGL(self, w, h):
glViewport(0, 0, w, h)
Expand Down Expand Up @@ -99,7 +102,7 @@ def paintGL(self):
glUniformMatrix4fv(projLoc, 1, GL_FALSE, projection)

model = np.identity(4, np.float32)
model = glm.scale(model, 0.2, 0.2, 0.2)
#model = glm.scale(model, 0.2, 0.2, 0.2)
model = glm.translate(model, 0.0, -1.75, 0.0)
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model)

Expand Down
26 changes: 16 additions & 10 deletions pysrc/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ def textureFromFile(path, gamma=False):
im = Image.open(path)
glBindTexture(GL_TEXTURE_2D, textureID)
if path.lower().endswith('jpg'):
iformat = GL_SRGB
iformat = GL_RGB
if gamma:
iformat = GL_SRGB
pformat = GL_RGB
elif path.lower().endswith('png'):
iformat = GL_SRGB_ALPHA
iformat = GL_RGBA
if gamma:
iformat = GL_SRGB_ALPHA
pformat = GL_RGBA
else:
iformat = GL_RGB
pformat = GL_RGB
glTexImage2D(GL_TEXTURE_2D, 0, iformat, im.size[0], im.size[1], 0, pformat, GL_UNSIGNED_BYTE, im.tostring())
glTexImage2D(GL_TEXTURE_2D, 0, iformat, im.size[0], im.size[1], 0, pformat, GL_UNSIGNED_BYTE, im.tobytes())
glGenerateMipmap(GL_TEXTURE_2D)

# parameters
Expand Down Expand Up @@ -57,6 +61,7 @@ def __init__(self, asset, assetDir):
self.vao = None

self.__setupMesh()
#self.__loadTextures()

def draw(self, shader):
for texture in self.textures:
Expand All @@ -69,7 +74,8 @@ def draw(self, shader):
glBindTexture(GL_TEXTURE_2D, texture.id)

glBindVertexArray(self.vao)
glDrawElements(GL_TRIANGLES, len(self.asset.faces), GL_UNSIGNED_INT, None)
#glDrawElements(GL_TRIANGLES, len(self.asset.faces), GL_UNSIGNED_INT, None)
glDrawElements(GL_TRIANGLES, self.asset.faces.size, GL_UNSIGNED_INT, None)
glBindVertexArray(0)

for texture in self.textures:
Expand All @@ -85,31 +91,31 @@ def __setupMesh(self):
glBufferData(GL_ARRAY_BUFFER, self.asset.vertices.nbytes, self.asset.vertices, GL_STATIC_DRAW)

glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, self.asset.vertices.itemsize, None)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)

glBindBuffer(GL_ARRAY_BUFFER, nbo)
glBufferData(GL_ARRAY_BUFFER, self.asset.normals.nbytes, self.asset.normals, GL_STATIC_DRAW)

glEnableVertexAttribArray(1)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, self.asset.normals.itemsize, None)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, None)

glBindBuffer(GL_ARRAY_BUFFER, tcbo)
glBufferData(GL_ARRAY_BUFFER, self.asset.texturecoords.nbytes, self.asset.texturecoords, GL_STATIC_DRAW)

glEnableVertexAttribArray(2)
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, self.asset.texturecoords.itemsize, None)
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, None)

glBindBuffer(GL_ARRAY_BUFFER, tbo)
glBufferData(GL_ARRAY_BUFFER, self.asset.tangents.nbytes, self.asset.tangents, GL_STATIC_DRAW)

glEnableVertexAttribArray(3)
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, self.asset.tangents.itemsize, None)
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, None)

glBindBuffer(GL_ARRAY_BUFFER, bbo)
glBufferData(GL_ARRAY_BUFFER, self.asset.bitangents.nbytes, self.asset.bitangents, GL_STATIC_DRAW)

glEnableVertexAttribArray(1)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, self.asset.bitangents.itemsize, None)
glEnableVertexAttribArray(4)
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, None)

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, self.asset.faces.nbytes, self.asset.faces, GL_STATIC_DRAW)
Expand Down

0 comments on commit ee5988e

Please sign in to comment.