Notices
Results 1 to 10 of 10

Thread: Why is my code for rotating what the camera views about the x-axis, not working?

  1. #1 Why is my code for rotating what the camera views about the x-axis, not working? 
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    i am required to use gluLookAt() in my excercise
    Notes
    const origin=0
    cam_positionx/y/z, are the coordinates of the camera in the x,y,and z axis, respectively.
    referencex/y/z, are the coordinates of the reference point of the camera, where its looking at.

    This is the function in myDisplay().
    Code:
    
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode (GL_MODELVIEW);
        glLoadIdentity ();
        gluLookAt (origin+cam_positionx, origin+cam_positiony, origin+cam_positionz, origin+referencex, origin+referencey, origin+referencez, 0.0, 1.0, 0.0);
    to rotate what the camera view about the x-axis, i decide to rotate the reference point about the camera by ____ degrees.
    This is the function i use to move the reference point to its proper position.
    It returns an array with size [2]. [0] is the position of the new y-axis, and [1] is the position of the new z-axis.
    Code:
    float* view_x (int angle, float cam_positiony, float cam_positionz){
        float returner[2];
        returner[0]=cam_positiony*cos(angle)-cam_positionz*sin(angle);
        returner[1]=cam_positiony*sin(angle)+cam_positionz*cos(angle);
        return returner;
    }
    This is how i call my function to get the new y and z coordinates of the new reference point.
    It is in the keyboard function, to check for if i input '2', it will move the reference point 5 degrees in the x-axis(not sure acw or cw, but it doesn't matter to me at this point), relative to the camera.
    Code:
    case '2':    referencey=view_x(5,referencey-cam_positiony,referencez-cam_positionz)[0]-cam_positiony;
        referencez=view_x(5,referencey-cam_positiony,referencez-cam_positionz)[1]-cam_positionz;
    break;
    This is in my main.
    Code:
        glutKeyboardFunc (Keyboard);
        init ();
        glutMainLoop ();



    I'm sorry if i didn't post my whole code here, but i tried my best to give as much information as i can think of now, my whole code is very long, with a lot of codes that didn't have to do anything with the rotation.

    Basically, i can't seem to spot any errors with my code, could you spot any errors for me?
    Or am i doing something wrongly?
    I understand using vector would be a little better for transformation in my view_x() function, but please kindly stick to my array[] method of transferring information.
    Help, i really hope someone will bother to answer me, please!
    I appreciate it deeply!
    std::cout<<"Thanks@!";
    haha


    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  2.  
     

  3. #2  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,538
    I assume this is OpenGL? It is many years since I did a tiny amount of GL coding so I can't help on the details of that.

    BTW it would be helpful to explain in what way your program "doesn't work" in questions like this: your attempt to rotate has no effect, has unexpected effects, crashes the program?

    But this looks a little suspicious:

    Code:
    float* view_x (int angle, float cam_positiony, float cam_positionz){
        float returner[2];
        returner[0]=cam_positiony*cos(angle)-cam_positionz*sin(angle);
        returner[1]=cam_positiony*sin(angle)+cam_positionz*cos(angle);
        return returner;
    }
    You are returning a pointer to the float array. But the array is a local variable, which means it is (probably) allocated on the functions call stack. It will not just go out of scope when the function returns but the memory is likely re-used. This makes the behaviour undefined.

    (When I was doing support, we used to get people asking "what does undefined mean, what will actually happen?" One of our standard replies was, "it will make dragons fly out of your nose".)


    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  4. #3  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    Thanks for answering so quickly!

    Quote Originally Posted by Strange View Post
    I assume this is OpenGL?
    Yes, im using OpenGL.
    Quote Originally Posted by Strange View Post
    BTW it would be helpful to explain in what way your program "doesn't work" in questions like this: your attempt to rotate has no effect, has unexpected effects, crashes the program?
    The way my program "doesn't work" is that at first the camera is normally oriented, then when i press '2' (the button '2' on my keyboard), the camera goes into some wierd orientation. looking at some awkward position.
    Quote Originally Posted by Strange View Post
    But this looks a little suspicious:

    Code:
    float* view_x (int angle, float cam_positiony, float cam_positionz){
        float returner[2];
        returner[0]=cam_positiony*cos(angle)-cam_positionz*sin(angle);
        returner[1]=cam_positiony*sin(angle)+cam_positionz*cos(angle);
        return returner;
    }
    You are returning a pointer to the float array. But the array is a local variable, which means it is (probably) allocated on the functions call stack. It will not just go out of scope when the function returns but the memory is likely re-used. This makes the behaviour undefined.
    im sorry, i dont get this part, about "But the array is a local variable, which means it is (probably) allocated on the functions call stack. It will not just go out of scope when the function returns but the memory is likely re-used. This makes the behaviour undefined."

    Quote Originally Posted by Strange View Post
    (When I was doing support, we used to get people asking "what does undefined mean, what will actually happen?" One of our standard replies was, "it will make dragons fly out of your nose".)
    haha thats a cool way to shug someone off with sarcasm!
    but i really dont understand what you mean by an undefined behaviour
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  5. #4  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    i tried replacing the
    view_x() function with two different functions,
    Code:
    float view_xy (int angle, float cam_positiony, float cam_positionz){
        float returner;
        returner=cam_positiony*cos(angle)-cam_positionz*sin(angle);
        return returner;
    }
    float view_xz(int angle, float cam_positiony, float cam_positionz){
        float returner;
            returner=cam_positiony*sin(angle)+cam_positionz*cos(angle);
            return returner;
    }
    which returns a single variable each, for the new y and z axes for the reference point, instead of a pointer to an array to hold the data.
    then i tested the code again. it still "doesn't work" the same way as before
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  6. #5  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,538
    Quote Originally Posted by RamenNoodles View Post
    im sorry, i dont get this part, about "But the array is a local variable, which means it is (probably) allocated on the functions call stack. It will not just go out of scope when the function returns but the memory is likely re-used. This makes the behaviour undefined."
    • Because the function returns a float* it is returning a pointer to the array (not copying the content of the array back to the caller).
    • But the array is a local variable in the function.
    • Local variables are allocated on the call stack for the function (this is one of the times when you do have to understand about where stuff is stored in memory).
    • I'm not sure if there is a standard for how local arrays are allocated memory, but because the array is small the compiler might create it on the stack.
    • When the function returns, the stack is reused by the next function you call, which could overwrite the content of the array with other data before you use it.



    But given your later post, maybe this isn't the problem (but it is still bad practice).

    haha thats a cool way to shug someone off with sarcasm!
    but i really dont understand what you mean by an undefined behaviour
    It's not really sarcasm. The point about undefined behaviour is that is not defined! Anything can happen or, more accurately, you can't rely on any specific behaviour.

    (In hardware design particularly, the bheviour might be well defined but we just don't want to publicise it - it might just be too much work, or we might want to change it in future.)
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  7. #6  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    can you pot any mistakes in my code? its really brain stressing for me
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  8. #7  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    i just found an error! (well, my problem isnt solved still)
    it shouldnt be
    Code:
    
    	case '2':
    		referencey=view_x(5,referencey-cam_positiony,referencez-cam_positionz)[0]-cam_positiony;
    		referencez=view_x(5,referencey-cam_positiony,referencez-cam_positionz)[1]-cam_positionz;
    		break;

    it should be(corrected)
    Code:
    
    	case '2':
    		referencey=view_x(5,referencey-cam_positiony,referencez-cam_positionz)[0]+cam_positiony;
    		referencez=view_x(5,referencey-cam_positiony,referencez-cam_positionz)[1]+cam_positionz;
    		break;

    still didnt work!! some other problem still existss!!
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  9. #8  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    16,538
    Just noticed you have 5 as the value for angle. Are you thinking 5 degrees? The sin and cos functions take an argument in radians...
    Without wishing to overstate my case, everything in the observable universe definitely has its origins in Northamptonshire -- Alan Moore
    Reply With Quote  
     

  10. #9  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    oh..... i didn't know that.

    i didn't expect that either..

    but, i put in radians form, and when i ran the program, and pressed '2', no movement/change in orientation happened


    referencez=view_x(0.0872664626,referencey-cam_positiony,referencez-cam_positionz)[1]+cam_positionz;
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  11. #10  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    i also changed the type of the angle input from INT to FLOAT.
    *i created a simple tiny cube located at the reference point for me to know where it is

    sort of solves the problem, thank you for your help.

    but a problem is that the reference point keeps getting closer to me....
    Last edited by RamenNoodles; November 1st, 2013 at 12:20 PM.
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

Similar Threads

  1. The Sun's Axis
    By MrMojo1 in forum Astronomy & Cosmology
    Replies: 4
    Last Post: May 18th, 2012, 06:25 PM
  2. Replies: 0
    Last Post: February 2nd, 2012, 09:14 AM
  3. The size of an axis
    By Ozolnyex in forum Physics
    Replies: 80
    Last Post: December 4th, 2011, 11:28 AM
  4. 0 Divided by 0: X and Y Axis
    By Eonos115 in forum Mathematics
    Replies: 3
    Last Post: December 3rd, 2011, 01:56 AM
  5. why earth rotates about its own axis?
    By sabarinath in forum Astronomy & Cosmology
    Replies: 6
    Last Post: November 22nd, 2008, 06:44 PM
Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •