c++ - OpenGL Textures rendering upside down, and blurred -


i developing game earlier , came across bit of issue! had started game single images. each texture had it's own file. bit realized how of idiot that, , tried switch on using 1 picture contains textures ( they're quite small anyway ). now, problem arose. when larger image original texture ( 10 x 10 ) loaded in, it's if it's not being displayed nearest neighbor sizing algorithm. blurred image. it's flipped... why either of these issues happening?

code , images below.

here first showing sprite sheet containing text , symbols. (it's quite small, 100 x 100 ) http://imgur.com/hu5zeao.jpg

this when program run. http://i.imgur.com/hevpdpy.jpg

here main.cpp file

#include <gl/glew.h> #include <gl/glut.h>  #include "spritesheet_class.h" #include "load_img.h"  gluint textsheet; spritesheet test;  void loadfiles() {      gluint textsheet = loadtexture( "davetica.png", 100, 100, 0 );      test.settex( textsheet, 100, 100, 10, 10 );  }  void reshape( int width, int height ) {      glviewport( 0, 0, (glsizei)width, (glsizei)height );     glmatrixmode( gl_projection );      glloadidentity();     glortho( 0.0f, width, 0.0f, height, 1.0f, 100.0f );      glmatrixmode( gl_modelview );  }  void display() {      glclear( gl_color_buffer_bit );     glloadidentity();      gltranslatef( 0.0f, 0.0f, -1.0f );      test.drawsprite( 1, 0, 0, 400, 400 );      glutswapbuffers();  }  int main( int argc, char **argv ) {      glutinit( &argc, argv );     glutinitdisplaymode( glut_double );      glutinitwindowposition( 200, 100 );     glutinitwindowsize( 512, 512 );      glutcreatewindow( "sprite sheets" );      glutdisplayfunc( display );     glutidlefunc( display );     glutreshapefunc( reshape );      loadfiles();      glutmainloop();  } 

now here class header in write spritesheet class.

#ifndef spritesheet_class_h_included #define spritesheet_class_h_included  #include "load_img.h"  class spritesheet {      public:         void settex( gluint tex, int imgw, int imgh, int horzam, int vertam );          void drawsprite( int spritenum, int xloc, int yloc, int width, int height );      private:         gluint texture;         int imagewidth, imageheight, horiam, vertam;  };  void spritesheet::settex( gluint tex, int imgw, int imgh, int horzam, int vertam ) {      imagewidth = imgw;     imageheight = imgh;     horiam = horzam;     vertam = vertam;      texture = tex;  }  void spritesheet::drawsprite( int spritenum, int xloc, int yloc, int width, int height ) {      glenable( gl_texture_2d );      glbindtexture( gl_texture_2d, texture );      glbegin( gl_quads );          gltexcoord2f( 1.0f, 0.0f );         glvertex2f( xloc, yloc );          gltexcoord2f( 1.0f, 1.0f );         glvertex2f( xloc, yloc+height );          gltexcoord2f( 0.0f, 1.0f );         glvertex2f( xloc+width, yloc+height );          gltexcoord2f( 0.0f, 0.0f );         glvertex2f( xloc+width, yloc );      glend();      gldisable( gl_texture_2d );  }  #endif // spritesheet_class_h_included 

finally, if need this, here file handles loadimage function in use load , format textures.

#ifndef load_img_h_included #define load_img_h_included  #include <iostream> #include <stbimg.h> #include <string>  bool loadcorrectly = true;  using namespace std;  gluint loadtexture( const char *filename, int w, int h, int n ) {      unsigned char *data = stbi_load( filename, &w, &h, &n, 4 );     gluint texture;      if( data == null )     {          cout << "error: '"<< filename << "' has been modified or missing." << endl;         return 0;      }else     {          cout << "loaded: '" << filename << "' successfully." << endl;      }      glgentextures( 1, &texture );     glbindtexture( gl_texture_2d, texture );     gltexenvf( gl_texture_env, gl_texture_env_mode,gl_modulate );      gltexparameterf( gl_texture_2d, gl_texture_min_filter,gl_nearest );     gltexparameterf( gl_texture_2d, gl_texture_mag_filter,gl_nearest );      gltexparameterf( gl_texture_2d, gl_texture_wrap_s, gl_mirrored_repeat );     gltexparameterf( gl_texture_2d, gl_texture_wrap_t, gl_mirrored_repeat );      glubuild2dmipmaps( gl_texture_2d, 3, w, h,gl_rgba, gl_unsigned_byte, data );      free( data );      return texture; } 

edit: found out issue. image not square of two, therefore not displaying properly.

  • your images flipped because have not flipped them oriented properly. sounds tautological, opengl's coordinate system maps textures down (0,0) in bottom left , (1,1) in top right. fixing textbook type of exercise.

  • you need call glactivetexture(gl_texture0) make subsequent gl* calls know texture they're modifying (ie make texture active). initial value gl_texture0 i'd still put in don't end potential problems down line.

  • i know it's not part of question, you're using opengl code that's been deprecated 7 years. general suggestion, should switch programmable pipeline. here's link ogldev basics in modern opengl, or @ least of 4.0+. in 3's shaders better ffp. here's short few swiftless. search opengl 4.0 or later , googles guide you. it's steeper learning curve worth it.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -