OpenGL problem

This is a discussion on OpenGL problem within the Game Programming forums, part of the General Programming Boards category; I have a problem with an openGL app I wrote. The window doesn't update/refresh, I don't know if it's just ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    10

    OpenGL problem

    I have a problem with an openGL app I wrote. The window doesn't update/refresh, I don't know if it's just my comp acting silly or something is wrong with my code, it does update when I minimize and than unminimize the window. I'm using Code:blocks with gcc on ubuntu 10.10, the libs I am using are opengl, glu, glut and DevIL. Here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GL/glut.h>
    #include <IL/il.h>
    
    float xRotate=0, yRotate=0;
    float xPos=0, yPos=-2, zPos=-0;
    GLUquadricObj *quadric;
    float angle=0;
    
    GLfloat lightColor0[]={1, 1, 1, 1};
    GLfloat lightPos0[]={0, 20, 0};
    
    ILuint sky, grass;
    ILboolean success;
    
    GLuint skyimage, grasstex;
    
    void initRendering()
    {
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_NORMALIZE);
        glEnable(GL_CULL_FACE);
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_COLOR_MATERIAL);
    }
    void initVars()
    {
        quadric=gluNewQuadric();
        gluQuadricOrientation(quadric, GLU_INSIDE);
        gluQuadricNormals(quadric, GLU_SMOOTH);
        gluQuadricDrawStyle(quadric, GLU_FILL);
        gluQuadricTexture(quadric, GL_TRUE);
    }
    void handleResize(int w, int h)
    {
        glViewport(0, 0, w, h);
    
        glMatrixMode(GL_PROJECTION);
    
    
        glLoadIdentity();
        gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
    }
    void handleKeypress(unsigned char a, int x, int y)
    {
        switch(a)
        {
            case 97: xPos+=0.1; break;
            case 100: xPos-=0.1; break;
            case 115: zPos+=0.1; break;
            case 119: zPos-=0.1; break;
        }
    }
    void handleKeyPressSpecial(unsigned char a, int x, int y)
    {
        switch(a)
        {
            case GLUT_KEY_LEFT: xPos+=0.1; break;
            case GLUT_KEY_RIGHT: xPos-=0.1; break;
            case GLUT_KEY_UP: zPos+=0.1; break;
            case GLUT_KEY_DOWN: zPos-=0.1; break;
        }
    }
    void handleMouse(int button, int state, int x, int y)
    {
    
    }
    void drawScene()
    {
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glRotatef(angle,xRotate, yRotate, 0);
        glTranslatef(xPos, yPos, zPos);
    
        GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f};
        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
    
        glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
        glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
    
        ilBindImage(sky);
        glBindTexture(GL_TEXTURE_2D, skyimage);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
                     ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
                     ilGetData());
    
        gluSphere(quadric, 50, 50, 50);
    
    
        ilBindImage(grass);
        glBindTexture(GL_TEXTURE_2D, grasstex);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
                     ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
                     ilGetData());
    
        glBegin(GL_QUADS);
    
        glNormal3f(0, 1, 0);
        glTexCoord2f(10,10);
        glVertex3f(50,0,50);
        glTexCoord2f(0,10);
        glVertex3f(50,0,-50);
        glTexCoord2f(0,0);
        glVertex3f(-50,0,-50);
        glTexCoord2f(10,0);
        glVertex3f(-50,0,50);
    
        glEnd();
    
        glutSwapBuffers();
    }
    void loadTex()
    {
        ilGenImages(1, &sky);
        ilBindImage(sky);
        success=ilLoadImage("skydometex.jpg");
        if(success)
        {
            ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
            glGenTextures(1, &skyimage);
        }
        ilGenImages(1, &grass);
        ilBindImage(grass);
        success=ilLoadImage("grass.jpg");
        if(success)
        {
            ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
            glGenTextures(1, &grasstex);
        }
    }
    int main(int argc, char** argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
        glutInitWindowSize(800,600);
        glutCreateWindow("Najmocniji program na svetu");
        initRendering();
        initVars();
        ilInit();
        loadTex();
        glutDisplayFunc(drawScene);
        glutKeyboardFunc(handleKeypress);
        glutSpecialFunc(handleKeyPressSpecial);
        glutReshapeFunc(handleResize);
        glutMouseFunc(handleMouse);
        //glutMotionFunc(handleMouseMotion);
    
        glutMainLoop();
    }
    Any help would be appreciated.
    Last edited by Charobnjak; 05-08-2011 at 10:48 AM. Reason: added libs

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You could try adding glutPostRedisplay() after glutSwapBuffers() in your DisplayFunc.

    If this is your first GL program, I have a tip for you: do not try to learn 3D graphics by coming up with some elaborate scene including textures, interactivity, and 3rd party libs straight off. First, make a flat colored cube and get it to rotate. When that works the way you want, you can start to add more.

    At each step, keep a version of the one previous. So, when the plain cube works, save it separately and then add textures. When that works, add some interactivity to the previous plain cube. The add that into the textured cube. By the time you get to the point you are at now, you should have at least 3-4 simpler programs that led up to this one, that you can go back and experiment with.

    That way, you will not run into fundamental problems like "the window doesn't update/refresh" with a long, complicated piece of code and be clueless about what could be wrong.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    Thanks, that did the work

    Also, a reply to your tip: no, it's not my first GL program, a have been learning it for some time and I though to experiment a little before I continue the tutorial. I passed all the stages that you mentioned and all those worked but I didn't save those...maybe I should from now on. Anyway thanks for the tip

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Quick comments: you can say case 'a' instead of case 97. Also, it doesn't matter with such a small program, but with more code it can be convenient to put all the resources like "grass.jpg" as #defines or (this being C++) const char* variables at the top of the file or in some header file. Makes them easier to find and change when you're experimenting.

    And always save programs and versions of your code that work! If you're feeling really adventurous you could learn a version control system like git, but copy-pasting/cp -r works too. Have fun with OpenGL.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    Thanks for the advices

    I really appreciate it since I just recently started learning openGL, even tho I have been learning C for quite some time now, I haven't done any major projects or something that requires long codes, so I don't really know how to keep my code clean and easily changeable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL Problem
    By GReaper in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2009, 11:49 AM
  2. OpenGL 100% CPU problem
    By tWiZtEr in forum Game Programming
    Replies: 6
    Last Post: 12-23-2002, 04:49 AM
  3. OpenGL Problem
    By slx47 in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2002, 08:00 PM
  4. OpenGL Tut problem
    By valar_king in forum Game Programming
    Replies: 3
    Last Post: 01-15-2002, 08:08 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21