Skip to main content
gamedev.net
🔒 Locked

Cube mapping + GLSL

Started by zurekx Feb 25, 2008 at 11:13 AM 6 replies 9.6k views
Original Post
zurekx
zurekx
Im trying to use cube mapping for some water reflections (opengl + glsl). The cube map, which I havent done by myself, is in .dds format. How do I load this and create a texture (or cube map) of it and pass it to glsl?
polymorphed
polymorphed
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:
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 mode



Rendering 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;}
while (tired) DrinkCoffee();
zurekx
zurekx
Thanks! I will try it. I just have a small question. How can I do to pass multiple textures to glsl? Supose I need another regular texture too, how do I pass it? (since I can only bind one texture at the time)
biggoron
biggoron
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.
dpadam450
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);
NBA2K, Madden, Maneater, Killing Floor, Sims 
zurekx
zurekx
Thanks! But the compiler cant find the glActiveTexture function?...
biggoron
biggoron
Try using glActiveTextureARB. What are you using to load the extensions?
polymorphed
polymorphed
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]
while (tired) DrinkCoffee();

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.