OpenScenegraph: Texture Mapping Quad - openscenegraph

I am trying to do texture map a quad geometry object generated by createTexturedQuadGeometry with a texture that I load from an image. I then add this drawable to a node, add that node to root and render the hierarchy.
The code below is how I do it. The code compiles and runs but I only get a blank black screen instead of the specified image. Can someone please point out what is wrong?
int main(int argc, char** argv)
{
osg::ref_ptr<osg::Group> root = new osg::Group;
osg::ref_ptr<osg::Texture2D> testTexture = new osg::Texture2D;
osg::ref_ptr<osg::Image> testImage = osgDB::readImageFile("testImage.png");
assert(testImage.valid());
int viewWidth = testImage->s();
int viewHeight = testImage->t();
testTexture->setImage(testImage);
osg::ref_ptr<osg::Geometry> pictureQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f),
osg::Vec3(viewWidth,0.0f,0.0f),
osg::Vec3(0.0f,0.0f,viewHeight),
0.0f,
viewWidth,
viewHeight,
1.0f);
pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, testTexture.get());
pictureQuad->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
osg::ref_ptr<osg::Geode> textureHolder = new osg::Geode();
textureHolder->addDrawable(pictureQuad);
root->addChild(textureHolder);
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
viewer.run();
}

So, I happened to figure out the error.
createTexturedQuadGeometry expects normalised texture coordinates.
So,
osg::ref_ptr<osg::Geometry> pictureQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0f),
osg::Vec3(viewWidth,0.0f,0.0f),
osg::Vec3(0.0f,0.0f,viewHeight),
0.0f,
0.0f,
1.0f,
1.0f);
solves the problem.

Related

Relating vertex buffers and vertex attributes in openGL

