OpenGL (using SOIL) always loads the same image - c++

When I load an image, the next image loaded for something else is the same image.
Basically:
I'm making a Pong replica and there is a texture for the paddle and for the ball (yes I have made sure that they are both different). You can find the source code here. The paddle loads an image fine, but when it comes to the ball it loads the same exact image as the paddle (Paddle = Paddle.cpp , Ball = Ball.cpp).
What could possibly be wrong with it? Am I using SOIL incorrectly?

From source:
glBindTexture(PadImg , GL_TEXTURE_2D);
This is way wrong, it should be
glBindTexture(GL_TEXTURE_2D, PadImg);
You should get familiar with glGetError, it will help you find problems like this.
Also, these calls are in the wrong order:
glVertex2f(-w,-10);
glTexCoord2f(0,0);
glVertex should always be the last function called per vertex (normals and texcoords come first).

Related

OpenGL glBindTexture() crash

The program freezes when I call glBindTexture(GL_TEXTURE_2D, _ID); in the draw(Sprite) method of the 'Renderer' class. (the actual code is sprite.getTexture()->bind(), but I have added a std::cout in that function before and after the glBindTexture() call and it only prints once).
I am struggling to understand why the program is freezing - when I call glBindTexture, the program doesn't respond and crashes. Rather than filing this page up with a long list of code, heres the link to the github: https://github.com/TheInfernalcow/OpenGL-game, the files which are relevant are mainly the src/graphics/renderer.cpp and src/graphics/texture.cpp.
If anyone has the time to read through the code and try and point me in the right direction I would be grateful, have been pondering over this for hours.
Is it Splashstate that is having issue? If so -
SplashState::SplashState(Game* game)
{
_game = game;
Texture2D backgroundTexture("res/darkguy.png", 96, 128);
Sprite _background;
_background.setTexture(backgroundTexture);
}
You are assigning a texture to the locally scoped Sprite, not the one on the SplashState -- so when you try to draw it in your render function, the class level Sprite has no texture.

C++ SFML Photo presentation

