Draw 2D text on 3D scene in OpenGL (C++) - c++

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

Related

glColor3f not working on gluSphere

I am currently rendering a solar system of planets in my 3d space but every single planet is black even when light hits the sphere. The spheres are rendered last in my render function. Had the colors working when the spheres were being rendered on their own but now i've added my sky box and other quads all the spheres refuse to be colored.
#include "Scene.h"
float rotation;
float rotation2;
int direction;
int speed;
Scene::Scene(Input *in)
{
// Initialise variables
rotation = 20;
rotation2 = 0;
direction = 1;
speed = 5;
myTexture = 0;
skyBox = 0;
// Store pointer for input class
input = in;
// OpenGL settings
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.39f, 0.58f, 93.0f, 1.0f); // Cornflour Blue Background
glClearDepth(1.0f); // Depth Buffer Setup
glClearStencil(0); // Clear stencil buffer
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glEnable(GL_LIGHTING); // Enables Lighting
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
// Other OpenGL / render setting should be applied here.
myTexture = SOIL_load_OGL_texture
(
"gfx/neongrid.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
skyBox = SOIL_load_OGL_texture
(
"gfx/starField.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
camera = new Camera();
}
void Scene::update(float dt)
{
// Update camera position
camera->update(input, dt);
// Handle user input
if (input->isKeyDown(43))
{
direction = 1;
input->SetKeyUp(43);
}
else if (input->isKeyDown(45))
{
direction = -1;
input->SetKeyUp(45);
}
// Update scene related variables
rotation += speed * dt;
rotation2 += (speed *2) * dt;
if (input->isKeyDown('p') && WF == false)
{
WF = true;
input->SetKeyUp('p');
glPolygonMode(GL_FRONT, GL_LINE);
}
if (input->isKeyDown('p') && WF == true)
{
WF = false;
input->SetKeyUp('p');
glPolygonMode(GL_FRONT, GL_FILL);
}
// Calculate FPS for output
calculateFPS();
}
void Scene::render() {
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt(camera->getPosition().x, camera->getPosition().y, camera->getPosition().z,
camera->getLookAt().x, camera->getLookAt().y, camera->getLookAt().z,
camera->getUp().x, camera->getUp().y, camera->getUp().z);
glutWarpPointer(400, 300);
glutSetCursor(GLUT_CURSOR_FULL_CROSSHAIR);
// Lighting
GLfloat Light_Ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat Light_Diffuse[] = { 9.0f, 9.0f, 9.0f, 1.0f };
GLfloat Light_Position[] = { 2.0f, 2.0f, 2.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, Light_Ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, Light_Diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, Light_Position);
glEnable(GL_LIGHT0);
glDisable(GL_DEPTH_TEST);
#pragma region skybox
glPushMatrix();
glTranslatef(camera->getPosition().x, camera->getPosition().y, camera->getPosition().z);
glBindTexture(GL_TEXTURE_2D, skyBox);
glBegin(GL_QUADS);
//Back
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
//Right
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
//front
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glNormal3f(0.0f, 0.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
//left
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
//top
glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
//bottom
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd();
#pragma endregion
glEnable(GL_DEPTH_TEST);
glPopMatrix();
glPushMatrix();
#pragma region wall
glBindTexture(GL_TEXTURE_2D, myTexture);
glTranslatef(0.0, 0.0, 0.0);
glScalef(5.0f, 5.0f, 5.0f);
glBegin(GL_QUADS);
// first face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
// second face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
// third face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
// fourth face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, myTexture);
glScalef(5.0f, 5.0f, 5.0f);
glBegin(GL_QUADS);
// first face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
// second face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
// third face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 0.0f, -1.0f);
// fourth face
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, -1.0f);
glEnd();
#pragma endregion
glPopMatrix();
glPushMatrix();
// Render sun
glEnable(GL_TEXTURE_2D);
glColor3f(1.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, NULL);
glTranslatef(0.5, 0.5, -0.5);
glColor3f(1.0f, 0.0f, 0.0f);
gluSphere(gluNewQuadric(), 0.20, 20, 20);
glPushMatrix(); // Push for default matrix
// Render Planet 1
glRotatef(rotation, 0.5, 0.5, 0);
glTranslatef(0, 0, 1);
glScalef(1, 1, 1);
glColor3f(0.0f, 2.0f, 0.0f);
gluSphere(gluNewQuadric(), 0.20, 10, 10);
glPopMatrix(); // Pop off stack back to sun matrix
glPushMatrix(); // Push for default matrix
// Render Planet 2
glRotatef(rotation2, 0, 1, 0);
glTranslatef(2, 0, 0);
glScalef(0.5, 0.5, 0.5);
glColor3f(0.0f, 0.0f, 1.0f);
gluSphere(gluNewQuadric(), 0.20, 5, 5);
glPushMatrix(); // Pop back to sun
// Render a moon around Planet 2
glRotatef((rotation*2.0), 0, 1, 0);
glTranslatef(1.5, 0, 0);
glScalef(0.3, 0.3, 0.3);
glColor3f(0.0f, 0.0f, 1.0f);
gluSphere(gluNewQuadric(), 0.20, 20, 20);
glPopMatrix(); // Pop to planet 2
glPushMatrix(); // Push for default matrix
// Render a SECOND moon around Planet 2
glRotatef((rotation * 2), 0, 0, 1);
glTranslatef(1.5, 0, 0);
glScalef(0.3, 0.3, 0.3);
glColor3f(0.0f, 0.0f, 1.0f);
gluSphere(gluNewQuadric(), 0.20, 20, 20);
glPopMatrix();
//glPopMatrix();
glPopMatrix(); // Go back to sun!
glPushMatrix();
glTranslatef(2, 2, 2);
glColor3f(1, 0, 0);
gluSphere(gluNewQuadric(), 0.5, 20, 20);
glPopMatrix();
// End render geometry --------------------------------------
// Render text, should be last object rendered.
renderTextOutput();
// Swap buffers, after all objects are rendered.
glutSwapBuffers();
}
// Handles the resize of the window. If the window changes size the perspective matrix requires re-calculation to match new window size.
void Scene::resize(int w, int h)
{
width = w;
height = h;
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == 0)
h = 1;
float ratio = (float)w / (float)h;
fov = 45.0f;
nearPlane = 0.1f;
farPlane = 100.0f;
// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);
// Reset Matrix
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(fov, ratio, nearPlane, farPlane);
// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
}
// Calculates FPS
void Scene::calculateFPS()
{
frame++;
time = glutGet(GLUT_ELAPSED_TIME);
if (time - timebase > 1000) {
sprintf_s(fps, "FPS: %4.2f", frame*1000.0 / (time - timebase));
timebase = time;
frame = 0;
}
}
// Compiles standard output text including FPS and current mouse position.
void Scene::renderTextOutput()
{
// Render current mouse position and frames per second.
sprintf_s(mouseText, "Mouse: %i, %i", input->getMouseX(), input->getMouseY());
displayText(-1.f, 0.96f, 1.f, 0.f, 0.f, mouseText);
displayText(-1.f, 0.90f, 1.f, 0.f, 0.f, fps);
}
// Renders text to screen. Must be called last in render function (before swap buffers)
void Scene::displayText(float x, float y, float r, float g, float b, char* string) {
// Get Lenth of string
int j = strlen(string);
// Swap to 2D rendering
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, 5, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Orthographic lookAt (along the z-axis).
gluLookAt(0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
// Set text colour and position.
glColor3f(r, g, b);
glRasterPos2f(x, y);
// Render text.
for (int i = 0; i < j; i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, string[i]);
}
// Reset colour to white.
glColor3f(1.f, 1.f, 1.f);
// Swap back to 3D rendering.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, ((float)width/(float)height), nearPlane, farPlane);
glMatrixMode(GL_MODELVIEW);
}
Updated sun code :
// Render sun
glTranslatef(0.5, 0.5, -0.5);
glColor3f(1.0f, 0.0f, 0.0f);
gluSphere(gluNewQuadric(), 0.20, 20, 20);
glPushMatrix(); // Push for default matrix
// Render Planet 1
glRotatef(rotation, 0.5, 0.5, 0);
glTranslatef(0, 0, 1);
glScalef(1, 1, 1);
glColor3f(0.0f, 2.0f, 0.0f);
gluSphere(gluNewQuadric(), 0.20, 10, 10);
glPopMatrix(); // Pop off stack back to sun matrix
glPushMatrix(); // Push for default matrix
Found out i was missing a OpenGL setting
glEnable(GL_COLOR_MATERIAL);
Inserted at the start of Scene
Try to end glEnable(GL_TEXTURE_2D); with glDisable(GL_TEXTURE_2D); after you render sun.
If problem appeared after you added skybox. Check if vertices are in right order.

opengl Framebuffer offscreen to texture, colour error

im trying to use a framebuffer for offscreen-render a cube. Then i try to translate it to a texture using its texture. The problem i get is when i try to put that texture on a plane. The texture colour is full based on one primary colour: Reed, Green or Blue. I dont know where is the problem, maybe a cast problem from float to double? I really dont know what to think or where to explore.
Here is a gif of what i get. In this gif you can see the normal cube without offscreen render. The second GREEN cube is the framebuffer's texture printed on a plane:
here is the code I use. Its based on Qt but there is no difference from this to glut. I explain it to make easier to understand:
initializeGL: I declare the framebuffer and its attachments
PaintGL: draws the cube without using the FB if i dont "left click" to enable this (first part of the gif, it works perfect). If i left click, then the routine changes and paint the cube on the FrameBuffer, and then i paint its texture on a plane. (second part of the gif, it paints my texture GREEN)
setProyection: set the glOrtho for the normal cube draw.
#include "WidgetOpenGL.h"
WidgetOpenGL::WidgetOpenGL(QWidget *parent) : QOpenGLWidget(parent)
{
}
void WidgetOpenGL::initializeGL()
{
desplazamientoX = 1;
desplazamientoY = 0;
initializeOpenGLFunctions();
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
buttonpressed = false;
glGenFramebuffers(1, &buffer);
glBindFramebuffer(GL_FRAMEBUFFER, buffer);
glGenTextures(1, &renderedTexture);
glBindTexture(GL_TEXTURE_2D, renderedTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 768, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glGenRenderbuffers(1, &renderedDepth);
glBindRenderbuffer(GL_RENDERBUFFER, renderedDepth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 1024, 768);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderedDepth);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderedTexture, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
setProyection();
setModelView();
}
void WidgetOpenGL::paintGL()
{
if (buttonpressed)
{
glBindFramebuffer(GL_FRAMEBUFFER, buffer);
glViewport(0, 0, 1024, 768);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
setProyection();
setModelView();
glTranslatef(0.0, 0.0, -2.0);
setRotation();
glBegin(GL_QUADS);
// top
glColor3f(1.0f, 0.0f, 0.0f);
glNormal3f(0.0f, 1.0f, 0.0f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glEnd();
glBegin(GL_QUADS);
// front
glColor3f(0.0f, 1.0f, 0.0f);
glNormal3f(0.0f, 0.0f, 1.0f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glEnd();
glBegin(GL_QUADS);
// right
glColor3f(0.0f, 0.0f, 1.0f);
glNormal3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glEnd();
glBegin(GL_QUADS);
// left
glColor3f(0.0f, 0.0f, 0.5f);
glNormal3f(-1.0f, 0.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glEnd();
glBegin(GL_QUADS);
// bottom
glColor3f(0.5f, 0.0f, 0.0f);
glNormal3f(0.0f, -1.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glEnd();
glBegin(GL_QUADS);
// back
glColor3f(0.0f, 0.5f, 0.0f);
glNormal3f(0.0f, 0.0f, -1.0f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
glEnd();
glFlush();
if (buttonpressed)
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// QImage img = frameBuffer->toImage();
// img.save("Hola.jpg");
//draw plane with texture
setViewport();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, renderedTexture);
glBegin(GL_QUADS);
glTexCoord2d(0.0f, 0.0f);
glVertex3f(-1.0, -1.0, -1.0);
glTexCoord2d(1.0f, 0.0f);
glVertex3f(1.0, -1.0, -1);
glTexCoord2d(1.0f, 1.0f);
glVertex3f(1.0, 1.0, -1.0);
glTexCoord2d(0.0f, 1.0f);
glVertex3f(-1.0, 1.0, -1.0);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
glFlush();
}
}
void WidgetOpenGL::resizeGL(int w, int h)
{
setViewport();
setProyection();
setModelView();
}
void WidgetOpenGL::setViewport()
{
int ancho = this->width();
int alto = this->height();
glViewport(0, 0, ancho, alto);
}
void WidgetOpenGL::setProyection()
{
int ancho = this->width();
int alto = this->height();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 10.0);
//gluPerspective(90.0f, ancho / (GLdouble)alto, 0.0f, 10.0f);
}
void WidgetOpenGL::setModelView()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void WidgetOpenGL::mouseMoveEvent(QMouseEvent *event)
{
desplazamientoY += ((event->x() - initX) * 360) / (GLdouble)width();
desplazamientoX += ((event->y() - initY) * 360) / (GLdouble)height();
initX = event->x();
initY = event->y();
if (((int)desplazamientoX % 360) == 0) desplazamientoX = 0;
if (((int)desplazamientoY % 360) == 0) desplazamientoY = 0;
repaint();
}
void WidgetOpenGL::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
initX = event->x();
initY = event->y();
}
else
{
buttonpressed = !buttonpressed;
repaint();
}
}
void WidgetOpenGL::setRotation()
{
glRotatef(desplazamientoX, 1.0, 0.0, 0.0);
glRotatef(desplazamientoY, 0.0, 1.0, 0.0);
}
and here is the header file if needed:
#ifndef WIDGETOPENGL_H
#define WIDGETOPENGL_H
#include <QtWidgets/QMainWindow>
#include <QtWidgets/qopenglwidget.h>
#include <QtOpenGL>
#include <GL/GLU.h>
class WidgetOpenGL : public QOpenGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
public:
WidgetOpenGL(QWidget *parent);
signals:
void screenClicked();
protected:
void initializeGL();
void paintGL();
void resizeGL(int w, int h);
void setViewport();
void setProyection();
void setModelView();
private:
int initX;
int initY;
QOpenGLFramebufferObject *frameBuffer;
GLuint buffer;
GLuint renderedTexture;
GLuint renderedDepth;
bool buttonpressed;
QImage image;
GLdouble desplazamientoX;
GLdouble desplazamientoY;
void setRotation();
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
};
#endif
Solved! the problem were that i was painting the texture on a green plane. Thats because the last glcolor3f is green (used when i paint the back plane of the real cube).
I only have to add glColor3f(1.0,1.0,1.0) before drawing the plane, to make a white plane.

