About the VIEWPORT matrix, how does it works?

Hi to all,

i have a code with this line to get the value of the VIEWPORT matrix:

glGetIntegerv

but when i print the values i only get this matrix:

[0]
[0]
[0]
[0]

First doubt:

I was waiting for finding some data about the size of the window i create (glViewport (0, 0, 200, 200)) but i dont see anything. Just 0’s…

Second doubt:

As you see, Im supposing the VIEWPORT matrix is a 4x1 matrix, is that true?

Any information about how VIEWPORT matrix is made will be wellcome.

First of all, there is no VIEWPORT matrix. Current viewport is stored as a set of 4 integers but it’s not a matrix.
Your code should look like this:

GLint array[4];
glGetIntegerv(GL_VIEWPORT, array);

Make sure you pass correct arguments to glGetInteger (you could have additional/missing & or * in your code) and that you print the correct array to screen.

This is my code:

glViewport(0, 0, 200, 200);	

	printf("Viewport

");
	
	GLint viewport[4];
	

	glGetIntegerv(GL_VIEWPORT, viewport);
	


	int k;
	
	for (k=0; k<4; k++)
	{
	printf("%f
", viewport[k]);
	}

but only prints

[0]
[0]
[0]
[0]

so no viewport values anywhere…

Whats the problem?

Do you have a valid context ?

Do you get any errors glGetError();

Pretty Basic question or … ?

this is the code. it is not well structured maybe :slight_smile: …sorry

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

typedef struct // Utilizamos esta estructura
{
GLubyte *imageData;
GLuint bpp;
GLuint width;
GLuint height;
GLuint texID;
} Imagen;
/*
---------------------
Carga_TGA
Carga un TGA en memoria. El TGA debe cumplir las siguientes características:
Ser de 24 bits + Canal ALPHA. (32 bits) y SIN COMPRIMIR
El tamaño debe ser cuadrado (x=y) y 32x32 o 64x64 o 128x128 o 256x256
Devuleve un puntero a la imagen y el tamaño (variable tam) de la imagen
-----------------------
*/
GLubyte *CargaTGA(char *filename,int *tam)
{
GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};

GLubyte TGAcompare[12];

GLubyte header[6];

GLuint bytesPerPixel;

GLuint imageSize;

GLuint temp,i;

GLuint type=GL_RGBA;

Imagen texture;

GLubyte *aux;

FILE *file = fopen(filename, "rb");

if (file == NULL)
printf("Error1");

/* Esto abre y comprueba que es un TGA */
fread(TGAcompare,1,sizeof(TGAcompare),file);
if (memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0)
printf("Its a TGA file");


/* Leemos la cabecera*/
fread(header,1,sizeof(header),file);


/* Determinamos el tamaño */
texture.width = header[1] * 256 + header[0];
texture.height = header[3] * 256 + header[2];


/* Vemos las características y comprobamos si son correctas*/
if( texture.width <=0 &#0124;&#0124;texture.height <=0 &#0124;&#0124;texture.width >256 &#0124;&#0124;texture.height !=texture.width &#0124;&#0124;( header[4]!=32))
{
fclose(file);
printf("Error - >  Las caracteristicas de la imagen no son las correctas.");
}


/* Calculamos la memoria que será necesaria */
texture.bpp = header[4];
bytesPerPixel = texture.bpp/8;
imageSize = texture.width*texture.height*bytesPerPixel;


/* Reservamos memoria */
texture.imageData=(GLubyte *)malloc(imageSize);


/* Cargamos y hacemos alguna comprobaciones */
if( texture.imageData==NULL &#0124;&#0124;
fread(texture.imageData, 1, imageSize, file)!=imageSize)
{
if(texture.imageData!=NULL)
free(texture.imageData);
fclose(file);
printf("Error6");
}


/* El TGA viene en formato BGR con, lo pasamos a RGB */
for(i=0; i<(GLuint)(imageSize); i+=bytesPerPixel)
{
temp=texture.imageData[i];
texture.imageData[i] = texture.imageData[i + 2];
texture.imageData[i + 2] = temp;
}
fclose (file);


/* Ahora, cambiamos el orden de las líneas, como si hiciesemosun flip vertical. */
aux=(GLubyte *)malloc(imageSize);
for(i=0; i<texture.height; i++)
memcpy(&aux[imageSize-((i+1)*texture.width*4)],&texture.imageData[i*texture.width*4],texture.width*4);


/* tam devolverá el tamaño */
*tam=texture.width;


/* Liberamos memoria */
free(texture.imageData);

/* Todo fue bien!*/
return aux;


}



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(1.0f, 0.0f, 0.0f);
	

	/*glBegin (GL_QUADS);
		
		glTexCoord2f(0.0f, 1.0f); 
		glVertex3f(-100.0f, -100.0f, 0.0f);


		glTexCoord2f(1.0f, 1.0f); 
		glVertex3f( 100.0f, -100.0f, 0.0f);

		glTexCoord2f(1.0f, 0.0f); 
		glVertex3f( 100.0f, 100.0f, 0.0f);


		glTexCoord2f(0.0f, 0.0f); 
		glVertex3f(-100.0f, 100.0f, 0.0f);

	
	

	glEnd();

	*/



	//glRotatef(45.0f, 4.0f, 2.0f, 6.0f);

	//Hace que el cubo tenga aspecto de alambre (wire)
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);




	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();		

	//glRectf(-1.0f, 1.0f, 1.0f, -1.0f);





	// Do the buffer Swap
    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);


    // Load the TGA file, get width, height, and component/format information
	pImage = CargaTGA("Sin titulo-1.tga", &iWidth);
	

    // Use Window coordinates to set raster position
	//glRasterPos2i(0, 0);
	

    // Escribe un bloque de pixels en el "frame buffer"
    //if(pImage != NULL)
    //    glDrawPixels(iWidth, iWidth, GL_RGBA, GL_UNSIGNED_BYTE, pImage);
	

	//Vinculamos la imagen a GL_TEXTURE_2D
	glTexImage2D(GL_TEXTURE_2D,
		0,        // Nivel de detalle de la imagen. Generalmente el 0
		3,        // Especifica el numero de componentes de color de la textura sin contar con el alpha?
		iWidth,   // Anchura de la imagen. SIEMPRE POTENCIA DE 2
		iWidth,   // Altura
		0,        // Numero de pixels que debe tener el borde la textura. Toma 0,1 o 2
		GL_RGBA,  // Formato de la imagen.
		GL_UNSIGNED_BYTE, // el tipo de datos en que va a ser pasada la informacion de la imagen cargada
		pImage    // Puntero que apunta a la imagen.
		);
	



	// Don't need the image data anymore
	free(pImage);
		


	////TEXTURE FILTERS
	////Cuando la imagen de la textura no se adecua con el tamaño de la superficie
	////que se quiere cubrir.
	////MIN_FILTER: cuando la superficie sea mayor que la imagen.
	////MAX_FILTER: cuando la superficie sea menor que la imagen.
    ////GL_LINEAR: en escaso se ha optado por una interpolacion lineal. Hay 6 mas.
	
	glTexParameteri(GL_TEXTURE_2D, 
		GL_TEXTURE_MIN_FILTER, 
		GL_LINEAR 
		);
		
	glTexParameteri(GL_TEXTURE_2D,
		GL_TEXTURE_MAG_FILTER,
		GL_LINEAR
		);



	////WRAP FILTERS
	////Cuando se pega la textura a una superficie debe indicarse la correspondecia
	////entre las coordenadas de la textura y los vertices de dicha superficie.

	glTexParameteri(GL_TEXTURE_2D, 
		GL_TEXTURE_WRAP_S,  // Se repite o se estira hacia el lado s? de la textura.
		GL_CLAMP         //La textura se puede estirar (GL_CLAMP) o se puede repetir (GL_REPEAT).
		);           


	glTexParameteri(GL_TEXTURE_2D, 
		GL_TEXTURE_WRAP_T,  // Se repite o se estira hacia el lado s? de la textura.
		GL_CLAMP    //La textura se puede estirar (GL_CLAMP) o se puede repetir (GL_REPEAT).
		);         






	////Una textura es un poster que adherimos al poligono, este poster lo podemos combinar con
	////el color del poligono. Podemos elegir 4 modos de combinar el color del poligono con el 
	////color de la textura.
	glTexEnvi(GL_TEXTURE_ENV,  //Obligatorio siempre.
		GL_TEXTURE_ENV_MODE,   //Al poner aqui GL_TEXTURE_ENV_MODE, la siguiente linea puede valer GL_DECAL, GL_REPLACE (usan el color de la textura, ignoran el del poligono), GL_MODULATE o GL_BLEND.
		GL_REPLACE   //
		);


	glEnable(GL_TEXTURE_2D);

	


}


