I am creating a Sky box with a cube map so I used this site to generate the sides of the cube map Sky box generator, now I have 6 512 png files, I used the following code to render it:
glGenTextures(1, &cubemapTexture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
for (GLuint i = 0; i < images.size(); i++) {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, images[i].width, images[i].height, 0, GL_RGB, GL_UNSIGNED_BYTE, images[i].data);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_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);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
the code was working fine with different images but those images that are downloaded from this site give me the following result
this is a sample of the image used not working:
this is a sample of the worked images:
It seems that PNG files contain alpha channel, so you should use GL_RGBA in your glTexImage2D call like this:
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, images[i].width, images[i].height, 0, GL_RGBA , GL_UNSIGNED_BYTE, images[i].data);
Related
i have tried to load stack of .png image files into glTexImage3D for volume rendering purpose. However, i just cant make it work as it keeps crashing. BTW, the problem seems to be related with pData.
Below is the code for your reference:
GLubyte * pData = new GLubyte[XDIM*YDIM*ZDIM];
ifstream volumeData;
getFileNameWithSpecificExtension(dir_path);
sort(begin(fileIndex), end(fileIndex));
for (int i = 0; i < 201; i ++)
{
volumeData.open(FrontPart + to_string(fileIndex[i]) + ".png", ios::binary);
}
volumeData.read(reinterpret_cast<char*>(pData), XDIM*YDIM*ZDIM*sizeof(GLubyte));
volumeData.close();
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_3D, textureID);
// set the texture parameters
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
//set the mipmap levels (base and max)
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 4);
//allocate data with internal format and foramt as (GL_RED)
glTexImage3D(GL_TEXTURE_3D, 0, GL_RED, XDIM, YDIM, ZDIM, 0, GL_RED, GL_UNSIGNED_BYTE, pData);
GL_CHECK_ERRORS
//generate mipmaps
glGenerateMipmap(GL_TEXTURE_3D);
//delete the volume data allocated on heap
delete[] pData;
Non-Power-of-2 is no issue since OpenGL-2.0 removed that constraint. For reading each image slice into the texture use glTexImage3D with a NULL pointer to initialize the texture, then in the loop iterating over the files, read each file, decode it and load it into the right slice using glTexSubImage3D. Also don't use explicit new and delete[] but a std::vector.
Properly implementing the "foreach file" loop and the "ImageFileReader" are left as an exercise for the reader. I suggest looking into the Generic Image library for one particular candidate (http://sourceforge.net/adobe/genimglib/home/Home/). Also keep in mind that it's a very bad idea to hardcode any image dimensions into your code.
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_3D, textureID);
// set the texture parameters
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
//set the mipmap levels (base and max)
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 4);
//allocate data with internal format and foramt as (GL_RED)
glTexImage3D(GL_TEXTURE_3D, 0, GL_RED, XDIM, YDIM, ZDIM, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
GL_CHECK_ERRORS
GLubyte * pData = new GLubyte[XDIM*YDIM*ZDIM];
std::vector<GLubyte> vData(XDIM*YDIM*ZDIM);
foreach file in filenames (...)
{
ImageFileReader ifr(FrontPart + to_string(fileIndex[i]) + ".png")
ifr.decode(vData);
glTexSubImage3D(GL_TEXTURE_3D, 0,
0 /* x offset */,
0 /* y offset */,
z,
XDIM, YDIM, 1,
GL_RED, GL_UNSIGNED_BYTE,
&vData[0] );
}
//generate mipmaps
glGenerateMipmap(GL_TEXTURE_3D);
I'm having trouble with rendering to texture output from this opengl example: http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_01
My framebuffer is setup as follows:
glGenTextures(1, &back_text);
glBindTexture(GL_TEXTURE_2D, back_text);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, WINDOW_WIDTH, WINDOW_HEIGHT, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
glGenFramebuffersEXT(1, &font_buffer);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, font_buffer);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, back_text, 0);
In best case I can get filled rectangle with color where letter should appear (it appears like alpha is always 1.0).
I've tried original example and it works correctly.
My question is: do I need to enable something to make this work or do I need to take different approach?
Edit:
It turns out that I need to use
glutInitContextVersion(2,0); instead of
glutInitContextVersion(3,2); and world is happy place!! :)
I'm trying to use awesomium with opengl (glut and cpp) and I'm having trouble rendering the webpages to textures. The textures are randomly (different every time I run the program) shifted in the x and y axis. Picture:
I thought GL_CLAMP_TO_EDGE would take care of that problem but it did not. What am I doing wrong?
The Awesomiumpart:
web_core = WebCore::Initialize(WebConfig());
view = web_core->CreateWebView(WIDTH, HEIGHT);
WebURL url(WSLit(URL));
view->LoadURL(url);
BindMethods(view);
while (view->IsLoading())
web_core->Update();
Sleep(300);
web_core->Update();
surface = (BitmapSurface*)view->surface();
The part loading the texture:
glBindTexture(GL_TEXTURE_2D, 13);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, WIDTH, HEIGHT, 0,
GL_BGRA_EXT, GL_UNSIGNED_BYTE, surface);
This is what I did. I know you solved the problem, but it might help someone in the future.
int w = surface->width();
int h = surface->height();
unsigned char *buffer = new unsigned char[w * h * 4];
surface->CopyTo(buffer, w * 4, 4, false, false);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, buffer);
delete[] buffer;
I'm trying to simply load 6 pictures as texture of cube in OpenGL. Blow is the loading code:
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
for (int i = 0;i < 6;++i)
{
int width, height, channel;
unsigned char* img = SOIL_load_image(skybox[i].c_str(), &width, &height, &channel, SOIL_LOAD_AUTO);
glTexImage2D(cubeTarget[i], 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
delete img;
}
glEnable(GL_TEXTURE_CUBE_MAP_ARB);
The rendering code is passed. The weird thing is that the cube rendered is white. It seems that the texture isn't loaded at all. I change the loading code to see whether a 2D texture will work:
glGenTextures(1, texture);
glBindTexture(GL_TEXTURE_2D, texture[0]);
int width, height, channel;
unsigned char* img = SOIL_load_image(skybox[0].c_str(), &width, &height, &channel, SOIL_LOAD_AUTO);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
delete img;
if(texture[0] == 0) return false;
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
The result is I can see the texture after all, no matter how strange the distribution caused by the texture coordinates. I gathered the following information:
The lib I use for loading image works well;
The setting of the cubemap is from the SuperBibble chapter 9. Almost the same code will work well when I compiling the code of the book.
BTW, does anyone have some suggestion about loading image library? The one I use seems to stop updating for a really long time...
Appending: What I find out now is that if I try to only load one img as all the skybox faces' texture, it will be shown. As long as use a variable to replace a specific value, nothing won't be displayed.
Finally I figure it out. It's because the resolution of the last picture is different from the others.
Realy kind of wasting a lot of time.
i have managed to load texture
// Load texture
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
pBytes = gltLoadTGA("../earth.tga", &iWidth, &iHeight, &iComponents, &eFormat);
glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes);
free(pBytes);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
when i texture the sphere like this it does not show (image 256x256 32bit)
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glutSolidSphere(35.0f, 30, 17);
glDisable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
but i have a ship made out of a pyramid shape the same technique works on it
glEnable(GL_TEXTURE_2D);
ship();
glDisable(GL_TEXTURE_2D);
anyone any idea why this is happening please...(please don't mark me down i'm working hard here)
You may have loaded the texture image. But you also need to supply texture coordinates. Your ship may do this. glutSolidSphere however does not, and there's nothing you can do about it. So don't use glutSolidSphere.