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 7 of 7

Thread: Help

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    4

    Help

    Hi I am new to OpenGl and just got started.I was trying to make a program that draws a square where ever i click the mouse button.
    It is not working.If anyone has any ideas please help.


    **********CODE*****************************

    #include<GL/glut.h>
    #include<iostream>
    #include<math.h>

    void myDisplay()
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glTranslatef(9,5,0);
    glutSwapBuffers();
    }
    void drawSquare()
    {
    glBegin(GL_QUADS);
    glVertex2f(10.0, 10.0);
    glVertex2f(-10.0, 10.0);
    glVertex2f(-10.0, -10.0);
    glVertex2f(10.0, -10.0);
    glEnd();
    }
    void drawSquare1()
    {
    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(1.0,0.0,0.0);
    glBegin(GL_QUADS);
    glVertex2f(10.0, 10.0);
    glVertex2f(-10.0, 10.0);
    glVertex2f(-10.0, -10.0);
    glVertex2f(10.0, -10.0);
    glEnd();
    }
    void myMouse(int button,int state,int x,int y)
    {
    if((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
    {
    drawSquare();
    myDisplay();
    }
    else
    drawSquare1();
    }



    void myinit()
    {

    glClearColor(1.0,1.0,1.0,1.0);
    glColor3f(1.0,1.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,50.0,0.0,50.0);
    glMatrixMode(GL_MODELVIEW);
    }
    int main(int argc,char** argv)
    {
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(0,0);
    glutCreateWindow("SQUARE PROGRAM");
    glutMouseFunc(myMouse);
    glutDisplayFunc(myDisplay);
    myinit();
    glutMainLoop();
    return 1;
    }

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,170
    Firstly, you don't seem to have read the posting guide. If you had, you would know about certain protocols for asking questions. Mainly things like 'Help me' type questions. You need to be more specific with you line of questions so people can be specific with their answers.

    Anyway, the problem you have is that you are not understanding the concept of events in Windows. The Mouse event handler is not the place to be putting drawing code - that's the job of your onDraw event handler.
    void myDisplay()
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glTranslatef(9,5,0);
    glutSwapBuffers();
    }
    This onDraw event is not going to do much is it? It does not even contain the code required to draw anything. You need to insert drawSquare();
    after the glTranslatef command to position the square before drawing it.

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    4
    I will go through the posting guide.Anyway even after putting the drawSquare function the mouse click events are not getting called.Can you go through the code properly and tell me if the code is ok or where am i going wrong.and should the myDisplay function be in the mouse event handler function.
    And what i want the program to do is--- it should display a white screen,when i click on a particular area it should display a square.

    Thank You
    Last edited by ayushman4; 04-26-2012 at 04:26 AM.

  4. #4
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,170
    I'm not going to do the code for you - that's where you get benefit from - by spend the time fixing things yourself. It's the only way to learn (by putting in the hours).
    You should obly be setting flags in the mouse events - no draw code - as I said earlier.
    Your draw code should be something like this:
    Code :
    void drawSquare1()
    {
    glColor3f(1.0,0.0,0.0);
    glBegin(GL_QUADS);
    glVertex2f(10.0, 10.0); 
    glVertex2f(-10.0, 10.0); 
    glVertex2f(-10.0, -10.0); 
    glVertex2f(10.0, -10.0);
    glEnd();
    Since you are not using textures, you should also disable texturing - glDisable (GL_TEXTURE2D) during init()

  5. #5
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    1
    There few thing you need to be more clear about:
    1. To use glutSwapBuffers() you need to init two buffers > glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);. if you want to use GLUT_SINGLE you need to use glFlash(). But I would suggest to continue use SwapBuffers.
    2. You need to use glutPostRedisplay function in the end of myMouse(...). That function set the flag that you would like to redraw the screen;
    3. As was mentioned before you should put the draw function in myDisplay(...) and put glTranslate in myMouse(). If you think about, you want to change coordinates of a quad when a mouse clicked, not when you draw it(if you will draw 100 times per second you will not see how it moved).

  6. #6
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    177
    It is not working.
    That's way to vague to inspire anyone to help you. Does this code compile and link? What happens when you run it? Forget about the mouse stuff for a while. Are you able to simply draw a square somewhere in your window? Get that working first. Then move on to the mouse stuff. Good luck.

  7. #7
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    4
    thank you .. got it working like i wanted to..

Posting Permissions

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