///////////////////////////////////////////////////////////
// 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
    glViewport(0, 0, w, h);

	printf("View

");
	
	GLint viewport[4];
	

	glGetIntegerv(GL_VIEWPORT, viewport);
	


	int k;
	
	for (k=0; k<4; k++)
	{
	printf("%f
", viewport[k]);
	}

	printf("

");


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

	// Establish clipping volume (left, right, bottom, top, near, far)
	
	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);


	int j;

	float array2[16];

	glGetFloatv (GL_PROJECTION_MATRIX, array2);

	for (j=0; j<16; j++)
	{
	printf("%f
", array2[j]);
	}
	
	printf("

");

	
	glMatrixMode(GL_MODELVIEW);
	
	glLoadIdentity();
	
	glTranslatef(10.0f, 6.0f, 0.0f);

	//glRotatef(40.0f, 8.0f, 6.0f, 1.0f);

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


	//float array[16]={1, 0, 0, 3, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};

	int i;

	float array[16];

	glGetFloatv (GL_MODELVIEW_MATRIX, array);

	

	int x=40;
	int y=36;


	
	
	GLdouble modelview[16];
	
	GLdouble projection[16];

	GLfloat winX, winY, winZ;
	
	GLdouble posX, posY, posZ;


	glGetDoublev(GL_MODELVIEW_MATRIX, modelview);	


	for (i=0; i<16; i++)
	{
	printf("%f
", modelview[i]);
	glGetDoublev(GL_PROJECTION_MATRIX, projection);
	}


	printf("

");


	for (j=0; j<16; j++)
	{
	printf("%f
", modelview[j]);
	}
	



	printf("

");

	winX=(float)x;
	//winY=(float)y;
	winY=(float)viewport[3] - (float)y;
	
	
	glReadPixels(x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

	//gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

	printf("%f", winZ);
	
	//printf ("%f %f %f", posX, posY, posZ);	
	
	
	


	
	//glMultMatrixf(array);




	}


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);
	
	glutInitWindowSize(200, 200);

	glutInitWindowPosition(250, 250);

	glutCreateWindow("Simple");
	
	
//OJO: RenderScene aparece arriba como funcion
	glutDisplayFunc(RenderScene);

	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();

	

}

Hardly an advanced OpenGL question.
You shouldn’t printf() integer values with a format string %f for float, use %d for integers.

thanks, its true… :slight_smile: