Advertisement Jump to content
Sign in to follow this  
Peyman Tahghighi

OpenGL Problem loading texture O

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

I'm new in openGLES and i'm working on a project "Rally2D".

i want to load a texture but i don't know how to read from a file. i write a method that read the pixels but it is a red black area with nothing in it,so any idea how to read a texture from a file with this method?

this is my code:

GLuint Texture2D::CreateTexture2D()
{
GLuint textureObj;
int  width = 256;
int height = 256;


auto pixels = unique_ptr<GLubyte []>(new GLubyte[width * height * 4]);
if (pixels == nullptr)
{
cout << "Could not allocate the memory for the pixels, error in Texture2D" << endl;
return 0;
}


int checkSize = 64;
for (int j = 0; j < height; ++j)
{
for (int i = 0; i < width; ++i)
{
GLubyte rColor = 0;
GLubyte bColor = 0;


if ((i / checkSize) % 2 == 0)
{
rColor = 255 * ((j / checkSize) % 2);
bColor = 255 * (1 - ((j / checkSize) % 2));
}
else
{
bColor = 255 * ((j / checkSize) % 2);
rColor = 255 * (1 - ((j / checkSize) % 2));
}


pixels[(j * height + i) * 3] = rColor;
pixels[(j * height + i) * 3 + 1] = 0;
pixels[(j * height + i) * 3 + 2] = bColor;
}
}


// Generate a texture object
glGenTextures(1, &textureObj);
// Bind to the 2D texture object
glBindTexture(GL_TEXTURE_2D, textureObj);
// Set the mipmap level 0
glTexImage2D(
GL_TEXTURE_2D,
0,//Mip Map Level
GL_RGB,//Internal format
width,
height,
0.0f,//Border of texture, ignored in OpenGL ES and should be 0. it was kept for compatibility with the desktop OpenGL interface
GL_RGB,//the format of the incoming texture data. Note that in OpenGL ES the format and internalFormat arguments must have the same value. 
GL_UNSIGNED_BYTE,//Type of data
pixels.get());


// Set the filtering mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


pixels.release();


return textureObj;
}

 

Share this post


Link to post
Share on other sites
Advertisement

maybe not the cause of your issue however border is an int not a float.

 

void glTexImage2D( GLenum target,
GLint level,
GLint internalFormat,
GLsizei width,
GLsizei height,
GLint border,
GLenum format,
GLenum type,
const GLvoid * data);
 

Share this post


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

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!