package com.jaamsim.testcases; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; import java.net.URL; import com.jogamp.common.util.VersionNumber; import com.jogamp.opengl.DebugGL4bc; import com.jogamp.opengl.GL2GL3; import com.jogamp.opengl.GL4bc; import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLCapabilities; import com.jogamp.opengl.GLContext; import com.jogamp.opengl.GLDrawableFactory; import com.jogamp.opengl.GLProfile; public class TestCaseRenderFramebuffer { public static Thread renderThread; public static GLContext sharedContext; public static void main(String[] args) { renderThread = new Thread(new Runnable() { @Override public void run() { mainRenderLoop(); } }, "RenderThread"); renderThread.start(); } static boolean checkGLVersion(VersionNumber glVersion, int majorVer, int minorVer) { if (glVersion.getMajor() > majorVer) return true; if (glVersion.getMajor() == majorVer) return glVersion.getMinor() >= minorVer; return false; } static void logException(Throwable ex) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter( sw ); ex.printStackTrace( pw ); pw.flush(); String stackTrace = sw.toString(); System.err.println(stackTrace); } public static void mainRenderLoop() { try { // GLProfile.initSingleton(); GLProfile glp = GLProfile.get(GLProfile.GL2GL3); GLCapabilities caps = new GLCapabilities(glp); caps.setSampleBuffers(true); caps.setNumSamples(4); caps.setDepthBits(24); final boolean createNewDevice = true; GLAutoDrawable dummyDrawable = GLDrawableFactory.getFactory(glp).createDummyAutoDrawable(null, createNewDevice, caps, null); dummyDrawable.display(); // triggers GLContext object creation and native realization. sharedContext = dummyDrawable.getContext(); GL2GL3 gl = sharedContext.getGL().getGL2GL3(); boolean gl3Supported = gl.isGL3(); boolean gl4Supported = gl.isGL4(); VersionNumber glVersion = sharedContext.getGLVersionNumber(); boolean indirectSupported = checkGLVersion(glVersion, 4, 3); // Shared Context initialization int res = sharedContext.makeCurrent(); sharedContext.setGL(new DebugGL4bc((GL4bc)sharedContext.getGL().getGL2GL3())); System.out.format("Found OpenGL version: %s%n", sharedContext.getGLVersion()); System.out.format("Found GLSL: %s%n", sharedContext.getGLSLVersionString()); boolean isCore = sharedContext.isGLCoreProfile(); System.out.format("OpenGL Major: %d Minor: %d IsCore:%s%n", glVersion.getMajor(), glVersion.getMinor(), isCore); if (glVersion.getMajor() < 2) { throw new Throwable("OpenGL version is too low. OpenGL >= 2.1 is required."); } if (!isCore && (!gl3Supported)) { String src = readSource("./testcase_font.vert"); //BOOM int _vsHandle = gl.glCreateShader(GL2GL3.GL_VERTEX_SHADER); } else { String src = readSource("./testcase_core_font.vert"); String vertsrc = src.replaceAll("@VERSION@", sharedContext.getGLSLVersionString()); //BOOM int _vsHandle = gl.glCreateShader(GL2GL3.GL_VERTEX_SHADER); } } catch (Throwable e) { System.out.println("Renderer encountered a fatal error:"); logException(e); } finally { if (sharedContext != null && sharedContext.isCurrent()) sharedContext.release(); } System.out.println("Done"); } static String readSource(String file) { URL res = TestCaseRenderFramebuffer.class.getResource(file); StringBuilder source = new StringBuilder(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(res.openStream())); while (true) { String line = reader.readLine(); if (line == null) break; source.append(line).append("\n"); } reader.close(); } catch (IOException e) {} return source.toString(); } }