java - LWJGL swing error - cant find the solution -
i try add lwjgl canvas buttons , 1 image panel
but wont work. how @ moment example image render in grey square right next test image.
if compile code following error:
exception in thread "thread-0" java.lang.runtimeexception: no opengl context found in current thread. @ org.lwjgl.opengl.glcontext.getcapabilities(glcontext.java:124) @ org.lwjgl.opengl.gl20.glcreateprogram(gl20.java:253) @ util.shaderprogram.createprogram(shaderprogram.java:53) @ util.shaderprogram.<init>(shaderprogram.java:47) @ exampleapplet$5.run(exampleapplet.java:163)
what have change?
thanks lot.
adding swing lwjgl driving crazy
here sourcecode:
import math.mat4; import math.vec3; import org.lwjgl.lwjglexception; import org.lwjgl.opengl.display; import org.lwjgl.opengl.gl11; import util.mesh; import util.objcontainer; import util.objgroup; import util.shaderprogram; import javax.imageio.imageio; import javax.swing.*; import java.awt.*; import java.awt.event.componentevent; import java.awt.event.componentlistener; import java.awt.event.windowevent; import java.awt.event.windowlistener; import java.awt.image.bufferedimage; import java.io.file; import java.util.arraylist; import static org.lwjgl.opengl.gl11.*; import static org.lwjgl.opengl.gl11.gl_depth_buffer_bit; import static org.lwjgl.opengl.gl11.glviewport; import static org.lwjgl.opengl.gl15.gl_static_draw; public class exampleapplet { public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { exampleapplet window = new exampleapplet(); window.frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } private shaderprogram shaderprogram; private arraylist<mesh> meshes; private mat4 modelmatrix; private mat4 viewmatrix; private volatile float[] vertices; private jframe frame; private canvas canvas; private jpanel controlpanel; private jpanel canvaspanel; private jpanel imagepanel; private thread gamethread; private boolean running; private int windowwidth; private int windowheight; private volatile boolean needvalidation; private volatile boolean needupdateviewport; public exampleapplet() { frame = new jframe(); frame.addwindowlistener(new windowlistener() { public void windowopened(windowevent arg0) { } public void windowiconified(windowevent arg0) { } public void windowdeiconified(windowevent arg0) { } public void windowdeactivated(windowevent arg0) { } public void windowclosing(windowevent arg0) { } public void windowclosed(windowevent arg0) { terminate(); } public void windowactivated(windowevent arg0) { } }); frame.settitle("swing + lwjgl"); frame.setsize(1500, 700); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().setlayout(new borderlayout(0, 0)); canvaspanel = new jpanel(new borderlayout(0, 0)); imagepanel = new jpanel(new borderlayout(0, 0)); canvas = new canvas() { private static final long serialversionuid = -1069002023468669595l; public void removenotify() { stopopengl(); } }; canvas.addcomponentlistener(new componentlistener() { public void componentshown(componentevent e) { setneedvalidation(); } public void componentresized(componentevent e) { setneedvalidation(); } public void componentmoved(componentevent e) { setneedvalidation(); } public void componenthidden(componentevent e) { setneedvalidation(); } }); canvas.setignorerepaint(true); canvas.setsize(500,500); canvaspanel.setsize(500,500); //canvas.setpreferredsize(new dimension(500, 500)); // canvas.setminimumsize(new dimension(320, 240)); canvas.setvisible(true); canvaspanel.add(canvas, borderlayout.center); try { bufferedimage myimg = imageio.read((new file("/home/manu/workspaces/lwjgl_swing/resources/itworks-hdtv_720p.png"))); jlabel piclabel = new jlabel(new imageicon(resize(myimg, 1000, 500))); imagepanel.add(piclabel); } catch (exception e) { } controlpanel = new jpanel(new borderlayout(0, 0)); frame.add(canvaspanel, borderlayout.line_end); frame.add(imagepanel, borderlayout.line_start); frame.pack(); jpanel controls = new jpanel(new gridlayout(2,3)); controlpanel.add(controls,borderlayout.east); jbutton openimage = new jbutton("bild öffnen"); jslider brigthness = new jslider(0,100,0); controls.add(openimage); controls.add(brigthness); frame.add(controlpanel, borderlayout.page_end); startopengl(); } private void setneedvalidation() { needvalidation = true; needupdateviewport = true; } private void startopengl() { system.out.println("startopengl"); gamethread = new thread() { public void run() { try { shaderprogram = new shaderprogram( "/home/manu/workspaces/lwjgl_swing/src/guisample/color_vs.glsl", "/home/manu/workspaces/lwjgl_swing/src/guisample/color_fs.glsl" ); modelmatrix = new mat4(); viewmatrix = mat4.translation( 0.0f, 0.0f, -3.0f ); meshes = new arraylist<mesh>(); windowwidth = 500; windowheight = 500; loadobj("monkey.obj"); glenable( gl_depth_test ); display.create(); display.setparent(canvas); running = true; } catch (lwjglexception e) { e.printstacktrace(); } int i=0; while (running) { glclear( gl_color_buffer_bit | gl_depth_buffer_bit ); float fov = 60; float near = 0.01f; float far = 500.0f; mat4 projectionmatrix = mat4.perspective( fov, windowwidth, windowheight, near, far ); glviewport( 0, 0, windowwidth, windowheight ); drawmeshes(viewmatrix, projectionmatrix); gl11.glviewport(0, 0, 500,500); if (i % 2 == 0) { gl11.glclearcolor(1.0f, 0.0f, 0.0f, 1.0f); gl11.glclear(gl11.gl_color_buffer_bit); } else { gl11.glclearcolor(1.0f, 1.0f, 0.0f, 1.0f); gl11.glclear(gl11.gl_color_buffer_bit); } display.update(); try{ thread.sleep(1000); }catch (exception e){ } i++; updategl(); } if (display.iscreated()) { display.destroy(); } } }; gamethread.start(); } public void drawmeshes( mat4 viewmatrix, mat4 projmatrix ) { shaderprogram.useprogram(); shaderprogram.setuniform("umodel", modelmatrix); shaderprogram.setuniform("uview", viewmatrix); shaderprogram.setuniform("uprojection", projmatrix); shaderprogram.setuniform("ucolor",new vec3(1.0f,0.0f,0.0f) ); shaderprogram.setuniform("uenableshading", 0); meshes.get(0).draw(); } private void setupvertices() { vertices = new float[4 * 2]; vertices[0] = 0.1f; vertices[1] = 0.3f; vertices[2] = 0.2f; vertices[3] = 0.8f; vertices[4] = 0.9f; vertices[5] = 0.6f; vertices[6] = 0.7f; vertices[7] = 0.05f; } private void updategl() { gl11.glclear(gl11.gl_color_buffer_bit); render(); display.update(); display.sync(60); if (needupdateviewport) { needupdateviewport = false; rectangle rect = canvas.getbounds(); int w = (int) rect.getwidth(); int h = (int) rect.getheight(); gl11.glmatrixmode(gl11.gl_projection); gl11.glloadidentity(); gl11.glortho(0, w, h, 0, -1, 1); gl11.glviewport(0, 0, w, h); } int error = gl11.glgeterror(); if (error != gl11.gl_no_error) { string msg = "unknown error"; switch (error) { case gl11.gl_invalid_operation: msg = "invalid operation"; break; case gl11.gl_invalid_value: msg = "invalid value"; break; case gl11.gl_invalid_enum: msg = "invalid enum"; break; case gl11.gl_stack_overflow: msg = "stack overflow"; break; case gl11.gl_stack_underflow: msg = "stack underflow"; break; case gl11.gl_out_of_memory: msg = "out of memory"; break; } throw new runtimeexception(msg); } } private void render() { float scale = 100; gl11.glbegin(gl11.gl_quads); gl11.glcolor4f(1, 0, 0, 1); gl11.glvertex3f(vertices[0] * scale, vertices[1] * scale, 0); gl11.glcolor4f(1, 0, 0, 1); gl11.glvertex3f(vertices[2] * scale, vertices[3] * scale, 0); gl11.glcolor4f(1, 0, 0, 1); gl11.glvertex3f(vertices[4] * scale, vertices[5] * scale, 0); gl11.glcolor4f(1, 0, 0, 1); gl11.glvertex3f(vertices[6] * scale, vertices[7] * scale, 0); gl11.glend(); } private void stopopengl() { system.out.println("stopopengl"); running = false; try { gamethread.join(); } catch (interruptedexception e) { e.printstacktrace(); } } private void terminate() { frame.dispose(); system.exit(0); } public static bufferedimage resize(bufferedimage img, int neww, int newh) { image tmp = img.getscaledinstance(neww, newh, image.scale_smooth); bufferedimage dimg = new bufferedimage(neww, newh, bufferedimage.type_int_argb); graphics2d g2d = dimg.creategraphics(); g2d.drawimage(tmp, 0, 0, null); g2d.dispose(); return dimg; } public void loadobj( string filename ) { if( !filename.tolowercase().endswith(".obj") ) { system.err.println( "error in sandbox.loadobj(): invalid file extension, expected \".obj\":\n" + filename ); return; } objcontainer objcontainer = objcontainer.loadfile( "monkey.obj" ); arraylist<objgroup> objgroups = objcontainer.getgroups(); for( objgroup group : objgroups ) { float[] positions = group.getpositions(); float[] normals = group.getnormals(); int[] indices = group.getindices(); mesh mesh = new mesh( gl_static_draw ); mesh.setattribute( 0, positions, 3 ); mesh.setattribute( 1, normals, 3 ); mesh.setindices( indices ); meshes.add( mesh ); } } }
you created opengl context in different thread. have use in same thread in it's created.
Comments
Post a Comment