gpt4 book ai didi

c++ - 球体纹理映射(opengl 和 c++)

转载 作者:太空狗 更新时间:2023-10-29 23:06:12 26 4
gpt4 key购买 nike

我正在尝试向球体添加纹理,它有效,但我不断收到错误消息。下面是我得到的图片,

enter image description here

我不知道这个 GL error: Invalid operation 错误指的是什么。我认为这与我的代码有关。我不确定我是否正确地读取了我的文件。

这是我用来绘制这个球体的代码。

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

这是 drawAroundPlanetGrid() 的样子。

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();
}

如果我的代码难以阅读,我深表歉意。如果您发现我的代码有什么问题,请告诉我。我尝试阅读此错误,但它仍然出现。有什么想法吗?

最佳答案

作为 glBindTexture 的文档声明“您必须使用 glGenTextures 生成一组新的纹理名称。”。这意味着您必须先用有效的纹理名称填充“表面”变量。这可能是您的 GL 错误的来源。其次,在调用“glBindTexture(GL_TEXTURE_2D,0)”之前再次解除绑定(bind)纹理。第三,要自行找出导致 GL 错误的原因,最好创建一个宏来调用 glGetError 并在出现错误时打印日志消息。比您可以在对 GL 的调用之间传播该宏以缩小错误发生的位置。

关于c++ - 球体纹理映射(opengl 和 c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16656776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com