Results 1 to 5 of 5

Thread: problem of glColor4f

  1. #1
    Join Date
    Mar 2013
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default problem of glColor4f

    Hi, I am writing a test program of the Qt and openGL project for drawing a red square.

    The code is:

    Qt Code:
    1. void GLImageWidget::initializeGL()
    2. {
    3. static const GLfloat lightPos[4] = { 5.0f, 5.0f, 10.0f, 1.0f };
    4.  
    5. glEnable(GL_LIGHTING);
    6. glEnable(GL_LIGHT0);
    7. glEnable(GL_DEPTH_TEST);
    8. glMatrixMode(GL_PROJECTION);
    9. glViewport(0, 0, image_nx, image_ny);
    10. glLoadIdentity();
    11. glMatrixMode(GL_MODELVIEW);
    12. glShadeModel( GL_SMOOTH );
    13. glClearDepth( 1.0f );
    14. glEnable( GL_DEPTH_TEST );
    15. glDepthFunc( GL_LEQUAL );
    16. glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
    17. glEnable(GL_NORMALIZE);
    18. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    19. slicelocation = 0;
    20.  
    21. /* checked OK!
    22. printf("nv = %d", nv);
    23. getchar();
    24. int i=0;
    25. for(i=0; i<nv; i++)
    26. {
    27. printf("%.4f,", fImage[i]);
    28. }*/
    29. }
    30.  
    31.  
    32. void GLImageWidget::paintGL()
    33. {
    34. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    35. glLoadIdentity();
    36. glTranslatef(0.0, 0.0, -3.0);
    37. glPushMatrix();
    38.  
    39. int i, j, k;
    40. int half_nx = image_nx/2;
    41. int half_ny = image_ny/2;
    42. printf("now paintGL width = %d, height = %d\n", image_nx, image_ny);
    43.  
    44. glPushMatrix();
    45. glRotatef(-90.0, 0, 0, 1);
    46. for ( j = 0; j < image_ny; j++)
    47. for ( k = 0; k < image_nx; k++)
    48. {
    49. glBegin(GL_QUADS);
    50. glColor4f(1.0, 0.0, 0.0, 1.0);
    51. glVertex3f(k-half_nx, j-half_ny, 0);
    52. glColor4f(1.0, 0.0, 0.0, 1.0);
    53. glVertex3f(k+1-half_nx, j-half_ny, 0);
    54. glColor4f(1.0, 0.0, 0.0, 1.0);
    55. glVertex3f(k+1-half_nx, j+1-half_ny, 0);
    56. glColor4f(1.0, 0.0, 0.0, 1.0);
    57. glVertex3f(k-half_nx, j+1-half_ny, 0);
    58.  
    59. glEnd();
    60. }
    61. glPopMatrix();
    62. }
    To copy to clipboard, switch view to plain text mode 

    The square is not drawn... anything wrong here?
    Thank you in advance!
    Last edited by wysota; 15th March 2013 at 06:44. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem of glColor4f

    Why do you think the problem is with glColor4f? I'd rather guess you're simply drawing outside your viewport.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2013
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: problem of glColor4f

    Hi Wysota,

    thank you so much for your answer, :-)

    I changed the third argument of glVertex3f(k-half_nx, j-half_ny, 0), such as 100, 200... it still doesn't work... Need me initial the coordinate? how? Sorry, I am a starter, :-)

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem of glColor4f

    What is the value of image_nx?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,316
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: problem of glColor4f

    For one thing, you are calling glPopMatrix() only once (line 61), but you are calling glPushMatrix() twice. Push and pop should be matched - you will eventually run out of stack otherwise.

    But I'm not really sure what you are trying to do with this code. You aren't drawing a red square, you are drawing (image_nx * image_ny) red squares. You are also applying a translation and a rotation to each vertex in the quads. I have not checked your code, but also make sure you are specifying the vertexes of the quads in the correct order. If you do it wrong, the faces of your quads either will not be created correctly or they will not be facing in the right direction to be illuminated by your light.

    You also do not need to call glColor4f() for every vertex. If all the vertexes are the same color, then you can move that call to outside the glBegin() (or even outside the loops completely).

    You may also need to set material properties using glMaterialfv() before your objects are visible using lighting.

    I suggest that if you are trying to teach yourself Qt and OpenGL at the same time, you start with a simple OpenGL example (like draw 1 red square using GL defaults for everything), port it to Qt, and get that to work first. Once that is working, then you can try to do something harder.
    Last edited by d_stranz; 15th March 2013 at 19:20.

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
  •  
Qt is a trademark of The Qt Company.