3
votes

I am trying to add a texture to a sphere, it works, but I keep getting an error. Below is a picture of what I get,

enter image description here

I don't know what this GL error: Invalid operation error is referring to. I think it has to do with my code. I'm not sure if I'm reading from my file correctly.

Here is the code I am using to draw this sphere.

glTranslate(0.0f, 0.0f, 0.0f);
glRotate(sunRot,0.0,0.0,1.0);
drawAroundPlanetGrid(100, 1, 50, 50, 1);
glPopMatrix();`

Here is what the drawAroundPlanetGrid() looks like.

void drawAroundPlanetGrid(double radius, int overSample, int numStrips, int numQuads, float radiusRatio)
{
GLMaterial test;
GLuint surf;
GLuint* surface;
Images::RGBImage surfaceImage;

surfaceImage=Images::readImageFile("/home/rodrtu/Desktop/SolarSystem/EarthTopography.png",Vrui::openFile("/home/rodrtu/Desktop/SolarSystem/EarthTopography.png"));

glEnable(GL_TEXTURE_2D);

//glGenTextures(1, surface);

glBindTexture(GL_TEXTURE_2D, surf);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

surfaceImage.glTexImage2D(GL_TEXTURE_2D,0,GL_RGB);

double b = radius * 0.9966463577;//Geoid's semi-minor axis
double e2 = 0.0067; //Geoid's squared eccentricity, derived from flattening factor

float texY1=float(0)/float(numStrips);
double lat1=(Math::Constants<double>::pi*double(0))/double(numStrips)-0.5*Math::Constants<double>::pi;
double s1=Math::sin(lat1);
double c1=Math::cos(lat1);
double chi1=Math::sqrt(1.0-e2*s1*s1);
double xy1=radius*radiusRatio/chi1*c1;
double z1=radius*(1.0-e2)/chi1*s1;

//* Draw latitude quad strips: *
for(int i=1;i<numStrips+1;++i)
    {
    float texY0=texY1;
    double s0=s1;
    double c0=c1;
    double xy0=xy1;
    double z0=z1;
    texY1=float(i)/float(numStrips);
    lat1=(Math::Constants<double>::pi*double(i))/double(numStrips)-0.5*Math::Constants<double>::pi;
    s1=Math::sin(lat1);
    c1=Math::cos(lat1);
    chi1=Math::sqrt(1.0-e2*s1*s1);
    xy1=radius/chi1*c1;
    z1=radius*(1.0-e2)/chi1*s1;

    glBegin(GL_QUAD_STRIP);
    for(int j=0;j<=numQuads;++j)
    {
    float texX=float(j)/float(numQuads)+0.5f;
    double lng=(2.0*Math::Constants<double>::pi*double(j))/double(numQuads);
    double cl=Math::cos(lng)*radiusRatio;
    double sl=Math::sin(lng)*radiusRatio;

    glTexCoord2f(texX,texY1);
    glNormal3d(c1*cl,c1*sl,s1);
    glVertex3d(xy1*cl,xy1*sl,z1);

    glTexCoord2f(texX,texY0);
    glNormal3d(c0*cl,c0*sl,s0);
    glVertex3d(xy0*cl,xy0*sl,z0);
    }
    glEnd();
    }
    glEndList();
}

I apologize if my code is confusing to read. Let me know if you see what is wrong with my code. I tried getting read of this error but it still shows up. Any ideas why?

2
I didn't see you calling glTexImage* to load your image into graphic memory ram - Amadeus
it's there: surfaceImage.glTexImage2D(GL_TEXTURE_2D,0,GL_RGB); - TRod
Images? What object is that? - Amadeus
Suggest you to run your program under a debugger and catch the function that is generating this error. - Amadeus
Silly question, but are you pushing a matrix onto the stack at any point? I see where you pop one, but maybe I'm missing where you pushed one on prior to that. - Darius Makaitis

2 Answers

1
votes

As the documentation of glBindTexture states "You must use glGenTextures to generate a set of new texture names.". That means you have to fill your "surface" variable with a valid texture name first. That is probably where your GL error comes from. Secondly you unbind the texture again before using it by calling "glBindTexture(GL_TEXTURE_2D,0)". Third, to figure out where what is causing the GL error on your own it's a good idea to create a macro that calls glGetError and prints a log message if there was an error. Than you can spread that macro between calls to the GL to narrow down where the error occured.

0
votes

The problem in this code was this line glEndList();. Once I removed it the program worked without any errors.