I'm trying to render grass leaves. The simple way is to draw two parallel quads crossing in the middle like this
The problem is that the first quad to be rendered won't be transparent for the next grass leaf to be drawed. If I draw A then B, B won't show behind A
This is how i'm rendering
void drawHighGrass(){
glDisable(GL_LIGHTING);
glClearColor(1.0, 1.0, 1.0, 1.0);
glColor4f(1.0, 1.0, 1.0, 0.95);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texturas[HIGH_GRASS]);
glCullFace(GL_FRONT);
glPushMatrix();
//glTranslatef(1000, 0, 1000);
glScalef(1, 1.5, 1);
glPushMatrix();
glTranslatef(-100 / 2, -10, 0);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0, 0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(100, 0, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(100, 40, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0, 40, 0);
glEnd();
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(100, 0, 0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0, 0, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0, 40, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(100, 40, 0);
glEnd();
glPopMatrix();
glPushMatrix();
//glTranslatef(1000, 0, 1000);
glTranslatef(0, -10, -100 / 2);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0, 0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0, 0, 100);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0, 40, 100);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0, 40, 0);
glEnd();
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0, 100);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0, 0, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0, 40, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0, 40, 100);
glEnd();
glPopMatrix();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glEnable(GL_LIGHTING);
glCullFace(GL_BACK);
}
I found the solution:
glAlphaFunc(GL_GREATER, 0.5);
glEnable(GL_ALPHA_TEST);
Related
I'm experimenting with 3D graphics in OpenGL and I've managed to create a 3D level. However, I also want to display some 2D text over the screen that stays in place (similar to an HP or ammo stat that you'd see in a first person shooter). Here is the entire method that I am using to render the level. Everything works exactly as I want it to, except for the text (which does not appear on screen at all). How can I make the text appear properly?
void render_Scene(void)
{
int i;
int j;
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Camera perspective
gluLookAt(x, y, 1.0, x + lx, y + ly, 1.0, 0.0, 0.0, 1.0);
glColor3f(0.4, 0.4, 0.4);
//Draw walls around the level
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, wall_Texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-75, -75, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-75, -75, 1.2);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(75, -75, 1.2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(75, -75, 0);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-75, 72, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-75, 72, 1.2);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(75, 72, 1.2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(75, 72, 0);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-75, -75, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-75, 72, 0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-75, 72, 1.2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-75, -75, 1.2);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(75, -75, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(75, 72, 0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(75, 72, 1.2);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(75, -75, 1.2);
glEnd();
glDisable(GL_TEXTURE_2D);
//Draw ground texture
for (int m = -150; m <= 140; m += 10)
{
for (int n = -150; n <= 140; n += 10)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, ground_Texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(n, m, 0.0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(n, m + 10, 0.0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(n + 10, m + 10, 0.0);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(n + 10, m, 0.0);
glEnd();
glDisable(GL_TEXTURE_2D);
}
}
//Draw trees in level (uses a seperate method)
for (i = -9; i < 9; i++)
{
for (j = -9; j < 9; j++)
{
glPushMatrix();
glTranslatef(i*7.5, j*7.5, 0);
draw_Trees();
glPopMatrix();
}
}
//Create light
GLfloat lightColor0[] = { 1.0, 1.0f, 1.0f, 1.0f };
GLfloat lightPos0[] = { lx, ly, deltaMove, 1.0f };
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//Here's where I'm trying to render the text (I've heard that glPushAttrib and glPopAttrib may be important for this part) but it does not appear anywhere on the screen
glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
glPushAttrib(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glLoadIdentity();
glRasterPos2i(10, 30);
void * font = GLUT_BITMAP_8_BY_13;
for (string::iterator i = strings[stage].begin(); i != strings[stage].end(); ++i)
{
glutBitmapCharacter(font, *i);
}
glPopAttrib();
glPopMatrix();
glutSwapBuffers();
}
Edit: I've been told that I should use glWindowPos2i instead of glRasterPos2i. The problem with this solution was that glWindowPos2i wasn't recognised and resulted in a build error. Following some online research I added the following lines of code:
PFNGLWINDOWPOS2IPROC glWindowPos2i;
glWindowPos2i = (PFNGLWINDOWPOS2IPROC)glutGetProcAddress("glWindowPos2i");
Now the program runs properly, but it crashes when I try to use the glWindowPos2i function. I am using and as included headers in case that helps anyone come up with a solution to get glWindowPos2i to work properly.
use glWindowPos2i instead of glRasterPos2i. glRasterPos2i uses positions in world coordinates. glWindowPos2i uses positions in screen coordinates.
https://www.opengl.org/sdk/docs/man2/xhtml/glWindowPos.xml
https://www.opengl.org/sdk/docs/man2/xhtml/glRasterPos.xml
I am trying to create a room in openGL and i have the following quads however wall2 and wall4 do not show. I am guessing this is something to do with perspectives as if i go outside of the room i can see they have been rendered.
glColor3f(0.1f, 0.9f, 0.9f);
//Wall1
glBegin(GL_QUADS);
glNormal3f(0,0,1);
glVertex3f(-10,0,-10);
glVertex3f( 10,0,-10);
glVertex3f( 10,5,-10);
glVertex3f(-10,5,-10);
glEnd();
//Wall2
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glNormal3f(0,0,1);
glVertex3f(-10,0, 10);
glVertex3f( 10,0, 10);
glVertex3f( 10,5, 10);
glVertex3f(-10,5, 10);
glEnd();
//Wall3
glColor3f(0.4f, 0.9f, 0.1f);
glBegin(GL_QUADS);
glNormal3f(-1,0, 0);
glVertex3f( 10,0,-10);
glVertex3f( 10,0,10);
glVertex3f( 10,5,10);
glVertex3f( 10,5,-10);
glEnd();
//Wall4
glColor3f(0.1f, 0.2f, 0.2f);
glBegin(GL_QUADS);
glNormal3f(1, 0, 0);
glVertex3f( -10,0,-10);
glVertex3f( -10,0,10);
glVertex3f( -10,5,10);
glVertex3f( -10,5,-10);
glEnd();
Try disabling face culling! :)
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();
I've begun learning OpenGL today, and it's just plain fantastic. However I cannot for the life of me make objects draw according to depth, instead of drawing order, so I hope someone can tell me what I'm doing wrong.
Here's the extremely simple code I'm using to create a cube:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Simple translation
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glColor3f(1.0f,0.5f,0.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glColor3f(1.0f,0.0f,0.0f);
//You get the point, continue with all sides
glEnd(); // End Drawing The Cube
SDL_GL_SwapBuffers();
Here's the set up code:
if (SDL_Init(SDL_INIT_EVERYTHING)<0)
return -1;
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_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
SDL_Surface* screen = SDL_SetVideoMode(screen_x,screen_y,32,SDL_HWSURFACE|SDL_GL_DOUBLEBUFFER|SDL_OPENGL);
if (screen == NULL)
return -2;
//glEnable(GL_DEPTH_TEST); //<-If this is uncommented look at figure 1
glDepthFunc(GL_LESS);
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
glViewport(0, 0, screen_x, screen_y);
glMatrixMode(GL_PROJECTION); //projection with ortho, model otherwise
glLoadIdentity();
gluPerspective(60.0,1.0,0.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Well, when I
glEnable(GL_DEPTH_TEST);
...
And if I don't enable it
So what am I supposed to do? I must be missing ... something? How do I fix my depth issue?
I found the answer!
http://www.opengl.org/resources/faq/technical/depthbuffer.htm
Here's where I found the answer, My near frame was at exactly 0, it needs to be slightly away from 0 in order for the depth buffer's precision to take effect. (If I read that correctly)
All is well.
I've reached another impasse I can't seem to resolve on my own. I really hope someone can help me out.
I've been trying to create a nice little bloom effect using GLSL, which worked quite well. When I tried including something moving into my scene I noticed that I forgot to clear my FBOs before rendering into them.
Without clearing it worked for never changing scenes because I was always using the same texture. With the glClear(); command it still works, but for the very first frame only, all I get after that is a black screen. So I guess my issue is that I can't get my FBOs to continuously be updated every frame.
I feel like I'm either missing something very obvious or doing something horribly wrong.
I'd be thankful for any suggestions you might have.
Here's what I get for the first frame:
Sources:
(using openFrameworks)
setup:
void testApp::setup(){
ofSetVerticalSync(true);
ofDisableSetupScreen();
width = ofGetWidth();
height = ofGetHeight();
//complie/link/generate ShaderObjects ....
horizontalBlurFrag.load("/opt/openframeworks/apps/examples/FBO_basic_shader_new_continued_v4_2/bin/data/fragment_shader_horizontal.glsl", GL_FRAGMENT_SHADER);
verticalBlurFrag.load("/opt/openframeworks/apps/examples/FBO_basic_shader_new_continued_v4_2/bin/data/fragment_shader_vertical.glsl", GL_FRAGMENT_SHADER);
BlurVertex.load("/opt/openframeworks/apps/examples/FBO_basic_shader_new_continued_v4_2/bin/data/horizontal_blur.glsl", GL_VERTEX_SHADER);
blendTextures.load("/opt/openframeworks/apps/examples/FBO_basic_shader_new_continued_v4_2/bin/data/blend_shader.glsl", GL_FRAGMENT_SHADER);
fboOriginal.initialize(width, height);
fboH800.initialize(width, height);
fboV800.initialize(width, height);
fboH400.initialize(width, height);
fboV400.initialize(width, height);}
draw:
void testApp::draw(){
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
//set orthographic projection
glOrtho( -1, 1, -1, 1, 1.0, 40.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glViewport( 0, 0, width, height);
glDisable(GL_TEXTURE_2D);
fboOriginal.bind();
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0, 0, width, height);
glPushMatrix();
glScalef(0.1f, 0.1f, 1.0f);
//generating values between 0 and 2
float x = 2 * (sin(time)+1.000001)/2;
//drawSOlidRect(xPos, yPos, width, height, red, green, blue);
drawSolidRect(-8.0f, 8.0f, x, x, 0.4f, 0.4f, 1.0f);
drawSolidRect(-5.0f, 8.0f, x, x, 0.4f, 1.0f, 0.4f);
drawSolidRect(-2.0f, 8.0f, x, x, 0.4f, 1.0f, 1.0f);
drawSolidRect( 1.0f, 8.0f, x, x, 1.0f, 0.4f, 0.4f);
drawSolidRect( 4.0f, 8.0f, x, x, 1.0f, 0.4f, 1.0f);
drawSolidRect( 7.0f, 8.0f, x, x, 1.0f, 1.0f, 0.4f);
glPopMatrix();
glPopAttrib();
fboOriginal.unbind();
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fboOriginal.fboTexture);
BlurVertex.enable();
horizontalBlurFrag.enable();
glUniform1i(glGetUniformLocation(horizontalBlurFrag.program, "RTScene"), 0);
glDisable(GL_TEXTURE_2D);
fboH800.bind();
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0, 0, width, height);
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0, 1.0, -1.0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0, 1.0, -1.0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0, -1.0, -1.0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0, -1.0, -1.0);
glEnd();
glPopMatrix();
glPopAttrib();
glDisable(GL_TEXTURE_2D);
fboH800.unbind();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, fboH800.fboTexture);
BlurVertex.enable();
verticalBlurFrag.enable();
glUniform1i(glGetUniformLocation(verticalBlurFrag.program, "RTBlurH"), 0);
glDisable(GL_TEXTURE_2D);
fboV800.bind();
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0, 0, width, height);
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0, 1.0, -1.0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0, 1.0, -1.0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0, -1.0, -1.0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0, -1.0, -1.0);
glEnd();
glPopMatrix();
glPopAttrib();
fboV800.unbind();
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, fboV800.fboTexture);
BlurVertex.enable();
horizontalBlurFrag.enable();
glUniform1i(glGetUniformLocation(horizontalBlurFrag.program, "RTScene"), 1);
glDisable(GL_TEXTURE_2D);
fboH400.bind();
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushAttrib(GL_VIEWPORT_BIT);
glPushMatrix();
glViewport(0, 0, width/4, height/4); //crude downscale
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0, 1.0, -1.0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0, 1.0, -1.0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0, -1.0, -1.0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0, -1.0, -1.0);
glEnd();
glPopMatrix();
glPopAttrib();
glDisable(GL_TEXTURE_2D);
fboH400.unbind();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, fboH400.fboTexture);
BlurVertex.enable();
verticalBlurFrag.enable();
glUniform1i(glGetUniformLocation(verticalBlurFrag.program, "RTBlurH"), 1);
glDisable(GL_TEXTURE_2D);
fboV400.bind();
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushAttrib(GL_VIEWPORT_BIT);
glPushMatrix();
glViewport(0, 0, width*4, height*4); //crude downscale
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0, 1.0, -1.0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0, 1.0, -1.0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0, -1.0, -1.0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0, -1.0, -1.0);
glEnd();
glPopMatrix();
glPopAttrib();
glDisable(GL_TEXTURE_2D);
fboV400.unbind();
glBindTexture(GL_TEXTURE_2D, 0);
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fboV800.fboTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, fboV400.fboTexture);
BlurVertex.enable();
blendTextures.enable();
glUniform1i(glGetUniformLocation(blendTextures.program, "originalSizeTex"), 0);
glUniform1i(glGetUniformLocation(blendTextures.program, "downscaledTex"), 1);
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glMultiTexCoord2fARB(GL_TEXTURE0, 0.0f, 1.0f);
glVertex3f(-1.0, 1.0, -1.0);
glMultiTexCoord2fARB(GL_TEXTURE0, 1.0f, 1.0f);
glVertex3f(1.0, 1.0, -1.0);
glMultiTexCoord2fARB(GL_TEXTURE0, 1.0f, 0.0f);
glVertex3f(1.0, -1.0, -1.0);
glMultiTexCoord2fARB(GL_TEXTURE0, 0.0f, 0.0f);
glVertex3f(-1.0, -1.0, -1.0);
glEnd();
glDisable(GL_TEXTURE_2D);}
FBO:
class FrameBufferObject{
public:
//handles
GLuint fbo, fboTexture, fboDepthbuffer;
public:
void initialize(GLuint width, GLuint height){
// generate namespace for the frame buffer, colorbuffer and depthbuffer
glGenFramebuffersEXT(1, &fbo);
glGenTextures(1, &fboTexture);
glGenRenderbuffersEXT(1, &fboDepthbuffer);
//switch to our fbo so we can bind stuff to it
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
//create the colorbuffer texture and attach it to the frame buffer
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, fboTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glGenerateMipmapEXT(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, fboTexture, 0);
// create a render buffer as our depthbuffer and attach it
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fboDepthbuffer);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24,width, height);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fboDepthbuffer);
// Go back to regular frame buffer rendering
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glDisable(GL_TEXTURE_2D);
}
void bind(){
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
}
void unbind(){
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
void clear(){
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}};
UPDATE:
The alpha value one the glClearColor at least seems to be part of my issue.
I tried messing around with it and what i got is this:
(with up- and downscaling quads as you'd expect)
Somehow i seem to be losing all color somewhere along the way.
Oddly enough i got the best result having (0, 0, 0, 0) for 4 of my FBOs and (0, 0, 0, 1) for one of them. Setting (0, 0, 0, 0) for all FBOs just yields a greyish picture (which i assume is the default window without anything in it).
Here's my "blending-together-shader":
uniform sampler2D originalSizeTex;
uniform sampler2D downscaledTex;
varying vec2 vTexCoord;
void main(void){
vec4 colorOriginal = vec4(0.0, 0.0, 0.0, 0.0);
vec4 colorDownscale = vec4(0.0, 0.0, 0.0, 0.0);
colorOriginal = texture2D(originalSizeTex, vTexCoord.xy);
colorDownscale = texture2D(downscaledTex, vTexCoord.xy);
gl_FragColor = vec4(colorOriginal + colorDownscale);
}
Any guesses?
Depends on how you're doing your blending, but -- you might need to set the clear color to 0,0,0,0 instead of 0,0,0,1 when you clear your FBO.