tags:

views:

428

answers:

6
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?

+2  A: 

You have no glBegin/glEnd.(not needed with solid sphere) Also, try glLoadIdentity before the glRotate as it sounds like the projection matrix isn't initialised. Talking of which, you probably need a gluPerspective as well.

Skizz

Skizz
glBegin/glEnd are not necessary when drawing with glutSolidSphere
Judge Maygarden
My OpenGL is a bit rusty - I've never used the glutSolidSphere function.
Skizz
I know from personal experience that it gets rusty quickly! -1 removed
Judge Maygarden
+2  A: 

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.

unwind
i've tried same result =[
A: 

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?

Mark Pim
+2  A: 

You're creating a solid sphere ... how are you supposed to tell whether it's rotated or not?

eduffy
i'm creating a group of spheres so they're suppose to rotate like a bunch of grapes..
Also, glutSolidSphere() draws an approximation of a sphere, to the precision specified in the 2nd and 4rd arguments. His is coarse enough that you should be able to see the vertices move around as it rotates.
Matt J
There is one rotation for all of the spheres. Therefore, it is a safe assumption that he wants to rotate the location of the spheres around the origin. That's pretty easy to see.
Judge Maygarden
+6  A: 

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.

Judge Maygarden
monjardin i tried your suggestion but the output is blank..
That may be because the rotation is now working and moving the objects outside of your view frustum. How is your viewport and projection matrix configured?
Judge Maygarden
can you please explain more? i'm kinda noob actually..
You should have calls to glViewport and also set the projection matrix with a call glMatrixMode(GL_PROJECTION) and then glFrustum or one of the higher level functions (e.g. gluLookAt)
Judge Maygarden
it is actually each loop will create a sphere i have 20k points there fore 20k loops..
80 was just experiment it should be less than 10 usually
Is this a static scene? Otherwise, you have to render your objects repeatedly with different transformations to achieve animation.
Judge Maygarden
how do i put it?i call the points from a .txt file, and the spheres was suppose to rotate in a group.. perhaps you can teach me a way to rotate the camera instead?
See gluPerspective (http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/glu/perspective.html)
Judge Maygarden
I personally didnt think those tutorials were very helpful. They don't explain anything.
Joe Philllips
Then buy this: http://www.amazon.com/OpenGL-SuperBible-Comprehensive-Tutorial-Reference/dp/0321498828/ref=cm_cr_pr_product_top
Judge Maygarden
A: 

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.

Ketan