openGL - testure mapping cube fails - c++

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].

Related

Texture is scaling instead of repeat in Qt

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.

Texture mapping with OpenGL and QT - C++

I'm having trouble getting my texture to map to a quad using OpenGL and Qt. I've looked at several other SO threads, but a lot of the function calls have to be used slightly differently for me to compile (Qt Verison 4.8.6). Here's my relevant code, right now all that happens is a window is displayed with a black background, but nothing else.
void LoadGLTextures( const char * name )
{
QImage img;
if(!img.load("resources/Green_Dragon.bmp")){
std::cerr << "ERROR in loading image" << std::endl;
}
QImage t = QGLWidget::convertToGLFormat(img);
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 );
glBindTexture( GL_TEXTURE_2D, 0 );
}
void GLWidget::initializeGL()
{
qglClearColor(qtBlack.dark());
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 GLWidget::paintGL()
{
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f);
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glShadeModel( GL_FLAT );
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glColor3f(0.5, 0.5, 0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.5f, 0.5f); // vertex 1
glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.5f, -0.5f); // vertex 2
glTexCoord2f(1.0f, 0.0f); glVertex2f(0.5f, -0.5f); // vertex 3
glTexCoord2f(1.0f, 1.0f); glVertex2f(0.5f, 0.5f); // vertex 4
glEnd();
glDisable(GL_TEXTURE_2D);
glFlush();
}
Looking at your comment, the answer is fairly clear now, sorry that I missed it in the question comments.
You are seeing a black screen because you are enabling depth testing, but you are not clearing the depth buffer between frames. Therefore, the previous frame's depth buffer values remain in place, and the depth tests fail for all subsequent frames (note that the default depth function is GL_LESS).
You may leave depth testing enabled. The correct solution is to clear your depth buffer in addition to your color buffer before each render. You have:
glClear( GL_COLOR_BUFFER_BIT );
But you need:
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
See also: glClear()
Removing glEnable(GL_DEPTH_TEST) solved this for me, in case anyone runs into a similar problem.

Transparency Opengl+SDL not working C++

I've been trying to get transparency working with SDL+ OpenGL.
Here are my functions that initialize OpenGL and SDL, draw an image, and create a texture
void initGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( 0.0, Screen_Width,Screen_Height, 0.0, 1.0, -1.0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, Screen_Width, Screen_Height);
}
void initSDL()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
Bob=SDL_SetVideoMode(0, 0, 32, SDL_OPENGL);
Screen_Width= Bob->w;
Screen_Height= Bob->h;
SDL_WM_SetCaption("Project", NULL);;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
}
void Texture::draw(int x1, int y1,int x2, int y2,std::string filename)
{
glClear( GL_COLOR_BUFFER_BIT );
CreateTexture(filename.c_str());
glEnable( GL_TEXTURE_2D );
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, Tex);
glBegin(GL_QUADS);
glTexCoord2f(0.f, 0.f);
glVertex2f( 0.f, 0.f );
glTexCoord2f(1.f, 0.f);
glVertex2f( 1920.f, 0.f );
glTexCoord2f(1.f, 1.f);
glVertex2f( 1920.f, 1080.f );
glTexCoord2f(0.f, 1.f);
glVertex2f( 0.f, 1080.f );
glEnd();
glDisable(GL_TEXTURE_2D);
SDL_GL_SwapBuffers();
}
void Texture::CreateTexture(std::string filename)
{
SDL_Surface* image =NULL;
image=IMG_Load( filename.c_str() );
glClearColor( 0, 0, 0, 0 );
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glGenTextures(1, &Tex);
glBindTexture(GL_TEXTURE_2D, Tex);
int mode = GL_RGB;
if(image->format->BytesPerPixel == 4)
mode = GL_RGBA;
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->w, image->h, 0, mode, GL_UNSIGNED_BYTE, image- >pixels);
SDL_FreeSurface(image);
}
You can check the texture format is should be RGBA and you can test a white texture(RGB(255,255,255)) that has alpha channel of 128, so the whole texture is just white with alpha half white. Drawing this on a black background should result a half white texture being drawn.
Also the vertex positions might not be right, you could try something like this (with identity matrix for projection and world transform) to see if everything is in order with opengl setup
glTexCoord2f(0.f, 0.f);
glVertex2f( 0.25f, 0.25f );
glTexCoord2f(1.f, 0.f);
glVertex2f( 0.25f,0.5f );
glTexCoord2f(1.f, 1.f);
glVertex2f( 0.5f,0.5f );
glTexCoord2f(0.f, 1.f);
glVertex2f( 0.5f,0.25f );
Hope this helps.
Razvan.
You're drawing exactly one quad, with one texture. What exactly do you expect to be visible in the translucent parts? The background color?
Or your desktop, or the windows beneath? That takes quite some additional work.
First you need a compositor running (on Windows this requires Windows Vista with Aero or later).
And the window must be configured in a way, that its alpha channel is actually considered when compositing; AFAIK SDL doesn't take those preparations.

