So I've just started playing around with OpenGL, and decided to make a little voxel render thing. I'm trying to light it, but weird effects happen when I rotate the camera around the y axis. For example, when I first spawn in, the light looks like this:
http://i.stack.imgur.com/zQ49y.png
But when I rotate around a bit I get this:
http://i.stack.imgur.com/PWvVo.png
Here's the code that does this stuff:
glRotatef(xrot, 1.0f, 0.0f, 0.0);
glRotatef(yrot, 0.0f, 1.0f, 0.0);
glTranslatef(-3.5f-xcam, ycam, -3.5f-zcam);
glEnable (GL_DEPTH_TEST); //enable the depth testing
glEnable (GL_LIGHTING); //enable the lighting
glEnable (GL_LIGHT0);
GLfloat specular[] = {1.0f-xcam, ycam+1.0, 1.0f-zcam, 1.0};
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, grnd);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glVertex3f(0, 0, 0);
glTexCoord2i(0, 1);
glVertex3f(0, 0, 7.0f);
glTexCoord2i(1, 1);
glVertex3f(7.0f, 0, 7.0f);
glTexCoord2i(1, 0);
glVertex3f(7.0f, 0, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
glLoadIdentity();
drawVoxel(2.f, 0.f, 2.f, tex);
drawVoxel(1.f, 0.f, 1.f, tex);
drawVoxel(2.f, 0.f, 2.f, tex);
drawVoxel(3.f, 0.f, 3.f, tex);
drawVoxel(4.f, 0.f, 4.f, tree);
drawVoxel(4.f, 1.f, 4.f, tree);
drawVoxel(4.f, 2.f, 4.f, tree);
drawVoxel(4.f, 3.f, 4.f, tree);
test.Flip();
Does anybody have any idea what's going on?
You also need to use glNormal3f().
.
.
.
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glNormal3f(0,1,0);
glVertex3f(0, 0, 0);
glTexCoord2i(0, 1);
glNormal3f(0,1,0);
glVertex3f(0, 0, 7.0f);
glTexCoord2i(1, 1);
glNormal3f(0,1,0);
glVertex3f(7.0f, 0, 7.0f);
glTexCoord2i(1, 0);
glNormal3f(0,1,0);
glVertex3f(7.0f, 0, 0);
glEnd();
Related
I just started working on opengl in qt, using texture i am displaying image in quard.
then i set gltexture wrap mode to gl_repeat but its not repeating.
I tried even gl_clamp_to_edge still its not working.
images size 256*256
code:
GLuint _textureId; //The id of the texture
int _wdth;
int _hight;
void LoadGLTextures( const char * name )
{
QImage img;
if(!img.load(name)){
qDebug() << "ERROR in loading image";
}
QImage t = QGLWidget::convertToGLFormat(img);
_wdth = t.width();
_hight = t.height();
qDebug()<<"width = "<<_wdth<<"height ="<<_hight;
glGenTextures(1, &_textureId);
glBindTexture(GL_TEXTURE_2D, _textureId);
glTexImage2D(GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits());
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
}
void GLWin::initializeGL()
{
qglColor(Qt::black);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_MULTISAMPLE);
static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
///cameraPos = 0;
glEnable(GL_TEXTURE_2D);
LoadGLTextures("resources/Green_Dragon.jpeg");
}
void GLWin::paintGL()
{
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f);
glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glColor3f(0.5, 0.5, 0);
glBindTexture(GL_TEXTURE_2D, _textureId);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.5f, -0.5f); // vertex 1
glTexCoord2f(1.0f, 0.0f); glVertex2f(0.5f, -0.5f); // vertex 2
glTexCoord2f(1.0f, 1.0f); glVertex2f(0.5f, 0.5f); // vertex 3
glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.5f, 0.5f); // vertex 4
glEnd();
glDisable(GL_TEXTURE_2D);
glFlush();
}
void GLWin::resizeGL(int width, int height)
{
glViewport(0, 0, (GLint)width, (GLint)height);
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity();
glOrtho(-0.5, 0.5, -0.5, 0.5, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.5f, -0.5f); // vertex 1
glTexCoord2f(1.0f, 0.0f); glVertex2f(0.5f, -0.5f); // vertex 2
glTexCoord2f(1.0f, 1.0f); glVertex2f(0.5f, 0.5f); // vertex 3
glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.5f, 0.5f); // vertex 4
If those are your texture coordinates, then it's not going to repeat. Texture wrap modes only apply when the texture coordinate exceeds the [0, 1] range. Since your texture coordinates are within that range, no repeating will happen.
You see scaling of the texture because you are scaling the positions of the triangles. And therefore, the texture will be mapped in accord with those scaled positions.
You could use the texture matrix to do some transformations on the texture coordinates if you want to scale them.
I am trying something quite easy, normally: applying a texture on the different surfaces of a cube.
I am able to apply it but it seems as if he just takes an average of the colors of my image.
why please?
my code:
void MyGLWidget::drawCube()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// glPushMatrix();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0.5, 0, 0.0);
glRotatef(getCubeAngle(), 0.0f, 1.0f, 0.0f);
glTranslatef(0, 0, 0);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
//back
glVertex3f(-0.1, 0.1,-0.1 );//upper left corner
glVertex3f(0.1, 0.1,-0.1); //uper right
glVertex3f(0.1,-0.1,-0.1 ); // down left
glVertex3f(-0.1,-0.1,-0.1); // down right
/* other code to create rest of the cube*/
glEnd();
glFlush();
// glPopMatrix();
}
void MyGLWidget::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glScalef(height *1./width, 1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void MyGLWidget::myTextureMapping()
{
QImage t;
QImage b;
if(!b.load("..../myImage.bmp"))
{qDebug("error with image\n");}
t = QGLWidget::convertToGLFormat(b);
glGenTextures( 1, &texture[0] );
glBindTexture( GL_TEXTURE_2D, texture[0] );
glTexImage2D( GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits() );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
}
void MyGLWidget::initializeGL()
{
myTextureMapping();
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
EDIT:
added those tex coordinates:
glTexCoord2f(-0.1, 0.1);
glVertex3f(-0.1, 0.1,0 );//upper left corner
glTexCoord2f(0.1, 0.1);
glVertex3f(0.1, 0.1,0); //uper right
glTexCoord2f(0.1, -0.1);
glVertex3f(0.1,-0.1,0 ); // down left
glTexCoord2f(-0.1, -0.1);
glVertex3f(-0.1,-0.1,0); // down right
But my image is bigger than the face of my cube:
source image : http://imgur.com/h48QARM
result in software: http://imgur.com/rxvK0Ot
You should be providing the texture co-ordinates for each vertex. What you have right now is just a position data for the Quad, texture co-ordinates are missing.
Have a look at this :
OpenGL Textured Cube Not Being Mapped Correctly
Try this :
glTexCoord2f(0, 0);
glVertex3f(-0.1, 0.1,0 );//upper left corner
glTexCoord2f(1, 0);
glVertex3f(0.1, 0.1,0); //uper right
glTexCoord2f(0, 1);
glVertex3f(-0.1,-0.1,0 ); // down left
glTexCoord2f(1, 1);
glVertex3f(0.1,-0.1,0); // down right
Isn't the texture coordinates wrong? To me it seems like you're going -0.1 to 0.1, while texture coordinates normally are defined in the interval [0,1].
I have this cube and I want to cover each side of it with a different image in which a letter appears.
My problem is that cube is covered by the same image, as you can see in following image.
but I want that letter A appears on top, B on left side and C on right side.
in order to convert/transform images (letters A,B,C) in textures I use this code:
glGenTextures(1, textureA);
glGenTextures(1, textureB);
glGenTextures(1, textureC);
glClearColor (0.0, 0.0, 0.0, 0.0);
if (flagA ==1){
cvtColor(imageA, imageA, CV_BGR2RGB);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, textureA);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//glTexImage2D(GL_TEXTURE_2D, 0, 4,image.cols, image.rows, 0, GL_RGB,GL_UNSIGNED_BYTE, image.data);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, imageA.cols, imageA.rows, GL_RGB, GL_UNSIGNED_BYTE, imageA.data);
}
if (flagC ==1){
cvtColor(imageC, imageC, CV_BGR2RGB);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, textureC);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//glTexImage2D(GL_TEXTURE_2D, 0, 4,image.cols, image.rows, 0, GL_RGB,GL_UNSIGNED_BYTE, image.data);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, imageC.cols, imageC.rows, GL_RGB, GL_UNSIGNED_BYTE, imageC.data);
}
if (flagB ==1){
cvtColor(imageB, imageB, CV_BGR2RGB);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, textureB);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//glTexImage2D(GL_TEXTURE_2D, 0, 4,image.cols, image.rows, 0, GL_RGB,GL_UNSIGNED_BYTE, image.data);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, imageB.cols, imageB.rows, GL_RGB, GL_UNSIGNED_BYTE, imageB.data);
}
I noted that if I switch block if(flagB==1){...} with if(flagC==1){...} (in this way image with C letter is the last in process of image-texture transformation), my cube is covered only by C letters.
Why this strange behaviour?
In order to display the result, I use this code:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, WIDTH, HEIGHT, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(flagA==1){
glBindTexture(GL_TEXTURE_2D, textureA);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f((GLfloat)((A[3].x)),(GLfloat)(A[3].y));
glTexCoord2f(1.0f, 0.0f); glVertex2f((GLfloat)((A[0].x)),(GLfloat)(A[0].y));
glTexCoord2f(1.0f, 1.0f); glVertex2f((GLfloat)((A[1].x)),(GLfloat)(A[1].y));
glTexCoord2f(0.0f, 1.0f); glVertex2f((GLfloat)((A[2].x)),(GLfloat)(A[2].y));
glEnd();
}
if(flagB==1){
glBindTexture(GL_TEXTURE_2D, textureB);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f((GLfloat)((B[3].x)),(GLfloat)(B[3].y));
glTexCoord2f(1.0f, 0.0f); glVertex2f((GLfloat)((B[0].x)),(GLfloat)(B[0].y));
glTexCoord2f(1.0f, 1.0f); glVertex2f((GLfloat)((B[1].x)),(GLfloat)(B[1].y));
glTexCoord2f(0.0f, 1.0f); glVertex2f((GLfloat)((B[2].x)),(GLfloat)(B[2].y));
glEnd();
}
if(flagC==1){
glBindTexture(GL_TEXTURE_2D, textureC);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f((GLfloat)((C[3].x)),(GLfloat)(C[3].y));
glTexCoord2f(1.0f, 0.0f); glVertex2f((GLfloat)((C[0].x)),(GLfloat)(C[0].y));
glTexCoord2f(1.0f, 1.0f); glVertex2f((GLfloat)((C[1].x)),(GLfloat)(C[1].y));
glTexCoord2f(0.0f, 1.0f); glVertex2f((GLfloat)((C[2].x)),(GLfloat)(C[2].y));
glEnd();
}
I work on windows7 x64 and OpenGL.
We don't see your rendering code.
Did you write this somewhere to generate the texture ids ?
glGenTextures(1, &textureA);
glGenTextures(1, &textureB);
glGenTextures(1, &textureC);
Did you write this when you are rendering your cube ?
glBindTexture(GL_TEXTURE_2D, textureA);
// Draw the A face
glBindTexture(GL_TEXTURE_2D, textureB);
// Draw the B face
glBindTexture(GL_TEXTURE_2D, textureC);
// Draw the C face
What you are doing is:
glGenTextures(1, textureA);
glGenTextures(1, textureB);
glGenTextures(1, textureC);
It should be:
glGenTextures(1, &textureA);
glGenTextures(1, &textureB);
glGenTextures(1, &textureC);
I assume the problem happens because the variables textureA, textureB and textureC do not get valid values and therefore glBindTexture calls are doing nothing (although i think you should have got segmentation fault).
Essentially, I'm rendering a raw buffer of pixels onto a texture, which is wrapped onto a quad.
My GL initiation code:
void INITOGL(int xres, int yres){
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//gluOrtho2D(0, xres, 0, yres);
}
Render loop code:
void RenderCycle(){
register MSG msg = {0};
while(1){
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
glEnable(GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
//glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_LINEAR
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE/*GL_DECAL*/);
glTexImage2D( GL_TEXTURE_2D, 0, 3, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, framebuffer);//viewport virtual size
glClear(GL_COLOR_BUFFER_B
IT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0, 0, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f( 800, 0, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 800, 600, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f( 0, 600, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
SwapBuffers(hdc);
}
}
The resize function:
void ResizeViewport(int height, int width){
if(!height)height++;
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
//gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_PROJECTION);
glViewport(0,0,width,height);
//gluOrtho2D(0, width, 0, height);
glLoadIdentity();
}
The buffer that holds the RGB data is just set to red using a for loop, and this is the output.
Have I improperly set my viewport, not scaled something correctly, or am I just completely missing something? If you need any more information, just ask. I have given all of the opengl sided code, the rest shouldn't make a difference. In theory, it should render red to the entire screen.
To make my maze type game faster I decided to put my drawed ball inside a texture, because i have to draw it otherwise once for every room and I'm drawing it like a concave polygon using the stencil buffer, it takes more time than using a texture. The problem is, that I'm getting it inside a texture correctly from the back buffer when I'm rendering the third frame since the start of the game and my question is, why is it like so?
When I'm using a texture from the thirst frame, I'm having texture with solid white color, so it has nothing inside. When I'm using textures from the second frame, then I have only the black background of the desired texture and when I take the texture from the third frame, then I have desired texture. For frame count I use the static variable "done" inside the "drawTexture" function.
Copying from the first frame:
Copying from the second frame:
Copying from the third frame (desired outcome):
void DrawBall::drawTexture(float imageD) {
static int done = 0;
if (done < 3) {
drawToTexture(imageD);
done++;
}
glEnable(GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, texture);
glColor3f(1, 1, 1);
glBegin (GL_QUADS);
glTexCoord2f (0.0, 0.0); glVertex3f (0.0, 0.0, -imageD);
glTexCoord2f (1.0, 0.0); glVertex3f (5.0, 0.0, -imageD);
glTexCoord2f (1.0, 1.0); glVertex3f (5.0, 5.0, -imageD);
glTexCoord2f (0.0, 1.0); glVertex3f (0.0, 5.0, -imageD);
glEnd ();
glDisable(GL_TEXTURE_2D);
}
void DrawBall::drawToTexture(float imageD) {
int viewport[4];
glGetIntegerv(GL_VIEWPORT, (int*) viewport);
int textureWidth = 64;
int textureHeight = 64;
texture = genEmptyTexture(textureWidth, textureHeight);
glViewport(0, 0, textureWidth, textureHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 1, 1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/*
This function calculates the vertexes for the ball
inside a vector<vector<float>> variable "test"
*/
_calculateCircleVertexes(0.0f, 0.0f, -2.0f, 0.249f, &test, 20);
_displayBall(&test, 0.0f, 0.0f, 0.5f, -2.0f, &*smallBallColor);
glBindTexture(GL_TEXTURE_2D, texture);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, textureWidth, textureHeight, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)viewport[2] / (GLfloat)viewport[3], 1.0f, imageD + 10.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
GLuint DrawBall::genEmptyTexture(unsigned int width, unsigned int height) {
GLuint txtIndex;
glGenTextures(1, &txtIndex);
glBindTexture(GL_TEXTURE_2D, txtIndex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
return txtIndex;
}
void DrawBall::_displayBall(vector<vector<GLfloat>> *vertexes, GLfloat x, GLfloat y
, GLfloat imageW, GLfloat imageD, color *color) {
glTranslatef(x, y, imageD);
glClearStencil(0);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_NEVER, 0, 1);
glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT);
glBegin(GL_POLYGON);
vector<vector<GLfloat>>::iterator it = vertexes->begin();
for (; it != vertexes->end(); it++) {
glVertex3f((*it)[0], (*it)[1], 0.0f);
}
glEnd();
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
glColor3f(color->r, color->g, color->b);
glBegin(GL_QUADS);
glVertex3f(-(imageW / 2.0f), -(imageW / 2.0f), 0.0f);
glVertex3f( (imageW / 2.0f), -(imageW / 2.0f), 0.0f);
glVertex3f( (imageW / 2.0f), (imageW / 2.0f), 0.0f);
glVertex3f(-(imageW / 2.0f), (imageW / 2.0f), 0.0f);
glEnd();
glDisable(GL_STENCIL_TEST);
glTranslatef(x, y, -imageD);
}
You should not use the window framebuffer (which includes both back- and frontbuffer) for render to texture operations. It just breaks to easily (you've experienced it). Instead use a so called Framebuffer Object, with the texture as rendering target.
Well, Datenwolf, thank you for your suggestion, you are probably right but I just want to use the advanced stuff as less as possible and I found my mistakes. I didn't get the desired outcome before the second frame because I didn't have yet enabled stencil test. Before the first frame I didn't get the desired outcome because in the window creation Windows sends WM_SIZE message and I had the draw message inside it but at that time the OpenGL isn't set up properly yet.