PDA

View Full Version : OpenGL textured quad not displaying correctly



vmsgman
1st May 2011, 01:55
This code is a port from a working project I did in OpenTK. The quad doesn't seem to display properly. Any help you can give would be great.

stampede
2nd May 2011, 10:04
Come on man, you expect someone to dig your project's source, because something is not working as you wish. You didn't even bother to tell what's the problem exactly, no rendering OpenGL code, nothing.

Any help you can give would be great
Yeah, I can give you a hint - be more specific.

vmsgman
2nd May 2011, 15:41
No Problem, sorry I don't usually ask for help. I just thought including all of the code would make it easier to run and see. I will post a couple of screen shots and the texurize function code. I hope that will help. I attached a working OpenTK application so you can see what the image should look like when rendered properly. Again apologies for the lack of details ...


void GLWidget::texturize()
{
for (uint i = 0; i < imageArrayLength; i++)
dspArray[i] = lutArray[imageArray[i]];
/*ushort val;
for (uint i = 0; i < imageArrayLength; i++)
{
val = imageArray[i];
dspArray[i] = lutArray[val];
}*/

glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);

// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR );
// when texture area is large, bilinear filter the original
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

// the texture wraps over at the edges (repeat)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );

//glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, image_array2D );
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, imageSize.width(), imageSize.height(), 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, dspArray );
}


void GLWidget::paintGL()
{
//glClear(GL_COLOR_BUFFER_BIT);
//qglColor(Qt::blue);
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();

glScalef(scale, scale, 1.0f);
glTranslated(-centerImage.x(), centerImage.y() - height, 0.0f);

//glColor3i(1.0,1.0,1.0);
qDebug() << "-centerImage.x(): " << -centerImage.x() << " centerImage.y() - height: " << centerImage.y() - height;

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, (GLfloat)height);
glTexCoord2f(1.0f, 0.0f); glVertex2f((GLfloat)width, (GLfloat)height);
glTexCoord2f(1.0f, 1.0f); glVertex2f((GLfloat)width, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, 0.0f);
glEnd();

swapBuffers();
}


void GLWidget::resizeGL(int w, int h)
{
width = w;
//height = h;
height = h?h:1;
w2 = (double)width / 2;
h2 = (double)height / 2;

scale = 1024;

while(imageSize.width() * scale > width || imageSize.height() * scale > height)
scale /= SCALE_FACTOR;

qDebug() << "Scale: " << scale;

centerImage.setX((int)w2);
centerImage.setY((int)h2);

glViewport(0, 0, (GLint)width, (GLint)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-w2, w2, -h2, h2, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

stampede
2nd May 2011, 20:16
Ok, sorry for being rude, this forum is just full of guys asking to do their homework for them :)
Are you sure this is a display problem ? Have you tried to convert your image to QImage and save it on disk ? For me it looks like wrong image format is selected for display, so if you manage to convert your image to QImage, you'll know which image format to choose.
I think your image reading method needs corrections, because you allocate an array of size "aLen" and then read the file content with max size set to "len", which is less than allocated size, so resulting data can be deficient.

vmsgman
2nd May 2011, 20:41
No worries, I've been out of school for years, way to many to mention. This is a project I am working on for work. I just wanted to say that so you know. So I understand any level of help, is given with that in mind. I know people's time is valuable. So thanks for helping me. So the image data type is ushort (unsigned short integer) 16 bits per pixel. There is a header that has some information in it. height, width, etc. it is 2048 bytes long. after that it goes from top left to bottom right corner in ushorts. The read function needs a length which since I have already read the header if the file type is .viv, which is what I attached in the zip file. Then we read the header and then fill in the image array. I believe I have tested the imageArray and the dspArray (this is simply a window/leveled (256) version of the imageArray) and have good values. It just happens to not display correctly. So I am thinking that I must have something wrong with my OpenGL code, but since I am asking for help, I will explain the question you asked above.

len = width * height // Of the image
aLen = # of bytes needed to read to fill in the image array, which is width * height * 2 (2 bytes per pixel)

Thanks for your help!

stampede
3rd May 2011, 11:26
Alright, I know what's the problem - when you call "texturize", the glwidget's opengl context is not current, so you have to call texturize() inside initializeGL(), or call makeCurrent() in texturize() yourself (before any OpenGL calls). Image is displayed correctly (I mean looks like the one attached to your post) after one of above modifications. There are some problems when resizing, but I guess that's separate issue.

vmsgman
4th May 2011, 00:23
Thanks alot, I can work on the other stuff. I certainly appreciate the help!:)