My entire code that is supposed to draw a triangle on the screen is:
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <string.h>
const GLint WIDTH=800, HEIGHT=600;
GLuint VAO, VBO, shader;
//Vertex Shader
/*static const char**/
const GLchar* vShader = "\n"
"\n"
"#version 330 \n"
"layout (location=0) in vec3 pos;\n"
"void main(){\n"
"gl_Position = vec4(pos.x,pos.y,pos.z,1.0);\n"
"\n"
"}\n"
"";
// fragment shader
const GLchar* fShader = "\n"
"#version 330 \n"
"out vec4 colour;\n"
"void main(){\n"
"colour = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n"
"\n"
"\n";
void CreateTriangle(){
GLfloat vertices[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
// vertex arrays
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// vertex buffers
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*9,vertices,GL_STATIC_DRAW);
glVertexAttribPointer(0,3, GL_FLOAT,GL_FALSE,0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void AddShader(GLuint theProgram, const GLchar* shaderCode, GLenum shaderType){
GLuint theShader = glCreateShader(shaderType);
const GLchar* theCode[1];
theCode[0] = shaderCode;
GLint codeLength[1];
codeLength[0] = strlen(shaderCode);
glShaderSource(theShader, 1, theCode, codeLength);
glCompileShader(theShader);
GLint result=0;
GLchar eLog[1024]={};
glGetShaderiv(theShader, GL_COMPILE_STATUS, &result);
if(!result){
glGetShaderInfoLog(theShader,sizeof(eLog),NULL, eLog);
std::cout<< "Error compiling"<<shaderType<<" "<<eLog <<std::endl;
return;
}
glAttachShader(theProgram,theShader);
}
void CompileShader(){
shader = glCreateProgram();
if(!shader){
std::cout<<"Error Creating Shader Program";
return;
}
AddShader(shader, vShader,GL_VERTEX_SHADER);
AddShader(shader, fShader,GL_FRAGMENT_SHADER);
// getting error codes
GLint result=0;
GLchar eLog[1024]={0};
// Creates the executables in the graphic card
glLinkProgram(shader);
// get information if program is linked properly
glGetProgramiv(shader, GL_LINK_STATUS, &result);
if(!result){
glGetProgramInfoLog(shader,sizeof(eLog),NULL,eLog);
std::cout<<"Error linking program"<<eLog<<std::endl;
return;
}
glValidateProgram(shader);
glGetProgramiv(shader,GL_VALIDATE_STATUS,&result);
if(!result){
glGetProgramInfoLog(shader, sizeof(eLog),NULL, eLog);
std::cout<<"Error validating program"<<eLog<<std::endl;
return;
}
}
int main(void){
if(!glfwInit()){
std::cout << "glfw initialization failed" << std::endl;
glfwTerminate();
return 1;
}
// glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow *mainWindow = glfwCreateWindow(WIDTH, HEIGHT, "NEW WINDOW", NULL, NULL);
if(!mainWindow){
std::cout<< "Window creation failed" <<std::endl;
glfwTerminate();
return 1;
}
int bufferWidth, bufferHeight;
glfwGetFramebufferSize(mainWindow, &bufferWidth, &bufferHeight);
glfwMakeContextCurrent(mainWindow);
if(glewInit() != GLEW_OK){
std::cout << "GLEW Initialization failed" << std::endl;
glfwDestroyWindow(mainWindow);
glfwTerminate();
return 1;
}
glViewport(0,0,bufferWidth, bufferHeight);
CreateTriangle();
CompileShader();
while(!glfwWindowShouldClose(mainWindow)){
glfwPollEvents();
glUseProgram(shader);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES,0,3);
glBindVertexArray(0);
glUseProgram(0);
glfwSwapBuffers(mainWindow);
std::cout<<"something"<<std::endl;
}
return 0;
}
It essentially draws a black screen and there's no error whatsoever but is supposed to draw a red triangle so I'm trying to debug this code and there is essentially some parts in the code that I don't understand.
1) How does the VBO (Vertex Buffer Object) relate to the VAO (Vertex Attribute object), we basically defined these using the following inside of the CreateTriangles() function:
...
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
// vertex buffers
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*9,vertices,GL_STATIC_DRAW);
glVertexAttribPointer(0,3, GL_FLOAT,GL_FALSE,0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
...
Note that we already unbind both the VAO and VBO, but during the drawing call inside the while loop:
while(!glfwWindowShouldClose(mainWindow)){
glfwPollEvents();
glUseProgram(shader);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES,0,3);
glBindVertexArray(0);
glUseProgram(0);
glfwSwapBuffers(mainWindow);
std::cout<<"something"<<std::endl;
}
we only rebind the VAO and not the VBO, which I think may be the result of the error, but I don't know for sure.
Also, the tutorial says that the VBO is bound inside the VAO but I don't see there being any linking or anything that related VBO to the VAO in the code, so I'm really being confused on how is it that we're binding them together and why we're only binding back the VAO and not the VBO during the drawing stage?
I'm using Linux OS and used the following to compile:
g++ -std=c++17 main.cpp -o main -lGL -lGLEW -lglfw && ./main
From comments above, credit to #Rabbid76:
When glVertexAttribPointer is called than the a name reference of the current VBO is stored in the current VAO. The current VAO is bound by glBindVertexArray(VAO); and the current VBO is bound by glBindBuffer(GL_ARRAY_BUFFER,VBO);. glVertexAttribPointer associates VBO to the resource index 0 in VAO. This association is stored in the state vector of VAO. So it is sufficient to bind VAO(glBindVertexArray(VAO)) before the draw call. Hence, only binding the VAO is sufficient.
As for the problem stated in the comments regarding the black screen try
Updating the glew version and your graphic card drive. If updating glew is not possible, then just set the glExperimental=GL_TRUE.

creat colored cube consisting triangles in android studio