OpenGL transparent object behind another transparent object

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);

Opengl outline font in conflict with textures

I want to render outline font near playing area, but these two parts are in conflict. If I render only outline font, it's ok. When I render also playing area, textures of cubes have defect and light is changed also. When I render only playing area, it's ok. Here are some pictures:
http://img.obrazok.com/Untitled.usdh.png
Outline font part:
//Display-lists
GLuint fontBase;
GLYPHMETRICSFLOAT gmf[256];
//********************************
//3D Font
//********************************
GLvoid BuildFont(GLvoid) // Build Our Bitmap Font
{
HFONT font; // Windows Font ID
fontBase = glGenLists(256); // Storage For 256 Characters
font = CreateFont( -12, // Height Of Font
0, // Width Of Font
0, // Angle Of Escapement
0, // Orientation Angle
FW_BOLD, // Font Weight
FALSE, // Italic
FALSE, // Underline
FALSE, // Strikeout
ANSI_CHARSET, // Character Set Identifier
OUT_TT_PRECIS, // Output Precision
CLIP_DEFAULT_PRECIS, // Clipping Precision
ANTIALIASED_QUALITY, // Output Quality
FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
"Comic Sans MS"); // Font Name
HDC hDC = GetDC(GetActiveWindow());
SelectObject(hDC, font); // Selects The Font We Created
wglUseFontOutlines( hDC, // Select The Current DC
0, // Starting Character
255, // Number Of Display Lists To Build
fontBase, // Starting Display Lists
0.0f, // Deviation From The True Outlines
0.2f, // Font Thickness In The Z Direction
WGL_FONT_POLYGONS, // Use Polygons, Not Lines
gmf); // Address Of Buffer To Recieve Data
}
GLvoid KillFont(GLvoid) // Delete The Font
{
glDeleteLists(fontBase, 256); // Delete All 256 Characters
}
void printString(const char *string, float x, float y, float z) // Custom GL "Print" Routine
{
float length=0; // Used To Find The Length Of The Text
for (unsigned int loop=0;loop<(strlen(string));loop++) // Loop To Find Text Length
{
length+=gmf[string[loop]].gmfCellIncX; // Increase Length By Each Characters Width
}
glPushMatrix();
glRotatef(90,0.0,0.0,1.0);
glRotatef(90,1.0,0.0,0.0);
glTranslatef(x,y,z);
glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits
glListBase(fontBase); // Sets The Base Character to 0
glCallLists(strlen(string), GL_UNSIGNED_BYTE, string); // Draws The Display List Text
glPopAttrib(); // Pops The Display List Bits
glPopMatrix();
}
Render playing area:
//Display-lists
GLuint ls_mapWalls;
void renderWalls() {
for (int x = 0; x < mapSize; x++) {
for (int y = 0; y < mapSize; y++) {
glPushMatrix();
glTranslatef(x*2.0f,y*2.0f,0.0f);
if (map[x][y] == 'X') {
renderWall();
}
glPopMatrix();
}
}
}
void renderWall() {
glPushMatrix();
renderTexturedCube(1); //1 or 2
glPopMatrix();
}
void renderTexturedCube(int color) {
glEnable(GL_TEXTURE_2D);
switch (color) {
case 1 :
//normal wall
setMaterialColor(1.0f, 1.0f, 1.0f, 0.2f);
break;
case 2 :
//red wall
setMaterialColor(1.0f, 0.0f, 0.0f, 1.0f);
break;
}
glBindTexture(GL_TEXTURE_2D, texid[0]);
glBegin(GL_QUADS);
// Front Face
glNormal3f(0.0,0.0,1.0);
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);
// Back Face
glNormal3f(0.0,0.0,-1.0);
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);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glNormal3f(0.0,1.0,0.0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
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);
// Bottom Face
glNormal3f(0.0,-1.0,0.0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glNormal3f(1.0,0.0,0.0);
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);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glNormal3f(-1.0,0.0,0.0);
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();
setMaterialColor(1.0f, 1.0f, 1.0f, 1.0f);
glDisable(GL_TEXTURE_2D);
}
Render part:
void render(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
GLdouble ex = vzd*cos(fi)*cos(xi);
GLdouble ey = vzd*sin(fi)*cos(xi);
GLdouble ez = vzd*sin(xi);
gluLookAt( ex, ey, ez, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f );
printString("Dyna Blaster Beta", 10, 10, 10, 1.0, 0.0, 0.0);
glCallList(ls_mapWalls);
glutSwapBuffers();
}
Init part:
bool init(void)
{
//setup OpenGL
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);
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_CULL_FACE);
//files load
loadMap();
loadTextures();
//next init
ls_mapWalls = glGenLists(1);
glNewList(ls_mapWalls, GL_COMPILE);
renderWalls();
glEndList();
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 2.0);
GLfloat light_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 10.0, 10.0, 10.0, 0.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
BuildFont();
fi = 0.385f; xi = 0.515f; vzd = 69.2f;
return true;
}
If you have some question or want see another part of code, just ask. Thank you.
The documentation for wglUseFontOutlines at MSDN says this:
With WGL_FONT_POLYGONS, the created display lists call glFrontFace( GL_CW )
or glFrontFace( GL_CCW ); thus the current front-face value might be altered.
OpenGL defults to GL_CCW; so chances are, if you set it back with
glFrontFace( GL_CCW );
after drawing the text, your playing area will render correctly.

openGL / GLSL: bloom/blur, rendering to FBO

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.