Search code examples
c++copenglglut

Loop to save vertices in openGL


I have a grid with tetragons and i want to save all the vertices in an array. I wrote ths code:

int counter=0;
int i = 0;
for(i=0; i<=600; i+=40){
    verticePosition[counter] = i;
    verticePosition[counter+1] = i;
    verticePosition[counter+2] = i+40;
    verticePosition[counter+3] = i;
    verticePosition[counter+4] = i;
    verticePosition[counter+5] = i+40;
    verticePosition[counter+6] = i+40;
    verticePosition[counter+7] = i+40;
    counter += 8;
}

I want to save four-four vertices in the table and then i call a function to fill every tetragon with a different color but im getting an error in this for loop:

prog.c:13:1: error: expected identifier or ‘(’ before ‘for’
 for(xpos=0; xpox<=600; xpos+=40){
 ^

and also another error:

prog.c:13:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<=’ token
 for(xpos=0; xpox<=600; xpos+=40){
             ^

I cant find what is wrong with my loop.


Solution

  • The variable xpos is used but not declared, you must declare and initialize it:

    for (int xpos = 0; xpos <= 600;  xpos += 40) {
    

    Or declare it before the loop:

    int xpos;
    for (xpos = 0; xpos <= 600;  xpos += 40) {