I have a grid with cubes and i want to fill the cubes with two different colors and with 3 different images(.bmp). So every cube is filled with colours or images. I wrote a code and when i press the <> button the grid is filled with 5 different colours. Can anyone tell me how can i change my code to fill the grid with 3 different images(randomly)? Here is my code:
void randomFilling();
void myprint();
struct square{
int v1x, v1y;
int v2x, v2y;
int v3x, v3y;
int v4x, v4y;
int color;
};
struct square Squares[12][15];
struct flags{
int b;
}flags;
void drawScene();
void findVerticesPosition(){
int counter=0;
for(int i=0; i<600; i+=40){
for(int j=0; j<480; j+=40){
Squares[j/40][i/40].v1x = i;
Squares[j/40][i/40].v1y = j;
Squares[j/40][i/40].v2x = i;
Squares[j/40][i/40].v2y = j+40;
Squares[j/40][i/40].v3x = i+40;
Squares[j/40][i/40].v3y = j+40;
Squares[j/40][i/40].v4x = i+40;
Squares[j/40][i/40].v4y = j;
}
}
for(int i=0; i<12; i++){
for(int j=0; j<15; j++){
Squares[i][j].color = rand()%5 + 1;
}
}
}
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
case 98: //b
randomFilling();
flags.b = 1;
drawScene();
}
}
void randomFilling(){
int randomNumber;
srand(time(NULL));
//randomNumber = rand() % 5 + 1;
for(int i=0; i<12; i++){
for(int j=0; j<15; j++){
randomNumber = Squares[i][j].color;
if(randomNumber == 1){
glBegin(GL_QUADS);
glColor3f(1.0,0.9,0.1);
glVertex2f(Squares[i][j].v1x, Squares[i][j].v1y);
glVertex2f(Squares[i][j].v2x, Squares[i][j].v2y);
glVertex2f(Squares[i][j].v3x, Squares[i][j].v3y);
glVertex2f(Squares[i][j].v4x, Squares[i][j].v4y);
glEnd();
}
else if(randomNumber == 2){
glBegin(GL_QUADS);
glColor3f(1.0,0.0,0.1);
glVertex2f(Squares[i][j].v1x, Squares[i][j].v1y);
glVertex2f(Squares[i][j].v2x, Squares[i][j].v2y);
glVertex2f(Squares[i][j].v3x, Squares[i][j].v3y);
glVertex2f(Squares[i][j].v4x, Squares[i][j].v4y);
glEnd();
}
else if(randomNumber == 3){
glBegin(GL_QUADS);
glColor3f(0,0.9,0.1);
glVertex2f(Squares[i][j].v1x, Squares[i][j].v1y);
glVertex2f(Squares[i][j].v2x, Squares[i][j].v2y);
glVertex2f(Squares[i][j].v3x, Squares[i][j].v3y);
glVertex2f(Squares[i][j].v4x, Squares[i][j].v4y);
glEnd();
}
else if(randomNumber == 4){
glBegin(GL_QUADS);
glColor3f(0,0,1);
glVertex2f(Squares[i][j].v1x, Squares[i][j].v1y);
glVertex2f(Squares[i][j].v2x, Squares[i][j].v2y);
glVertex2f(Squares[i][j].v3x, Squares[i][j].v3y);
glVertex2f(Squares[i][j].v4x, Squares[i][j].v4y);
glEnd();
}
else if(randomNumber == 5){
glBegin(GL_QUADS);
glColor3f(0,0.9,1);
glVertex2f(Squares[i][j].v1x, Squares[i][j].v1y);
glVertex2f(Squares[i][j].v2x, Squares[i][j].v2y);
glVertex2f(Squares[i][j].v3x, Squares[i][j].v3y);
glVertex2f(Squares[i][j].v4x, Squares[i][j].v4y);
glEnd();
}
else{
printf("WTF\n");
}
}
}
}
//Initialize OpenGL
void init(void) {
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,600.0,-60.0,480.0);
flags.b=0;
}
void drawScene(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
//glPointSize(3.0);
if(flags.b==1){
randomFilling(); //------------
}
glColor3f(0,0,0);
glBegin(GL_LINES);
for(int i = 0; i <= 600; i += 40)
{
glVertex2f((float)i, 0.0f);
glVertex2f((float)i, 480.0f);
glVertex2f(0.0f, (float)i);
glVertex2f(600.0f, (float)i);
}
glEnd();
glFlush();
}
int main(int argc, char**argv) {
findVerticesPosition();
//myprint();
glutInit(&argc, argv);
glutInitWindowPosition(300,80);
glutInitWindowSize(600,540);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("BraXaPsa II");
init();
glutDisplayFunc(drawScene);
//glutDisplayFunc(randomFilling);
glutKeyboardFunc(handleKeypress);
glutMainLoop();
}
Here I post code to display images(textures) instead of colors.
The result will be like:
The two essential parts is to load textures to memory and specifying texture coordinates to primitives for proper texture wrapping.
Texture loading process is displayed here.
And texture coord maping process.
As you cannot load tga bitmaps directly. I post code to deal with this problem.