-1

Referring to the reading of OpenGL Superbible's SMB format file where the source code is, I created a program with some help. Now I am comparing the results to what I'm actually getting.

What I'm getting: enter image description here

What the result should look like: enter image description here

Any suggestions as to why the output is incorrect? I'm stumped at this point. Thanks.

Stan S.
  • 237
  • 7
  • 22

1 Answers1

1

The torus is clipped by the near plane of the projection.

Change the projection matrix:

proj_matrix = m3dPerspective(m3dDegToRad(60.0), float(self.width) / float(self.height), 0.1, 100.0);

and the view matrix:

mv_matrix = (GLfloat * 16)(*identityMatrix)
m3dRotationMatrix44(mv_matrix, currentTime * m3dDegToRad(45.0), 1.0, 0.0, 0.0)
m3dTranslateMatrix44(mv_matrix, 0.0, 0.0, -3.0)

If you want to concatenate different matrices, then you've to implement an matrix "multiplication":

def m3dMultiply(A, B):
    C = (GLfloat * 16)(*identityMatrix)
    for k in range(0, 4):
        for j in range(0, 4):
            C[k*4+j] = A[0*4+j] * B[k*4+0] + A[1*4+j] * B[k*4+1] + \
                       A[2*4+j] * B[k*4+2] + A[3*4+j] * B[k*4+3]
    return C

e.g. Concatenate a translation matrix and the rotation matrices around x and y axis:

T = (GLfloat * 16)(*identityMatrix)
m3dTranslateMatrix44(T, 0, 0, -4)

RX = (GLfloat * 16)(*identityMatrix)
m3dRotationMatrix44(RX, currentTime * m3dDegToRad(17.0), 1.0, 0.0, 0.0)

RY = (GLfloat * 16)(*identityMatrix)
m3dRotationMatrix44(RY, currentTime * m3dDegToRad(13.0), 0.0, 1.0, 0.0)

mv_matrix = m3dMultiply(T, m3dMultiply(RY, RX))


Note, if you want to use numpy.matmul instead of m3dMultiply, then you've to .reshape the 16 element array to a 2 dimensional 4x4 array. e.g.:

R = np.matmul(np.array(RX).reshape(4,4), np.array(RY).reshape(4,4))
mv_matrix = np.matmul(R, np.array(T).reshape(4,4))

Or numpy.matrix and the *-operator:

mv_matrix = np.matrix(RX).reshape(4,4) * np.matrix(RY).reshape(4,4) * np.matrix(T).reshape(4,4)

See also Python: How to get cube to spin and move in OpenGL Superbible example

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thank you so much! Any idea on how to get it to also twist around the y axis while doing the current horizontal flip? – Stan S. Jun 24 '19 at 15:39
  • Incredible! Thanks. The extended answer with the function m3dMultiply amazing. I'm surprised there not a numpy solution to matrix multiply a 4x4 of either the a.dot(b) or np.matmul(a, b) based on my attempts it would not go for whatever reason. – Stan S. Jun 24 '19 at 17:42
  • 1
    @StanS. Of course you can use *numpy*, but you've to `.reshape()` to 4x4. See the last part of the answer. – Rabbid76 Jun 24 '19 at 17:53
  • 1
    @StanS. Hint: Do `glutInitWindowSize(512, 512)` before `glutCreateWindow`. Change to `scene = Scene(512, 512)` and add `def reshape(self, width, height):` `self.width = width` `self.height = height` – Rabbid76 Jun 24 '19 at 18:00
  • Excellent! Having options with either your m3dMultiply or with numpy is awesome and I learn a lot seeing what numpy is actually doing when multiplying matrices. Thank You. Next I'll work on the pattern1.ktx loader of this project as a part 2 and again convert the code to python, seems ok for now. Any questions I'll be sure to ask. – Stan S. Jun 24 '19 at 18:03
  • 1
    @StanS. Thank you. You're welcome. By the way, [this](https://pastebin.com/Etefk02B) is the current example code, which use. – Rabbid76 Jun 24 '19 at 18:04
  • 1
    Fantastic results! I think this project could also benefit the community of programmers as well being that the format relatively new. TY – Stan S. Jun 24 '19 at 18:07