How is Texture Splatting in OpenGL implemented?

I have been reading for a while the different techniques used to texture terrains and came across texture splatting. I have found a lot of articles that discuss how to do this in OpenGL, but most only discuss it theoretically and provide little to no code that I can study. Does anyone know/have some code that illustrates this in OpenGL?
Just to clarify, I want to be able to load four different textures, and based on the height of the quad/vertices, change the texture from one gradually to the next.
Edit: Below is a quick bit of code to help show what I want to know
#include <windows.h>
#include <SFML/Graphics.hpp>
#include <gl/gl.h>
#include <gl/glu.h>
#define GL_CLAMP_TO_EDGE 0x812F
class Scene {
public:
void resize( int w, int h ) {
// OpenGL Reshape
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 120.0, (GLdouble)w/(GLdouble)h, 0.5, 500.0 );
glMatrixMode( GL_MODELVIEW );
}
};
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Test");
///Setup the scene, materials, lighting
Scene scene;
scene.resize(800,600);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glColorMaterial(GL_FRONT_AND_BACK, GL_EMISSION);
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LIGHT0);
float XL = .5, YL = .1, ZL = 1;
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat lightpos[] = {XL, YL, ZL, 0.};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
///Test terrain texture splatting
///Load the textures
sf::Image tex1;
tex1.loadFromFile("texture1.png");
sf::Image tex2;
tex2.loadFromFile("texture2.png");
///Set the first texture
GLuint grass;
glGenTextures(1, &grass);
glBindTexture(GL_TEXTURE_2D, grass);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tex1.getSize().x, tex1.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)tex1.getPixelsPtr() );
///Set the second texture
GLuint dirt;
glGenTextures(1, &dirt);
glBindTexture(GL_TEXTURE_2D, dirt);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tex2.getSize().x, tex2.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)tex2.getPixelsPtr() );
///Start loop
while( window.isOpen() ) {
sf::Event event;
while( window.pollEvent( event ) ) {
if( event.type == sf::Event::Closed )
window.close();
}
///Clear buffer and set camera
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0, 1.0, 1.0, 50);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1, 0, 1, 0, 0, 0, 0, 1, 0);
///Begin rendering quad
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, grass);
///I know that around here I should enable blending in order to get my two textures to mix, but I am not certain
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(-0.5, -0.5, 0.0);
glTexCoord2f(1, 0);
glVertex3f(-0.5, 0.5, 0.0);
glTexCoord2f(1, 1);
glVertex3f(0.5, 0.5, 0.0);
glTexCoord2f(0, 1);
glVertex3f(0.5, -0.5, 0.0);
glEnd();
///Reset env settings for SFML
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
window.display();
}
return 1;
}
As people mentioned above, use programmable pipeline, use shaders. In the fragment shader you can pass all the textures and interpolate between them based on vertex data you receive from the vertex shader.
Quick search gave me this result. I am sure that is what you need. Also take a look at this post. And this paper explains the technique very well.

Back buffer contents into a texture in OpenGL

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.