Create a grid

OpenGl
Glut
Code::Blocks

Hi. I’m trying do draw a grid with this program:

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

void display(void){
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 1.0, 1.0);
    glBegin(GL_LINES);

//glVertex2f(0.0, 0.1); glVertex2f(1.0, 0.1);
//glVertex2f(0.0, 0.2); glVertex2f(1.0, 0.2);
//glVertex2f(0.0, 0.3); glVertex2f(1.0, 0.3);
//glVertex2f(0.0, 0.4); glVertex2f(1.0, 0.4);

for(int i=1; i<9; i++){
glVertex2f(0.0, i/10);
glVertex2f(1.0, i/10);
glVertex2f(i/10, 0.0);
glVertex2f(i/10, 1.0);}
    glEnd();
    glFlush ();
}

void init (void){
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (250, 250);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("hello");
    init ();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

If I use vertexes in comment, the program works, but if i try to draw the lines with the for command, it doesn’t.

Problem: Both values i and number (10) are integers and the integer division will give u 0.
Solution: Cast your data type to float like this


glVertex2f(0.0, i/10.0f);
glVertex2f(1.0, i/10.0f);
glVertex2f(i/10.0f, 0.0);
glVertex2f(i/10.0f, 1.0);

See if this helps.

Thank you! Another problem, read the comment.

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

void display(int t){
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 1.0, 1.0);
    glBegin(GL_LINES);

for(int i=1; i<10; i++){
 glVertex2f(0.0, i/10.0f);
 glVertex2f(1.0, i/10.0f);
 glVertex2f(i/10.0f, 0.0);
 glVertex2f(i/10.0f, 1.0);}
  glEnd();
 //Here, i will put the rule that will draw the same grid and some
 //squares. The grup of this squares will change position with
 //different values of t.
 glBegin(GL_QUADS);
 glVertex2f(0.2, 0.2); glVertex2f(0.3, 0.2);
 glVertex2f(0.2, 0.3); glVertex2f(0.3, 0.3);
 glVertex2f(0.3, 0.3); glVertex2f(0.4, 0.3);
 glVertex2f(0.3, 0.4); glVertex2f(0.4, 0.4);
  glEnd();
  glFlush ();
  glutTimerFunc(200, display, t+1);
}

void init (void){
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (250, 250);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("hello");
    init();
    glutDisplayFunc(display(0));
    glutMainLoop();
    return 0;
}

Build message:
49: glutDisplayFunc(display(0));
49: error, invalid use of void expression
Why?

Also, the square ins’t well shaped :eek:

Well the display callback does not accept any parameters i.e it has to be defined like this,


void Display() { //or void Display(void) {
}                //   } 

For doing what u want, just store t as a global variable i.e. declare and define it on top like this,


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

//make t global
int t=10;

void display(){
   //do whatever using t
}

Also, the square ins’t well shaped

Try this,


glBegin(GL_QUADS);
   glVertex2f(0.2, 0.2); glVertex2f(0.3, 0.2);
   glVertex2f(0.3, 0.3); glVertex2f(0.2, 0.3);

   glVertex2f(0.3, 0.3); glVertex2f(0.4, 0.3);
   glVertex2f(0.4, 0.4); glVertex2f(0.3, 0.4); 
glEnd();

Thank you very much. Try to run this, cellular automaton rule 30:

#include <GL/gl.h>
#include <GL/glut.h>
#define BIANCO 1.0, 1.0, 1.0
#define NERO 0.0, 0.0, 0.0

