# OpenGL pinpong issues in opengl

## Recommended Posts

Hello

I'm doing some ping ponging stuff and I am experiencing some weird behaviour with my code. I have 2 shaders which alter the texture and a third one which just displays stuff.

I have a certain number of passes in my program which i can increase and decrease (to make sure that the ping-ponging work). According to my logic, if the number of passes is 0, the color displayed should be black, for 1 pass it should be black, for 2 passes red, for 3 passes yellow, 4 passes orange, ...

However, for 2 passes I'm already getting yellow.

Any help would be very greatly appreciated. Been stuck with that for some time!!!

Thanks

The important part of the code is below:

[quote]//some global variables
unsigned char textureArray[4*512*512];

int numberIterations = 0;
GLuint tex_id[2];
GLenum attachmentpoints[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT};
int writeTex = 0;
GLuint frameBuffer;
int textureDim = 512;

bool panSc, zoomSc, rotateSc;
float eye[3], lookat[3];

GLint buf_1, buf_2, buf_Out;

// main
int main(int argc, char* argv[])
{
glutInit(&argc, argv);

if (argc > 1)
numberIterations = atoi(argv[1]);

glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition( 50, 50);
glutInitWindowSize( windowWidth, windowHeight);

main_window = glutCreateWindow( "Computer Graphics" );
myInitDisplay(windowWidth, windowHeight);

eye[0] = 0.0; eye[1] = 0.0; eye[2] = 2.0;
lookat[0] = 0.0; lookat[1] = 0.0; lookat[2] = 0.0;

panSc = zoomSc = rotateSc = false;

initGL();

// Register some callbacks - basically what function is to be associated to what event
glutDisplayFunc( myDisplay );
glutReshapeFunc( myReshape );
glutIdleFunc( myGlutIdle );
glutKeyboardFunc( myKeyboard );
glutMotionFunc( myMotion );
glutMouseFunc( myMouse );

glutMainLoop();

glDeleteFramebuffersEXT(1, &frameBuffer);

return 0;
}

void myInitDisplay( int width, int height )
{
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background and set its Color To Black - 4 params coz of RGBA

glDepthFunc( GL_LESS ); // The Type Of Depth Test To Do
glEnable( GL_DEPTH_TEST ); // Enables Depth Testing
glShadeModel( GL_SMOOTH ); // Enables Smooth Color Shading - GL_SMOOTH or GL_FLAT

glutAttachMenu( GLUT_RIGHT_BUTTON ); // Attach the menu to the right mouse button

return;
}

void initGL(){
glEnable(GL_TEXTURE_2D);

for (int i=0; i<(textureDim*textureDim); i++)
textureArray[i*4 +0] = textureArray[i*4 +1] = textureArray[i*4 +2] = textureArray[i*4 +3] = 0;

glGenTextures(2, tex_id);
for (int i=0; i<2; i++){
glBindTexture(GL_TEXTURE_2D, tex_id[i]);
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);
//NULL means reserve texture memory, but texels are undefined
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureDim, textureDim, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureArray);
}

glGenFramebuffersEXT(1, &frameBuffer);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBuffer);

// attach two textures to FBO
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attachmentpoints[writeTex], GL_TEXTURE_2D, tex_id[writeTex], 0);

GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if (status == GL_FRAMEBUFFER_COMPLETE_EXT){
// cout << "All Good!!!" << endl;
}
else{
cout << "Error: Binding framebuffer failed" << endl;
exit(0);
}

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

}

void myDisplay( void )
{
for (int i=0; i<(textureDim*textureDim); i++)
textureArray[i*4 +0] = textureArray[i*4 +1] = textureArray[i*4 +2] = textureArray[i*4 +3] = 0;

writeTex = 0; readTex = 1;

glEnable(GL_TEXTURE_2D);
for (int i=0; i<2; i++){
glBindTexture(GL_TEXTURE_2D, tex_id[i]);
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);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, textureDim, textureDim, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureArray); //NULL);
}

glEnable( GL_DEPTH_TEST );

if (numberIterations == 0){
///////////////////////////
glViewport (0, 0, (GLsizei) textureDim, (GLsizei) textureDim); // set the size of our viewport (window)

glMatrixMode( GL_PROJECTION ); // load the projection matrix; the subsequent transformations are going to affect it
glLoadIdentity(); // initialise the projection matrix before applying the projection matrix

// Specify that we are going to use a perspective projection - changing our projection matrix - NEW!!!!!
gluPerspective(45.0f, (GLfloat)textureDim/(GLfloat)textureDim, 0.1f, 100.0f); //FOV, aspect ratio, near clipping plane, far clipping plane

glMatrixMode( GL_MODELVIEW ); // load the model view matrix - subsequent matrices are going to affect this one
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
gluLookAt(eye[0], eye[1], eye[2], lookat[0], lookat[1], lookat[2], 0, 1, 0);
///////////////////////////

glUseProgramObjectARB(glsl_programOut);

glActiveTexture(GL_TEXTURE0);
glUniform1iARB(buf_Out, 0);

glBegin(GL_POLYGON);
glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);
glVertex3f(-0.5, -0.5, -0.5);

glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0);
glVertex3f( 0.5, -0.5, -0.5);

glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0);
glVertex3f( 0.5, 0.5, -0.5);

glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0);
glVertex3f(-0.5, 0.5, -0.5);
glEnd();

glUseProgramObjectARB(0);

}
else{
for (int i=0; i<numberIterations; i++){
//cout << endl << endl << "Pass " << i << " : " << endl;

glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushMatrix();

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBuffer);
glDrawBuffer(attachmentpoints[writeTex]);

/////////////////
glMatrixMode(GL_PROJECTION);
glOrtho(-0.5,0.5,-0.5,0.5,-0.5,0.5);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glViewport(0, 0, textureDim, textureDim);
/////////////////////

if (writeTex == 0){
glUseProgramObjectARB(glsl_program1);

glActiveTexture(GL_TEXTURE0);
glUniform1iARB(buf_1, 0);
}
else{
glUseProgramObjectARB(glsl_program2);

glActiveTexture(GL_TEXTURE0);
glUniform1iARB(buf_2, 0);
}

glNormal3f(0.0, 0.0, -1.0);
glBegin(GL_POLYGON);
glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);
glVertex3f(-0.5, -0.5, -0.5);

glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0);
glVertex3f( 0.5, -0.5, -0.5);

glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0);
glVertex3f( 0.5, 0.5, -0.5);

glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0);
glVertex3f(-0.5, 0.5, -0.5);
glEnd();

glUseProgramObjectARB(0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

glPopMatrix();
glPopAttrib();

///////////////////////////
glViewport (0, 0, (GLsizei) textureDim, (GLsizei) textureDim); // set the size of our viewport (window)

glMatrixMode( GL_PROJECTION ); // load the projection matrix; the subsequent transformations are going to affect it
glLoadIdentity(); // initialise the projection matrix before applying the projection matrix

// Specify that we are going to use a perspective projection - changing our projection matrix - NEW!!!!!
gluPerspective(45.0f, (GLfloat)textureDim/(GLfloat)textureDim, 0.1f, 100.0f); //FOV, aspect ratio, near clipping plane, far clipping plane

glMatrixMode( GL_MODELVIEW ); // load the model view matrix - subsequent matrices are going to affect this one
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
gluLookAt(eye[0], eye[1], eye[2], lookat[0], lookat[1], lookat[2], 0, 1, 0);
///////////////////////////

glUseProgramObjectARB(glsl_programOut);

glActiveTexture(GL_TEXTURE2);
glUniform1iARB(buf_Out, 0);

glBegin(GL_POLYGON);
glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);
glVertex3f(-0.5, -0.5, -0.5);

glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0);
glVertex3f( 0.5, -0.5, -0.5);

glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0);
glVertex3f( 0.5, 0.5, -0.5);

glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0);
glVertex3f(-0.5, 0.5, -0.5);
glEnd();

glUseProgramObjectARB(0);

// swap read and write indexes
writeTex = 1 - writeTex;
}
}

glFlush();
glutSwapBuffers();

return;
}

{
glUseProgramObjectARB(glsl_program1);

buf_1 = glGetUniformLocationARB(glsl_program1, "tex1");
// glUniform1iARB(buf_1, 0);

glUseProgramObjectARB(0);
}

{
glUseProgramObjectARB(glsl_program2);

buf_2 = glGetUniformLocationARB(glsl_program2, "tex1");
// glUniform1iARB(buf_2, 0);

glUseProgramObjectARB(0);
}

{
glUseProgramObjectARB(glsl_programOut);

buf_Out = glGetUniformLocationARB(glsl_programOut, "tex1");
// glUniform1iARB(buf_Out, 0);

glUseProgramObjectARB(0);
}

uniform sampler2D tex1;
varying vec4 vVertex;
void main(){
vec4 colorBuf = texture2D(tex1, gl_TexCoord[0].xy);
gl_FragData[0] = vec4(0.75,0.0,0.0, 0.25) + colorBuf;
}

uniform sampler2D tex1;
varying vec4 vVertex;
void main(){
vec4 colorBuf = texture2D(tex1, gl_TexCoord[0].xy);
if (colorBuf.a < 0.5)
gl_FragData[0] = vec4(0.0,0.75,0.0, 0.25) + colorBuf;
else
gl_FragData[0] = vec4(0.0,0.0,0.4, 0.25) + colorBuf;
}

uniform sampler2D tex1;
varying vec4 vVertex;
void main(){
vec4 color = texture2D(tex1, gl_TexCoord[0].xy);
gl_FragColor = color;
}

varying vec4 vVertex;
void main(){
gl_TexCoord[0] = gl_MultiTexCoord0.xyzw;
vVertex = vec4(gl_ModelViewProjectionMatrix * gl_Vertex);
gl_Position = ftransform();
}[/quote]

##### Share on other sites
Got the answer!. There was a typo in my code

glActiveTexture(GL_TEXTURE2); should have been glActiveTexture(GL_TEXTURE0);

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627662
• Total Posts
2978515
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 10
• 12
• 22
• 13