Jump to content
  • Advertisement
Sign in to follow this  
blake5634

OpenGL Texture Mapping prob in OpenGL

This topic is 3812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been banging my head against this one for about four days! I've got a basic OpenGL app working pretty well inside a QGLWidget (Qt environment). Now I want to add texture mapping to the polygons. No matter what I do they come out blank. I've found other apps both with and without Qt that I have downloaded and compiled and they CAN do texture mapping. Naturally I have carefully compared all my initializations etc and my code all seems correct. The FIRST sign of trouble is the call to glGenTextures(int, *int). This call is supposed to place a free texture ID in the second int but in my app it does not modify the second arg at all. I have found on the net that you do not HAVE to use glGenTextures if you keep track of tags yourself. Since I have only one texture I tried this to no avail. I figure the focus should be on the glGenTextures problem since in the other apps I can compile it cheerfully returns 1. I also read that this can happen if there is not a valid opengl context. So, consider the following code:
  jpeg_tex->id = 0x99;
     fprintf(stderr, "Asking for a texture ID: (current val: %X)\n", jpeg_tex->id);
   fprintf(stderr,"loadJPEGTexture: ");
   if(isValid()) {
	fprintf(stderr, "valid GL Context\n");
        }
   else {
	fprintf(stderr, "*INVALID* GL Context\n");
        }

      glGenTextures (1, &jpeg_tex->id);
 
     fprintf(stderr, "Got texture ID: %X  Error code: %X  GL_No_Error = %X\n", jpeg_tex->id, glGetError(), GL_NO_ERROR);
      if(jpeg_tex->id == 0x99) {
        fprintf(stderr,"glGenTextures does NOT act like it has a valid OpenGL context ...  exiting.\n");
        exit(1);
       }
      glBindTexture (GL_TEXTURE_2D, jpeg_tex->id);

When this runs it produces
Gl_model: valid GL Context
ReadJPEGFromFile: opened "bh.jpg"!
Texture Image Dimensions: 256 x 256  (bh.jpg)
Asking for a texture ID: (current val: 99)
loadJPEGTexture: valid GL Context
Got texture ID: 99  Error code: 0  GL_No_Error = 0
glGenTextures does NOT act like it has a valid OpenGL context ...  exiting.

I have found an app on the net which compiles and works and I have carefully compared all the initializations and calls and made them the same, yet still A) glGenTextures doesn't return an ID and B) textures don't render. Any ideas out there? THX

Share this post


Link to post
Share on other sites
Advertisement
I don't see anything wrong in there, glGenTextures should return a different name. So if you have a valid context, etc. then it might be a driver bug? Have you updated your opengl drivers?

Share this post


Link to post
Share on other sites
Thanks Arex,

The thing is, I have another app (mview) that I downloaded from internet, which I CAN compile and DOES work. In that app, glGenTextures consistently works and the texture does appear. So I think I have the right libraries.

What I have been doing is replacing my blocks of code with analogous blocks from mview so that I am initializing OpenGl the exact same way and calling everything exactly the same.

What are remaining differences? Well in my app I am building it with Qt's standard qmake Makefile. Mview has it's own hand crafted makefile. Do I have to dig into that? If something were wrong there, wouldn't errors show up?

Share this post


Link to post
Share on other sites
There might be a hidden error here. glGenTextures fails if either n is negative (GL_INVALID_VALUE) or it was called between glBegin() and glEnd() (GL_INVALID_OPERATION). n clearly isn't negative, so I assume it's the latter case. Unfortunately glGetError() won't help you find this one because the glGetError man page says:

Quote:
GL_INVALID_OPERATION is generated if glGetError is executed between the execution of glBegin and the corresponding execution of glEnd. In this case glGetError returns 0.


That might explain this behaviour of failing while saying everything is fine. I think it's possible you forgot an glEnd() somewhere.

Share this post


Link to post
Share on other sites
Kolrabi,

Nice idea - thanks. However, this is not possible because all the code takes place BEFORE any drawing, i.e. before glBegin or glEnd is ever called. Just to make sure I greppped them and there are no stray glBegins.

My latest thinking is that it must be something in the way the app is set up within Qt. The only difference I can see at this point is that mview (the working app) starts out like:

[source lang=c++]
class MView: public QMainWindow
{
Q_OBJECT


while mine looks like
[source lang=c++]
Gl_model::Gl_model(QWidget *parent) : QGLWidget(parent)
{



This is because in Gl_model opengl code is in a secondary window instead of the "QMainWindow". Any Qt experts know if this is the cause?

Share this post


Link to post
Share on other sites
Kolrabi,

Nice idea - thanks. However, this is not possible because all the code takes place BEFORE any drawing, i.e. before glBegin or glEnd is ever called. Just to make sure I greppped them and there are no stray glBegins.

My latest thinking is that it must be something in the way the app is set up within Qt. The only difference I can see at this point is that mview (the working app) starts out like:


class MView: public QMainWindow
{
Q_OBJECT


while mine looks like

Gl_model::Gl_model(QWidget *parent) : QGLWidget(parent)
{



This is because in Gl_model opengl code is in a secondary window instead of the "QMainWindow". Any Qt experts know if this is the cause?

Share this post


Link to post
Share on other sites
I'm not a QT expert but the issues are the same no matter what wrapper and OS you use. If you are creating another GL window and your program is single threaded, you need to make the GL context of the first window non current and in the second window, make the GL context current.
There can only be 1 GL context current per thread.

Just stick a call to glXMakeCurrent (X window) or wglMakeCurrent (Windows) in the right place.

Share this post


Link to post
Share on other sites
It says here:
http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/gentextures.html
that:
The generated textures have no dimensionality; they assume the dimensionality of the texture target to which they are first bound.
I am by no means a C expert. Does that mean that it wouldn't work anyway? Perhaps this means there is no problem?
HTH
G

Share this post


Link to post
Share on other sites
Thanks V-Man!! This was the missing piece! Even though the code was based on an example in the Qt documentation (without the texture mapping)!

However just for reference, in Qt the call is makeCurrent(); with no args.

I've been banging my head against this one for over a week!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!