OpenGL, draw two polygons in the same time (by mouse clicks)

Posted by YoungSalafi on Stack Overflow See other posts from Stack Overflow or by YoungSalafi
Published on 2012-03-21T17:26:51Z Indexed on 2012/03/21 17:29 UTC
Read the original article Hit count: 137

Filed under:
|
|

im trying to draw 2 polygons at the same time depending on user input from the opengl screen... so i made 2 arrays which each one of them will carry the vertices of each polygon ... i think my logic is right but the program still prints only polygon and delete the old polygon if you draw a polygon again . and its acting weird too please check the code yourself here it is : P.S dont mind the delete function right now.. i know it missing something.

#include <windows.h>
#include <gl/gl.h>
#include <gl/glut.h>

void Draw();
void Set_Transformations();
void Initialize(int argc, char *argv[]);
void OnKeyPress(unsigned char key, int x, int y);
void DeleteVer();
void MouseClick(int bin, int state , int x , int y);
void GetOGLPos(int x, int y,float* arrY,float* arrX);
void DrawPolygon(float* arrX,float* arrY);
float xPos[20];
float yPos[20];
float xPos2[20];
float yPos2[20];
float fx = 0,fy = 0;
float size = 10;
int count = 0;
bool done = false;
bool flag = true;

void Initialize(int argc, char *argv[]) 
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(600, 600);
    glutCreateWindow("OpenGL Lab1");
    Set_Transformations();
    glutDisplayFunc(Draw);
    glutMouseFunc(MouseClick);
    glutKeyboardFunc(OnKeyPress);
    glutMainLoop();
}

void Set_Transformations() 
{
    glClearColor(1, 1, 1, 1);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-200, 200, -200, 200);
}

void OnKeyPress(unsigned char key, int x, int y)
{
    if (key == 27)
    exit(0);
        switch(key)
        {
        case 13: //enter key it will draw
        done = true;
        glutPostRedisplay();
        flag=!flag; // this flag to switch to the other array that the vertices will be stored in, in order to draw the second polygon
        break;
        }
}


void MouseClick(int button, int state , int x , int y)
{
    switch (button) 
    {
    case GLUT_RIGHT_BUTTON:
        if (state == GLUT_DOWN) 
        {
        if (count>0) 
        {
            DeleteVer(); //dont mind this right now

                 }
        }
        break;
     case GLUT_LEFT_BUTTON:
        if (state == GLUT_DOWN) 
        {
             if(count<20)
            {
                if(flag =true){ // drawing first polygon 
                    GetOGLPos(x, y,xPos,yPos);}
                if (flag=false) //drawing second polygon after Enter is pressed
                GetOGLPos(x, y,xPos2,yPos2);
            }
    }
        break;
}
}

void GetOGLPos(int x, int y,float* arrY,float* arrX) //getting the vertices from the user
{
    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX, winY, winZ;
    GLdouble posX, posY, posZ;

    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
    glGetDoublev( GL_PROJECTION_MATRIX, projection );
    glGetIntegerv( GL_VIEWPORT, viewport );

    winX = (float)x;
    winY = (float)viewport[3] - (float)y;
    glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

    gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

    arrX[count] = posX;
    arrY[count] = posY;
    count++;
    glPointSize( 6.0 );


    glBegin(GL_POINTS);
    glVertex2f(posX,posY);
    glEnd();
    glFlush();

 }

void DeleteVer(){ //dont mind this
    glColor3f ( 1, 1, 1);
            glBegin(GL_POINTS);
                glVertex2f(xPos[count-1],yPos[count-1]);
                glEnd();
                glFlush();

              xPos[count] = NULL;
              yPos[count] = NULL;
                count--;
                glColor3f ( 0, 0, 0);
}
void DrawPolygon(float* arrX,float* arrY)
{
    int n=0;
    glColor3f ( 0, 0, 0);
    glBegin(GL_POLYGON);

        while(n<count)
        {
            glVertex2f(arrX[n],arrY[n]);
            n++;
        }
        count=0;
    glEnd();
    glFlush();

}


void Draw() //main drawing func
{

    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0, 0, 0);

    if(done)
    {
        DrawPolygon(xPos,yPos);

        DrawPolygon(xPos2,yPos2);
    }

    glFlush();

}
int main(int argc, char *argv[]) 
{
    Initialize(argc, argv);
    return 0;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about visual-studio