Jump to content

  • Log In with Google      Sign In   
  • Create Account

Problem loading texture O


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 PeymanKOP   Members   -  Reputation: 108

Like
0Likes
Like

Posted 17 March 2014 - 01:11 PM

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;
}

 



Sponsor:

#2 hdxpete   Members   -  Reputation: 488

Like
0Likes
Like

Posted 17 March 2014 - 07:01 PM

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);
 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS