c++ - OpenGL Texture not loading properly -
i'm following set of tutorials , i've hit point code i've copied line line stopped working. i'm trying render bmp image loaded, when displaying image, multicolored lines showing, instead of loaded texture.
heres 'texture' when being displayed :
messed texture http://i40.tinypic.com/10hlg1v.jpg , here supposed :
correct texture http://i40.tinypic.com/16hp0tz.jpg
here code loading bitmap :
file* fp; fp = fopen(filename, "r"); if (!fp) { perror("file not read"); fclose(fp); return null; } char* headerfield = new char[2]; fread(headerfield,2,sizeof(char),fp); if (strcmp(headerfield, "bm")) { delete[] headerfield; perror("file not bitmap"); fclose(fp); return null; } delete[] headerfield; unsigned int bmpdatalocation; unsigned int bmpwidth; unsigned int bmpheight; unsigned short numcolorplanes; unsigned short bitsperpixel; unsigned int compressionmethod; unsigned int bmpdatasize; fseek(fp, 0x000a, seek_set); fread(&bmpdatalocation, 1, sizeof(unsigned int), fp); fseek(fp, 0x0012, seek_set); fread(&bmpwidth, 1, sizeof(unsigned int), fp); fread(&bmpheight, 1, sizeof(unsigned int), fp); fread(&numcolorplanes, 1, sizeof(unsigned short), fp); fread(&bitsperpixel, 1, sizeof(unsigned short), fp); fread(&compressionmethod, 1, sizeof(unsigned int), fp); fread(&bmpdatasize, 1, sizeof(unsigned int), fp); if (numcolorplanes != 1 || bitsperpixel != 24 || compressionmethod != 0) { cout << "file not raw bmp24" << endl; fclose(fp); return null; } unsigned char* bmpdata = new unsigned char[bmpdatasize]; fseek(fp, bmpdatalocation, seek_set); fread(&bmpdata, bmpdatasize, sizeof(unsigned char), fp); fclose(fp); this texture parameter things i'm using :
glgentextures(1, &textureid); glbindtexture(gl_texture_2d, textureid); glteximage2d(gl_texture_2d, 0, gl_rgba, w, h, 0, format, gl_unsigned_byte, data); gltexparameteri(gl_texture_2d, gl_texture_wrap_s, gl_repeat); gltexparameteri(gl_texture_2d, gl_texture_wrap_t, gl_repeat); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); glbindtexture(gl_texture_2d, 0); and here code rendering , displaying :
glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glloadidentity(); //camara transformations glrotatef(camara::rotation.x, 1, 0, 0); glrotatef(camara::rotation.y, 0, 1, 0); glrotatef(camara::rotation.z, 0, 0, 1); gltranslatef(-camara::position.x, -camara::position.y, -camara::position.z); glbegin(gl_triangles); glcolor3f(1.0f, 0.0f, 0.0f); glvertex3f(-1, 0, -3); glcolor3f(0.0f, 1.0f, 0.0f); glvertex3f(0, 2, -3); glcolor3f(0.0f, 0.0f, 1.0f); glvertex3f(1, 0, -3); glend(); glbindtexture(gl_texture_2d, tex->gettextureid()); glbegin(gl_quads); glcolor3f(1, 1, 1); gltexcoord2f(100, 100); glvertex3f(100, 0, 100); gltexcoord2f(-100, 100); glvertex3f(-100, 0, 100); gltexcoord2f(-100, -100); glvertex3f(-100, 0, -100); gltexcoord2f(100, -100); glvertex3f(100, 0, -100); glend(); glbegin(gl_quads); glcolor3f(1, 1, 1); gltexcoord2f(100, 100); glvertex3f(100, 10, 100); gltexcoord2f(-100, 100); glvertex3f(-100, 10, 100); gltexcoord2f(-100, -100); glvertex3f(-100, 10, -100); gltexcoord2f(100, -100); glvertex3f(100, 10, -100); glend(); glbindtexture(gl_texture_2d, 0); glutswapbuffers(); all normal gl setup things done, because left on messed texture triangle.
edit : apparently messed called texture random, looks : messed texture 2 http://i40.tinypic.com/w1d00z.jpg
this if wanted sky , floor inside of computer, dont want that...
edit : heres code in main function
//init glut , opengl/al glutinit(&argc, argv); glutinitwindowposition(0, 0); glutinitwindowsize(window_width,window_height); glutinitdisplaymode(glut_rgba | glut_double | glut_depth); //create window stuffs glutcreatewindow(window_title); //setup glut callback functions glutdisplayfunc(display); glutreshapefunc(reshape); glutidlefunc(display); glutkeyboardfunc(keyboard::keydown); glutkeyboardupfunc(keyboard::keyup); glutmotionfunc(mouse::move); glutpassivemotionfunc(mouse::move); //enable features glenable(gl_depth_test); glenable(gl_texture_2d); glgeterror(); //setup subcontent tex = texture::loadbitmap("texture.bmp"); if (!tex) { return 1; } camara::position.y = 1.6f; //enter main loop glutmainloop(); return 0;
your problem should on how you're reading data, opengl code , texture binding seems correct me. point out part think have problem bmp loading, that's c 1 , not c++.
anyways suspect on reading of header, instance numberofplanes, bitsperpixel, typecompression have read 2 bytes each one, see 1 on side. wiki contains such specifics.
once done it, advice you, order of bytes data flipped bgr format, on side need take care of (either on texture binding or while loading image).
as mentioned on comments, it's best if use third party library (devil, freeimage, whatever) creating own loader every format gonna take quit time, don't think stick bmp specially 1 of relevant opengl features, texture compression.
Comments
Post a Comment