Reputation:
void RenderBrain(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
ifstream myFile("brainData.txt");
if (!myFile.is_open())
{
cout << "Unable to open file";
exit(1); // terminate with error
}
glRotatef(80.0f, 1.0f, 0.0f, 0.0f);
while (! myFile.eof())
{
myFile>>a[0];
myFile>>a[1];
myFile>>a[2];
myFile>>a[3];
glColor3f(0.60f,0.80f,0.90f);
glLoadIdentity();
glTranslatef((a[0]-1.15)*26, a[2]*30, a[1]*30);
glutSolidSphere(6, 5, 5);
}
myFile.close();
glFlush();
glutSwapBuffers();
}
Above is part of my code, I have problem making it rotate after adding glLoadIdentity(); inside the loop. If I remove it it'll result that all my spheres to fly towards all directions.
Can anybody help?
Upvotes: 3
Views: 1750
Reputation: 71070
You have no (not needed with solid sphere) Also, try glBegin
/glEnd
.glLoadIdentity
before the glRotate
as it sounds like the projection matrix isn't initialised. Talking of which, you probably need a gluPerspective
as well.
Upvotes: 3
Reputation: 1015
Are you setting you ProjectionMatrix
correctly? (using glutOrtho
etc.)
As others have already pointed out, glLoadIdentity
makes your model-view-matrix identify and after that you are doing translate, so when you render sphere, it is only going to be translated. As for flying, you are doing transformations in loop and OpenGL is the state machine, so all your transforms are being applied one after another. i.e.
for (each point)
{
modelMatrix *= translate(newPt); // this is adding all transaltes..
}
Thus your starting points might show up correctly (probably only first) but subsequent would look like flying.. as they are getting translations of all previous points.
What you need to do is apply translations for each pt independently, i.e.
for (each pt)
{
push_matrix
glLoadIdentity
glRotate // matrix being applied is in reverse order of being called..
glTranslate // so translate and rotate
glutSphere
pop matrix
}
But above is not of help to you, as you want to move whole brain. So you should render your brain with translates as you do but apply rotation matrix to all objects.
Hope this clarifies.
Upvotes: 0
Reputation: 27583
I highly recommend working through the NeHe OpenGL Tutorials if OpenGL is new to you.
glLoadIdentity
is negating the effect of glRotate
. Instead, use glPushMatrix
/glPopMatrix
to achieve independent translations:
glPushMatrix();
glTranslatef((a[0]-1.15)*26, a[2]*30, a[1]*30);
glutSolidSphere(6, 5, 5);
glPopMatrix();
If the spheres are no longer visible, then you need to provide your viewport and projection matrix setup code so we can determine the problem. On the other hand, if you have not setup the viewport and projection at all then see the tutorial above and fix that!
Also, you are rotating the model-view matrix by 80 degrees every time you render the scene. Is that the intended result? How frequently is this function called? I would consider maintaining a desired rotation in a variable and then loading the identity matrix and rotating by that angle on each pass instead. For example:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
angle += angularVelocity * deltaTime;
glRotate(angle, 1, 0, 0);
It is also ambiguous whether you are calling this function only once and expecting the glRotate call to continuously update the scene (a very incorrect assumption), or are calling this function repeatedly to render the animated scene. If it is the former case, then please read the tutorials I linked to above. If it is the latter case, then you should consider performing the file I/O once and building a display list to be called in the render loop.
Upvotes: 8
Reputation: 40224
You're creating a solid sphere ... how are you supposed to tell whether it's rotated or not?
Upvotes: 2
Reputation: 10064
My OpenGL is rusty, but I think you want to do the rotation after loading the identity matrix.
glLoadIdentity();
glRotatef(80.0f, 1.0f, 0.0f, 0.0f);
glTranslatef((a[0]-1.15)*26, a[2]*30, a[1]*30);
glutSolidSphere(6, 5, 5);
But the best solution (if I remember correctly) is to push the matrix after the rotation and then pop it instead of glLoadIdentity()
every iteration of the loop but I'm less certain of that. Perhaps others know more about the OpenGL matrix stack?
Upvotes: 0
Reputation: 399813
You must remember that OpenGL's matrix stack is independent of your frames. The stack remains, so it must be either saved using push/pop, or completely reconstructed for each frame.
In your case, try moving the glRotate() call to after the glLoadIdentity(), to do a new rotation. Of course, you must then change the angle to achieve a change in rotation.
Upvotes: 2