5

The title is my question: How can I load an *.obj 3d model in Python and apply a texture to it. I managed to load a 3d model, view it and apply textures to simple blocks and walls, but it seems impossible to apply a texture to a 3d model. How can I do that? Is there a script to load a 3d model with a texture in Python.

I'm using PyOpenGL as platform and I'm not allowed to use pygame from my teacher.

Joost Verbraeken
  • 883
  • 2
  • 15
  • 30

1 Answers1

3

you might want to check out ASSIMP, it has python bindings: http://assimp.sourceforge.net

also, if you feel like writing your own .obj loader (it is a pretty simple format) check out this tutorial:

http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Load_OBJ

  • Using enters in a comment seems not possible, so please click this link: https://onedrive.live.com/redir?resid=8F6597268E454CF3!1385&authkey=!AA_rpoahhKLkxIM&ithint=file%2c.txt – Joost Verbraeken Jun 21 '14 at 10:12
  • you might be misreading the windows bitmap (.bmp) file... there is a header in there. try using stb_image.h to load the image to rule that out: https://github.com/nothings/stb/blob/master/stb_image.h –  Jun 22 '14 at 00:53
  • oops, sorry forgot you were using python. actually, now that i look at your codes you are using glGenTexures incorrectly... it's not the return value that refers to the texture object, but rather the second (out parameter) argument. i don't even know how that is compiling, glGenTextures should always take two arguments. –  Jun 22 '14 at 00:56
  • For example I use two models, each with one textures. glGenTextures(2, t) glBindTexture(GL_TEXTURE_2D, t[0]) gives an error, but also glGenTextures(1, t) glBindTexture(GL_TEXTURE_2D, t) gives an error. And although the docs agree with you all examples on internet use the return value. – Joost Verbraeken Jun 22 '14 at 12:17
  • hmm, you are right does this example help: http://stackoverflow.com/questions/5907613/render-a-textured-rectangle-with-pyopengl –  Jun 22 '14 at 14:53
  • 1
    Thanks racarate, that example helped me a great deal. Everything works now fine, but there is one thing that still is a bit strange: the camera is flipped. I mean, how can I flip my display horizontally? How can I draw things on the left side of my screen draw at the right side? – Joost Verbraeken Jun 24 '14 at 20:26
  • I used the wrong word, how can I mirror my screen horizontally? – Joost Verbraeken Jun 25 '14 at 12:27
  • i don't know offhand, when i've built projection matrices manually ive used a negative z-scale value to mirror objects... but i think you are working at a higher level of abstraction here –  Jun 26 '14 at 22:46
  • The negative z-scale didn't work, but After some experimenting using a negative x-scale before calling gluPerspective did work. Everything works now fine. – Joost Verbraeken Jun 27 '14 at 05:18