Hello, I have a problem which I seem can't fix. I tried using everything in the past few hours, nothing worked. Essentially what I'm trying to do just bind some 128x128 .png format texture on to a square in OpenGL with SFML 2.0. I got no errors, but all I can see is white square and thats all. Here's the simplified code I'm trying to get to work:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(800,600,32),"Title");
glViewport(0,0,800,600);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glOrtho(0,800,0,600,1,-1);
sf::Image texture;
texture.loadFromFile("Resources/texture.png");
GLuint id;
glGenTextures(1,&id);
glBindTexture(GL_TEXTURE_2D,id);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,128,128,0,GL_RGBA,GL_UNSIGNED_BYTE,texture.getPixelsPtr());
unsigned short indices[6] = {0,1,2,2,3,0};
float vertices[8] = {100,100,
200,100,
200,200,
100,200};
float textureCoords[8] = {0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2,GL_FLOAT,0,vertices);
glTexCoordPointer(2,GL_FLOAT,0,textureCoords);
while(window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
}
glClear(GL_COLOR_BUFFER_BIT);
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_SHORT, indices);
window.display();
}
return 0;
}
I also tried using gluBuild2DMipmaps() instead of glTexImage2D() but I got some very strange artifacts in my texture, it doesn't even look like my texture anymore, it just KIND OF has same colors but it is very distorted and looks random.
Maybe i'm doing something wrong? How do you bind textures to your OpenGL games/applications?
I ported this code from Android OpenGL example from a book which i'm reading, and there it works fine.
Thanks for your help in advance.