Original Post
🔒 Locked
Cube mapping + GLSL
Advertisement
I'm unfamiliar with the .dds format, but I assume that it's some form of image format. You need to find an image library which can load this format, and convert it to raw image data. Once you have the six sides of the cubemap ready and loaded up, you use the following approach to create a cubemap texture object in OpenGL:
(I haven't tested this code I wrote now, so you might have to fix a few typos here and there, but it should work.)
Loading the cubemap:
Rendering the cubemap:
GLSL vertex shader:
GLSL fragment shader:
(I haven't tested this code I wrote now, so you might have to fix a few typos here and there, but it should work.)
Loading the cubemap:
void *data_px, *data_nx, *data_py, *data_ny, *data_pz, *data_nz;unsigned int cmap_obj;int width, height;// ...// Load image contents into data pointers here// ...glGenTextures(1, &cmap_obj); // Make texture objectglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, cmap_obj); // Make it a cubemapglTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data_px); // postive xglTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data_nx); // negative xglTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data_py); // postive yglTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data_ny); // negative yglTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data_pz); // positive zglTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data_nz); // negative zglTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Set far filtering modeglTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Set near filtering modeRendering the cubemap:
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, cmap_obj);// ... GLSL vertex shader:
varying vec3 vnorm;void main (){ vnorm = gl_NormalMatrix * gl_Normal; gl_Position = ftransform();}GLSL fragment shader:
varying vec3 vnorm;uniform samplerCube texcube;void main (){ vec3 norm = normalize(vnorm); vec4 frag = textureCube(texcube, norm); gl_FragColor = frag;}
Make sure your program is linked and in use. Then call
glUniform1i(glUniformLocation(prog, "unit0"), 0);
glUniform1i(glUniformLocation(prog, "unit1"), 1);
Where unit0 and unit1 are the names of your samplers. This will set unit0 to 0 so it references the correct unit, and the same for unit1.
To bind to these units, do this:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ...);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, ...);
Of course, if you just need to use one 2D texture and one cube texture you can do that anyway. The limit is one texture per target, not one texture.
glUniform1i(glUniformLocation(prog, "unit0"), 0);
glUniform1i(glUniformLocation(prog, "unit1"), 1);
Where unit0 and unit1 are the names of your samplers. This will set unit0 to 0 so it references the correct unit, and the same for unit1.
To bind to these units, do this:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ...);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, ...);
Of course, if you just need to use one 2D texture and one cube texture you can do that anyway. The limit is one texture per target, not one texture.
Quote:
Original post by dpadam450
In repsonse to his post in your shader you would have:
uniform sampler2D unit0;
uniform sampler2D unit1;
//This code says texture unit 0, is referenced as "unit0" in your shader.
glUniform1i(glUniformLocation(prog, "unit0"), 0);
glUniform1i(glUniformLocation(prog, "unit1"), 1);
Don't forget that it's samplerCube not sampler2D [smile]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement