@Override public void init(GLAutoDrawable glad) { System.out.println("init"); canvas.setAutoSwapBufferMode(false); GL3 gl3 = glad.getGL().getGL3(); buildShaders(gl3); programObject.bind(gl3); { programObject.setUniform(gl3, "frustumScale", new float[] {1.0f}, 1); programObject.setUniform(gl3, "zNear", new float[] {1.0f}, 1); programObject.setUniform(gl3, "zFar", new float[] {3.0f}, 1); } programObject.unbind(gl3); initializeVertexBuffer(gl3); gl3.glGenVertexArrays(1, IntBuffer.wrap(vertexArrayObject)); gl3.glBindVertexArray(vertexArrayObject[0]); gl3.glEnable(GL3.GL_CULL_FACE); gl3.glCullFace(GL3.GL_BACK); gl3.glFrontFace(GL3.GL_CW); }
@Override public void init(GLAutoDrawable drawable) { final GL3 gl = drawable.getGL().getGL3(); gl.glEnable(GL3.GL_DEPTH_TEST); gl.glDisable(GL3.GL_CULL_FACE); initBuffers(gl); initShaders(gl); vao = initVAO(gl); gl.setSwapInterval(1); }
/** * This constructor is called by {@link GLRenderPanel}. * * @param drawable the OpenGL rendering context. All OpenGL calls are directed to this object. */ public GLRenderContext(GLAutoDrawable drawable) { // Some OpenGL initialization gl = drawable.getGL().getGL3(); gl.glEnable(GL3.GL_DEPTH_TEST); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Load and use the default shader defaultShader = (GLShader) makeShader(); try { defaultShader.load("../jrtr/shaders/default.vert", "../jrtr/shaders/default.frag"); } catch (Exception e) { System.out.print("Problem with shader:\n"); System.out.print(e.getMessage()); } useDefaultShader(); }
/** * Set up a material for rendering. Activate its shader, and pass the material properties, * textures, and light sources to the shader. * * @param m the material to be set up for rendering */ private void setMaterial(Material m) { // Set up the shader for the material, if it has one if (m != null && m.shader != null) { // Identifier for shader variables int id; // Activate the shader useShader(m.shader); // Activate the diffuse texture, if the material has one if (m.diffuseMap != null) { // OpenGL calls to activate the texture gl.glActiveTexture(GL3.GL_TEXTURE0); // Work with texture unit 0 gl.glEnable(GL3.GL_TEXTURE_2D); gl.glBindTexture(GL3.GL_TEXTURE_2D, ((GLTexture) m.diffuseMap).getId()); gl.glTexParameteri(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_MAG_FILTER, GL3.GL_LINEAR); gl.glTexParameteri(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_MIN_FILTER, GL3.GL_LINEAR); // We assume the texture in the shader is called "myTexture" id = gl.glGetUniformLocation(activeShaderID, "myTexture"); gl.glUniform1i( id, 0); // The variable in the shader needs to be set to the desired texture unit, i.e., 0 } // Pass a default light source to shader String lightString = "lightDirection[" + 0 + "]"; id = gl.glGetUniformLocation(activeShaderID, lightString); if (id != -1) gl.glUniform4f(id, 0, 0, 1, 0.f); // Set light direction else System.out.print("Could not get location of uniform variable " + lightString + "\n"); int nLights = 1; // Iterate over all light sources in scene manager (overwriting the default light source) Iterator<Light> iter = sceneManager.lightIterator(); Light l; if (iter != null) { nLights = 0; while (iter.hasNext() && nLights < 8) { l = iter.next(); // Pass light direction to shader, we assume the shader stores it in an array // "lightDirection[]" lightString = "lightDirection[" + nLights + "]"; id = gl.glGetUniformLocation(activeShaderID, lightString); if (id != -1) gl.glUniform4f( id, l.direction.x, l.direction.y, l.direction.z, 0.f); // Set light direction else System.out.print("Could not get location of uniform variable " + lightString + "\n"); nLights++; } // Pass number of lights to shader, we assume this is in a variable "nLights" in the shader id = gl.glGetUniformLocation(activeShaderID, "nLights"); if (id != -1) gl.glUniform1i(id, nLights); // Set number of lightrs // Only for debugging // else // System.out.print("Could not get location of uniform variable nLights\n"); } } }
/** * Binds the BufferedImage byte-stream into video memory. BufferedImage must be in 4BYTE_ABGR. * 4BYTE_ABGR removes endinese problems. */ public Texture bind(final BufferedImage _image, final InternalFormat _format) { final GL3 gl = GLRenderer.getCanvas().getContext().getCurrentGL().getGL3(); if (gl == null) { System.out.println("GL context doesn't exist"); return null; } gl.glEnable(GL.GL_TEXTURE_2D); final int textureID = glGenTextures(gl); gl.glBindTexture(GL3.GL_TEXTURE_2D, textureID); gl.glTexParameteri(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_WRAP_S, GL3.GL_REPEAT); gl.glTexParameteri(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_WRAP_T, GL3.GL_REPEAT); gl.glTexParameteri(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_MAG_FILTER, GL3.GL_LINEAR); gl.glTexParameteri(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_MIN_FILTER, GL3.GL_LINEAR_MIPMAP_LINEAR); final int width = _image.getWidth(); final int height = _image.getHeight(); final int channels = _image.getSampleModel().getNumBands(); int internalFormat = GL3.GL_RGB; if (gl.isExtensionAvailable("GL_EXT_abgr") == true) { switch (channels) { case 4: imageFormat = GL2.GL_ABGR_EXT; break; case 3: imageFormat = GL3.GL_BGR; break; case 1: imageFormat = GL3.GL_RED; break; } } else { switch (channels) { case 4: imageFormat = GL3.GL_RGBA; break; case 3: imageFormat = GL3.GL_RGB; break; case 1: imageFormat = GL3.GL_RED; break; } } gl.glPixelStorei(GL3.GL_UNPACK_ALIGNMENT, 1); gl.glTexImage2D( GL3.GL_TEXTURE_2D, 0, getGLInternalFormat(channels, _format), width, height, 0, imageFormat, GL3.GL_UNSIGNED_BYTE, getByteBuffer(_image)); gl.glGenerateMipmap(GL3.GL_TEXTURE_2D); gl.glBindTexture(GL.GL_TEXTURE_2D, 0); // Reset to default texture return new Texture(new GLImage(textureID, width, height)); }