Jump to content
  • Advertisement
Sign in to follow this  
Tim Stoddard

OpenGL OpenGL GLUT, C++ with classes and Textures that don't appear

This topic is 2305 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

[color=#282828][font=helvetica, arial, sans-serif]For anyone who knows how to use C++, OpenGL and GLUT if possible.[/font]

[color=#000000][font=helvetica, arial, sans-serif]I'm having problems trying to load textures in an OpenGL GLUT project using classes. Here's some code that includes the texturing stuff:[/font]
[color=#000000][font=helvetica, arial, sans-serif]Declaring a Textured Model from a subclass of a model class.[/font]

[color=#000000][font=helvetica, arial, sans-serif]TextureModel * title = new TextureModel("Box.obj", "title.raw");[/font]

[color=#000000][font=helvetica, arial, sans-serif]Constructor method of TextureModel subclass:[/font]

[font=arial,helvetica,sans-serif][color=#2B91AF][background=transparent]TextureModel[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]string fName[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] string tName[/background][color=#282828][background=transparent])[/background][color=#282828][background=transparent] [/background][color=#282828][background=transparent]:[/background][color=#282828][background=transparent] [/background][color=#2B91AF][background=transparent]Model[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]fName[/background][color=#282828][background=transparent]),[/background][color=#282828][background=transparent] textureFile[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]tName[/background][color=#282828][background=transparent])[/background]
[color=#282828][background=transparent]{[/background]
[color=#282828][background=transparent]material newMat [/background][color=#282828][background=transparent]=[/background][color=#282828][background=transparent] [/background][color=#282828][background=transparent]{{[/background][color=#800000][background=transparent]0.63[/background][color=#282828][background=transparent],[/background][color=#800000][background=transparent]0.52[/background][color=#282828][background=transparent],[/background][color=#800000][background=transparent]0.1[/background][color=#282828][background=transparent],[/background][color=#800000][background=transparent]1.0[/background][color=#282828][background=transparent]},{[/background][color=#800000][background=transparent]0.63[/background][color=#282828][background=transparent],[/background][color=#800000][background=transparent]0.52[/background][color=#282828][background=transparent],[/background][color=#800000][background=transparent]0.1[/background][color=#282828][background=transparent],[/background][color=#800000][background=transparent]1.0[/background][color=#282828][background=transparent]},{[/background][color=#800000][background=transparent]0.2[/background][color=#282828][background=transparent],[/background][color=#800000][background=transparent]0.2[/background][color=#282828][background=transparent],[/background][color=#800000][background=transparent]0.05[/background][color=#282828][background=transparent],[/background][color=#800000][background=transparent]0.5[/background][color=#282828][background=transparent]},[/background][color=#800000][background=transparent]10[/background][color=#282828][background=transparent]};[/background]
[color=#2B91AF][background=transparent]Material[/background][color=#282828][background=transparent] [/background][color=#282828][background=transparent]=[/background][color=#282828][background=transparent] newMat[/background][color=#282828][background=transparent];[/background]
[color=gray][background=transparent]// enable texturing[/background]
[color=#282828][background=transparent]glEnable[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]GL_TEXTURE_2D[/background][color=#282828][background=transparent]);[/background]

[color=#282828][background=transparent]loadcolTexture[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]textureFile[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]glGenTextures[/background][color=#282828][background=transparent]([/background][color=#800000][background=transparent]1[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] [/background][color=#282828][background=transparent]&[/background][color=#282828][background=transparent]textureRef[/background][color=#282828][background=transparent]);[/background]
[color=gray][background=transparent]// specify the filtering method[/background]
[color=#282828][background=transparent]glTexParameteri[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]GL_TEXTURE_2D[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] GL_TEXTURE_WRAP_S[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] GL_REPEAT[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]glTexParameteri[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]GL_TEXTURE_2D[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] GL_TEXTURE_WRAP_T[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] GL_REPEAT[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]glTexParameteri[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]GL_TEXTURE_2D[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] GL_TEXTURE_MAG_FILTER[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] GL_LINEAR[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]glTexParameteri[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]GL_TEXTURE_2D[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] GL_TEXTURE_MIN_FILTER[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] GL_LINEAR[/background][color=#282828][background=transparent]);[/background]
[color=gray][background=transparent]// associate the image read in to the texture to be applied[/background]
[color=#282828][background=transparent]gluBuild2DMipmaps[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]GL_TEXTURE_2D[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] [/background][color=#800000][background=transparent]3[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] [/background][color=#800000][background=transparent]256[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] [/background][color=#800000][background=transparent]256[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] GL_RGB[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] GL_UNSIGNED_BYTE[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] image_array[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]}[/background]

[color=#000000]Texture loading function to read in data in RAW file:
[color=#00008B][background=transparent]int[/background][color=#282828][background=transparent] loadcolTexture[/background][color=#282828][background=transparent]([/background][color=#00008B][background=transparent]const[/background][color=#282828][background=transparent] string fileName[/background][color=#282828][background=transparent])[/background][color=#282828][background=transparent] [/background][color=#282828][background=transparent]{[/background]
[color=#282828][background=transparent]ifstream inFile[/background][color=#282828][background=transparent];[/background]
[color=#282828][background=transparent]inFile[/background][color=#282828][background=transparent].[/background][color=#282828][background=transparent]open[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]fileName[/background][color=#282828][background=transparent].[/background][color=#282828][background=transparent]c_str[/background][color=#282828][background=transparent](),[/background][color=#282828][background=transparent] ios[/background][color=#282828][background=transparent]::[/background][color=#282828][background=transparent]binary [/background][color=#282828][background=transparent]);[/background]

[color=#00008B][background=transparent]if[/background][color=#282828][background=transparent] [/background][color=#282828][background=transparent](![/background][color=#282828][background=transparent]inFile[/background][color=#282828][background=transparent].[/background][color=#282828][background=transparent]good[/background][color=#282828][background=transparent]())[/background]
[color=#282828][background=transparent]{[/background]
[color=#282828][background=transparent]cerr [/background][color=#282828][background=transparent]<<[/background][color=#282828][background=transparent] [/background][color=#800000][background=transparent]"Can't open texture file "[/background][color=#282828][background=transparent] [/background][color=#282828][background=transparent]<<[/background][color=#282828][background=transparent] fileName [/background][color=#282828][background=transparent]<<[/background][color=#282828][background=transparent] endl[/background][color=#282828][background=transparent];[/background]
[color=#00008B][background=transparent]return[/background][color=#282828][background=transparent] [/background][color=#800000][background=transparent]1[/background][color=#282828][background=transparent];[/background]
[color=#282828][background=transparent]}[/background]
[color=#282828][background=transparent]inFile[/background][color=#282828][background=transparent].[/background][color=#282828][background=transparent]seekg [/background][color=#282828][background=transparent]([/background][color=#800000][background=transparent]0[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] ios[/background][color=#282828][background=transparent]::[/background][color=#282828][background=transparent]end[/background][color=#282828][background=transparent]);[/background]
[color=#00008B][background=transparent]int[/background][color=#282828][background=transparent] size [/background][color=#282828][background=transparent]=[/background][color=#282828][background=transparent] inFile[/background][color=#282828][background=transparent].[/background][color=#282828][background=transparent]tellg[/background][color=#282828][background=transparent]();[/background]
[color=#282828][background=transparent]image_array [/background][color=#282828][background=transparent]=[/background][color=#282828][background=transparent] [/background][color=#00008B][background=transparent]new[/background][color=#282828][background=transparent] [/background][color=#00008B][background=transparent]char[/background][color=#282828][background=transparent] [/background][color=#282828][background=transparent][[/background][color=#282828][background=transparent]size[/background][color=#282828][background=transparent]];[/background]
[color=#282828][background=transparent]inFile[/background][color=#282828][background=transparent].[/background][color=#282828][background=transparent]seekg [/background][color=#282828][background=transparent]([/background][color=#800000][background=transparent]0[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] ios[/background][color=#282828][background=transparent]::[/background][color=#282828][background=transparent]beg[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]inFile[/background][color=#282828][background=transparent].[/background][color=#282828][background=transparent]read [/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]image_array[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] size[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]inFile[/background][color=#282828][background=transparent].[/background][color=#282828][background=transparent]close[/background][color=#282828][background=transparent]();[/background]
[color=#00008B][background=transparent]return[/background][color=#282828][background=transparent] [/background][color=#800000][background=transparent]0[/background][color=#282828][background=transparent];}[/background]

[color=#000000]Method to draw the triangles:

[color=#00008B][background=transparent]virtual[/background][color=#282828][background=transparent] [/background][color=#00008B][background=transparent]void[/background][color=#282828][background=transparent] drawTriangle[/background][color=#282828][background=transparent]([/background][color=#00008B][background=transparent]int[/background][color=#282828][background=transparent] f1[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] [/background][color=#00008B][background=transparent]int[/background][color=#282828][background=transparent] f2[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] [/background][color=#00008B][background=transparent]int[/background][color=#282828][background=transparent] f3[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] [/background][color=#00008B][background=transparent]int[/background][color=#282828][background=transparent] t1[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] [/background][color=#00008B][background=transparent]int[/background][color=#282828][background=transparent] t2[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] [/background][color=#00008B][background=transparent]int[/background][color=#282828][background=transparent] t3[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] [/background][color=#00008B][background=transparent]int[/background][color=#282828][background=transparent] n1[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] [/background][color=#00008B][background=transparent]int[/background][color=#282828][background=transparent] n2[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] [/background][color=#00008B][background=transparent]int[/background][color=#282828][background=transparent] n3[/background][color=#282828][background=transparent])[/background]
[color=#282828][background=transparent]{[/background]
[color=#282828][background=transparent]glColor3f[/background][color=#282828][background=transparent]([/background][color=#800000][background=transparent]1.0[/background][color=#282828][background=transparent],[/background][color=#800000][background=transparent]1.0[/background][color=#282828][background=transparent],[/background][color=#800000][background=transparent]1.0[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]glBegin[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]GL_TRIANGLES[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]glBindTexture[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]GL_TEXTURE_2D[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] textureRef[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]glNormal3fv[/background][color=#282828][background=transparent](&[/background][color=#282828][background=transparent]normals[/background][color=#282828][background=transparent][[/background][color=#282828][background=transparent]n1[/background][color=#282828][background=transparent]].[/background][color=#282828][background=transparent]x[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]glTexCoord2f[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]textures[/background][color=#282828][background=transparent][[/background][color=#282828][background=transparent]t1[/background][color=#282828][background=transparent]].[/background][color=#282828][background=transparent]u[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] textures[/background][color=#282828][background=transparent][[/background][color=#282828][background=transparent]t1[/background][color=#282828][background=transparent]].[/background][color=#282828][background=transparent]v[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]glVertex3fv[/background][color=#282828][background=transparent](&[/background][color=#2B91AF][background=transparent]Model[/background][color=#282828][background=transparent]::[/background][color=#282828][background=transparent]vertices[/background][color=#282828][background=transparent][[/background][color=#282828][background=transparent]f1[/background][color=#282828][background=transparent]].[/background][color=#282828][background=transparent]x[/background][color=#282828][background=transparent]);[/background]

[color=#282828][background=transparent]glNormal3fv[/background][color=#282828][background=transparent](&[/background][color=#282828][background=transparent]normals[/background][color=#282828][background=transparent][[/background][color=#282828][background=transparent]n2[/background][color=#282828][background=transparent]].[/background][color=#282828][background=transparent]x[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]glTexCoord2f[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]textures[/background][color=#282828][background=transparent][[/background][color=#282828][background=transparent]t2[/background][color=#282828][background=transparent]].[/background][color=#282828][background=transparent]u[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] textures[/background][color=#282828][background=transparent][[/background][color=#282828][background=transparent]t2[/background][color=#282828][background=transparent]].[/background][color=#282828][background=transparent]v[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]glVertex3fv[/background][color=#282828][background=transparent](&[/background][color=#2B91AF][background=transparent]Model[/background][color=#282828][background=transparent]::[/background][color=#282828][background=transparent]vertices[/background][color=#282828][background=transparent][[/background][color=#282828][background=transparent]f2[/background][color=#282828][background=transparent]].[/background][color=#282828][background=transparent]x[/background][color=#282828][background=transparent]);[/background]

[color=#282828][background=transparent]glNormal3fv[/background][color=#282828][background=transparent](&[/background][color=#282828][background=transparent]normals[/background][color=#282828][background=transparent][[/background][color=#282828][background=transparent]n3[/background][color=#282828][background=transparent]].[/background][color=#282828][background=transparent]x[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]glTexCoord2f[/background][color=#282828][background=transparent]([/background][color=#282828][background=transparent]textures[/background][color=#282828][background=transparent][[/background][color=#282828][background=transparent]t3[/background][color=#282828][background=transparent]].[/background][color=#282828][background=transparent]u[/background][color=#282828][background=transparent],[/background][color=#282828][background=transparent] textures[/background][color=#282828][background=transparent][[/background][color=#282828][background=transparent]t3[/background][color=#282828][background=transparent]].[/background][color=#282828][background=transparent]v[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]glVertex3fv[/background][color=#282828][background=transparent](&[/background][color=#2B91AF][background=transparent]Model[/background][color=#282828][background=transparent]::[/background][color=#282828][background=transparent]vertices[/background][color=#282828][background=transparent][[/background][color=#282828][background=transparent]f3[/background][color=#282828][background=transparent]].[/background][color=#282828][background=transparent]x[/background][color=#282828][background=transparent]);[/background]
[color=#282828][background=transparent]glEnd[/background][color=#282828][background=transparent]();[/background]
[color=#282828][background=transparent]}[/background][/font]

[color=#000000][font=helvetica, arial, sans-serif]I also have Lighting, Depth Testing and Double Buffering enabled.[/font]
[color=#000000][font=helvetica, arial, sans-serif]Models and Lighting work fine, but the textures don't appear. Any reasons why it won't work will be great.[/font][color=#282828][font=helvetica, arial, sans-serif] [/font]

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!