1
我嘗試添加到我的LWJGL帆布一些按鈕和一個圖像面板LWJGL揮杆錯誤 - 不能找到解決
,但它不會工作。 這就是它現在的樣子example image 我喜歡在測試圖像旁邊的灰色方塊中進行渲染。
如果我編譯我的代碼,我得到以下錯誤:
Exception in thread "Thread-0" java.lang.RuntimeException: No OpenGL context found in the current thread.
at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
at org.lwjgl.opengl.GL20.glCreateProgram(GL20.java:253)
at util.ShaderProgram.createProgram(ShaderProgram.java:53)
at util.ShaderProgram.<init>(ShaderProgram.java:47)
at ExampleApplet$5.run(ExampleApplet.java:163)
我有你改變什麼?
非常感謝。
增加揮杆LWJGL是推動我瘋了
這裏是我的源代碼:
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);
}
}
}
那麼我如何從opengl渲染分離GUI。最後應該有2個線程。一個用於LWJGL的GUI,但都在同一個窗口中呈現 –