void display(void){
    float n=500;
    int m=n;
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (BIANCO);
//    glBegin(GL_LINES);
// for(int i=1; i<n; i++){
// glVertex2f(0.0, float(i/n));
// glVertex2f(1.0, float(i/n));
// glVertex2f(float(i/n), 0.0);
// glVertex2f(float(i/n), 1.0);}
//    glEnd();

     int value[m][m];
 for(int i=0; i<m; i++){
 for(int j=0; j<m; j++)
 value[i][j]=0;}
 value[0][(m)/2]=1;

 for(int i=0; i<(m-1); i++){
 for(int j=0; j<(m-2); j++){
 if(value[i][j]==1 && value[i][j+1]==1 && value[i][j+2]==1)value[i+1][j+1]=0; else
 if(value[i][j]==1 && value[i][j+1]==1 && value[i][j+2]==0)value[i+1][j+1]=0; else
 if(value[i][j]==1 && value[i][j+1]==0 && value[i][j+2]==1)value[i+1][j+1]=0; else
 if(value[i][j]==1 && value[i][j+1]==0 && value[i][j+2]==0)value[i+1][j+1]=1; else
 if(value[i][j]==0 && value[i][j+1]==1 && value[i][j+2]==1)value[i+1][j+1]=1; else
 if(value[i][j]==0 && value[i][j+1]==1 && value[i][j+2]==0)value[i+1][j+1]=1; else
 if(value[i][j]==0 && value[i][j+1]==0 && value[i][j+2]==1)value[i+1][j+1]=1; else
 if(value[i][j]==0 && value[i][j+1]==0 && value[i][j+2]==0)value[i+1][j+1]=0;}
}
 int t[m][m];
 for(int i=0; i<m; i++){
 for(int j=0; j<m; j++){
 t[j][i]=value[i][j];}}

 for(int i=0; i<m; i++){
 for(int j=0; j<m; j++){
 if(t[i][j]==1)
 glBegin(GL_QUADS);
 glVertex2f(float(i/n), 1.0-float(j/n)); glVertex2f(float(i/n)+float(1.0/n), 1.0-float(j/n));
 glVertex2f(float(i/n)+float(1.0/n), 1.0-float(j/n)-float(1.0/n)); glVertex2f(float(i/n), 1.0-float(j/n)-float(1.0/n));
  glEnd();}}
  glFlush();
}