I only managed to fix a triangle. This is the code but do not know what to do to get it as a cube.
I tried to fix the code but did not get the cube, so this code is before I destroyed it. Will try to get it to rotate too.
public class Triangle {
static final int VERTEX_POS_SIZE = 4;
static final int COLOR_SIZE = 4;
static final int VERTEX_POS_INDEX = 0;
static final int COLOR_INDEX = 1;
static final int VERTEX_POS_OFFSET = 0;
static final int COLOR_OFFSET = 0;
static final int VERTEX_ATTRIB_SIZE = VERTEX_POS_SIZE;
static final int COLOR_ATTRIB_SIZE = COLOR_SIZE;
private final int VERTEX_COUNT = triangleData.length / VERTEX_ATTRIB_SIZE;
private FloatBuffer vertexDataBuffer;
private FloatBuffer colorDataBuffer;
static float triangleData[] = { // in counterclockwise order:
0.0f, 0.0f, 0.0f, 1.0f, // top
1.0f, 0.0f, 0.0f, 1.0f, // bottom left
1.0f, 0.0f, -1.0f, 1.0f, // bottom right
0.0f, 0.0f, -1.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, -1.0f, 1.0f,
0.0f, 1.0f, -1.0f, 1.0f,
};
static float colorData[] = { // in counterclockwise order:
1.0f, 0.0f, 0.0f, 1.0f, // Red
0.0f, 1.0f, 0.0f, 1.0f, // Green
0.0f, 0.0f, 1.0f, 1.0f// Blue
};
// Set color with red, green, blue and alpha (opacity) values
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };
private final int mProgram;
private final String vertexShaderCode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
"attribute vec4 vPosition; \n" +
"attribute vec4 vColor; \n" +
"uniform mat4 uMVPMatrix;\n" +
"varying vec4 c; \n" +
"void main() { \n" +
" c = vColor; \n" +
// the matrix must be included as a modifier of gl_Position
// Note that the uMVPMatrix factor *must be first* in order
// for the matrix multiplication product to be correct.
" gl_Position = uMVPMatrix * vPosition;\n" +
"}";
private final String fragmentShaderCode =
"precision mediump float;\n" +
"varying vec4 c;\n" +
"void main() {\n" +
" gl_FragColor = c;\n" +
"}";
// Use to access and set the view transformation
private int mMVPMatrixHandle;
private int positionHandle;
private int colorHandle;
public Triangle() {
// initialize vertex byte buffer for shape coordinates
ByteBuffer bbv = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
triangleData.length * 4);
// use the device hardware's native byte order
bbv.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexDataBuffer = bbv.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexDataBuffer.put(triangleData);
// set the buffer to read the first coordinate
vertexDataBuffer.position(0);
// initialize vertex byte buffer for shape coordinates
ByteBuffer bbc = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
colorData.length * 4);
// use the device hardware's native byte order
bbc.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
colorDataBuffer = bbc.asFloatBuffer();
// add the coordinates to the FloatBuffer
colorDataBuffer.put(colorData);
// set the buffer to read the first coordinate
colorDataBuffer.position(0);
int vertexShader = CGRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
vertexShaderCode);
int fragmentShader = CGRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
fragmentShaderCode);
// create empty OpenGL ES Program
mProgram = GLES20.glCreateProgram();
// add the vertex shader to program
GLES20.glAttachShader(mProgram, vertexShader);
// add the fragment shader to program
GLES20.glAttachShader(mProgram, fragmentShader);
// creates OpenGL ES program executables
GLES20.glLinkProgram(mProgram);
}
public void draw(float[] mvpMatrix) {
// Add program to OpenGL ES environment
GLES20.glUseProgram(mProgram);
// get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
positionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
GLES20.glEnableVertexAttribArray(positionHandle);
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(positionHandle, VERTEX_POS_SIZE,
GLES20.GL_FLOAT, false,
VERTEX_ATTRIB_SIZE * 4, vertexDataBuffer);
colorHandle = GLES20.glGetAttribLocation(mProgram, "vColor");
GLES20.glEnableVertexAttribArray(colorHandle);
GLES20.glVertexAttribPointer(colorHandle, COLOR_SIZE,
GLES20.GL_FLOAT, false,
COLOR_ATTRIB_SIZE * 4, colorDataBuffer);
// Pass the projection and view transformation to the shader
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, VERTEX_COUNT);
// Disable vertex array
GLES20.glDisableVertexAttribArray(positionHandle);
GLES20.glDisableVertexAttribArray(colorHandle);
}
}

Panning in a MFC Control using OpenGL

