Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Help needed with OpenGL

Options
  • 03-08-2011 10:58am
    #1
    Registered Users Posts: 121 ✭✭


    Im looking for some help with OpenGL for an exam I have. Im doing some sample questions to prepare but stuck with some parts!

    Code which draws a 2-dimensional star shape from two triangles...
    #include <gl/glut.h>
    void drawStar() {
    glBegin(GL_TRIANGLES);
    glVertex2f(-0.5, -0.5);
    glVertex2f(0.5, -0.5);
    glVertex2f(0.0, 0.5);
    glVertex2f(-0.5, 0.15);
    glVertex2f(0.5, 0.15);
    glVertex2f(0.0, -0.85);
    glEnd();
    }
    void display(void) {
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluOrtho2D(-1, 1, -1, 1);
    drawStar();
    glFlush ();
    }
    int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutCreateWindow ("Star");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
    }

    Question:
    a) Modify the display() function so that a 3-dimensional (perspective) projection is used rather than an orthographic projection

    (b) Modify the display() and drawStar() functions so that the star is drawn at a 3-dimensional location and colour as specified in its parameter list:
    drawStar(float x, float y, float z, float r, float g, float b)

    (c) Extend the program so that a 'starfield' animation is displayed using the Idle Callback approach. Multiple stars should be stored in an array, and initialised to random x, y and z co-ordinates. They should move towards the camera along the z axis, and when they pass the camera they should be moved back to the far distance again.

    Part C is the bit Im stuck with so hopefully someone might be able to help. Thanks in advance smile.gif


Comments

  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Part (c) builds on (a) and (b) so please post your answers for those.

    The "idle function" it refers to is basically a function that runs between frames, you put your update code here and move each star a little bit every frame. In a proper application you would use a high resolution timer to make animation independent from framerate but that's overkill for this.

    Please post your attempt at (c), your StarObject class (or whatever you called it) and the loop where you create lots of them.


  • Registered Users Posts: 121 ✭✭gemma188


    So far I just have Part A done.
    void display(void) {
    //...
    float fov=..., aspect=..., near=..., far = ..;
    glMatrixMode(GL_PROJECTION);
    gluPerspective(fov,aspect, near, far);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0,0,-2);
    drawStar();
    glFlush ();
    }

    Not sure if this is correct?


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Not checked it exactly... but looks ok. They just want you to replace gluOrtho2D (an orthographic 2d projection) with gluPerspective (3d perspective projection).

    Now go do part (b) then we can start on (c).

    Btw, writing code is no good. Have you compile and run it? Does it render on screen as you expect?

    (float fov=..., aspect=..., near=..., far = ..; ) I hope you actually put real numbers in there, ellipsis will not make compiler happy.

    If you want to learn graphics programming you have to compile and run code, observe result on screen, tweak code, repeat etc etc. Anything else is useless.


  • Registered Users Posts: 121 ✭✭gemma188


    For part B:
    void display(void) {
    //...
    float fov=..., aspect=..., near=..., far = ..;
    glMatrixMode(GL_PROJECTION);
    gluPerspective(fov,aspect, near, far);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0,0,-2);

    glTranslate3f(x,y,z);
    glColor3f(r,g,b);

    drawStar();

    glFlush ();
    }


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Please read the question properly. You have only answered part of (b). Where is your modified drawStar function? Your answer would get partial marks. Yes the end result would be correct, but it isn't what they asked for. It's important because part (c) builds on it :)

    Are you actually running and compiling this? Take a screenshot and show please. What are your values for fov, aspect, near and far?


  • Advertisement
  • Registered Users Posts: 121 ✭✭gemma188


    No I'm not running it. Since it's a written exam that I have to do I have been writing the code.


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Start compiling and running it or you won't learn anything. I'll wait for a screenshot of part (b) in action before helping you with (c).


  • Registered Users Posts: 121 ✭✭gemma188


    srsly78 wrote: »
    Start compiling and running it or you won't learn anything. I'll wait for a screenshot of part (b) in action before helping you with (c).

    Im on my work computer so Im not able to run it. Is it completely necessary to run it for doing part C?


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    To verify your code is correct it's completely necessary to run it and observe the result on the screen yes.

    You still haven't answered (b) correctly by the way.


  • Registered Users Posts: 121 ✭✭gemma188


    To answer B correctly do I just need to put those two function calls into a drawStar()?


  • Advertisement
  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Pretty much yes. And change the parameters on the drawStar function. Can you see why this is important for (c)?


  • Registered Users Posts: 121 ✭✭gemma188


    To be honest no Im not fully sure of this part. As I said before part C is completely unknown to me so I really dont know what to do with it. This is what I really need the help with!


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    How to solve (c):

    0. Define a StarObject with properties needed to render a star (ie position)
    1. Create an initialise function that creates lots of stars
    2. Create an update function that moves each star by a little bit
    3. Modify existing code to hook in the new stuff (glutIdlefunc(update()) or something to register the update call). Modify render function to draw N stars rather than 1.

    Now show your attempt please. I hope you realise memorising answers to old papers is not gonna help much in an opengl exam.


Advertisement