Need Help on polygon creations!!

I am new to OpenGL and C and I need to create a 2d game that has about 24 moving squares with two different colors. It took me forever to create just one moving square but I am having a difficult time creating more than one! Does anyone have any hints or idea, or maybe can give me sum kinda structure to work off of? If i can create at least two…I can build off of that.

Heres what i have so far:


[#include <stdio.h>
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include <time.h>
#include <stdlib.h>

//Prototypes
void RenderScene(void);
void TimerFunction(int);
void myInit(void);
void ChangeSize(GLsizei, GLsizei);
void myKey(unsigned char, int, int);
void createThing();

GLfloat createThings[4]; 

//Array of clolors
GLfloat RED[3] = {1,0,0};
GLfloat YELLOW[3] = {1,1,0};
GLfloat PURPLE[3] = {1,0,1};
GLfloat BLACK[3] = {0,0,0};

// Initial square position and size
GLfloat xpos = 100.0f;
GLfloat ypos = 150.0f;
GLsizei rsize = 50;

// size of square
GLfloat x1 = 0.1;
GLfloat y1 = 0.1;

// Step size in x and y directions
// (number of pixels to move each time)
GLfloat xstep = 1.0f;
GLfloat ystep = 1.0f;

// Keep track of windows changing width and height
GLfloat windowWidth;
GLfloat windowHeight;


void createThing ()
{
    
    // Draw a filled rectangle with current color
    glRectf(xpos, ypos, xpos+rsize, ypos+rsize);
    
    glBegin(GL_QUADS);
    
    // Set current drawing color to yellow 
    glColor3fv(YELLOW);
    glVertex2f(-x1, y1);
    glVertex2f(x1, y1);
    glVertex2f(x1, -y1);
    glVertex2f(-x1, -y1);
    
    glEnd();
   
}
// Called to draw scene
void RenderScene()
{
    
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    createThing();
    
    
    // Flush drawing commands
    glutSwapBuffers();
}

void myKey(unsigned char key, int x, int y)
{
    if ((key == 'Q') || (key == 'q')) {
        exit(0);
    }
}


// Called by GLUT library when idle (window not being
// resized or moved)
void TimerFunction(int value)
{
    // Reverse direction when you reach left or right edge
    if(xpos > windowWidth-rsize || xpos < 0)
        xstep = -xstep;
    
    // Reverse direction when you reach top or bottom edge
    if(ypos > windowHeight-rsize || ypos < 0)
        ystep = -ystep;
    
    // Check bounds.  This is incase the window is made
    // smaller and the rectangle is outside the new
    // clipping volume
    if(xpos > windowWidth-rsize)
        xpos = windowWidth-rsize-1;
    
    if(ypos > windowHeight-rsize)
        ypos = windowHeight-rsize-1;
    
    // Actually move the square
    xpos += xstep;
    ypos += ystep;
    
    // Redraw the scene with new coordinates
    glutPostRedisplay();
    glutTimerFunc(8,TimerFunction, 1);
}


// Setup the rendering state
void myInit(void)
{
    // Set clear color to black
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}


// Called by GLUT library when the window has chanaged size
void ChangeSize(GLsizei w, GLsizei h)
{
    // Prevent a divide by zero
    if(h == 0)
        h = 1;
    
    // Set Viewport to window dimensions
    glViewport(0, 0, w, h);
    
    // Reset coordinate system
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
    // Keep the square square, this time, save calculated
    // width and height for later use
    if (w <= h) {
        windowHeight = 400.0f*h/w;
        windowWidth = 400.0f;
    }
    else {
        windowWidth = 400.0f*w/h;
        windowHeight = 400.0f;
    }
    
    // Set the clipping volume
    gluOrtho2D(0.0f, windowWidth, 0.0f, windowHeight);
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}


// Main program entry point
int main(int argc, char** argv)
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInit(&argc, argv);
    glutInitWindowSize(windowWidth, windowHeight);
    glutCreateWindow("Bounce Beeatch");
    glutDisplayFunc(RenderScene);
    glutReshapeFunc(ChangeSize);
    glutKeyboardFunc(myKey);
    glutTimerFunc(8, TimerFunction, 1);
    myInit();
    
    glutMainLoop();
    return 0;
}
]

Firstly, I don’t think your attachment name is very appropriate - do you? You may have some fustrations, but don’t let that get the better of your judgement.

To render a peice of geometry multiple time, you could encapsulate the verticies in a Display list (I’m assuming OpenGL compatibility context). This is great as rendering is just 1 function all.

so your code


glBegin(GL_QUADS);

// Set current drawing color to yellow 
glColor3fv(YELLOW);
glVertex2f(-x1, y1);
glVertex2f(x1, y1);
glVertex2f(x1, -y1);
glVertex2f(-x1, -y1);
glEnd();

becomes …


Square_DL := glGenLists(1);
glNewList(Square_DL, GL_COMPILE);
glBegin(GL_QUADS);
glVertex2f(-x1, y1);
glVertex2f(x1, y1);
glVertex2f(x1, -y1);
glVertex2f(-x1, -y1);
glEnd();
glEndList;

…perhaps turn that code into a function or something. The x,y paramters just need to define the size of the square relative to the origin, not it’s fixed world space position.

To poisition this square and countless others is now trivial.
in a loop (depending upon how many to draw), lookup the color and position in an array and perform the following:


for loop...
  glColor4fv (@squares_colors[n]);
  glpushmatrix;
  glTranslatef (squares_Positions[n].x,squares_Positions[n].y,squares_Positions[n].z);
  glCallList (Square_DL);
  glPopMatrix;
end loop

So this loop is:
Setting the colour of each vertex
preserving the current camera (aka the GL_MODELVIEWMATRIX)
positioning each square object into view by the camera (building an internal world-to-camera matrix)
sending the geometry to the GL
restoring the current camera.

That should do it.

Sorry if I offended you about the name, I guess I was seeing red and not the title. But thank you for your help! I really appreciate it! :slight_smile:

I couldn’t give a #@£! about these kinds of things personally. But, you do have to remember this is a public forum and you should always try to respect others who may take offence.
Lesson learnt? OK, preaching over now.

lesson learnt…and ur right!