PDA

View Full Version : Cube Mapping OpenGl ES



kobayashi
02-06-2012, 12:03 PM
Im having some trouble whit the 6 tga faces for my cube, i cant see the image cleary in the face, I just see kind of like the color of the tga but no the image clearly. I load the cube from an obj model, I ve been using different models of cube.obj with diferent texture coords ant still the same result.
Then I realize that the cube is just like takin the negative X and positive Y images, and expanding them all over the cube and not even well just like the color of the tga.

Here is part of my code





U5.Loc = glGetUniformLocation ( U5.programObject, "u_s_texture" );

U5.TexId =Cubemap ("pos_x.tga","pos_y.tga","pos_z.tga","neg_x.tga","neg_y.tga","neg_z.tga");


GLuint Cubemap( char *PX,char*PY, char *PZ,char *NX,char*NY, char *NZ)
{
GLuint textureId;
int width,
height;
int width2,
height2;
int width3,
height3;
int width4,
height4;
int width5,
height5;
int width6,
height6;

char *BPX = esLoadTGA ( PX, &width, &height );
char *BPY = esLoadTGA ( PY, &width2, &height2 );
char *BPZ = esLoadTGA ( PZ, &width3, &height3 );
char *BNX = esLoadTGA ( NX, &width4, &height4 );
char *BNY = esLoadTGA ( NY, &width5, &height5 );
char *BNZ = esLoadTGA ( NZ, &width6, &height6 );
GLuint texId;

if ( BPX == NULL || BPY == NULL || BPZ == NULL || BNX == NULL ||BNY == NULL || BNZ == NULL )
{
esLogMessage ( "Error loading (%s) image.\n", PX );
system("pause");
return 0;
}
// Generate a texture object
glGenTextures ( 1, &textureId );

glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

// Bind the texture object
glBindTexture ( GL_TEXTURE_CUBE_MAP, textureId );
// for ( q=0;q<6;q++){
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X,0,GL_R GB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,BPX);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X,0,GL_R GB,width4,height4,0,GL_RGB,GL_UNSIGNED_BYTE,BNX);//---
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y,0,GL_R GB,width2,height2,0,GL_RGB,GL_UNSIGNED_BYTE,BPY);//**
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,0,GL_R GB,width5,height5,0,GL_RGB,GL_UNSIGNED_BYTE,BNY);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z,0,GL_R GB,width3,height3,0,GL_RGB,GL_UNSIGNED_BYTE,BPZ);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,0,GL_R GB,width6,height6,0,GL_RGB,GL_UNSIGNED_BYTE,BNZ);
//}
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);



GLbyte vShaderStr[] =
"uniform mat4 u_mvpMatrix; \n"
"attribute vec4 a_position; \n"
"attribute vec4 a_texCoord; \n"
"varying vec4 v_pos; \n"
"void main() \n"
"{ \n"
" gl_Position = u_mvpMatrix * a_position; \n
" v_pos = a_texCoord; \n"
"} \n";

GLbyte fShaderStr[] =
"precision mediump float; \n"
"varying vec2 v_texCoord; \n"
"uniform sampler2D s_betty; \n"
"varying vec4 v_pos; \n"
"uniform samplerCube u_s_texture; \n"
"void main() \n"
"{ \n"
" gl_FragColor=textureCube(u_s_texture, v_pos.xyz); \n"
"} \n";

carsten neumann
02-06-2012, 05:19 PM
Please use [ code]/[ /code] (without space after '[') around code snippets to make them easier to read.

Cube textures require 3 component texture coordinates, because they are meant for cases where you do a texture lookup based on a direction (e.g. environment mapping). Your model files probably only contain the usual 2 component texture coordinates which don't work for cube textures.
If you want to map a different texture to each face of a cube you need to split the cube into 6 different objects (so that you can switch textures between draw calls), combine your textures into one (texture atlas, texture array, cube texture) and adjust the texture coordinates.

kobayashi
02-06-2012, 08:23 PM
sorry for the code, so you are telling me that I need 3 components for the texture coordinates, and also divide the the cube.obj in six parts?...While I was looking for cubes with six different faces I only see two components in the coordinates and their examples seems to work.
I dont understand why i need to split in 6 parts also.

I`ve been looking for a 3 components texture cube obj, and I can't found can you lead to an example?

BionicBytes
02-07-2012, 04:26 AM
I can't quite figure out if you are trying to implement 'cube mapping' or just loading a OBJ file which happens to be a cube.

If we are talking about 'cube mapping' then you don't need any texture coordinates from the OBJ model. The Vertex Shader will compute texture coordinates based on your eye-space vertex positions.

Secondly, the loading of the 6 faces can be simplified by ensuring all texture dimensions are exactly the same for each face. Thus there is no need for width2,3,4,5,6 as all are the same.

Finally,


glBindTexture ( GL_TEXTURE_CUBE_MAP, textureId );
...
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


It looks like you are using cube mapping by the virtue of binding a cube map texture. You then generate mipmaps for it but you have mistakenly used only GL_LINEAR as the minification filter. It should be GL_LINEAR_MIPMAP_LINEAR.

kobayashi
02-07-2012, 07:17 AM
I cant understand the difference Im trying to make an skybox, but I load the cube from a obj model..i take the suggestions and now i only see one color in the cube, not even quite the texture as before when i use texture coords.




glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X,0,GL_R GB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,BPX);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X,0,GL_R GB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,BNX);//---
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y,0,GL_R GB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,BPY);//**
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,0,GL_R GB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,BNY);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z,0,GL_R GB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,BPZ);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,0,GL_R GB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,BNZ);

glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR );
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR );
glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE );

/

BionicBytes
02-07-2012, 07:25 AM
glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR

No, only the minification filter uses mipmaps.
The magnification filter is just LINEAR.
glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER,GL_LINEAR);

kobayashi
02-07-2012, 07:30 AM
Already change it, and the same result, any other tip, i just see one color now in all the cube, all my 6 tga are the same size and I think everything its ok.

BionicBytes
02-07-2012, 07:37 AM
I don't see you Vertex Shader anywhere.
I'm assuming you have set the pass-through texture coordinates in the Vertex shader = gl_Vertex.xyz ?

kobayashi
02-07-2012, 07:42 AM
Isnt here what you are talking...?





GLbyte vShaderStr[] =
"uniform mat4 u_mvpMatrix; \n"
"attribute vec4 a_position; \n"
"attribute vec4 a_texCoord; \n"
"varying vec4 v_pos; \n"
"void main() \n"
"{ \n"
" gl_Position = u_mvpMatrix * a_position; \n
" v_pos = a_texCoord; \n"
"} \n";

GLbyte fShaderStr[] =
"precision mediump float; \n"
"varying vec2 v_texCoord; \n"
"uniform sampler2D s_betty; \n"
"varying vec4 v_pos; \n"
"uniform samplerCube u_s_texture; \n"
"void main() \n"
"{ \n"
" gl_FragColor=textureCube(u_s_texture, v_pos.xyz); \n"
"} \n";
/

BionicBytes
02-07-2012, 07:48 AM
so it is....
In that case, just change the VS to:


GLbyte vShaderStr[] =
"uniform mat4 u_mvpMatrix; \n"
"attribute vec4 a_position; \n"
"attribute vec4 a_texCoord; \n"
"varying vec4 v_pos; \n"
"void main() \n"
"{ \n"
" gl_Position = u_mvpMatrix * a_position; \n
" v_pos = a_position; \n"
"} \n";

kobayashi
02-07-2012, 07:52 AM
Thanks a lot BionicBytes its done :)

BionicBytes
02-07-2012, 07:56 AM
No problem. Simple fix.