I'm very new to OpenGL so this might be a very silly question.
What I'm using: Visual Studio 2010, MFC framework, Windows 7.
What I'm trying to do: A MFC control based on OpenGL that can show some files preview with zooming, panning and rotation.
What I've already done:
Derived a class from CButton to handle all events and having a easy access to CClientDC;
Following a less or more recent tutorial I've encapsulated the creation of the OpenGL Device in a class.I don't know if it's good or if I've understood what I've done, but it works.
Let me show you some code:
int COpenGLCtrl::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
if( CButton::OnCreate( lpCreateStruct ) == -1 )
return -1;
m_pDC = new CClientDC( this );
// This is my encapsulated device.
m_GLDevice.Create( m_pDC->m_hDC );
InitGL();
return 0;
}
void COpenGLCtrl::InitGL( void )
{
glShadeModel( GL_SMOOTH );
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
}
// Drawing inside button.
void COpenGLCtrl::OnPaint( void )
{
m_GLDevice.MakeCurrent();
DrawGLScene();
}
// This function is called before DrawGLScene the first time the control is being redrawed.
void COpenGLCtrl::ZoomOut( int nVal )
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0f, nVal * 1.0f, 0.0f, nVal * 1.0f, nVal * 1.0f, 0.0f );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
void COpenGLCtrl::DrawGLScene( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glRotatef( m_fRotation_X, 1.0f, .0f, 0.0f );
glRotatef( m_fRotation_Y, 0.0f, 1.0f, 0.0f );
glTranslatef( m_fTraslation_X, m_fTraslation_Y, 0.0f );
glScalef( 1.0f, 1.0f, 0.0f );
glBegin( GL_TRIANGLES );
glColor3f( 1.0f, 0.0f, 0.0f );
glVertex3f( 0.0f, 0.0f, 0.0f );
glColor3f( 0.0f, 1.0f, 0.0f );
glVertex3f( 0.5f, 1.0f, 0.0f );
glColor3f( 0.0f, 0.0f, 1.0f );
glVertex3f( 1.0f, 0.0f, 0.0f );
glEnd();
SwapBuffers( m_pDC->m_hDC );
}
All this code is sufficient to draw a small triangle in my control with low-left vertex centered in OpenGL axis origin. All good, I showed you all this stuff to see if there are some error somewhere.
Now I want to pan around my triangle: from what I've understood, I've to modify the value of m_fTranslation_X and m_fTranslation_Y. I've decided to pan the view when the user holds down the mouse left button and moves it around:
void COpenGLCtrl::OnMouseMove( UINT nFlags, CPoint point )
{
int nRes;
CString strOut;
GLfloat winX, winY, winZ;
GLint viewport[4];
GLdouble vPos[3], modelview[16], projection[16];
// Converting CPoint MFC coordinates to OpenGL viewport coordinates.
m_p2dPosMouse = point;
glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );
winX = (GLfloat)point.x;
winY = (GLfloat)viewport[3] - (GLfloat)point.y;
glReadPixels( point.x, int( winY ), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
nRes = gluUnProject( winX, winY, winZ, modelview, projection, viewport, &vPos[0], &vPos[1], &vPos[2] );
m_vPosMouse[0] = (GLfloat)vPos[0];
m_vPosMouse[1] = (GLfloat)vPos[1];
m_vPosMouse[2] = (GLfloat)vPos[2];
if( nFlags == MK_LBUTTON )
{
if( !m_bTrasla )
{
ATLTRACE2( _T("\nInizio traslazione\n\n") );
memcpy( m_vPosMouse_ult, m_vPosMouse, 3 * sizeof( GLfloat ) );
m_p2dUltPos = point;
m_bTrasla = true;
}
/*OPENGL-WAY
m_fTraslation_X += m_vPosMouse[0] - m_vPosMouse_ult[0];
m_fTraslation_Y += m_vPosMouse[1] - m_vPosMouse_ult[1];
*/
/*MFC-WAY*/
m_fTraslation_X += ( point.x - m_p2dUltPos.x ) / 5000.0f;
m_fTraslation_Y += ( m_p2dUltPos.y - point.y ) / 5000.0f;
CString strBuf;
strBuf.Format( _T("%f - %f || CPoint.x: %ld CPoint.y: %ld\n"), m_fTraslation_X, m_fTraslation_Y, point.x, point.y );
ATLTRACE2( strBuf );
///////////////////////
// EDIT: Added in edit to solve MFC-WAY
m_p2dUltPos = point;
///////////////////////
memcpy( m_vPosMouse_ult, m_vPosMouse, 3 * sizeof( GLfloat ) );
}
if( m_CB_PosAgg.Valid() )
m_CB_PosAgg.Execute();
Invalidate();
}
In short words: I keep track of mouse last position and calculate how much I've moved on X and on Y. These differences are my traslations.
Finally my problems:
If I use OpenGL coordinate (commented and labelled OPENGL-WAY) the pan is smooth and precise but shakes. This is because even if my CPoint coordinates increase (or decrease) and so the difference, the OpenGL don't. Here's some values:
m_fTraslate_X m_fTraslate_Y CPoint.x CPoint.y
0.000000 0.000000 274 328
0.014377 0.141573 283 265
0.020767 0.152809 287 260
0.001597 0.002247 288 259
0.007987 0.020225 292 251
0.025559 0.164045 295 246
0.027157 0.170786 296 243
As you can see, while mouse control position increases (in MFC Y axis is inverted), at step 3 the values for X-axis are suddenly decreased from 0.02 to 0.002 and then back to 0.02! Why this?
EDIT: solved in my answer. If I use MFC coordinate, labelled MFC-WAY, I've got no shakes but a strong inertia. Sometimes happen that if I start moving my mouse to the right (assuming no up-down) then change the direction back to left, my view continues to move to the right. Where is the error?
Thanks in advance, hope you won't ban me for this noobish essay.
Problem 1)
The first problem was that when the view gets invalidated and then redrawed, the traslation has still to be performed! Solution:
if( nFlags == MK_RBUTTON && m_bTrasla )
{
// Converting MFC coordinates to OpenGL System.
ConversioneCoordinateMFC_A_OPENGL( m_vPosMouse_ult, m_p2dUltPos );
m_fTraslation_X += m_vPosMouse[0] - m_vPosMouse_ult[0];
m_fTraslation_Y += m_vPosMouse[1] - m_vPosMouse_ult[1];
Problem 2)
MFC-STYLE panning is not updating the last position of mouse pointer. Adding this line:
m_p2dUltPos = point; solved the problem.

