A problem with reflections.

Hi all!

I wrote a simple program which draws:

  • a rectangle R below semiplane XZ,
  • a mirror on the semiplane,
  • a reflection of rectangle R.

To make it look like a real reflection I have to use the stencil buffer. I downloaded sample code and read a lesson about it but my code (very similar to the downloaded one) doesn’t work.


#include <GL/glut.h>
#include "imageloader.h"

GLuint pict;

void handleKeypress(unsigned char key, int x,int y) {
	switch(key) {
		case 27: exit(0);
	}
}

GLuint loadTexture(Image* image) {
	GLuint textureId;
	glGenTextures(1, &textureId);
	glBindTexture(GL_TEXTURE_2D, textureId);
	glTexImage2D(GL_TEXTURE_2D,
			0,
			GL_RGB,
			image->width, image->height,
			0,
			GL_RGB,
			GL_UNSIGNED_BYTE,
			image->pixels);
	return textureId;
}

void handleResize(int w, int h) {
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, double(w)/double(h), 1.0, 200.0);
}

void initRendering() {
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_NORMALIZE);
	glEnable(GL_COLOR_MATERIAL);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	Image* image = loadBMP("pict.bmp");
	pict = loadTexture(image);
	delete image;
}

const float WYS = 5.0f;
const float X=5.0f, Y=5.0f;
const float MIR=20.0f;

void drawMirror() {
	glBegin(GL_QUADS);
		glNormal3f(0, 1, 0);
		glVertex3f(-MIR/2, 0, MIR/2);
		glVertex3f(-MIR/2, 0, -MIR/2);
		glVertex3f(MIR/2, 0, -MIR/2);
		glVertex3f(MIR/2, 0, MIR/2);
	glEnd();
}

void drawPict() {
	glBegin(GL_QUADS);
		glNormal3f(0, 0, 1);
		glVertex3f(0, 0, 0);
		glVertex3f(X, 0, 0);
		glVertex3f(X, Y, 0);
		glVertex3f(0, Y, 0);
	glEnd();
}

void drawScene() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glTranslatef(0.0f, 0.0f, -40.0f);
	glRotatef(30.0f, 1.0f, 0.0f, 0.0f);

	GLfloat ambientLight[] = {1.3f, 1.3f, 1.3f, 1.0f};
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);

	glPushMatrix();
	glTranslatef(0.0f, WYS, 0.0f);
	drawPict();
	glPopMatrix();

	glEnable(GL_STENCIL_TEST);
	glColorMask(0, 0, 0, 0);
	glDisable(GL_DEPTH_TEST);
	glStencilFunc(GL_ALWAYS, 1, 1);
	glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

	drawMirror();

	glColorMask(1, 1, 1, 1);
	glEnable(GL_DEPTH_TEST);

	glStencilFunc(GL_EQUAL, 1, 1);
	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

	glPushMatrix();
	glScalef(1, -1, 1);
	glTranslatef(0, WYS, 0);
	drawPict();
	glPopMatrix();

	glDisable(GL_STENCIL_TEST);

	glEnable(GL_BLEND);
	glColor4f(1, 1, 1, 0.7f);
	drawMirror();
	glDisable(GL_BLEND);

	glutSwapBuffers();
}

int main(int argc, char **argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(400, 400);

	glutCreateWindow("Kolejny warzywny prezent... :)");
	initRendering();

	glutDisplayFunc(drawScene);
	glutKeyboardFunc(handleKeypress);
	glutReshapeFunc(handleResize);

	glutMainLoop();
	return 0;
}

“imageloader.h” is a header file not releated to my problem. I even don’t use any function declared in it.

Any suggestions how to improve my code?

I forgot do add | GLUT_STENCIL in glutInitDisplayMode()…

Thanks :stuck_out_tongue:

What exactly does not ‘work’.
To actually get someone to answer your post, you need to be as specific as possible and include as much relevant information as possible without having to make people wade through papges and pages of information.
This is just good manners so you are not wasting other people’s time - asking for help with just ‘it does not work’ is a bit pointless because what does not work? Perhaps a screen shot of the problem along with some explaination?