Trying to project a cube to an specific position.

Hi!

with the code below, Im trying to create the OpenGL space that appears at this image:

  [http://aycu39.webshots.com/image/4118/2005800707481452997_rs.jpg](http://aycu39.webshots.com/image/4118/2005800707481452997_rs.jpg)    

As you see, I have a cube (20x20x20) in the middle of the OpenGL space, and I want to project its P1, P2, P3 and P4 coordinates to the points (75,25) (25,25) (75,75) (75,25) respectively.

In general, a 3D world-point is projected to a 2D image point by a 3x4 projection Matrix. See here the relation between the matrices:

  [http://img100.imageshack.us/img100/6710/matrizcu0.th.png](http://img100.imageshack.us/img100/6710/matrizcu0.th.png)      

And the equations from that relation in my case are these:

x1 = p11 * X1 + p12 * Y1 + p13 * Z1 + p14
y1 = p21 * X1 + p22 * Y1 + p23 * Z1 + p24

x2 = p11 * X2 + p12 * Y2 + p13 * Z2 + p14
y2 = p21 * X2 + p22 * Y2 + p23 * Z2 + p24

x3 = p11 * X3 + p12 * Y3 + p13 * Z3 + p14
y3 = p21 * X3 + p22 * Y3 + p23 * Z3 + p24

x4 = p11 * X4 + p12 * Y4 + p13 * Z4 + p14
y4 = p21 * X4 + p22 * Y4 + p23 * Z4 + p24


75 = p11 * 10 + p12 * (-10) + p13 * (-10) + p14
25 = p21 * 10 + p22 * (-10) + p23 * (-10) + p24

25 = p11 * (-10) + p12 *(-10) + p13 * 10 + p14
25 = p21 * (-10) + p22 *(-10) + p23 * 10 + p24

75 = p11 * 10 + p12 * 10 + p13 * 10 + p14
75 = p21 * 10 + p22 * 10 + p23 * 10 + p24

75 = p11 * 10 + p12 * (-10) + p13 * 10 + p14
25 = p21 * 10 + p22 * (-10) + p23 * 10 + p24

And the results, as you see in the code, are these:

p11=2.5 p12=0.0 p13=0.0 p14=50.0

p21=2.5 p22=0.0 p23=0.0 p24=50.0

So in my case, the projection matrix will be this:

2.5 0.0 0.0 50.0
2.5 0.0 0.0 50.0
0.0 0.0 0.0 1.0

and then I load this matrix as the projection matrix but the cube is not projected at the place i want.

Does anybody know why?

#include "stdafx.h"
#include "GL/glut.h"
#include "GL/glaux.h"
#include "GL/glu.h"
#include "stdio.h"




void RenderScene (void)
{
	

	
    // Borra la ventana con el color de borrado actual
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    
	//Establecemos el color del cuadrado que queremos dibujar.
	glColor3f(0.0f, 0.0f, 1.0f);
	
	glPushMatrix();


	glScalef(3.0f, 3.0f, 3.0f);


	glBegin(GL_QUADS);		// Draw The Cube Using quads
    glColor3f(0.0f,1.0f,0.0f);	// Color Blue
    glVertex3f( 10.0f, 10.0f,-10.0f);	// Top Right Of The Quad (Top)
    glVertex3f(-10.0f, 10.0f,-10.0f);	// Top Left Of The Quad (Top)
    glVertex3f(-10.0f, 10.0f, 10.0f);	// Bottom Left Of The Quad (Top)
    glVertex3f( 10.0f, 10.0f, 10.0f);	// Bottom Right Of The Quad (Top)
    glColor3f(1.0f,0.5f,0.0f);	// Color Orange
    glVertex3f( 10.0f,-10.0f, 10.0f);	// Top Right Of The Quad (Bottom)
    glVertex3f(-10.0f,-10.0f, 10.0f);	// Top Left Of The Quad (Bottom)
    glVertex3f(-10.0f,-10.0f,-10.0f);	// Bottom Left Of The Quad (Bottom)
    glVertex3f( 10.0f,-10.0f,-10.0f);	// Bottom Right Of The Quad (Bottom)
    glColor3f(1.0f,0.0f,0.0f);	// Color Red	
    glVertex3f( 10.0f, 10.0f, 10.0f);	// Top Right Of The Quad (Front)
    glVertex3f(-10.0f, 10.0f, 10.0f);	// Top Left Of The Quad (Front)
    glVertex3f(-10.0f,-10.0f, 10.0f);	// Bottom Left Of The Quad (Front)
    glVertex3f( 10.0f,-10.0f, 10.0f);	// Bottom Right Of The Quad (Front)
    glColor3f(1.0f,1.0f,0.0f);	// Color Yellow
    glVertex3f( 10.0f,-10.0f,-10.0f);	// Top Right Of The Quad (Back)
    glVertex3f(-10.0f,-10.0f,-10.0f);	// Top Left Of The Quad (Back)
    glVertex3f(-10.0f, 10.0f,-10.0f);	// Bottom Left Of The Quad (Back)
    glVertex3f( 10.0f, 10.0f,-10.0f);	// Bottom Right Of The Quad (Back)
    glColor3f(0.0f,0.0f,1.0f);	// Color Blue
    glVertex3f(-10.0f, 10.0f, 10.0f);	// Top Right Of The Quad (Left)
    glVertex3f(-10.0f, 10.0f,-10.0f);	// Top Left Of The Quad (Left)
    glVertex3f(-10.0f,-10.0f,-10.0f);	// Bottom Left Of The Quad (Left)
    glVertex3f(-10.0f,-10.0f, 10.0f);	// Bottom Right Of The Quad (Left)
    glColor3f(1.0f,0.0f,1.0f);	// Color Violet
    glVertex3f( 10.0f, 10.0f,-10.0f);	// Top Right Of The Quad (Right)
    glVertex3f( 10.0f, 10.0f, 10.0f);	// Top Left Of The Quad (Right)
    glVertex3f( 10.0f,-10.0f, 10.0f);	// Bottom Left Of The Quad (Right)
    glVertex3f( 10.0f,-10.0f,-10.0f);	// Bottom Right Of The Quad (Right)
  glEnd();		

	glPopMatrix();

	
	//Hace el intercambio de buffers (obligatorio en animaciones) 
	//Mira mas abajo "glutInitDisplayMode"
    glutSwapBuffers();


}


void SetupRC(void)
{

	GLubyte *pImage;
	GLint iWidth;
	
	//Establece el color azul como color para borrar la ventana.
	glClearColor(0.0f,0.0f,1.0f,1.0f);

	// Targa's are 1 byte aligned
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);


}


///////////////////////////////////////////////////////////
// Called by GLUT library when the window has chanaged size
void ChangeSize(int w, int h)
	
	{
	

	GLfloat aspectRatio;

	// Prevent a divide by zero
	if(h == 0)
		h = 1;
	
	
	// Set Viewport to window dimensions
	// PAGINA 87 LIBRO
    glViewport(0, 0, w, h);


	// Reestablece el sistema de coordenadas antes de que se ejecute ninguna manipulacion de matr
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();



	// Establecer el volumen de recorte (clipping volumen)
	// (PLANOS izquierdo, derecho, inferior, superior, delantero (o cercano (VENTANA)), trasero (o lejano (DENTRO DEL MONITOR ;))))
	
	aspectRatio = (GLfloat)w / (GLfloat)h;
    if (w <= h) 	

		glOrtho (-100.0, 100.0,	-100 / aspectRatio,	100.0 / aspectRatio, 200.0,	-200.0);
    
	else 
		
		glOrtho (-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 200.0, -200.0);

	GLfloat *projection;

	
	GLfloat array[16]={2.5f, 2.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 50.0f, 50.0f, 1.0f};


	projection=array;
	
	glLoadMatrixf(projection);


	glMatrixMode(GL_MODELVIEW);

	
	glLoadIdentity();
	

	}


void main (void)
{

	
	/*
	InitDisplayMode: el modo de presentacion (display) que se usará al crear la ventana.
	GLUT_SINGLE: usaremos una ventana de un solo buffer, pero en la mayoria del resto de 
	los casos (siempre que se requiera animacion) usaremos el doble buffer.
	GLUT_RGB: modo de color que usaremos. (RGBA=RGB)
	*/
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
	

	//Tamaño de la ventana
	glutInitWindowSize(320, 240);


	//Crea la ventana y le da un titulo
	glutCreateWindow("Simple");
	
	
	//OJO: RenderScene aparece arriba como funcion
	glutDisplayFunc(RenderScene);

	
	//ChangeSize se ejecuta cuando la ventana es cambiada de tamaño y la primera vez
	//que le aplicacion es ejecuta
	glutReshapeFunc(ChangeSize);


	//OJO: RenderScene esta empalmada con otra funcion (mirar arriba)
	//Establece el color de borrado de la pantalla pero no la borra.	
	SetupRC();

	
	//Inicia la estructura principal de GLUT
	glutMainLoop();

	

}

I’m sorry, but what is it you’re trying to do?

You can project a cube using a projection matrix. Is that what you mean?

I want to project a cube to a concrete coordinates. In this case, to these (75,25) (25,25) (75,75) (75,25).

Yes, using the projection matrix.

You mean like for a shadow projection? Like projection geometry onto a ground plane or something?

No, no shadows. Just the projection to the window where you can see your OpenGL space.