I've installed SFML on my VS2015. I know how to display sprite. Problem occured when I wanted to display another images in the same program.
My goal is to write program which samples without replacement numbers from specified range, shows result on the screen (with few seconds' breaks beetween each) then saves result in txt file. I've done this stage. I only want to display images during sampling. Another specified image for every chosen number.
I don't need code to copy-paste. I just want to get any advice. Which methods available in SFML could help me?
You should declare a sprite
( sf::Sprite sprite )
and a texture ( sf::Texture texture ). The, simply load the texture from a file( one of your images ) ( texture.loadFromFile( "img.png") and set the sprite's texture(sprite.setTexture( texture )). Now, if you draw the sprite on the screen you should see your full image. I'm 100% sure it works on Windows 10 ,but i don't know if it works on Mac OS or Linux.

Should I vertically flip the lines of an image loaded with stb_image to use in OpenGL?

I'm working on an OpenGL-powered 2d engine.
I'm using stb_image to load image data so I can create OpenGL textures. I know that the UV origin for OpenGL is bottom-left and I also intend to work in that space for my screen-space 2d vertices i.e. I'm using glm::ortho( 0, width, 0, height, -1, 1 ), not inverting 0 and height.
You probably guessed it, my texturing is vertically flipped but I'm 100% sure that my UV are specified correctly.
So: is this caused by stbi_load's storage of pixel data? I'm currently loading PNG files only so I don't know if it would cause this problem if I was using another file format. Would it? (I can't test right now, I'm not at home).
I really want to keep the screen coords in the "standard" OpenGL space... I know I could just invert the orthogonal projection to fix it but I would really rather not.
I can see two sane options:
1- If this is caused by stbi_load storage of pixel data, I could invert it at loading time. I'm a little worried about that for performance reason and because I'm using texture arrays (glTexture3d) for sprite animations meaning I would need to invert texture tiles individually which seems painful and not a general solution.
2- I could use a texture coordinate transformation to vertically flip the UVs on the GPU (in my GLSL shaders).
A possible 3rd option would be to use glPixelStore to specify the input data... but I can't find a way to tell it that the incoming pixels are vertically flipped.
What are your recommendations for handling my problem? I figured I can't be the only one using stbi_load + OpenGL and having that problem.
Finally, my target platforms are PC, Android and iOS :)
EDIT: I answered my own question... see below.
I know this question's pretty old, but it's one of the first results on google when trying to solve this problem, so I thought I'd offer an updated solution.
Sometime after this question was originally asked stb_image.h added a function called "stbi_set_flip_vertically_on_load", simply passing true to this function will cause it to output images the way OpenGL expects - thus removing the need for manual flipping/texture-coordinate flipping.
Also, for those who don't know where to get the latest version, for whatever reason, you can find it at github being actively worked on:
https://github.com/nothings/stb
It's also worth noting that in stb_image's current implementation they flip the image pixel-by-pixel, which isn't exactly performant. This may change at a later date as they've already flagged it for optimsation. Edit: It appears that they've swapped to memcpy, which should be a good bit faster.
Ok, I will answer my own question... I went thru the documentation for both libs (stb_image and OpenGL).
Here are the appropriate bits with reference:
glTexImage2D says the following about the data pointer parameter: "The first element corresponds to the lower left corner of the texture image. Subsequent elements progress left-to-right through the remaining texels in the lowest row of the texture image, and then in successively higher rows of the texture image. The final element corresponds to the upper right corner of the texture image." From http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml
The stb_image lib says this about the loaded image pixel: "The return value from an image loader is an 'unsigned char *' which points to the pixel data. The pixel data consists of *y scanlines of *x pixels, with each pixel consisting of N interleaved 8-bit components; the first pixel pointed to is top-left-most in the image." From http://nothings.org/stb_image.c‎
So, the issue is related the pixel storage difference between the image loading lib and OpenGL. It wouldn't matter if I loaded other file formats than PNG because stb_image returns the same data pointer for all formats it loads.
So I decided I'll just swap in place the pixel data returned by stb_image in my OglTextureFactory. This way, I keep my approach platform-independent. If load time becomes an issue down the road, I'll remove the flipping at load time and do something on the GPU instead.
Hope this helps someone else in the future.
Yes, you should. This can be easily accomplished by simply calling this STBI function before loading the image:
stbi_set_flip_vertically_on_load(true);
Since this is a matter of opposite assumptions between image libraries in general and OpenGL, Id say the best way is to manipulate the vertical UV-coord. This takes minimal effort and is always relevant when loading images using any image library and passing it to OpenGL.
Either feed tex-coords with 1.0f-uv.y in vertex-population OR reverse in shader.
fcol = texture2D( tex, vec2(uv.x,1.-uv.y) );

Loading a texture in GLM

hey im using GLM(made by nate robins) with SFML and opengl on mingw32 with the IDE as CodeBlocks(windows)
when loading my texture with the GLM from: http://www.3dcodingtutorial.com/Working-with-3D-models/Getting-GLM.html
hey i managed to get rid of the color problem by changing up my code to better load the textures, but now im not able to get the texture to display...
heres the NEW link to my main: http://pastebin.com/gasu1Hux
i have been looking up GLm tutorials but i cant find any correct answers about my texture not displaying at all.....
maybe im missing something?
/////////////////////OLD/////////////////////////////
also i tried the one from devernay.free.fr, but i always get a texture error
(not gonna post because everytime i do, my question gets downed...)
i had gotten a small glitch where my whole model is blue instead of the default gray...
i found out that the GLM library i have doesnt load textures by itself..
so i managed to find a texture loader from 3dcodingtutorial.com
when i load the texture its not put on the model, it just changes its color.
right now im wondering why my model is one single color instead of the texture i setup.
heres some of the code that i used to make the texture and draw the model:
ok heres the main.cpp
sorry wrong paste ._.
the paste has been updated!!
http://pastebin.com/tcwwasb9
The default GL_TEXTURE_ENV_MODE is GL_MODULATE. Make sure you aren't inadvertently setting your color state somewhere, or force the issue with glColor3ub(255,255,255) before you render something with a texture.
EDIT: GL_DECAL is also an option.

Texture Mapping C++ OpenGL

I have read around on this, including Nehe and here for solutions, but I cant find a specific answer.
I am trying to load a a photo, called stars.jpg. I want to make this my background of the scene, by mapping it using uv coordinates, doing it by
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(0,0);
However I am just very confused about how to load the actual textures in, all the calls for
glActiveTexture();
glEnable(GL_TEXTURE_2d);
glBindTexture(GL_TEXTURE);
All they do is confuse me, what do all these mean/do, and in what order am I suppose to put these in, in order to get the stars.jpg to be my background?
Your number-one tool for loading textures in OpenGL is the Simple OpenGL Image Loader (SOIL) library. You just need to pass the filename and some flags and you'll get your texture ID.
Also, you're learning very old and outdated version of OpenGL now - you might want to have a google for newer tutorials or browse the specs whenever you feel ready.
Here's a step by step tutorial on loading textures
http://www.nullterminator.net/gltexture.html
Its important to remember that OpenGL is a state machine, so you have to tell it "I'm going to talk about textures now" that's where the glActiveTexture(); comes in.
Also keep in mind that you will have to load in pixel by pixel the colors from your .jpg (compressed) to your texture array, so either you will need to find a library that will give you bitmap values of your .jpg file or you will need to pre-convert it into a .ppm or a .bmp which will make reading in the values easier.