views:

174

answers:

2

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.

+1  A: 

Yes, you can use a glTranslate multiple times. This will construct a new translate matrix, and multiply it onto your current matrix.

Some thoughts:

Your initCT function looks redundant.

gluOrtho2D is usually used to modify the projection matrix. Try something like this:

....
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100.0, 400.0, -400.0, 400.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
....

Usually, one starts their drawing by resetting their modelview matrix to identity. It's possible that glut does this for you, but in case that's not happening, try this:

....
glClear (GL_COLOR_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
drawHexagon();
....
Daniel Yankowsky
You say that his initCT function is redundant, then recommend that he do what's in the initCT?
Stobor
He switches into modelview and loads identity twice. I added a switch to projection and a loadIdentity, moved one modelview loadIdentity down, and suggested that he remove the other. That's all.
Daniel Yankowsky
Fair enough... That makes sense.
Stobor
Thank you and Thank You, I will take your observations into account and fix the application accordingly.
ThorDivDev
+2  A: 

You're translating behind the camera; Each translation goes up in the y axis by 20, and in the z axis by 1. After the second step, your hexagons are behind the camera, and can't be seen in the viewport.

Try

glTranslated(0.0f,20.0f,0.0f);

for each translation; that should help.

Stobor