I am attempting to learn openGL, and I am trying to create a square that can move around the window.
When I attempt to translate the square, it disapears, can someone tell me what I am doing wrong?
Written in C++ on Ubuntu Linux
test.cpp
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <time.h>
#include "tile.h"
clock_t clk;
tile block[3][3];
void polygon(int X, int Y, int W, int H)
{
/* draw a polygon from a list of vetices */
printf("%d %d %d %d \n", X, Y, X+W, Y+H);
glBegin(GL_POINTS);
glColor3f(1.0,0.0,0.0);
glVertex2d(X,Y);
glVertex2d(X+W,Y);
glVertex2d(X+W,Y+H);
glVertex2d(X,Y+H);
glEnd();
}
void createObjects()
{
/* map vertices to faces */
polygon(block[0][0].getX(),block[0][0].getY(),block[0][0].getWidth(),block[0][0].getHeight());
//polygon(block[0][1].getX(),block[0][1].getY(),block[0][1].getWidth(),block[0][1].getHeight());
//polygon(block[0][2].getX(),block[0][2].getY(),block[0][2].getWidth(),block[0][2].getHeight());
//polygon(block[1][0].getX(),block[1][0].getY(),block[1][0].getWidth(),block[1][0].getHeight());
//polygon(block[1][1].getX(),block[1][1].getY(),block[1][1].getWidth(),block[1][1].getHeight());
//polygon(block[1][2].getX(),block[1][2].getY(),block[1][2].getWidth(),block[1][2].getHeight());
//polygon(block[2][0].getX(),block[2][0].getY(),block[2][0].getWidth(),block[2][0].getHeight());
//polygon(block[2][1].getX(),block[2][1].getY(),block[2][1].getWidth(),block[2][1].getHeight());
//polygon(block[2][2].getX(),block[2][2].getY(),block[2][2].getWidth(),block[2][2].getHeight());
printf("**\n");
printf("**\n");
block[0][0].setX(block[0][0].getX()+1);
}
void display()
{
/* display callback, clear frame buffer and Z buffer, rotate cube and draw
* swap buffers */
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslated(block[0][0].getX(),block[0][0].getY(),0.0);
createObjects();
glFlush();
glutSwapBuffers();
}
void idle()
{
/*display */
clk = clock () + 0.5 * CLOCKS_PER_SEC ;
while (clock() < clk) {}
glutPostRedisplay();
}
void mouse(int btn, int state, int x, int y)
{
/*mouse callBack, selects and axis on which to rotate */
}
void myReshape(int w, int h)
{
glViewport(0,0,50,30);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.0,2.0,-2.0 * (GLfloat) h / (GLfloat) w,
2.0 * (GLfloat) w / (GLfloat) h,-10.0,10.0);
else
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0,10.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
//get values for all of the tiles
int i=0,p=0,curX=0,curY=0,TWidth=50,THeight=50;
for (p=0;p<3;p++)
{
for (i=0;i<3;i++)
{
block.setX(curX);
block.setY(curY);
block.setWidth(TWidth);
block.setHeight(THeight);
printf("i=%d p=%d %d %d %d %d \n",i,p, block.getX(),block.getY(),block.getWidth(),block.getHeight());
curX = curX+TWidth+5;
}
curX=50;
curY= curY+THeight+3;
}
printf("%f %f %f",block[0][0].getRf(),block[0][0].getGf(),block[0][0].getBf());
for (p=0;p<3;p++)
{
for (i=0;i<3;i++)
{
printf("i=%d p=%d %d %d %d %d \n",i,p, block.getX(),block.getY(),block.getWidth(),block.getHeight());
printf("\n");
}
}
/* need both double buffering and Z buffer */
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("createObjects");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(idle);
glutMouseFunc(mouse);
glEnable(GL_DEPTH_TEST); /*enable hidden surface removal */
glEnable(GL_TEXTURE_2D);
glutMainLoop();
}
tile.h
#ifndef TILE_H_
#define TILE_H_
class tile
{
int x,y,width,height;
float color[3];
bool blocked;
int id;
public:
tile();
tile(int X, int Y, int W, int H);
void setX(int X);
void setY(int Y);
void setWidth(int H);
void setHeight(int W);
int getX();
int getY();
int getWidth();
int getHeight();
float getRf();
float getGf();
float getBf();
virtual ~tile();
};
#endif /*TILE_H_*/
tile.cpp
#include "tile.h"
tile::tile()
{
x=0;
y=0;
width=0;
height=0;
blocked=false;
id=0;
color[0]=1.0;
color[1]=0.0;
color[2]=0.0;
}
tile::tile(int X, int Y, int W, int H)
{
blocked=false;
id=0;
color[0]=1.0;
color[1]=0.0;
color[2]=0.0;
x=X;
y=Y;
width=W;
height=H;
}
tile::~tile()
{
}
void tile::setX(int X)
{
x=X;
}
void tile::setY(int Y)
{
y=Y;
}
void tile::setWidth(int W)
{
width=W;
}
void tile::setHeight(int H)
{
height=H;
}
int tile::getX()
{
return x;
}
int tile::getY()
{
return y;
}
int tile::getWidth()
{
return width;
}
int tile::getHeight()
{
return height;
}
float tile::getRf()
{
return color[0];
}
float tile::getGf()
{
return color[1];
}
float tile::getBf()
{
return color[2];
}