public static int createGLResource(int type, int param) { int id = 0; if (type == GL_TEXTURE_OBJECT) { int[] temp = new int[1]; gl.glGenTextures(1, temp, 0); id = temp[0]; glTextureObjects.add(id); } else if (type == GL_VERTEX_BUFFER) { int[] temp = new int[1]; gl.glGenBuffersARB(1, temp, 0); id = temp[0]; glVertexBuffers.add(id); } else if (type == GL_FRAME_BUFFER) { int[] temp = new int[1]; gl.glGenFramebuffersEXT(1, temp, 0); id = temp[0]; glFrameBuffers.add(id); } else if (type == GL_RENDER_BUFFER) { int[] temp = new int[1]; gl.glGenRenderbuffersEXT(1, temp, 0); id = temp[0]; glRenderBuffers.add(id); } else if (type == GLSL_PROGRAM) { id = gl.glCreateProgram(); glslPrograms.add(id); } else if (type == GLSL_SHADER) { id = gl.glCreateShader(param); glslShaders.add(id); } return id; }
/** * initPermTexture(GLuinttexID) - create and load a 2D texture for a combined index permutation * and gradient lookup table. This texture is used for 2D and 3D noise, both classic and simplex. */ private void initPermTexture(GL gl, int[] texID) { ByteBuffer pixels; int i, j; gl.glGenTextures(1, texID, 0); // Generate a unique texture ID gl.glBindTexture(GL.GL_TEXTURE_2D, texID[0]); // Bind the texture to texture unit 0 pixels = ByteBuffer.allocateDirect(256 * 256 * 4); for (i = 0; i < 256; i++) for (j = 0; j < 256; j++) { int offset = (i * 256 + j) * 4; int value = perm[(j + perm[i]) & 0xFF]; pixels.put(offset, (byte) (grad3[value & 0x0F][0] * 64 + 64)); // Gradient x pixels.put(offset + 1, (byte) (grad3[value & 0x0F][1] * 64 + 64)); // Gradient y pixels.put(offset + 2, (byte) (grad3[value & 0x0F][2] * 64 + 64)); // Gradient z pixels.put(offset + 3, (byte) value); // Permuted index } // GLFW texture loading functions won't work here - we need // GL.GL_NEAREST lookup. gl.glTexImage2D( GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, 256, 256, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, pixels); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); }
/** * Creates a texture in the given OpenGL context. * * @param gl the context * @param image the RGB or RGBA image serving as texture source * @param repeat whether the texture should should have repeat mode activated * @return the texture name (ID) */ private static int createTexture(GL gl, BufferedImage image, boolean repeat) { if (image == null) { return -1; } final int[] tmp = new int[1]; gl.glGenTextures(1, tmp, 0); int tex = tmp[0]; gl.glBindTexture(GL_TEXTURE_2D, tex); int[] data = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); ByteBuffer dest = ByteBuffer.allocate(data.length * BufferUtil.SIZEOF_INT); // TODO direct? dest.order(ByteOrder.nativeOrder()); dest.asIntBuffer().put(data, 0, data.length); gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); int wrapMode = (repeat) ? GL_REPEAT : GL_CLAMP_TO_EDGE; gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode); gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode); int oglFormat = (image.getType() == BufferedImage.TYPE_INT_ARGB) ? GL_RGBA : GL_RGB; gl.glTexImage2D( GL_TEXTURE_2D, 0, oglFormat, image.getWidth(), image.getHeight(), 0, GL_BGRA, GL_UNSIGNED_BYTE, dest); // (new GLU()).gluBuild2DMipmaps(GL.GL_TEXTURE_2D, GL.GL_RGB, image.getWidth(), // image.getHeight(), GL.GL_BGRA, // GL.GL_UNSIGNED_BYTE, dest); return tex; }
void LoadTextures(final GL gl) { // There is only one texture needed here--we'll set up a basic // checkerboard--which is used to modulate the diffuse channel in the // fragment shader. final int[] handle = new int[1]; gl.glGenTextures(1, handle, 0); // Basic OpenGL texture state setup gl.glBindTexture(GL.GL_TEXTURE_2D, handle[0]); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_GENERATE_MIPMAP_SGIS, GL.GL_TRUE); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE); gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE); // Fill in the texture map. final int RES = 512; final float[] data = new float[RES * RES * 4]; int dp = 0; for (int i = 0; i < RES; ++i) for (int j = 0; j < RES; ++j) { if ((i / 32 + j / 32) % 2 != 0) { data[dp++] = .7f; data[dp++] = .7f; data[dp++] = .7f; } else { data[dp++] = .1f; data[dp++] = .1f; data[dp++] = .1f; } data[dp++] = 1.0f; } gl.glTexImage2D( GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, RES, RES, 0, GL.GL_RGBA, GL.GL_FLOAT, FloatBuffer.wrap(data)); // Tell Cg which texture handle should be associated with the sampler2D // parameter to the fragment shader. CgGL.cgGLSetTextureParameter( CgGL.cgGetNamedParameter(fragmentProgram, "diffuseMap"), handle[0]); }
public static int genTexture(GL gl) { int[] handle = new int[1]; gl.glGenTextures(1, handle, 0); return handle[0]; }
/** * Creates a new texture ID. * * @param gl the GL object associated with the current OpenGL context * @return a new texture ID */ private static int createTextureID(GL gl) { int[] tmp = new int[1]; gl.glGenTextures(1, tmp, 0); return tmp[0]; }
public void updateTexImageData(Image img, Texture.Type type, int unit) { int texId = img.getId(); GL gl = GLContext.getCurrentGL(); if (texId == -1) { // create texture gl.glGenTextures(1, ib1); texId = ib1.get(0); img.setId(texId); objManager.registerObject(img); statistics.onNewTexture(); } // bind texture int target = convertTextureType(type); // if (context.boundTextureUnit != unit) { // glActiveTexture(GL_TEXTURE0 + unit); // context.boundTextureUnit = unit; // } if (context.boundTextures[unit] != img) { gl.glEnable(target); gl.glBindTexture(target, texId); context.boundTextures[unit] = img; statistics.onTextureUse(img, true); } // Check sizes if graphics card doesn't support NPOT if (!gl.isExtensionAvailable("GL_ARB_texture_non_power_of_two") && img.isNPOT()) { // Resize texture to Power-of-2 size MipMapGenerator.resizeToPowerOf2(img); } if (!img.hasMipmaps() && img.isGeneratedMipmapsRequired()) { // No pregenerated mips available, // generate from base level if required // Check if hardware mips are supported if (gl.isExtensionAvailable("GL_VERSION_1_4")) { gl.glTexParameteri(target, GL2ES1.GL_GENERATE_MIPMAP, GL.GL_TRUE); } else { MipMapGenerator.generateMipMaps(img); } img.setMipmapsGenerated(true); } else { } if (img.getWidth() > maxTexSize || img.getHeight() > maxTexSize) { throw new RendererException( "Cannot upload texture " + img + ". The maximum supported texture resolution is " + maxTexSize); } /* if (target == GL_TEXTURE_CUBE_MAP) { List<ByteBuffer> data = img.getData(); if (data.size() != 6) { logger.log(Level.WARNING, "Invalid texture: {0}\n" + "Cubemap textures must contain 6 data units.", img); return; } for (int i = 0; i < 6; i++) { TextureUtil.uploadTexture(img, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, i, 0, tdc); } } else if (target == EXTTextureArray.GL_TEXTURE_2D_ARRAY_EXT) { List<ByteBuffer> data = img.getData(); // -1 index specifies prepare data for 2D Array TextureUtil.uploadTexture(img, target, -1, 0, tdc); for (int i = 0; i < data.size(); i++) { // upload each slice of 2D array in turn // this time with the appropriate index TextureUtil.uploadTexture(img, target, i, 0, tdc); } } else {*/ TextureUtil.uploadTexture(img, target, 0, 0, false); // } img.clearUpdateNeeded(); }
/** * Loads a TGA file into memory * * @param gl * @param texture * @param filename * @throws IOException */ private void loadTGA(GL gl, TextureImage texture, String filename) throws IOException { // Used To Compare TGA Header ByteBuffer TGAcompare = GLBuffers.newDirectByteBuffer(12); // Uncompressed TGA Header byte[] TGAheader = new byte[] {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}; ByteBuffer header = GLBuffers.newDirectByteBuffer(6); // First 6 Useful // Bytes // From The // Header int bytesPerPixel, // Holds Number Of Bytes Per Pixel Used In The TGA // File imageSize, // Used To Store The Image Size When Setting Aside Ram type = GL.GL_RGBA; // Set The Default GL Mode To RBGA (32 BPP) ReadableByteChannel file = null; try { file = Channels.newChannel(ResourceRetriever.getResourceAsStream(filename)); readBuffer(file, TGAcompare); readBuffer(file, header); for (int i = 0; i < TGAcompare.capacity(); i++) // Does The Header Match What We Want? if (TGAcompare.get(i) != TGAheader[i]) { throw new IOException("Invalid TGA header"); } texture.width = header.get(1) << 8 | header.get(0); // Determine The // TGA // Width(highbyte*256+lowbyte) texture.height = header.get(3) << 8 | header.get(2); // Determine // The TGA // Height(highbyte*256+lowbyte) if (texture.width <= 0) { // Is The Width Less Than Or Equal To Zero throw new IOException("Image has negative width"); } if (texture.height <= 0) { // Is The Height Less Than Or Equal To // Zero throw new IOException("Image has negative height"); } if (header.get(4) != 24 && header.get(4) != 32) { // Is The TGA 24 // or 32 Bit? throw new IOException("Image is not 24 or 32-bit"); } texture.bpp = header.get(4); // Grab The TGA's Bits Per Pixel (24 or // 32) bytesPerPixel = texture.bpp / 8; // Divide By 8 To Get The Bytes Per // Pixel // Calculate the memory required for the TGA Data imageSize = texture.width * texture.height * bytesPerPixel; // Reserve memory to hold the TGA Data texture.imageData = GLBuffers.newDirectByteBuffer(imageSize); readBuffer(file, texture.imageData); // Loop Through The Image Data for (int i = 0; i < imageSize; i += bytesPerPixel) { // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue) // Temporarily Store The Value At Image Data 'i' byte temp = texture.imageData.get(i); // Set the 1st Byte to the value Of the 3rd Byte texture.imageData.put(i, texture.imageData.get(i + 2)); // Set The 3rd Byte To The Value In 'temp' (1st Byte Value) texture.imageData.put(i + 2, temp); } // Build A Texture From The Data gl.glGenTextures(1, texture.texID, 0); // Generate OpenGL texture // IDs gl.glBindTexture(GL.GL_TEXTURE_2D, texture.texID[0]); // Bind Our // Texture gl.glTexParameterf( GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR); // Linear Filtered gl.glTexParameterf( GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR); // Linear Filtered if (texture.bpp == 24) // Was The TGA 24 Bits type = GL.GL_RGB; // If So Set The 'type' To GL_RGB gl.glTexImage2D( GL.GL_TEXTURE_2D, 0, type, texture.width, texture.height, 0, type, GL.GL_UNSIGNED_BYTE, texture.imageData); } finally { if (file != null) { try { file.close(); } catch (IOException n) { } } } }
/** * Issue ogl commands needed for this component * * @param gl The gl context to draw with */ public void render(GL gl) { if (numSources == 0) return; Integer t_id = textureIdMap.get(gl); if (t_id == null) { int[] tex_id_tmp = new int[1]; gl.glGenTextures(1, tex_id_tmp, 0); textureIdMap.put(gl, new Integer(tex_id_tmp[0])); gl.glBindTexture(textureType, tex_id_tmp[0]); // Set the flag so that we update later in the method imageChanged.put(gl, true); stateChanged.put(gl, true); updateManagers[0].addContext(gl); } else { gl.glBindTexture(textureType, t_id.intValue()); } if (stateChanged.getState(gl)) { stateChanged.put(gl, false); gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); gl.glTexParameteri(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_WRAP_S, boundaryModeS); gl.glTexParameteri(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_WRAP_T, boundaryModeT); gl.glTexParameteri(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_WRAP_R, boundaryModeR); int mode = 0; switch (magFilter) { case MAGFILTER_FASTEST: case MAGFILTER_BASE_LEVEL_POINT: mode = GL.GL_NEAREST; break; case MAGFILTER_NICEST: case MAGFILTER_BASE_LEVEL_LINEAR: mode = GL.GL_LINEAR; break; default: System.out.println("Unknown mode in MagFilter: " + magFilter); } gl.glTexParameteri(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_MAG_FILTER, mode); switch (minFilter) { case MINFILTER_FASTEST: case MINFILTER_BASE_LEVEL_POINT: mode = GL.GL_NEAREST; break; case MINFILTER_BASE_LEVEL_LINEAR: mode = GL.GL_LINEAR; break; case MINFILTER_MULTI_LEVEL_LINEAR: mode = GL.GL_LINEAR_MIPMAP_LINEAR; break; case MINFILTER_MULTI_LEVEL_POINT: mode = GL.GL_NEAREST_MIPMAP_NEAREST; break; case MINFILTER_NICEST: mode = (numSources > 1) ? GL.GL_LINEAR_MIPMAP_LINEAR : GL.GL_LINEAR; break; default: System.out.println("Unknown mode in MinFilter: " + minFilter); } gl.glTexParameteri(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_MIN_FILTER, mode); if (anisotropicMode != ANISOTROPIC_MODE_NONE) { gl.glTexParameterf(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropicDegree); } if (priority >= 0) { gl.glTexParameterf(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_PRIORITY, priority); } if (borderColor != null) { gl.glTexParameterfv(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_BORDER_COLOR, borderColor, 0); } if (format == FORMAT_DEPTH_COMPONENT) { gl.glTexParameterf(GL.GL_TEXTURE_3D, GL.GL_DEPTH_TEXTURE_MODE, depthComponentMode); gl.glTexParameterf(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_COMPARE_MODE, compareMode); gl.glTexParameterf(GL.GL_TEXTURE_3D, GL.GL_TEXTURE_COMPARE_FUNC, compareFunction); } } if (imageChanged.getState(gl)) { imageChanged.put(gl, false); TextureComponent3D img = (TextureComponent3D) sources[0]; int width = img.getWidth(); int height = img.getHeight(); int depth = img.getDepth(); int num_levels = (mipMapMode == MODE_BASE_LEVEL) ? 1 : img.getNumLevels(); for (int i = 0; i < num_levels; i++) { ByteBuffer pixels = img.getData(i); pixels.rewind(); int comp_format = img.getFormat(i); int int_format = GL.GL_RGB; int ext_format = GL.GL_RGB; switch (comp_format) { case TextureComponent.FORMAT_RGB: int_format = GL.GL_RGB; ext_format = GL.GL_RGB; break; case TextureComponent.FORMAT_RGBA: int_format = GL.GL_RGBA; ext_format = GL.GL_RGBA; break; case TextureComponent.FORMAT_BGR: int_format = GL.GL_BGR; ext_format = GL.GL_BGR; break; case TextureComponent.FORMAT_BGRA: int_format = GL.GL_BGRA; ext_format = GL.GL_BGRA; break; case TextureComponent.FORMAT_INTENSITY_ALPHA: int_format = GL.GL_LUMINANCE_ALPHA; ext_format = GL.GL_LUMINANCE_ALPHA; break; case TextureComponent.FORMAT_SINGLE_COMPONENT: switch (format) { case FORMAT_INTENSITY: int_format = GL.GL_INTENSITY; ext_format = GL.GL_LUMINANCE; break; case FORMAT_LUMINANCE: int_format = GL.GL_LUMINANCE; ext_format = GL.GL_LUMINANCE; break; case FORMAT_ALPHA: int_format = GL.GL_ALPHA; ext_format = GL.GL_ALPHA; } break; } gl.glTexImage3D( GL.GL_TEXTURE_3D, i, int_format, width, height, depth, 0, ext_format, GL.GL_UNSIGNED_BYTE, pixels); pixels.clear(); pixels = null; // TODO: Do we want this? We lose caching but it saves one copy of the texture // sources[0].clearData(i); if (width > 1) width = width >> 1; if (height > 1) height = height >> 1; if (depth > 1) depth = depth >> 1; } } // Any updates? Do those now int num_updates = updateManagers[0].getNumUpdatesPending(gl); if (num_updates != 0) { TextureUpdateData[] tud = updateManagers[0].getUpdatesAndClear(gl); for (int i = 0; i < num_updates; i++) { tud[i].pixels.rewind(); gl.glTexSubImage3D( GL.GL_TEXTURE_3D, tud[i].level, tud[i].x, tud[i].y, tud[i].z, tud[i].width, tud[i].height, tud[i].depth, tud[i].format, GL.GL_UNSIGNED_BYTE, tud[i].pixels); } } }
public void prerenderToTexture(GL gl) { int texSize = 256; final int[] tmp = new int[1]; gl.glGenTextures(1, tmp, 0); textureID = tmp[0]; gl.glBindTexture(GL_TEXTURE_2D, textureID); gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); gl.glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, texSize, texSize, 0, GL_BGRA, GL_UNSIGNED_BYTE, null); final int[] fbo = new int[1]; gl.glGenFramebuffersEXT(1, IntBuffer.wrap(fbo)); gl.glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo[0]); gl.glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, textureID, 0); gl.glDrawBuffers(1, IntBuffer.wrap(new int[] {GL_COLOR_ATTACHMENT0_EXT})); final int[] rba = new int[1]; gl.glGenRenderbuffersEXT(1, IntBuffer.wrap(rba)); gl.glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rba[0]); gl.glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, texSize, texSize); gl.glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, rba[0]); gl.glPushMatrix(); gl.glLoadIdentity(); gl.glPushAttrib(GL_VIEWPORT_BIT); gl.glViewport(0, 0, texSize, texSize); gl.glMatrixMode(GL.GL_PROJECTION); gl.glPushMatrix(); gl.glLoadIdentity(); gl.glOrtho(0, texSize, 0, texSize, 0, 10); gl.glMatrixMode(GL.GL_MODELVIEW); Set<MapElement> map = State.getInstance().getMapInfo().queryElements(detailLevel, bounds, true); gl.glDisable(GL_TEXTURE_2D); gl.glColor3f(1, 1, 1); for (MapElement element : map) { if (element instanceof Street) { drawLine( gl, ((Street) element).getDrawingSize() / (float) Projection.getZoomFactor(detailLevel), ((Street) element).getNodes()); } } gl.glColor3f(0.3f, 0.3f, 0.3f); for (MapElement element : map) { if ((element instanceof Area) && (((Area) element).getWayInfo().isBuilding())) { gl.glBegin(GL_POLYGON); for (Node node : ((Area) element).getNodes()) { Coordinates pos = getLocalCoordinates(node.getPos()); gl.glVertex3f(pos.getLongitude(), pos.getLatitude(), 0f); } gl.glEnd(); } } gl.glEnable(GL_TEXTURE_2D); gl.glMatrixMode(GL.GL_PROJECTION); gl.glPopMatrix(); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glPopAttrib(); gl.glPopMatrix(); gl.glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); gl.glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0); gl.glDeleteFramebuffersEXT(1, fbo, 0); gl.glDeleteRenderbuffersEXT(1, rba, 0); }