Making Data Structures for a rectangle and square in opengl, c++

So this makes an output of the head (a square), and the leg (rectangle. How do I make my points into a data structure in the shortest yet simplest way possible? Our professor explained data structures yet I still get confused a lot. I would like to get a better grasp in this. Thank you. :)


#include <vector>
#include <time.h>

using namespace std;

#include "Glut_Setup.h"

float
head1x= -0.5, head1y=3, head1z=0,
head2x= 0.5, head2y=3, head2z=0,
head3x= 0.5, head3y=2, head3z=0,
head4x= -0.5, head4y=2, head4z=0;


float
leg1x=-0.6, leg1y=-2, leg1z=0,
leg2x=-0.4, leg2y=-2, leg2z=0,
leg3x=-0.4, leg3y=-3, leg3z=0,
leg4x=-0.6, leg4y=-3, leg4z=0;











void GameScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(leg1x, leg1y, leg1z);
glVertex3f(leg2x, leg2y, leg2z);
glVertex3f(leg3x, leg3y, leg3z);
glVertex3f(leg4x, leg4y, leg4z);
glEnd();



glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(head1x, head1y, head1z);
glVertex3f(head2x, head2y, head2z);
glVertex3f(head3x, head3y, head3z);
glVertex3f(head4x, head4y, head4z);
glEnd();


glutSwapBuffers();


}

void Keys(unsigned char key, int x, int y)
{
switch(key)
{
case 27 : exit(0); break;
case 'a':
{

head1x=head1x-1;
head2x=head2x-1;
head3x=head3x-1;
head4x=head4x-1;


leg1x=leg1x+1;
leg2x=leg2x+1;
leg3x=leg3x+1;
leg4x=leg4x+1;




break;

}

case 'd':
{

head1x=head1x+1;
head2x=head2x+1;
head3x=head3x+1;
head4x=head4x+1;

leg1x=leg1x-1;
leg2x=leg2x-1;
leg3x=leg3x-1;
leg4x=leg4x-1;


break;
}
}
}

void SpecialKeys(int key, int x, int y)
{
switch(key)
{


}
}

void Timer(int value)
{
glutTimerFunc(50, Timer, value);
glutPostRedisplay();
}

void changeSize(int w, int h)
{
float ratio = 1.0 * w / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45, ratio, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 30.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void main(int argc, char **argv)
{
srand(time(NULL));
GLUTInit(&argc, argv);
}
First, declare your struct:

1
2
3
4
struct Vertex
{
   float x, y, z;
};


Then use it to declare and initialise an array of the type of your declared struct:

1
2
3
4
5
Vertex head[4] = 
   {{-0.5, 3.0, 0.0},
   {0.5, 3.0, 0.0},
   {0.5, 2.0, 0.0},
   {-0.5, 2.0, 0.0}};


HTH
Thank you! :) Except, may I know what the [4] is for? :) And also, will I have to modify the:

glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(head1x, head1y, head1z);
glVertex3f(head2x, head2y, head2z);
glVertex3f(head3x, head3y, head3z);
glVertex3f(head4x, head4y, head4z);
glEnd();


part of the code in order to give my vertex head a color? :) Thank you.
Last edited on

Except, may I know what the [4] is for


4 Vertex's in the array, an array of 4.

If you want, you could leave it as:

1
2
3
4
5
Vertex head[] = 
   {{-0.5, 3.0, 0.0},
   {0.5, 3.0, 0.0},
   {0.5, 2.0, 0.0},
   {-0.5, 2.0, 0.0}};


Which will still declare an array of 4 because you have 4 sets of data.
Last edited on
Thank you very much!!!! :) I finally get it now. :) Cheers!
If you want to increment/decrement a variable by 1, you can use:

1
2
x++;
y--;


Instead of:

 
x = x +1;


So, to do the same with your struct instances:

1
2
3
head[0].x++;
head[1].y--;
// etc... 
Last edited on
Where do I put:

head[0].x++;
head[1].y--;

