以下是错误:
at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
at org.lwjgl.opengl.GL20.glCreateShader(GL20.java:219)
at com.evolve.shaders.ShaderProgram.loadShader(ShaderProgram.java:63)
at com.evolve.shaders.ShaderProgram.<init>(ShaderProgram.java:17)
at com.evolve.shaders.StaticShader.<init>(StaticShader.java:9)
at com.evolve.main.Main.<init>(Main.java:28)
at com.evolve.main.Main.main(Main.java:46)
错误中有一个类:
主要:
package com.evolve.main;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import com.evolve.display.DisplayManager;
import com.evolve.display.Fps;
import com.evolve.input.KeyboardInput;
import com.evolve.input.MouseInput;
import com.evolve.model.Loader;
import com.evolve.model.RawModel;
import com.evolve.model.TextureModel;
import com.evolve.render.Camera;
import com.evolve.render.Renderer;
import com.evolve.shaders.StaticShader;
import com.evolve.textures.ModelTexture;
public class Main {
private static boolean isRunning = false;
Camera camera;
/*********VAO / VBO*************/
Loader loader = new Loader();
Renderer renderer = new Renderer();
/**********SHADERS*************/
StaticShader shader = new StaticShader();
float[] vertices = {
-0.5f, 0.5f, 0f,
-0.5f, -0.5f, 0f,
0.5f, -0.5f, 0f,
0.5f, 0.5f, 0f,
};
int[] indices = {
0, 1, 3,
3, 1, 2
};
RawModel model = loader.loadToVAO(vertices, indices);
public static void main(String[] args)
{
Main main = new Main();
main.start();
}
public Main()
{
Time.startTime();
DisplayManager.createDisplay(1200, 840, "Evolve", "v0.2");
System.out.println(DisplayManager.getWidth() + " " + DisplayManager.getHeight());
camera = new Camera(new Vector3f(0, 0, 0));
camera.setPerspectiveProjection(70.0f, 0.1f, 1000.0f);
Util.glDepthCull(true);
Util.initGL();
}
//start the game
public void start()
{
isRunning = true;
//FPS statistics
Fps.getDelta();
Fps.lastFPS = Fps.getTime();
//getting infomations
System.out.println("OpenGL version: " + Util.getOpenGLVersion());
System.out.println("OPEN SUCCESSFUL");
System.out.println("Opened in: " + Time.endTime() + " sec(s).");
loop();
}
//stop the game
public void stop()
{
isRunning = false;
}
//core loop
public void loop()
{
while(isRunning){
if(Display.isCloseRequested())
stop();
//FPS
Fps.updateFPS();
Fps.getDelta();
////// Input Listener /////
KeyboardInput.getKeyboardInput();
MouseInput.getMouseMovement();
////// END Input Li. /////
//rendering mesh and stuff..
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
cameraRender();
//updating the display
DisplayManager.updateDisplay();
}
exit();
}
//exit window and stuff..
public void exit()
{
//close display, memory and others...
DisplayManager.destroyDisplay();
shader.cleanUp();
loader.cleanUp();
System.out.println("QUIT SUCCESSFUL");
System.exit(0);
}
//camera settings
public void cameraRender()
{
renderer.prepare();
shader.start();
Util.clearScreen();
camera.getPerspectiveProjection();
camera.update();
render2D();
render3D();
shader.stop();
}
public void render2D()
{
}
public void render3D()
{
renderer.render(model);
}
}
`
StaticShader :
private static final String VERTEX_FILE = "src/com/evolve/shaders/vertexShader.txt";
private static final String FRAGMENT_FILE = "src/com/evolve/shaders/fragmentShader.txt";
public StaticShader() {
super(VERTEX_FILE, FRAGMENT_FILE);
}
@Override
protected void bindAttributes() {
super.bindAttribute(0, "position");
}`
和ShaderProgram :
private int programID;
private int vertexShaderID;
private int fragmentShaderID;
public ShaderProgram(String vertexFile,String fragmentFile){
vertexShaderID = loadShader(vertexFile,GL20.GL_VERTEX_SHADER);
fragmentShaderID = loadShader(fragmentFile,GL20.GL_FRAGMENT_SHADER);
programID = GL20.glCreateProgram();
GL20.glAttachShader(programID, vertexShaderID);
GL20.glAttachShader(programID, fragmentShaderID);
bindAttributes();
GL20.glLinkProgram(programID);
GL20.glValidateProgram(programID);
}
public void start(){
GL20.glUseProgram(programID);
}
public void stop(){
GL20.glUseProgram(0);
}
public void cleanUp(){
stop();
GL20.glDetachShader(programID, vertexShaderID);
GL20.glDetachShader(programID, fragmentShaderID);
GL20.glDeleteShader(vertexShaderID);
GL20.glDeleteShader(fragmentShaderID);
GL20.glDeleteProgram(programID);
}
protected abstract void bindAttributes();
protected void bindAttribute(int attribute, String variableName){
GL20.glBindAttribLocation(programID, attribute, variableName);
}
private static int loadShader(String file, int type){
StringBuilder shaderSource = new StringBuilder();
try{
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while((line = reader.readLine())!=null){
shaderSource.append(line).append("\n");
}
reader.close();
}catch(IOException e){
e.printStackTrace();
System.exit(-1);
}
int shaderID = GL20.glCreateShader(type);
GL20.glShaderSource(shaderID, shaderSource);
GL20.glCompileShader(shaderID);
if(GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS )== GL11.GL_FALSE){
System.out.println(GL20.glGetShaderInfoLog(shaderID, 500));
System.err.println("Could not compile shader!");
System.exit(-1);
}
return shaderID;
}
}
`
我被困了一段时间(得到了吗?)而且我不知道为什么lwjgl
会把这个错误扔给我。我真的需要一些帮助!非常感谢
答案 0 :(得分:1)
在Main
类的构造函数中,尝试在StaticShader
之后将DisplayManager.createDisplay(...)
对象的初始化移动到构造函数的主体中:
StaticShader shader;// = new StaticShader();
public Main() {
Time.startTime();
DisplayManager.createDisplay(1200, 840, "Evolve", "v0.2");
System.out.println(DisplayManager.getWidth() + " " + DisplayManager.getHeight());
shader = new StaticShader(); //<--- like this
camera = new Camera(new Vector3f(0, 0, 0));
camera.setPerspectiveProjection(70.0f, 0.1f, 1000.0f);
Util.glDepthCull(true);
Util.initGL();
}
根据Loader
和Renderer
课程中的内容,您可能需要为他们执行相同操作。
我不确切知道你的DisplayManager.createDisplay(...)
方法会发生什么,因为代码没有发布(截至撰写本文时),但我假设它有类似的内容:
//pre LWJGL 3
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
或者:
//LWJGL 3
GLFW.glfwCreateWindow(width, height, "Window Title", NULL, NULL);
如果是这种情况,那么获得异常的原因是因为在调用DisplayManager.createDisplay(...)
之前没有为线程分配OpenGL上下文,并且在调用glCreateShader()
之前需要当前的OpenGL上下文}。
Display.create()
(在LWJGL 3之前)或GLFW.glfwCreateWindow()
(在LWJGL 3中)创建OpenGL上下文。