Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 9 of 9

Thread: Bouncing lights problem during animation

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2014
    Posts
    6

    Bouncing lights problem during animation

    Hi,
    I posted a question with my issue here (beginners forums). No one replied. Could someone help me with it?

    The issue:
    Here's the video of my problem:
    https://www.youtube.com/watch?v=MBCaS9qEXHo

    As you can see, the lightning effects aren't fading completely, but bouncing from one state to another.
    Here's my full code:

    Code :
    //
    //  main.c
    //  MAMAN15Q5
    //
    //  Created by Vitali Pom on 6/6/14.
    //  Copyright (c) 2014 Vitali Pom. All rights reserved.
    //
    #include <stdio.h>
    #include <OpenGL/gl.h>
    #include <GLUT/glut.h>
    #include <time.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <math.h>
    #include <stdarg.h>
    #include <string.h>
    #include "simclist.h"
     
    #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
    #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
     
    #define KEY_ESCAPE 27
     
    GLfloat CLIP_HEIGHT = 50;
    GLfloat CLIP_WIDTH = 50;
     
    typedef struct {
        int width;
    	int height;
        int aspectRatio;
    	char* title;
     
    } glutWindow;
     
     
    int DELAY = 30;
    GLfloat rotate_by=1;
    glutWindow win;
     
    list_t polygons;
     
    size_t szelem(const void *el) {
        return sizeof(list_t);
    }
     
    size_t szelem2(const void *el) {
        return sizeof(GLfloat [3]);
    }
     
    void * list_get_last(const list_t *restrict l){
        return list_get_at(l, list_size(l)-1);
    }
     
     
    void addPoint(list_t *polygon, GLfloat (*point)[3]){
        list_append(polygon, point);
     
    }
     
     
    void addPolygon(int size, GLfloat (*vertex)[3], ...){
        list_t polygon1;
        list_init(&polygon1);
        list_attributes_copy(&polygon1, szelem2, 1);
        va_list args;
        va_start(args, vertex);
        addPoint(&polygon1, vertex);
        for(int i=0;i<size-1; i++){
            GLfloat (*p)[3];
            p=va_arg(args, GLfloat (*)[3]);
            addPoint(&polygon1, p);
        }
     
        list_append(&polygons, &polygon1);
    }
     
    void * normal_vec(GLfloat *p1, GLfloat *p2, GLfloat *p3){
        GLfloat *normal_v=malloc(3*sizeof(GLfloat));
        //for(int i =0; i<3;i++){
            //*(normal_v+i) = (*(p1+i)- *(p2+i)) * (*(p3+i)-*(p2+i));
        //}
        *(normal_v+0) = ((*(p1+1)- *(p2+1))*(*(p3+2)-*(p2+2)))-((*(p1+2)- *(p2+2))*(*(p3+1)-*(p2+1)));
        *(normal_v+1) = -(((*(p1+0)- *(p2+0))*(*(p3+2)-*(p2+2)))-((*(p1+2)- *(p2+2))*(*(p3+0)-*(p2+0))));
        *(normal_v+2) = ((*(p1+0)- *(p2+0))*(*(p3+1)-*(p2+1)))-((*(p1+1)- *(p2+1))*(*(p3+0)-*(p2+0)));
        GLfloat magnitude = sqrt((*(normal_v+0) * *(normal_v+0)) + (*(normal_v+1) * *(normal_v+1)) + (*(normal_v+2) * *(normal_v+2)));
        *(normal_v+0) /= magnitude;
        *(normal_v+1) /= magnitude;
        *(normal_v+2) /= magnitude;
        return normal_v;
    }
     
    void * sub_vec(GLfloat *p1, GLfloat *p2){
        GLfloat *sub_v = malloc(3*sizeof(GLfloat));
        for(int i =0; i<3;i++){
            sub_v[i] = (*(p1+i)- *(p2+i));
        }
        return sub_v;
    }
     
    GLfloat dot_vec(GLfloat *p1, GLfloat *p2){
        float result =0;
        for(int i =0;i<3;i++){
            GLfloat v1 = *(p1+i);
            //printf("V1:%f ",v1);
            GLfloat v2 = *(p2+i);
            //printf("V2:%f",v2);
     
            result +=  v1*v2;
            //printf("result:%f ",result);
            //printf("\n");
        }
        return result;
    }
     
     
     
    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT );
        GLfloat view_from[3] = {7,7, -5};
        GLfloat view_at[3] = { 7, 7,18};
        gluLookAt(view_from[0], view_from[1], view_from[2], view_at[0], view_at[1] , view_at[2], 0, 1, 0);
        glClearColor(0.0, 0.0, 0.0, 1.0);
        //glMatrixMode(GL_PROJECTION);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    	float var =0.0;
        GLfloat *mysub = sub_vec(&view_at, &view_from); //good
        //printf("(%f,%f,%f)\n",*(mysub+0), *(mysub+1), *(mysub+2));
     
        for(int i =0; i<list_size(&polygons);i++){
            list_t *polygon = list_get_at(&polygons, i);
            GLfloat (*pol_point0)= list_get_at(polygon, 0);
            GLfloat (*pol_point1) = list_get_at(polygon, 2);
            GLfloat (*pol_point2) = list_get_at(polygon, 3);
            GLfloat (*mynorm) = normal_vec(pol_point0, pol_point1, pol_point2); //good
     
            //printf("(%f,%f,%f)\n",*(mynorm+0), *(mynorm+1),*(mynorm+2)); //good
            //printf("(%f,%f,%f)\n",*(mynorm+0), *(mynorm+1),*(mynorm+2)); //good
     
            GLfloat mydot = dot_vec(mynorm,mysub); 
            //printf("dot=%f\n",mydot);
            if(mydot<0.0f){
                //printf("drawing %i\n",i);
     
                glColor3f(0.9, var+=0.2, 0.5);
                //glColorMaterial(<#GLenum face#>, <#GLenum mode#>);
                glBegin(GL_POLYGON);
                glNormal3fv(mynorm);
                for(int j=0;j<list_size(polygon);j++){
     
                    GLfloat (*point_ptr) = list_get_at(polygon, j);
                    //GLfloat *point = *point_ptr;
     
                    glVertex3fv(point_ptr);
                }
                glEnd();
            }
        }
        //glLineWidth(4.0);
        glEnable(GL_MULTISAMPLE_ARB);    
        glutSwapBuffers();
    }
     
    void rotate(){
     
        for(int i =0; i<list_size(&polygons);i++){
            list_t *polygon = list_get_at(&polygons, i);
            for(int j =0; j<list_size(polygon);j++){
                GLfloat *point = list_extract_at(polygon, j);
                GLfloat point1[16] = {0};
                point1[0] = *(point+0);
                point1[1] = *(point+1);
                point1[2] = *(point+2);
                point1[3] = 1;
                //multiply:
                glPushMatrix();
                glMatrixMode(GL_MODELVIEW);
                glLoadIdentity();
                glTranslatef(7, 7, 6);
                glRotatef(rotate_by,1, 0, 0);
                glTranslatef(-7, -7, -6);
                glMultMatrixf(point1);
                GLfloat resultMat[16];
                glGetFloatv(GL_MODELVIEW, resultMat);
                //glLoadIdentity();
     
                //glMultMatrixf(resultMat);
                //glGetFloatv(GL_MODELVIEW, resultMat);
                *(point+0) = resultMat[0];
                *(point+1) = resultMat[1];
                *(point+2) = resultMat[2];
                //printf("new point: (%f,%f,%f)\n",*(point+0), *(point+1), *(point+2));
                list_insert_at(polygon, point, j);
                glPopMatrix();
            }
        }
    }
     
    void initialize ()
    {
     
        list_init(&polygons);
        list_attributes_copy(&polygons, szelem, 1);
     
        //front
        addPolygon(4, &((GLfloat[3]){5,5,5}), &((GLfloat[3]){10,5,5}), &((GLfloat[3]){10,10,5}),&((GLfloat[3]){5,10,5}));
        //back
        addPolygon(4, &((GLfloat[3]){5,10,7}), &((GLfloat[3]){10,10,7}), &((GLfloat[3]){10,5,7}),  &((GLfloat[3]){5,5,7}));
        //bottom
        addPolygon(4, &((GLfloat[3]){5,5,5}), &((GLfloat[3]){5,5,7}), &((GLfloat[3]){10,5,7}),&((GLfloat[3]){10,5,5}));
        //left side
        addPolygon(4, &((GLfloat[3]){5,10,5}),&((GLfloat[3]){5,10,7}),&((GLfloat[3]){5,5,7}), &((GLfloat[3]){5,5,5}));
        //right
        addPolygon(4, &((GLfloat[3]){10,5,5}), &((GLfloat[3]){10,5,7}), &((GLfloat[3]){10,10,7}),&((GLfloat[3]){10,10,5}));
        //top
        addPolygon(4, &((GLfloat[3]){5,10,5}),&((GLfloat[3]){10,10,5}), &((GLfloat[3]){10,10,7}), &((GLfloat[3]){5,10,7}));
     
        glClearColor(0.0, 0.0, 0.0, 0.0);
     
        //GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
        //GLfloat mat_shininess[] = { 60.0 };
        GLfloat light_position[] = { 0, 0, -5, 1.0f };
     
     
        GLfloat direction[] = {5,5,7};
     
        // grey diffuse light
        float diffuse_light [] = {0.6f, 0.6f, 0.6f, 1};
     
        // yellow specular light
        float specular_light [] = {0.4f, 0.4f, 0.4f, 1};
     
     
        //
        //glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
        ///
        //glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
        glLightfv(GL_LIGHT0, GL_POSITION, light_position);
        glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction );
        // grey diffuse light
        glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse_light);
        glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 10);
     
        // yellow specular light
        glLightfv (GL_LIGHT0, GL_SPECULAR, specular_light);
     
        GLfloat ambientColor[] = {.1, .1, .1, 1}; //Color(0.2, 0.2, 0.2)
        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
     
        //glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
        glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
        glEnable(GL_LIGHTING);
        glEnable(GL_COLOR_MATERIAL);
        glColorMaterial (GL_FRONT, GL_DIFFUSE);
        glColorMaterial (GL_FRONT, GL_SPECULAR);
        glEnable(GL_LIGHT0);
     
        glShadeModel(GL_SMOOTH);
        glEnable(GL_CULL_FACE);
        glCullFace(GL_FRONT);
        glFrontFace(GL_CCW);
     
        rotate_by = 360.0f*DELAY/(15000.0f);
     
    }
     
    void keyboard ( unsigned char key, int mousePositionX, int mousePositionY )
    {
        switch ( key )
        {
            case KEY_ESCAPE:
                exit ( 0 );
                break;
     
            default:
                break;
        }
    }
     
     
     
    void resizeWindow(GLsizei w, GLsizei h){
        if(h==0 || w==0){
            glutReshapeWindow(1, 1);
        }
        win.height = h;
        win.width = w;
        //glMatrixMode(GL_MODELVIEW);
        //glLoadIdentity();
         GLdouble aspect = (GLdouble)( (double)win.width /(double) win.height);
         if(aspect>1){
            glViewport(0, 0,  ((GLdouble)win.width)/aspect,  win.height);
         }else{
            glViewport(0, 0,  ((GLdouble)win.width),  win.height*aspect);
        }
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-15, 15, -15, 15, 0, 20);
     
        //glFrustum(-10, 10, -10, 10, 6, 20);
        //gluPerspective(90, aspect, 0.1f, 100.0f);
        //gluLookAt(0, 0, 0, 0, 0, 0, 0, 1, 0);
     
    }
     
    void Timer(int i){
        rotate();
        glutTimerFunc(DELAY, Timer, 0);
        glutPostRedisplay();
    }
     
    int main(int argc,  char * argv[])
    {
     
      	// set window values
    	win.width =700;
    	win.height = 700;
        win.aspectRatio = win.width/win.height;
    	win.title = "Q5";
     
     
    	// initialize and run program
    	glutInitWindowSize(win.width,win.height);
     
     
        glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH|GLUT_MULTISAMPLE  );
     
    	glutCreateWindow(win.title);
    	glutReshapeFunc(resizeWindow);
        initialize();
     
        glutDisplayFunc(display);
     
        glutKeyboardFunc( keyboard );
        Timer(0);
        glutMainLoop();
    	return 0;
    }

    Q: Why is the change in the lightning isn't smooth and how to fix it?

    Thanks ahead,
    Vitali Pom.

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,224
    The reason you didn't get any replies is because you just posted a lot of code and asked for someone else to debug your problem. You didn't actually say what, if anything, you've done to try and diagnose (or at least pair down) the problem yourself, or ask any specific technical questions so we can help you nail it down.

    Also, the code you posted can't just be compiled by anyone because it depends on headers/types that aren't generally available (e.g. STL). Finally, it'd be helpful if you'd describe what you are "trying" to do. It's far from obvious from the video you posted.

    See the Forum Posting Guidelines for more details.

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2014
    Posts
    6
    Hi,
    First of all, thanks for your reply. Now to the fire:
    "The reason you didn't get any replies is because you just posted a lot of code and asked for someone else to debug your problem."
    I'm afraid that this is not the reason. I've being participating in enough discussions daily, starting from my academic studies that began at mid school, to tell you that I do know how to ask questions, adding to it that usually when someone doesn't have a clue about the answer, he/she can simply tell "I don't know." And if he/she is nice and kindly, maybe also try offer to talk to someone who actually can help.
    "See the Forum Posting Guidelines for more details."
    Please don't confuse your goals, in partiular "filtering trolls/noobs/bots" or whatever you which from the forum, with my goals as a student, in particular "to get a good answer".

    And to the meat: "Also, the code you posted can't just be compiled by anyone because it depends on headers/types that aren't generally available (e.g. STL). Finally, it'd be helpful if you'd describe what you are "trying" to do. It's far from obvious from the video you posted."
    I posted a clear video with a clear title of my problem and of anything I'm trying to do (common, we learned to understand such titles in school…): "bouncing lights problem" -> I'm trying to apply lights without the 'bouncing' problem. So what is the 'bouncing' problem? I believe that once you open the video I uploaded, (and for insurance that I make myself clear, here is what I'm trying to achieve: https://www.youtube.com/watch?v=CvaGd4KqlvQ - do you see bouncing lights? Me neither.) you can understand my problem.

    P.S Regarding "...you just posted a lot of code and asked for someone else to debug your problem." - Hey, I can be rude sometimes, but not cruel. I don't wish anyone to debug my code. I posted it for those who'd like to help and take a look at my algorithms in case they're wrong.

    If you still wish to run it on your own system (you surely have some reason), the library that is missing can be found here: http://mij.oltrelinux.com/devel/simclist/ . Just don't forget to update #include <OpenGL/gl.h> and #include <GLUT/glut.h> to your os' once.

    Thanks for your time.

    Kindly,
    Vitali
    Last edited by vitaminP21; 06-25-2014 at 12:06 PM.

  4. #4
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,224
    Run a memchecker on it. If you do, I think you'll find it's operatoring on unitialized values. In fact, in your display function the pol_point# vars are coming back with garbage elements (NaNs, e+33 magnitude numbers, etc.). Didn't bother to trace it down through that special list type. Also FWIW, your code compiles with a bunch of type warnings and even besides that I had to add 3 casts to get around 3 compile errors just to get it built.

    Also, when the GL window comes up, I see nothing rendered.

  5. #5
    Junior Member Newbie
    Join Date
    Jun 2014
    Posts
    6
    Hi, I rechecked what you're claiming (I also had checked it before), here is an example of the po_point results in the display func:
    Code :
    (5.000000, 8.123627, 4.066723)
    (5.000000, 6.127529, 3.941123)
    (10.000000, 6.127529, 3.941123)
    (10.000000, 8.123627, 4.066723)
    So I guess we both agree that it's a platform change issue (I use OS X).
    As I said, I was hoping that the root of the cause is not my C skills, but something with the algorithms. That's basically why I didn't except others to compile it on their own machines.


    As about memory check, I didn't clean the memory, so I shall have memory leaks as a result of laziness. But, I don't think it shall affect the lights problem.
    Thanks.

    [EDIT:]
    Here's the 1st polygon (i==0) normal while visible (somewhere there the lights jumps from state to state):
    Code :
    (0.000000, -0.999921, -0.012566)
    (0.000000, -0.999289, -0.037690)
    (0.000000, -0.998027, -0.062790)
    (0.000000, -0.996134, -0.087851)
    (0.000000, -0.993611, -0.112856)
    (0.000000, -0.990461, -0.137790)
    (0.000000, -0.986686, -0.162637)
    (0.000000, -0.982287, -0.187381)
    (0.000000, -0.977268, -0.212007)
    (0.000000, -0.971632, -0.236499)
    (0.000000, -0.965382, -0.260841)
    (0.000000, -0.958522, -0.285019)
    (0.000000, -0.951057, -0.309016)
    (0.000000, -0.942991, -0.332819)
    (0.000000, -0.934329, -0.356411)
    (0.000000, -0.925077, -0.379779)
    (0.000000, -0.915241, -0.402906)
    (0.000000, -0.904827, -0.425779)
    (0.000000, -0.893842, -0.448383)
    (0.000000, -0.882291, -0.470704)
    (0.000000, -0.870184, -0.492727)
    (0.000000, -0.857527, -0.514439)
    (0.000000, -0.844328, -0.535826)
    (0.000000, -0.830596, -0.556875)
    (0.000000, -0.816339, -0.577572)
    (0.000000, -0.801567, -0.597905)
    (0.000000, -0.786289, -0.617859)
    (0.000000, -0.770513, -0.637424)
    (0.000000, -0.754252, -0.656585)
    (0.000000, -0.737513, -0.675333)
    (0.000000, -0.720309, -0.693653)
    (0.000000, -0.702650, -0.711536)
    (0.000000, -0.684547, -0.728968)
    (0.000000, -0.666012, -0.745941)
    (0.000000, -0.647056, -0.762442)
    (0.000000, -0.627691, -0.778462)
    (0.000000, -0.607930, -0.793990)
    (0.000000, -0.587785, -0.809017)
    (0.000000, -0.567269, -0.823533)
    (0.000000, -0.546394, -0.837528)
    (0.000000, -0.525175, -0.850994)
    (0.000000, -0.503623, -0.863923)
    (0.000000, -0.481754, -0.876307)
    (0.000000, -0.459580, -0.888136)
    (0.000000, -0.437116, -0.899405)
    (0.000000, -0.414376, -0.910106)
    (0.000000, -0.391374, -0.920232)
    (0.000000, -0.368125, -0.929776)
    (0.000000, -0.344643, -0.938734)
    (0.000000, -0.320944, -0.947098)
    (0.000000, -0.297042, -0.954865)
    (0.000000, -0.272952, -0.962028)

    [EDIT #2:]
    If I turn add an angle (i.e. add another 40 degrees) to the GL_SPOT_CUTOFF, then the pretty effect of vertices shadow disappears the bouncing stops. I guess that this is in general because I had only one normal for the whole polygon.
    I still have a problem with flickering sides (see in the video from the 1st message, second 0.04 - the upper side disappears for a split of second).
    I'l keep updating.

    [EDIT #3:]
    I changed the place of the light, changed the specular and the diffuse lights from gl_front (which I don't know what it means) and except for a small sudden change in color of one of the sides, which is hard to notice everything is smooth.

    The next challenge is to make those white points on the cube, like here: http://xoax.net/cpp/crs/opengl/lessons/Lesson4/

    [EDIT #4:]
    Succeeded to add shininess with:
    Code :
     glMaterialf(GL_FRONT, GL_SHININESS, 90.0);
    and ADDING this to my existing code (tells OpenGL about the reflectivity of the material):
    Code :
    float colorBlue[4]       = { 1.0, 1.0, 1.0, 1.0 };
    glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBlue);
    glMaterialfv(GL_FRONT, GL_SPECULAR, colorBlue);
    Final animation result:
    https://www.youtube.com/watch?v=zILNOVHk4uk
    Last edited by vitaminP21; 06-28-2014 at 12:24 AM. Reason: Solved

  6. #6
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,224
    Quote Originally Posted by vitaminP21 View Post
    HSo I guess we both agree that it's a platform change issue (I use OS X).
    Yeah, probably so. Running Linux with GCC 4.8.1 here.

  7. #7
    Junior Member Newbie
    Join Date
    Jun 2014
    Posts
    6
    Thanks for your responsiveness! It's not the only place where I posted the Q, but thanks to you it's the only place where I posted the solution .

  8. #8
    Junior Member Newbie
    Join Date
    Jun 2014
    Posts
    6

    Important Correction

    (Admins please add this correction to the upper message with all the edits, I can't edit anymore.)
    [EDIT #5:]
    My bad, it should be:
    Code :
    //glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 550); //make it 180 or comment it 
    glMaterialf(GL_FRONT, GL_SHININESS, 90.0);

  9. #9
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,224
    Glad you got it figured out!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •