Camera aligned/anchored quads - how can it be done?

Started by
11 comments, last by idinev 15 years, 9 months ago
Quote:Original post by Lord_Evil
stuff


Thanks for the reply

I actually got it to work in a different way.

Here is the code snippet:

void RenderCameraAlignedQuads(void){	float modelview_matrix[16];	float *inverse_modelview = (float*) malloc(4*4*sizeof(float));			glPushMatrix();	glGetFloatv(GL_MODELVIEW_MATRIX, modelview_matrix);	m4_inverse(inverse_modelview, modelview_matrix);	glMultMatrixf(inverse_modelview);	for(int i = 0; i < no_quads; i++)	{		glPushMatrix();		glTranslatef(0.0,0.0,-i*10.0);		glBegin(GL_QUADS);			glColor3f(0.5,0.5,0.4);				glVertex3f(-1.0,-1.0,0.0);			glColor3f(0.5,0.5,0.4);				glVertex3f(1.0,-1.0,0.0);			glColor3f(0.5,0.5,0.4);				glVertex3f(1.0,1.0,0.0);			glColor3f(0.5,0.5,0.4);				glVertex3f(-1.0,1.0,0.0);		glEnd();		glPopMatrix();	}        glPopMatrix();        free(inverse_modelview);}


Now what i need is to scale each quad so that when transformed to clip-space, it fills up the screen and i am done :)
Advertisement
Quote:Original post by shodanjr_gr
glGetFloatv(GL_MODELVIEW_MATRIX, modelview_matrix);
m4_inverse(inverse_modelview, modelview_matrix);
glMultMatrixf(inverse_modelview);

What you do here is to load the modelview matrix, calculate the inverse and multiply the current matrix with its inverse. The result will be (or should be due to possible precision errors) the identity matrix.

So replace those 3 lines with glIdentity(); for more efficiency.


Edit: what you do is to render quads in the center of the screen and just move them further away from the camera. So you do exactly what I asked you earlier: "If I understand your initial question correctly, do you want to have quads that always have the same size and position on the screen, independent of the camera?" ;)

Just keep in mind that the first quad would never be rendered if perspective projection was applied since you'd not have a near clipping plane at 0.0.

Edit2: if you want each quad to fill the screen, just don't apply any projection matrix. Clip space goes from 1- to 1, so your quads already have unprojected full-screen size. But you should rethink the z-values then.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
If it's about full-screen postprocessing, simply don't transform in your vertex-shader:

C++:
void ulDrawFullScreenQuad2D(){	glDisable(GL_DEPTH_TEST);	glBegin(GL_QUADS);		glTexCoord2f(0,0); glVertex2f(-1,-1);		glTexCoord2f(1,0); glVertex2f( 1,-1);		glTexCoord2f(1,1); glVertex2f( 1, 1);		glTexCoord2f(0,1); glVertex2f(-1, 1);	glEnd();	glEnable(GL_DEPTH_TEST);}


shader:
#include "system.h"varying vec2 coord0;#if IS_VERTEXvoid main(){	gl_Position=gl_Vertex;	coord0=gl_MultiTexCoord0.xy;}#endif#if IS_FRAGMENTuniform sampler2D tex0;void main(){	gl_FragColor = texture2D(tex0,coord0);	}#endif


you can then also use the transformation matrices for some varying parameter to pass to the frag-shader, if you'll be making a view-dependent effect. It's the vertex-position transformation headaches that we took care of easily and flawlessly with the above base code.

This topic is closed to new replies.

Advertisement