Created
July 12, 2011 09:05
-
-
Save getflourish/1077659 to your computer and use it in GitHub Desktop.
How to generate a cube map from 6 textures for use with a shader. Uses GLGraphics and OpenGL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import codeanticode.glgraphics.*; | |
import processing.opengl.*; | |
import javax.media.opengl.*; | |
import java.nio.IntBuffer; | |
GL gl; | |
GLSLShader shader; | |
PGraphicsOpenGL pgl; | |
int[] envMapTextureID = {0}; | |
GLTexture[] textures; | |
String[] textureNames = {"+x.jpg", "-x.jpg", "+y.jpg", "-y.jpg", "+z.jpg", "-z.jpg"}; | |
void setup () { | |
size(400, 400, GLConstants.GLGRAPHICS); | |
hint(ENABLE_OPENGL_4X_SMOOTH); | |
pgl = (PGraphicsOpenGL) g; | |
gl = pgl.gl; | |
shader = new GLSLShader(this, "fluxus.vert", "fluxus.frag"); | |
textures = new GLTexture[6]; | |
for (int i = 0; i < 6; i++) { | |
GLTexture tex = new GLTexture(this, textureNames[i]); | |
textures[i] = tex; | |
} | |
// init cubemap textures | |
gl.glGenTextures(1, envMapTextureID, 0); | |
gl.glBindTexture(GL.GL_TEXTURE_CUBE_MAP, envMapTextureID[0]); | |
gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE); | |
gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); | |
gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_WRAP_R, GL.GL_CLAMP_TO_EDGE); | |
gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST); | |
gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); | |
for (int i = 0; i < 6; i++) { | |
GLTexture tex = textures[i]; | |
int[] pix = new int[tex.width * tex.height]; | |
tex.getBuffer(pix, ARGB, GLConstants.TEX_BYTE); | |
gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL.GL_RGBA, tex.width, tex.height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, IntBuffer.wrap(pix)); | |
} | |
} | |
void draw () { | |
background(255); | |
noStroke(); | |
translate(mouseX, mouseY, 0); | |
shader.start(); | |
shader.setFloatUniform("RefractionIndex", 0.5); | |
shader.setVecUniform("SpecularColour", 1.0, 1.0, 1.0); | |
shader.setFloatUniform("Roughness", 1.0); | |
shader.setFloatUniform("SpecularIntensity", 0.5); | |
sphere(100); | |
shader.stop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment