   stencil operations
  Hi,I am doing some work with 3dtv and I need to interlace two images together. I found some examples online which use stencilling to achieve this. I tried to tailor the examples to my own program but I'm have some weird problem with co-ordinates or something. Only one line from the left image gets stencilled..I have a feeling it might have something to do with gluOrtho2d or something similar related to viewport co-ordinates..Here is my code (should compile using glut and SOIL). The important bits are in reshape and display functions.Also, the files I am using are d:/lena.bmp and d:/cell.bmp.. just change these to any two random pictures if you would like to see the problem.Thank you for any suggestions,#include <GL/glut.h>#include <GL/soil.h>#include <iostream>#include <string>void initialize();void visibility_callback(int);GLvoid idle_callback(GLvoid);GLvoid reshape_callback(int, int);GLvoid display_callback(GLvoid);void draw();int width, height;struct{GLuint left;GLuint right;} textures;GLuint load_texture(const char* filename){GLuint texture_ID;texture_ID = SOIL_load_OGL_texture(filename, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID,SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT | SOIL_FLAG_INVERT_Y);if (texture_ID > 0){glBindTexture(GL_TEXTURE_2D, texture_ID);return texture_ID;}else{return NULL;}}void initialize(){textures.left = load_texture("d:/lena.bmp");textures.right = load_texture("d:/cell.bmp");if (!textures.left || !textures.right){perror("Error loading texture\n");exit(1);}glClearColor(0.0,0.0,0.0,0.0);gluOrtho2D(0,width-1,0,height-1);glEnable(GL_TEXTURE_2D);// Enable Texture MappingglShadeModel(GL_SMOOTH);// Enable Smooth ShadingglClearColor(0.0f, 0.0f, 0.0f, 0.0f);// Black BackgroundglClearDepth(1.0f);// Depth Buffer SetupglEnable(GL_DEPTH_TEST);// Enables Depth TestingglDepthFunc(GL_LEQUAL);// The Type Of Depth Testing To DoglHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// Really Nice Perspective Calculations}void visibility_callback(int v){glutIdleFunc(idle_callback);}GLvoid idle_callback(GLvoid){glutPostRedisplay();}GLvoid reshape_callback(int w, int h){GLfloat aspect = (GLfloat)width / (GLfloat)height;h = (GLfloat)w / (GLfloat)width * (GLfloat)height;if (h == 0)h = 1.0f;width = w;height = h;glViewport(0,0,width,height);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,width-1,0.0,height-1);//glFrustum(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f);gluPerspective(40.0f,aspect,0.0f,1.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();// Clear and configure stencilglDrawBuffer(GL_BACK);glEnable(GL_STENCIL_TEST);glClearStencil(0);glClear(GL_STENCIL_BUFFER_BIT);glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);glDisable(GL_DEPTH_TEST);glStencilFunc(GL_ALWAYS,1,1);// Draw stencil patternglColor4f(1,1,1,0);for (GLint gliY = 0; gliY < height; gliY += 2){glLineWidth(1);glBegin(GL_LINES);glVertex2i(0,gliY);glVertex2i(width,gliY);glEnd();}glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);glFlush();}GLvoid display_callback(GLvoid){glEnable(GL_DEPTH_TEST);glEnable(GL_STENCIL_TEST);glDrawBuffer(GL_BACK);// draw into both back buffersglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// clear color and depth buffers// LEFT VIEW{glStencilFunc(GL_NOTEQUAL,1,1);// draws if stencil != 1glDrawBuffer(GL_BACK_LEFT);// draw into back left bufferglMatrixMode(GL_PROJECTION);glLoadIdentity();glMatrixMode(GL_MODELVIEW);glLoadIdentity();glPushMatrix();glTranslatef(0.0f,0.0f,0.0f);glBindTexture(GL_TEXTURE_2D, textures.left);glBegin(GL_QUADS);glTexCoord2i( 0.0f,  0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);glTexCoord2i( 1.0f,  0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);glTexCoord2i( 1.0f,  1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);glTexCoord2i( 0.0f,  1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);glEnd();glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);glPopMatrix();}// RIGHT VIEW{glStencilFunc(GL_EQUAL,1,1);// draws if stencil == 1glDrawBuffer(GL_BACK_RIGHT);//draw into back right bufferglMatrixMode(GL_PROJECTION);glLoadIdentity();glMatrixMode(GL_MODELVIEW);glLoadIdentity();glPushMatrix();glTranslatef(0.0f,0.0f,0.0f);glBindTexture(GL_TEXTURE_2D, textures.right);glBegin(GL_QUADS);glTexCoord2f( 0.0f,  0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);glTexCoord2f( 1.0f,  0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);glTexCoord2f( 1.0f,  1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);glTexCoord2f( 0.0f,  1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);glEnd();glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);}glutSwapBuffers();}int main(int argc, char** argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_RGBA | GLUT_STENCIL);width = 512;height = 512;glutInitWindowSize(width,height);glutInitWindowPosition(100,100);glutCreateWindow(argv[0]);initialize();glutVisibilityFunc(visibility_callback); glutDisplayFunc(display_callback);glutReshapeFunc(reshape_callback);glutMainLoop();return 0;}
  Here is another example, this time with just a single view and a square instead of images. Again, the interesting stuff is in display and reshape. Don't bother looking at anything else...What I should be seeing is a yellow square with every second line cut out -- a series of parallel yellow horizontal lines, 1 pixel thick. Instead, once again all that happens is that half a line gets cut out of the right hand side...I've spent most of the day trying to figure it out and I have no idea what the problem is..By the way, I should mention I've only been using OpenGL for two days. Thanks again,#include <stdlib.h>#include <GL/glut.h>#include <GL/soil.h>void display_callback();void reshape_callback(int, int);void visibility_callback(int);void idle_callback();void menu_callback(int);void keyboard_callback(unsigned char, int, int);int width, height;struct{GLuint left;GLuint right;} textures;GLuint load_texture(const char* filename){GLuint texture_ID;texture_ID = SOIL_load_OGL_texture(filename, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID,SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT | SOIL_FLAG_INVERT_Y);if (texture_ID > 0){glBindTexture(GL_TEXTURE_2D, texture_ID);return texture_ID;}else{return NULL;}}void display_callback(){glClear(GL_COLOR_BUFFER_BIT);glStencilFunc(GL_NOTEQUAL,1,1);glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0, 1.0, 0.0);glBegin(GL_POLYGON);glVertex2f(-0.5,-0.5);glVertex2f(-0.5, 0.5);glVertex2f( 0.5, 0.5);glVertex2f( 0.5,-0.5);glEnd();glutSwapBuffers();}void reshape_callback(int w, int h){GLdouble aspect;GLfloat gliY;if (h == 0)h = 1;width = w;height = h;aspect = (GLdouble)width / (GLdouble)height;glViewport(0,0,width,height);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glMatrixMode(GL_PROJECTION);glLoadIdentity();//gluOrtho2D(0.0,width-1,0.0,height-1);glMatrixMode(GL_MODELVIEW);glLoadIdentity();// Clear and configure stencilglDrawBuffer(GL_BACK);glEnable(GL_STENCIL_TEST);glClearStencil(0);glClear(GL_STENCIL_BUFFER_BIT);glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);glDisable(GL_DEPTH_TEST);glStencilFunc(GL_ALWAYS,1,1);// Draw stencil patternglColor4f(1,1,1,0);for (gliY = 0; gliY < h; gliY += 2){glLineWidth(1);glBegin(GL_LINES);glVertex2f(0,gliY);glVertex2f((GLfloat)w,gliY);glEnd();}glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);glFlush();}void visibility_callback(int v){glutIdleFunc(idle_callback);}void idle_callback(){glutPostRedisplay();}void menu_callback(int choice){switch (choice){case 2:glutPostRedisplay();break;case 1:glutPostRedisplay();break;}}void keyboard_callback(unsigned char c, int x, int y){switch (c){case VK_ESCAPE:exit(0);break;default:break;}}int main(int argc, char *argv[]){glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA |GLUT_STENCIL);width = 512;height = 512;glutInitWindowSize(width,height);glutInitWindowPosition(100,100);glutCreateWindow(*argv);textures.left = load_texture("d:/lena.bmp");textures.right = load_texture("d:/cell.bmp");if (!textures.left || !textures.right){perror("Error loading textures\n");exit(1);}glutDisplayFunc(display_callback);glutReshapeFunc(reshape_callback);glutVisibilityFunc(visibility_callback);glutCreateMenu(menu_callback);glutAddMenuEntry("one", 1);glutAddMenuEntry("two", 2);glutAttachMenu(GLUT_RIGHT_BUTTON);glutKeyboardFunc(keyboard_callback);glutMainLoop();return 0;}