OpenGL double buffers on Ubuntu does not work

I'm testing out several of Sumantha Guha's code and there's something that isn't working quite right... All of the sample code where he uses GLUT_DOUBLE and glutSwapBuffers() does not work on my ubuntu machine, but works on my Windows machine. More accurately the window that pops out simply traces the background.
I've had this issue before on Windows where Flush and single buffers don't work, but now this is happening on Linux where Double buffers and glutSwapBuffers do not work. Any idea as to what may be causing this?
Sample of code that I tried loading. Compiles fine, just get a window that traces the background.
///////////////////////////////////////////////////////////////////////////////////////////////////////
// loadTextures.cpp
//
// This stripped-down program shows how to load both external and program-generated images as textures.
//
// NOTE: The Textures folder must be in the same one as this program.
//
// Interaction:
// Press the left and right arrow keys to rotate the square.
// Press space to toggle between textures.
// Press delete to reset.
//
// Sumanta Guha
///////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////
// TEXTURE GREDITS:
// canLabel.bmp, thanks anonymous.
// canTop.bmp, thanks www.acoustica.com.
// cray2.bmp, thanks NASA website www.nasa.gov.
// grass.bmp, thanks www.amazingtextures.com.
// launch.bmp, thanks NASA website www.nasa.gov.
// nightsky.bmp, thanks anonymous.
// sky.bmp, thanks www.mega-tex.nl.
// trees.bmp, thanks anonymous.
////////////////////////////////////////////////
#include <cstdlib>
#include <iostream>
#include <fstream>
#ifdef __APPLE__
# include <GLUT/glut.h>
# include <OpenGL/glext.h>
#else
# include <GL/glut.h>
# include <GL/glext.h>
#endif
using namespace std;
// Globals.
static unsigned int texture[2]; // Array of texture indices.
static unsigned char chessboard[64][64][3]; // Storage for chessboard image.
static float angle = 0.0; // Angle to rotate textured square.
static int id = 0; // Currently displayed texture id.
// Struct of bitmap file.
struct BitMapFile
{
int sizeX;
int sizeY;
unsigned char *data;
};
// Routine to read a bitmap file.
// Works only for uncompressed bmp files of 24-bit color.
BitMapFile *getBMPData(string filename)
{
BitMapFile *bmp = new BitMapFile;
unsigned int size, offset, headerSize;
// Read input file name.
ifstream infile(filename.c_str(), ios::binary);
// Get the starting point of the image data.
infile.seekg(10);
infile.read((char *) &offset, 4);
// Get the header size of the bitmap.
infile.read((char *) &headerSize,4);
// Get width and height values in the bitmap header.
infile.seekg(18);
infile.read( (char *) &bmp->sizeX, 4);
infile.read( (char *) &bmp->sizeY, 4);
// Allocate buffer for the image.
size = bmp->sizeX * bmp->sizeY * 24;
bmp->data = new unsigned char[size];
// Read bitmap data.
infile.seekg(offset);
infile.read((char *) bmp->data , size);
// Reverse color from bgr to rgb.
int temp;
for (int i = 0; i < size; i += 3)
{
temp = bmp->data[i];
bmp->data[i] = bmp->data[i+2];
bmp->data[i+2] = temp;
}
return bmp;
}
// Load external textures.
void loadExternalTextures()
{
// Local storage for bmp image data.
BitMapFile *image[1];
// Load the texture.
image[0] = getBMPData("Textures/launch.bmp");
// Activate texture index texture[0].
glBindTexture(GL_TEXTURE_2D, texture[0]);
// Set texture parameters for wrapping.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set texture parameters for filtering.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Specify an image as the texture to be bound with the currently active texture index.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image[0]->sizeX, image[0]->sizeY, 0,
GL_RGB, GL_UNSIGNED_BYTE, image[0]->data);
}
// Routine to load a program-generated image as a texture.
void loadProceduralTextures()
{
// Activate texture index texture[1].
glBindTexture(GL_TEXTURE_2D, texture[1]);
// Set texture parameters for wrapping.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set texture parameters for filtering.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// Specify an image as the texture to be bound with the currently active texture index.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, chessboard);
}
// Create 64 x 64 RGB image of a chessboard.
void createChessboard(void)
{
int i, j;
for (i = 0; i < 64; i++)
for (j = 0; j < 64; j++)
if ( ( ((i/8)%2) && ((j/8)%2) ) || ( !((i/8)%2) && !((j/8)%2) ) )
{
chessboard[i][j][0] = 0x00;
chessboard[i][j][1] = 0x00;
chessboard[i][j][2] = 0x00;
}
else
{
chessboard[i][j][0] = 0xFF;
chessboard[i][j][1] = 0xFF;
chessboard[i][j][2] = 0xFF;
}
}
// Initialization routine.
void setup(void)
{
glClearColor(0.8, 0.8, 0.8, 0.0);
// Create texture index array.
glGenTextures(2, texture);
// Load external texture and generate and load procedural texture.
loadExternalTextures();
createChessboard();
loadProceduralTextures();
// Turn on OpenGL texturing.
glEnable(GL_TEXTURE_2D);
// Specify how texture values combine with current surface color values.
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}
// Drawing routine.
void drawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 0.0, 20.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
// Activate a texture.
glBindTexture(GL_TEXTURE_2D, texture[id]);
// Map the texture onto a square polygon.
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 0.0); glVertex3f(-10.0, -10.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(10.0, -10.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(10.0, 10.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-10.0, 10.0, 0.0);
glEnd();
glutSwapBuffers();
}
// OpenGL window reshape routine.
void resize(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
case ' ':
id++;
if (id == 2) id = 0;
glutPostRedisplay();
break;
case 127:
angle = 0.0;
glutPostRedisplay();
break;
default:
break;
}
}
// Callback routine for non-ASCII key entry.
void specialKeyInput(int key, int x, int y)
{
if (key == GLUT_KEY_LEFT)
{
angle -= 5.0;
if (angle < 0.0) angle += 360.0;
}
if (key == GLUT_KEY_RIGHT)
{
angle += 5.0;
if (angle > 360.0) angle -= 360.0;
}
glutPostRedisplay();
}
// Routine to output interaction instructions to the C++ window.
void printInteraction(void)
{
cout << "Interaction:" << endl;
cout << "Press the left and right arrow keys to rotate the square." << endl
<< "Press space to toggle between textures." << endl
<< "Press delete to reset." << endl;
}
// Main routine.
int main(int argc, char **argv)
{
printInteraction();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("loadTextures.cpp");
setup();
glutDisplayFunc(drawScene);
glutReshapeFunc(resize);
glutKeyboardFunc(keyInput);
glutSpecialFunc(specialKeyInput);
glutMainLoop();
return 0;
}

