1

How to move camera in OpenGL to capture 6 cube face images and save into files (like image below)?

enter image description here

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • You have to adjust your projection and modelview matrices. Would be helpful to know which environment you are using. I can give you an example using Qt and python if thats helpful. – Sebastian R. Dec 11 '19 at 14:14

2 Answers2

2

What does "plugin" means? I'm confused that you need how to calculate camera position&direction vectors for capture each side of dice or how to implement lookat&perspective functions. for lookat&perspective functions, there are many resources to refer :

Can't understand gluLookAt arguments

gluPerspective parameters- what do they mean?

these functions are usually provided on many libraries, but if you need, then I will post my implementation.

Camera position and direction/up vector is calculated for viewing each side of dice squarely. to do this, you have to care about perspective FOV(field of view) with respect to distance between camera and dice. If you read above posts carefully, you can determine arguments for these functions.

If once you see each side on screen, I think you need the method combining the result scene of each dice into one screen(or FBO) and save it. If once you obtain 6 sets of arguments for lookat and perspective, you can use glViewPort.

// if pixel per each side : 10
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

//back side draw
glViewPort(0, 10, 10, 10);
//call glulookat & gluperspective or equivalent functions with arguments so that back side of dice drew on your Viewport fully
gluLookAt(...);
gluPerpective(...);
glDrawElements(...);

//up side draw
glViewPort(10, 0, 10, 10);
gluLookAt(...);
gluPerpective(...);
glDrawElements(...);

//left side draw
glViewPort(10, 10, 10, 10);
gluLookAt(...);
gluPerpective(...);
glDrawElements(...);

...

The above code draw 6 times in each selected viewport of your result FBO.

H.U.N.
  • 95
  • 9
1

An example using PyQt5 for making an image of a plane with size X, Y in the z=0 plane.

Xt = X/2 #center of your camera in X
Yt = Y/2 #center of your camera in Y
dist = math.tan(math.radians(60))*Y/2 #Compute the distance of the campera from plane
 #assuming a 60 degree projection
aspect = float(self.width)/float(self.height) #aspect ratio of display
center = QtGui.QVector3D(Xt, Yt, 0) #look at this point
eye = QtGui.QVector3D(Xt, Yt, dist) #Point of Camera in space
up = QtGui.QVector3D(0, 1, 0)

self.modelview = QtGui.QMatrix4x4()
self.modelview.lookAt(eye,center,up) #build modelview matrix with given parameters

self.projection = QtGui.QMatrix4x4()
self.projection.perspective(60.0, aspect, dist*0.0001, dist*10000.0) #build projection matrix

repeating this process for each side + adjusting the z distance to your cube should yield the desired result. The you can just write your results to a framebuffer and read that buffer into an array.

Sebastian R.
  • 412
  • 4
  • 10
  • I'm workin on plugin. I already have model loaded by host application and initial camera orientation is done. All I can change is camera position, direction vector and up vector. I'm interested in exact algorithm if anyone did this before. – Aliaksei Plashchanski Dec 11 '19 at 19:02