in this code? :/

struct Vertex
{
float x, y, z;
};

Vertex head[4] =
{{-0.5, 3.0, 0.0},
{0.5, 3.0, 0.0},
{0.5, 2.0, 0.0},
{-0.5, 2.0, 0.0}};
You would declare a function to do that, passing your array and the number of items in the array:

1
2
3
4
5
6
7
8
9
10
void foo(Vertex v[], const int count)
{
   glBegin(GL_QUADS);
   glColor3f(1.0, 0.0, 0.0);
   for (int n = 0; n < count; ++n)
   {
      glVertex3(v[n].x, v[n].y, v[n].z);
   }
   glEnd();
}


? Hang on a minute, you deleted your last question to which I just responded!!!
Last edited on
I see. Thank you. :) Oops sorry about that I thought you didn't see the question so I deleted it. :/ Thank you very much! You explain it MUCH cleearer then our programming professor. :/

By the way, I've tried inserting the code above and this was the result:

void GameScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



void foo(Vertex v[], const int count)
{ <---- it gets a red underline
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 0.0);
for (int n = 0; n < count; ++n)
{
glVertex3(v[n].x, v[n].y, v[n].z);
}
glEnd();
}


glutSwapBuffers();
}


the { part at the top has this red underline under it. :/
Last edited on
the { part at the top has this red underline under it. :/


Because you're trying to define a function inside another function. That's not legal C++.

Also, please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Does this help you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
struct Vertex
{
   float x, y, z;
};

struct MyColor
{
   float c1, c2, c3;
};

void GameScene(Vertex v[], const int count, MyColor& c)
{
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glBegin(GL_QUADS);
   glColor3f(c.c1, c.c2, c.c3);

   for (int n = 0; n < count; ++n)
      glVertex3f(v[n].x, v[n].y, v[n].z);

   glEnd();
   glutSwapBuffers();	// does this need to be called outside of this function?
}

void Keys(unsigned char key, int x, int y, 
	Vertex* head, const int head_count, MyColor& c1,
	Vertex* leg, const int leg_count, MyColor& c2)
{
   switch(key)
   {
      case 27 : exit(0); break;
      case 'a': 
      {
         for (int n = 0; n < head_count; ++n)
            (*head++).x--;

         for (int n = 0; n < leg_count; ++n)
            (*leg++).x++;

         break;
      }

      case 'd':
      {
         for (int n = 0; n < head_count; ++n)
            (*head++).x++;

         for (int n = 0; n < leg_count; ++n)
            (*leg++).x--;

         break;
      }
   }
}

void SpecialKeys(int key, int x, int y)
{
   switch(key)
   {


   }
}

void Timer(int value)
{
   glutTimerFunc(50, Timer, value);
   glutPostRedisplay();
}

void changeSize(int w, int h)
{
   float ratio = 1.0 * w / h;
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glViewport(0, 0, w, h);
   gluPerspective(45, ratio, 1, 1000);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   gluLookAt(0.0, 0.0, 30.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void main(int argc, char **argv)
{
   srand(time(NULL));
   GLUTInit(&argc, argv);

   MyColor head_color = {1.0, 1.0, 1.0};
   MyColor leg_color = {1.0, 0.0, 0.0};

   Vertex head[] = 
   {{-0.5, 3.0, 0.0},
   {0.5, 3.0, 0.0},
   {0.5, 2.0, 0.0},
   {-0.5, 2.0, 0.0}};

   Vertex leg[] = 
   {{-0.6, -2.0, 0.0},
   {-0.4, -2.0, 0.0},
   {-0.4, -3.0, 0.0},
   {-0.6, -3.0, 0.0}};

   GameScene(head, sizeof(head)/sizeof(Vertex), head_color);
   GameScene(leg, sizeof(leg)/sizeof(Vertex), leg_color);

   // etc...
}


Personally, I would encapsulate the Color and Vertex into a single Struct but thought it would confuse you at the moment.
Last edited on
Topic archived. No new replies allowed.