Bind Opengl texture from the first fbo to a second fbo, use shader and render the second result (ping pong)

I've been working on ping pong shading and had thought that I had cracked it after my previous question. However, with further shader knowledge it looks like while I'm able to run the shader on FBO A and on FBO B, the output from A is not used as the source to B. In other words I'm not binding it correctly.
The code I'm using is below. The output of the second shader is showing colour based output but the first shader sets the data to grayscale. So consquently I know this isn't working as required.
I'd be grateful for any (yet further!!) assistance.
Code below,
Cheers,
Simon
- (void) PingPong:(CVImageBufferRef)cameraframe;
{
// Standard texture coords for the rendering pipeline
static const GLfloat squareVertices[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,
};
static const GLfloat textureVertices[] = {
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
};
if (context)
{
[EAGLContext setCurrentContext:context];
}
// Create two textures with the same configuration
int bufferHeight = CVPixelBufferGetHeight(cameraframe);
int bufferWidth = CVPixelBufferGetWidth(cameraframe);
// texture 1
glGenTextures(1, &tex_A);
glBindTexture(GL_TEXTURE_2D, tex_A);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Using BGRA extension to pull in video frame data directly
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(cameraframe));
// Texture 2
glGenTextures(1, &tex_B);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Bind framebuffer A
glBindFramebuffer(GL_FRAMEBUFFER, fbo_A);
glViewport(0, 0, backingWidth, backingHeight);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex_A);
// Update uniform values
glUniform1i(uniforms[UNIFORM_VIDEOFRAME], 0);
// Update attribute values.
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_TEXTUREPOSITON, 2, GL_FLOAT, 0, 0, textureVertices);
glEnableVertexAttribArray(ATTRIB_TEXTUREPOSITON);
// Use the first shader
glUseProgram(greyscaleProgram);
// Render a quad
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Use the second shader
glUseProgram(program);
// Bind framebuffer B
glBindFramebuffer(GL_FRAMEBUFFER, fbo_B);
// Bind texture A and setup texture units for the shader
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex_A);
// Render output of FBO b is texture B
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex_B, 0);
// Update uniform values
glUniform1i(uniforms[UNIFORM_VIDEOFRAME], 0);
// Update attribute values.
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_TEXTUREPOSITON, 2, GL_FLOAT, 0, 0, textureVertices);
glEnableVertexAttribArray(ATTRIB_TEXTUREPOSITON);
// Render a quad
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Render the whole thing
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];
glDeleteTextures(1, &tex_A);
glDeleteTextures(1, &tex_B);
}
I think what might be happening is that you are still rendering into frame buffer memory and not texture memory. iirc glFramebufferTexture2D does not act as a resolve/copy but instead binds the framebuffer to the texture so that future render operations get written to the texture. You could have more problems however I'm pretty sure that your call to glFramebufferTexture2D should happen directly after your first call to glBindFramebuffer. This may not be your only problem, but it seems to be a significant one.

Resources