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.
Related
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.
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 to draw a sphere and a cube at the same time on my screen. But the colors of my cube(which gets drawn first) dissapear. I don't understand why.
the sphere on the right is fine. But my cube on the left isn't.
I added texture to both:
I can perfectly draw both of them seperately, but when I try to draw both of them on one widget something goes wrong.
I tought the popping and pushing would solve this issue, but it doesn't.
code:
void MyGLWidget::drawCube()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(position,0.5,-0.1,-0.5,-0.5,0,0,0,1);
glTranslatef( 0.5, 0, 0.0);
glRotatef(getCubeAngle(), 1.0f, 0.0f, 0.0f);
glTranslatef(0, 0, 0);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
//back
glTexCoord2f(0.0, 1.0); glVertex3f(-0.1, 0.1,-0.1 );
glTexCoord2f(1.0, 1.0); glVertex3f(0.1, 0.1,-0.1);
glTexCoord2f(1.0, 0.0); glVertex3f(0.1,-0.1,-0.1);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.1,-0.1,-0.1);
/*rest of cube gets drawn*/
glEnd();
glFlush();
glPopMatrix();
}
void MyGLWidget::drawSun()
{
glPushMatrix();
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, texturePlanet[0]);
glPushMatrix();
glScalef(1,1,1);
glLoadIdentity();
GLUquadricObj *quadric=gluNewQuadric();
gluQuadricTexture(quadric, GLU_TRUE);
gluQuadricNormals(quadric, GLU_SMOOTH);
glEnable(GL_TEXTURE_2D); //
glBindTexture(GL_TEXTURE_2D,texturePlanet[0]);//
gluSphere(quadric, 0.25, 360,360);
glDisable(GL_TEXTURE_2D);//
gluDeleteQuadric(quadric);
glPopMatrix();
}
void MyGLWidget::paintGL()
{
drawCube();
drawSun();
}
It is because In your cube drawing, you didn't enable texturing.
glEnable(GL_TEXTURE_2D); //ADD THIS TO ENABLE TEXTURING
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
//back
glTexCoord2f(0.0, 1.0); glVertex3f(-0.1, 0.1,-0.1 );
glTexCoord2f(1.0, 1.0); glVertex3f(0.1, 0.1,-0.1);
glTexCoord2f(1.0, 0.0); glVertex3f(0.1,-0.1,-0.1);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.1,-0.1,-0.1);
/*rest of cube gets drawn*/
glEnd();
glFlush();
glPopMatrix();
glDisable(GL_TEXTURE_2D); // ADD THIS TO DISABLE TEXTURING
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
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.