0

I am trying to draw some line strips using GL_LINE_STRIP in GLUT and OpenGL. I am taking the vertices input from two pointer (for x and y) and plotting them using a for loop. however, I am only getting final two lines as my output (from (0,0)[co-ordinate I have not mentioned in the pointer] to x0,y0),all the other vertices for the lineStrips are missing. Its in the display method.

the code I have been working with is given below. Thank you

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif


#include <stdlib.h>
#include "stdio.h"

int* pointValx;
int* pointValy;
int numPoint;


void displayCB(void)        /* function called whenever redisplay needed */
{
 glClear(GL_COLOR_BUFFER_BIT);      /* clear the display */
 glColor3f(1.0, 1.0, 1.0);      /* set current color to white */
 glPointSize(10.12);
 glBegin(GL_LINE_STRIP); //Begin triangle coordinates

 for(int i = 0; i < numPoint; i++)
 {
  glVertex2i(pointValx[i], pointValx[i]);
  printf("i >> %d\n",i);
 }
 glEnd(); //End Co-ord
 glFlush();
 glutSwapBuffers();
 }

 int takeInput()
{
printf("Screen Size is 0 - 400 in X and 0 - 500 in Y\n");
printf("Lab for Line and Point\n");
printf("number of lines >> \n");
scanf("%d",&numPoint); //comment this line for Line

pointValx = (int*)malloc(numPoint * sizeof(int));
pointValy = (int*)malloc(numPoint * sizeof(int));

printf("numPoint >> %d\n",numPoint);

for(int i = 0; i < numPoint;)
{
    int x,y;
    printf("Input for X >> %d\n", i);
    scanf("%d",&x);
    printf("numPoint >> %d\n",numPoint);
    if(x >= 0 && x <= 400)
    {
        printf("Input for Y >> %d\n", i);
        scanf("%d",&y);
        if(y >= 0 && y <= 500)
        {
            pointValx[i] = x;
            pointValy[i] = y;
            i++;
        }
        else
        {
            printf("Y value crossed the limit\n");
        }
    }
    else
    {
       printf("X value crossed the limit\n");
    }
 }

printf("End of Input file\n");
return -1;
}








int main(int argc, char *argv[])
 {
int win;

glutInit(&argc, argv);      /* initialize GLUT system */

glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(400,500);        /* width=400pixels height=500pixels */
win = glutCreateWindow("GL_LINES and Points");  /* create window */

/* from this point on the current window is win */
takeInput();

glClearColor(0.0,0.0,0.0,0.0);  /* set background to black */
gluOrtho2D(0,400,0,500);        /* how object is mapped to window */
glutDisplayFunc(displayCB);     /* set window's display callback */

glutMainLoop();         /* start processing events... */

/* execution never reaches this point */

free(pointValx);
free(pointValy);

return 0;
}
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 3
    1. This is pure C, there's not a single bit of C++ in there. 2. `glVertex2i(pointValx[i], pointValx[i]);` - maybe there should be a `pointValy` in there somewhere? – Angew is no longer proud of SO Nov 10 '15 at 12:44
  • @Angew, Thanks for the answer. Sorry Its my bad. and I am not sure about the difference between C and C++. I am a native Java developer. This is the first time I am working with either C/C++ – MD Kamal Hossain Shajal Nov 10 '15 at 12:50
  • in main() you use glut to initialize the screen and you only ask for RGB colors which is a single buffered window by default. In your display function you use both flush() and glutSwapBuffers(). The later causes a flush() plus a change of the back buffer which is wrong since there is only one buffer and that;s the screen... – KostasRim Nov 10 '15 at 12:51
  • If you're switching from Java to a different language, pick *one* and work on learning it. Either C (which is a simpler language, but more removed from Java), or C++ (a more complicated language, but somewhat closer to Java). For C++, we have a [list of good books](http://stackoverflow.com/q/388242/1782465) which you could grab as a starting point. – Angew is no longer proud of SO Nov 10 '15 at 12:52
  • @KostasRim, so should I keep flush() or glutSwapBuffers() ? – MD Kamal Hossain Shajal Nov 10 '15 at 12:53
  • @MDKamalHossainShajal depends on what you need, but in general this approach is better: glutInitDisplayMode(GLUT_RGB |GLUT_DOUBLE); which asks glut for a double buffered window. Then remove the flash() command. Since you get input from command line you also need a glutTimerFunc() to render again on screen – KostasRim Nov 10 '15 at 12:56

0 Answers0