Thread: Building a camera in C

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    18

    Building a camera in C

    Can someone take a look at what I have and these errors to tell me what I am doing wrong? Sorry its a lot but i cant figure out what im doing wrong. Any help would be greatly appreciated Im new to the whole programming thing. My .h files are at the bottom so you can see what is in the cam and what not.
    camera.c:45:38: error: expected ‘)’ before ‘pixel_dim’
    camera.c:45:51: error: expected ‘)’ before ‘(’ token
    Code:
    cam->pixmap=malloc(sizeof(irgb_t * (pixel_dim->x)( pixel_dim->y)));
    camera.c:96:58: error: request for member ‘view_point’ in something not a structure or union
    Code:
    else if(strcmp(attrib_name, "viewpoint")==0) {
                            count= fscanf(in,"%d %d %d", &cam.view_point.x, &cam.view_point.y,&cam.view_point.z);
    camera.c:155:5: error: request for member ‘x’ in something not a structure or union
    camera.c:165:1: error: incompatible type for argument 2 of ‘vec_diff’
    Code:
    void camera_getdir(camera_t *cam, int x, int y, vec_t *uvec) {        assert(cam->cookie == CAM_COOKIE);
    
    
    
    
            // convert pixel coordinates to world screen coordinates
    uvec.x=cam->world_dim[0] * x /(cam->pixel_dim[x]-1);
    uvec.y=cam->world_dim[1] * y /(cam->pixel_dim[y]-1);
    uvec.z=0.0;
    
    
    
    
    
    
    
    
            // compute vector from viewpoint to world screen coordinates
            //              don't forget about the vector functions from your vector.c file
    
    
    uvec=vec_diff(uvec,cam->view_point,uvec);
    camera.c:190:4: error: request for member ‘r’ in something not a structure or unioncamera.c:191:4: error: request for member ‘g’ in something not a structure or union
    camera.c:192:4: error: request for member ‘b’ in something not a structure or union
    camera.c:194:10: error: invalid operands to binary > (have ‘drgb_t’ and ‘int’)
    camera.c:196:7: error: incompatible types when assigning to type ‘drgb_t’ from type ‘int’
    camera.c:199:15: error: invalid operands to binary < (have ‘drgb_t’ and ‘int’)
    camera.c:201:7: error: incompatible types when assigning to type ‘drgb_t’ from type ‘int’
    Code:
    void camera_store_pixel(camera_t *cam, int x, int y, drgb_t *pix) {        assert(cam->cookie == CAM_COOKIE);
    double row=0.0;
    double col=0.0;
    int i=0;
    irgb_t ipix;
            // scale and clamp pix values to integer values; formula given in notes
    pix.r *255 +0.5;
    pix.g *255 +0.5;
    pix.b *255 +0.5;
    //if greater then 255 make it 255 if less the 0 make it 0
    if(pix[i]>255)
    {
    pix[i]=255;
    i++;
    }
    else if(pix[i]<0)
    {
    pix[i]=0;
    i++;
    }
    Here is my ray.h file and part of my vector one that is used in my program
    Code:
    typedef struct camera_type
    {
       int    cookie;
       char   name[NAME_LEN];
       int    pixel_dim[2];    /* Projection screen size in pixels */
       double world_dim[2];    /* Screen size in world coords      */
       vec_t  view_point;      /* Viewpt Loc in world coords       */
       irgb_t *pixmap;         /* Build image here                 */
    }  camera_t;
    Code:
    void vec_diff(
    vec_t *v1,   /* subtrahend */
    vec_t *v2,   /* minuend    */
    vec_t *v3)   /* result     */
    {
    v3->x = v2->x - v1->x;
    v3->y = v2->y - v1->y;
    v3->z = v2->z - v1->z;
    }
    Last edited by bgibs24; 02-08-2015 at 12:53 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I suppose the first lesson would be to compile more often, before you get swamped with errors you have no idea how to solve.

    > Any help would be greatly appreciated Im new to the whole programming thing
    So this isn't your code, but you just edited it, and promptly broke it?

    I suggest you step back and actually learn some things before starting to edit other people's code.

    Getting something to compile is only 10% of the work.
    Making it actually do what you want (and only what you want) at run time is where all the interesting stuff happens.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    18
    It actually is my code for a school assignment so dont jump to conclusions. Im not asking for someone to do my project. Just some good feedback would be appreciated because its something due soon and I would like to know how to make it work and what and why I am doing what I am doing wrong. Just because I am new doesnt mean I dont know how to do things. The main thing i need help with are my viewpoint and pix.r pix.g etc. Im not too sure how to point to the structure within the structures x,y,z location. Thats where my errors are deriving from I think
    Last edited by bgibs24; 02-08-2015 at 12:52 PM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Read about the sizeof() and how to use use it.
    http://en.cppreference.com/w/cpp/language/sizeof

    NOTE: (x)(y) does NOT multiply x times y.

    I try to never do math operations inside the sizeof parens.

    Tim S.
    Last edited by stahta01; 02-08-2015 at 07:40 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > because its something due soon
    Of course it is - but your poor time management isn't our problem.

    > and I would like to know how to make it work
    You learn the language properly.
    All you seem to be interested in is, here is some code, I screwed it up with some edits, please fix it so I can relax and get a good mark.

    > Just because I am new doesnt mean I dont know how to do things.
    Nonsense code otherwise not included.

    > Im not too sure how to point to the structure within the structures x,y,z location
    Well I'm sure there are plenty of other examples in the unmodified code which use these structures.
    Perhaps careful analysis of these will lead to enlightenment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Maybe we should be a little less aggressive to new members
    Even if that code isn't his, the fact that he broke it means he actually tried modifying it, and there's nothing wrong about using other peoples' code as reference. He didn't simply copy-pasted it at least. I'm not saying that you're overreacting, and I think you already know this Salem, but not everyone has the same level of analytical capabilities. This of course get easier with practice but as bgibs said, he's new to this game. So let's not treat everyone as guilty until proven innocent, ok?

    Now, talking to bgibs24, you should pay close attention to your compiler errors. Don't give up everytime an error occurs, try to fix it. Most of the time, the error will be either on the line it tells you or the one above that. Go through that code and, if you still don't understand well the mechanics of the language, try to picture in your head what it would do. NOT what you think it should do, but what it'll actually do. As a programmer, you're likely to see as many error lines as code lines in your carrier, so you should nail down these basic skills early on.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Nov 2014
    Posts
    18
    Thank you GReaper. Ive edited my program a bit and only am getting an error on this line of code. If you look at my first post pix is a pointer to a drgb_t . Is this not how you access the r g b components of this type?

    Code:
    pix.r * 255 +0.5;pix.g * 255 +0.5;
    pix.b * 255 +0.5;
    //if greater then 255 make it 255 if less the 0 make it 0
    if(pix.r>255)
    {
    pix.r=255;
    }
    else if(pix.r<0)
    {
    pix.r=0;
    }
    else
    {
    pix.r=pix.r;
    }//end if

  8. #8
    Registered User
    Join Date
    Nov 2014
    Posts
    18
    And Salem thanks for ALLLL THE GREAT help. Much appreciated major confidence boost as well. If you're going to be hostile why even reply? I came here for help not to be attacked.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One problem I see is that this has no net effect:
    Code:
    pix.r * 255 +0.5;pix.g * 255 +0.5;
    pix.b * 255 +0.5;
    You are just multiplying pix.r, pix.g and pix.b by 255 and adding 0.5, but the result is not stored anywhere. Perhaps you should write:
    Code:
    pix.r = pix.r * 255 + 0.5;
    pix.g = pix.g * 255 + 0.5;
    pix.b = pix.b * 255 + 0.5;
    Notice that I placed each statement on its own line for clarity.

    Next, this looks like it could work, but the comment just repeats the code, plus assigning pix.r to itself is rather pointless:
    Code:
    //if greater then 255 make it 255 if less the 0 make it 0
    if(pix.r>255)
    {
    pix.r=255;
    }
    else if(pix.r<0)
    {
    pix.r=0;
    }
    else
    {
    pix.r=pix.r;
    }//end if
    I suggest:
    Code:
    // force pix.r into the range [0, 255]:
    if (pix.r > 255)
    {
        pix.r = 255;
    }
    else if (pix.r < 0)
    {
        pix.r = 0;
    }
    Notice that I indented the code properly, so I could also remove the "end if" comment as the structure of the if/else chain became evident through indentation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by GReaper View Post
    Maybe we should be a little less aggressive to new members
    Even if that code isn't his, the fact that he broke it means he actually tried modifying it, and there's nothing wrong about using other peoples' code as reference. He didn't simply copy-pasted it at least. I'm not saying that you're overreacting, and I think you already know this Salem, but not everyone has the same level of analytical capabilities. This of course get easier with practice but as bgibs said, he's new to this game. So let's not treat everyone as guilty until proven innocent, ok?
    It was a while ago, but I remember these kind of people from my days of university studying.

    Most assignments are in groups of 2 and up, so they get by for several years by doing and learning nothing, but sooner or later they hit that brick wall where they actually have to do things themselves and then they are screwed.

    His edits looks like he has never seen C before, but from the example code that Salem found he should most probably have seen C or he is in the wrong class.

    And I can see that the OP's attitude can rub veterans the wrong way...

    The OP should read How To Ask Questions The Smart Way and Short, Self Contained, Correct Example and restate his question.
    Last edited by Jimmy; 02-09-2015 at 08:44 AM. Reason: Fixed tags.
    "A Professor of Computer Science gave a paper on how he uses Linux to teach his undergraduates about operating systems. Someone in the audience asked why use Linux rather than Plan 9?' and the professor answered:Plan 9 looks like it was written by experts; Linux looks like something my students could aspire to write'."

  11. #11
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by bgibs24 View Post
    And Salem thanks for ALLLL THE GREAT help. Much appreciated major confidence boost as well. If you're going to be hostile why even reply? I came here for help not to be attacked.
    See, that attitude is a great way to not get help. Salem is one of our most senior members who have been here for years helping people.
    "A Professor of Computer Science gave a paper on how he uses Linux to teach his undergraduates about operating systems. Someone in the audience asked why use Linux rather than Plan 9?' and the professor answered:Plan 9 looks like it was written by experts; Linux looks like something my students could aspire to write'."

  12. #12
    Registered User
    Join Date
    Nov 2014
    Posts
    18
    Quote Originally Posted by laserlight View Post
    One problem I see is that this has no net effect:
    Code:
    pix.r * 255 +0.5;pix.g * 255 +0.5;
    pix.b * 255 +0.5;
    You are just multiplying pix.r, pix.g and pix.b by 255 and adding 0.5, but the result is not stored anywhere. Perhaps you should write:
    Code:
    pix.r = pix.r * 255 + 0.5;
    pix.g = pix.g * 255 + 0.5;
    pix.b = pix.b * 255 + 0.5;
    Notice that I placed each statement on its own line for clarity.

    Next, this looks like it could work, but the comment just repeats the code, plus assigning pix.r to itself is rather pointless:
    Code:
    //if greater then 255 make it 255 if less the 0 make it 0
    if(pix.r>255)
    {
    pix.r=255;
    }
    else if(pix.r<0)
    {
    pix.r=0;
    }
    else
    {
    pix.r=pix.r;
    }//end if
    I suggest:
    Code:
    // force pix.r into the range [0, 255]:
    if (pix.r > 255)
    {
        pix.r = 255;
    }
    else if (pix.r < 0)
    {
        pix.r = 0;
    }
    Notice that I indented the code properly, so I could also remove the "end if" comment as the structure of the if/else chain became evident through indentation.
    Thank you very much , this helped . I did have them equal to something and on their own lines I guess I just messed up copying it over

  13. #13
    Registered User
    Join Date
    Nov 2014
    Posts
    18
    Jimmy , I didn't mean to reply so hostile but it was the fist thing I saw when I woke up so it kinda ticked me off . And as for your comment , I have seen C before in my 101 class last semester with my awful teacher . Everyone says 102 is the hardest class here because it's literally the biggest jump because you do almost nothing in 101 , I could be asking someone in class for help but I thought I would rather get an understanding to what I am doing wrong and that's why I came here .

  14. #14
    Registered User
    Join Date
    Nov 2014
    Posts
    18
    Quote Originally Posted by Salem View Post
    >
    > Im not too sure how to point to the structure within the structures x,y,z location
    Well I'm sure there are plenty of other examples in the unmodified code which use these structures.
    Perhaps careful analysis of these will lead to enlightenment.
    And I didnt modify code. For our assignment we were given the .h file and told to make about half a dozen functions from it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ip camera
    By sgh in forum C# Programming
    Replies: 3
    Last Post: 03-12-2009, 01:50 PM
  2. Following camera
    By pandu in forum Game Programming
    Replies: 5
    Last Post: 06-10-2008, 07:20 PM
  3. How to set up a top view camera?
    By salman86 in forum Game Programming
    Replies: 6
    Last Post: 08-08-2005, 07:31 PM
  4. Digital Camera -> Slo-Mo Camera??
    By Masa in forum Tech Board
    Replies: 6
    Last Post: 12-24-2003, 11:11 AM
  5. My New Digital Camera!!!
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 07-29-2003, 04:38 PM