Results 1 to 4 of 4

Thread: picking/selecting primitive object and performing glut function on it...!

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2014
    Posts
    3

    picking/selecting primitive object and performing glut function on it...!

    hello i'm a newbie to openlGL, plz guide me on my simple project.
    i want to create a simple game(click game) in which a object(rectange) appears in the window and is to be clicked and when it is clicked a variable(score) is updated and object is drawn at a random postion and i also need the to add timer (game exits after 30seconds). basically in this game you try to click the object (rectangle) as fast you can in a specified time i.e 30s.....

    i ve written a code(not full), could you plz check whether i'm going in the right direction,

    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GL/glut.h>
    #include <stdlib.h>
    #define N 2
    #define SIZE 1024

    void init(void)
    {
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_FLAT);
    }

    void reshape(int w, int h)
    {
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho2D(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

    void mouse(int button, int state, int x, int y)
    {
    unsigned int score=0;
    GLuint nameBuffer[SIZE];
    GLint hits;
    GLint viewport[4];
    if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
    {
    score++;
    glInitNames();
    glPushName(0);
    glSelectBuffer(SIZE,nameBuffer);
    glGetIntegerv(GL_VIEWPORT,viewport);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(xmin,xmax,ymin,ymax);
    draw(GL_SELECT);
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glFlush();
    hits=glRenderMode(GL_RENDER);
    proces****s(hits,nameBUff);
    glutPostRedisplay();
    printf("score=%d",score);
    }
    }

    void draw()
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadname(1);
    glColor3f(1.0, 1.0, 1.0);
    glRectf(-25.0, -25.0, 25.0, 25.0);

    }

    void display()
    {
    glClear(GL_BUFFER_BIT);
    draw();
    glFlush();
    }

    void proces****s(GLint hits,GLuint buffer[])
    {
    unsigned int i,j;
    GLuint names,*ptr;
    printf("")
    }

    int main(int argc, char** argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
    glutCreateWindow("click on the rectangle!")
    glutInitWindowSize (250, 250);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutMainLoop();
    return 0;
    }

    thank you and unlimited gratitude

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    I don't recommend you do a draw function inside your mouse event. Just use the glutPostRedisplay function to request a redraw event

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2014
    Posts
    3
    thanks for your suggestion i'll try implementing it, i need one more help, how do i add timer and random positioning of the object(rectangle) ??

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    c++ has a random number generator with the rand() function and various time functions

Similar Threads

  1. glut picking function
    By simtekz in forum OpenGL: General
    Replies: 0
    Last Post: 05-26-2012, 12:22 AM
  2. Performing Color Picking when using Vertex arrays
    By pontifikas in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 10-08-2010, 01:06 AM
  3. Color Picking for selecting objects
    By m.sirin in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 08-20-2010, 07:53 AM
  4. Selecting/Picking and Non-Opaque Objects
    By Zed Gimbal in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 02-20-2009, 09:02 PM
  5. picking or selecting a triangle of a mesh?
    By lyd in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 11-03-2004, 02:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean