This is a homework question I am sorry but I am lost. What happens is the following I am asked to do a tiling of hexagons. Like the grid map in many Risk games and Wild Arms XF. I understand the current transformation is the matrix that translates the points I give to OpenGL to screen coordinates and if you apply a transformation it moves the the center point usually (0,0) to wherever.
If you use glPushMatrix and glPopMatrix. What it does is make a replica of the current CT matrix and you do operations on it and when you pop it you return to the matrix without the transformation.
The problem is the following I am trying to draw a hexagon, Translate(Displace, I like it better), draw another Hexagon, translate and Draw the last hexagon. What happens is the third hexagon dissapears from the face of the viewport and I am only raising it twice. What is funny is that consecutive rotates and scales give me no problems.
so a small sample of the code.
#include <windows.h>
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <iostream>
#include <ctime>
using namespace std;
void initCT(void)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void drawHexagon()
{
glBegin(GL_LINE_STRIP);
glVertex2i(0,0);
glVertex2i(20, 0);
glVertex2i(25,10);
glVertex2i(20,20);
glVertex2i(0,20);
glVertex2i(-5,10);
glVertex2i(0,0);
glEnd();
}
void myInit(void)
{
initCT();
glClearColor(1.0f,1.0f,1.0f,0.0f);
glColor3f(0.0f, 0.0f, 0.0f); //set the drawin color
glPointSize(1.0); //a 'dot'is 4 by 4 pixel
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(-100.0, 400.0, -400.0, 400.0);
glViewport(0, 0, 640, 480);
}
void myDisplay (void) {
glClear (GL_COLOR_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW );
drawHexagon();
glTranslated(0.0f,20.0f,1.0f);
drawHexagon();
glTranslated(0.0f,20.0f,1.0f);
drawHexagon();
glTranslated(0.0f,20.0f,1.0f);
drawHexagon();
glFlush(); //send all output to display
}
void main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (640, 480);
glutInitWindowPosition (100, 150);
glutCreateWindow ("Hexagon Tiling");
glutDisplayFunc (myDisplay);
myInit();
glutMainLoop();
}
I have used this code with the similar result, only two hexagons draw the others go into the Hitchhikers guide to the galaxy. I changed the colors of each hexagon and only the first two appear.
THANK YOU GUYS YOU ARE THE MEN. I am sorry if giving the answer to one upsets the other but you both are right.
Also for people using Google this is from Computer Graphics using Open GL there is an error in the translation function that the author gives you in the translate it should be 0 in the Z axis.