void init (void){
glClearColor (NERO, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("Rule 30");
    init ();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

Look at this. Conway’s game of life. Again, I can’t make it move. If I change t manually from 0 to 4 I can see the different patterns, can you explicitly tell me what to animate it?

#include <GL/gl.h>
#include <GL/glut.h>
#define NERO 0.0, 0.0, 0.0
#define VERDE 0.0, 1.0, 0.0

void display(){
    float n=10;
    int m=n;
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f(VERDE);
int a=5;
int b=n, c=n;

int v[a][b][c];
int s[a][b][c];

//inizializzo i vettori..............OK
for(int t=0; t<a; t++){
for(int i=0; i<b; i++){
for(int j=0; j<c; j++){
v[t][i][j]=s[t][i][j]=0;}}}

v[0][2][1]=1;
v[0][3][2]=1;
v[0][3][3]=1;
v[0][2][3]=1;
v[0][1][3]=1;

//eseguo la sommatoria del vicinato..OK
for(int t=0; t<a; t++){
for(int i=0; i<(b-2); i++){
for(int j=0; j<(c-2); j++){

for(int k=0; k<3; k++){
for(int l=0; l<3; l++){
s[t][i+1][j+1]+=v[t][i+k][j+l];}}
s[t][i+1][j+1]-=v[t][i+1][j+1];

//applico la regola
if(s[t][i][j]==2)v[t+1][i][j]=v[t][i][j]; else
if(s[t][i][j]==3)v[t+1][i][j]=1; else
v[t+1][i][j]=0;
}}}

int tr[a][b][c];
for(int t=0; t<m; t++){
for(int i=0; i<m; i++){
for(int j=0; j<m; j++){
tr[t][j][i]=v[t][i][j];}}}

 int t=0;
 for(int i=0; i<m; i++){
 for(int j=0; j<m; j++){
 if(v[t][i][j]==1)
 glBegin(GL_QUADS);
 glVertex2f(float(i/n), 1.0-float(j/n)); glVertex2f(float(i/n)+float(1.0/n), 1.0-float(j/n));
 glVertex2f(float(i/n)+float(1.0/n), 1.0-float(j/n)-float(1.0/n)); glVertex2f(float(i/n), 1.0-float(j/n)-float(1.0/n));
  glEnd();}}

    glColor3f (VERDE);
    glBegin(GL_LINES);
 for(int i=1; i<n; i++){
 glVertex2f(0.0, float(i/n));
 glVertex2f(1.0, float(i/n));
 glVertex2f(float(i/n), 0.0);
 glVertex2f(float(i/n), 1.0);}
    glEnd();
    glFlush();
}

void init (void){
glClearColor (NERO, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (".::Conway's Game of Life::. The Glider");
    init ();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

glut is event-driven, not sequential, so you have to adapt your display code to produce a single image for each call, and use glutPostRedisplay() to trigger the next display (either from the idle call back, or from a timer).

Use GLFW if you do not understand what this means.

Too complicated. I used #include<windws.h> with Sleep(time in ms) to hold the output execution:

#include <GL/gl.h>
#include <GL/glut.h>
#define NERO 0.0, 0.0, 0.0
#define VERDE 0.0, 1.0, 0.0
#include <windows.h>

void display(int t){
    float n=9;
    int m=n;
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f(VERDE);
int a=6;
int b=n, c=n;

int v[a][b][c];
int s[a][b][c];

//inizializzo i vettori..............OK
for(int t=0; t<a; t++){
for(int i=0; i<b; i++){
for(int j=0; j<c; j++){
v[t][i][j]=s[t][i][j]=0;}}}

v[0][2][1]=1;
v[0][3][2]=1;
v[0][3][3]=1;
v[0][2][3]=1;
v[0][1][3]=1;

//eseguo la sommatoria del vicinato..OK
for(int t=0; t<a; t++){
for(int i=0; i<(b-2); i++){
for(int j=0; j<(c-2); j++){

for(int k=0; k<3; k++){
for(int l=0; l<3; l++){
s[t][i+1][j+1]+=v[t][i+k][j+l];}}
s[t][i+1][j+1]-=v[t][i+1][j+1];

//applico la regola
if(s[t][i][j]==2)v[t+1][i][j]=v[t][i][j]; else
if(s[t][i][j]==3)v[t+1][i][j]=1; else
v[t+1][i][j]=0;
}}}

int tr[a][b][c];
for(int t=0; t<m; t++){
for(int i=0; i<m; i++){
for(int j=0; j<m; j++){
tr[t][j][i]=v[t][i][j];}}}

 for(int i=0; i<m; i++){
 for(int j=0; j<m; j++){
 if(v[t][i][j]==1)
 glBegin(GL_QUADS);
 glVertex2f(float(i/n), 1.0-float(j/n)); glVertex2f(float(i/n)+float(1.0/n), 1.0-float(j/n));
 glVertex2f(float(i/n)+float(1.0/n), 1.0-float(j/n)-float(1.0/n)); glVertex2f(float(i/n), 1.0-float(j/n)-float(1.0/n));
  glEnd();}}

    glColor3f (VERDE);
    glBegin(GL_LINES);
 for(int i=1; i<n; i++){
 glVertex2f(0.0, float(i/n));
 glVertex2f(1.0, float(i/n));
 glVertex2f(float(i/n), 0.0);
 glVertex2f(float(i/n), 1.0);}
    glEnd();
    glFlush();
    t++;
    if(t<=a){
        Sleep(500);
        display(t);}
    //else display(0);
}

void init (void){
glClearColor (NERO, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

void vai(){
init();
display(0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (250, 250);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (".::Conway's Game of Life::. The Glider");
    glutDisplayFunc(vai);
    glutMainLoop();
    return 0;
}

Is there a way to close automatically the window with a command like if(true)CloseWindow(); ?

But there’s a strange problem. If I put there, in the main part of the last program, b && c > 12 the output gives me always v[t][i][j]==0.

#include<iostream>
using namespace std;

int main(){
int a=6;
int b=15;
int c=15;

int v[a][b][c];
int s[a][b][c];

//inizializzo i vettori..............OK
for(int t=0; t<a; t++){
for(int i=0; i<b; i++){
for(int j=0; j<c; j++){
v[t][i][j]=s[t][i][j]=0;}}}

v[0][2][1]=1;
v[0][3][2]=1;
v[0][3][3]=1;
v[0][2][3]=1;
v[0][1][3]=1;

//eseguo la sommatoria del vicinato..OK
for(int t=0; t<a; t++){
for(int i=0; i<(b-2); i++){
for(int j=0; j<(c-2); j++){

for(int k=0; k<3; k++){
for(int l=0; l<3; l++){
s[t][i+1][j+1]+=v[t][i+k][j+l];}}
s[t][i+1][j+1]-=v[t][i+1][j+1];

//applico la regola
if(s[t][i][j]==2)v[t+1][i][j]=v[t][i][j]; else
if(s[t][i][j]==3)v[t+1][i][j]=1; else
v[t+1][i][j]=0;

}}}

//stampo a schermo...................OK
for(int t=0; t<a; t++){
for(int i=0; i<b; i++){
for(int j=0; j<c; j++){
if(v[t][i][j]==1)
cout<<"*"; else
cout<<"0";}
cout<<"
";}
cout<<"

";}

return 0;
}

Is there any logical problem?