LWJGL制作简单形状的问题:"No OpenGL context found in the current thread"



我只是想在一个窗口中画几个形状。在其他问题中,我现在遇到的最大问题是我在过去几个小时里一直试图修复的这个令人讨厌的错误:

Exception in thread "main" java.lang.RuntimeException: No OpenGL context found in the current thread.
at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
at org.lwjgl.opengl.GL11.glEnd(GL11.java:689)
at project1.project1.process(project1.java:71)
at project1.project1.main(project1.java:22)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

我刚开始使用LWJGL和openGL,所以我对自己出现错误并不感到惊讶。我在圆和直线算法方面也有问题,但在我开始圆方法之前,程序实际上是在一个窗口中绘制多条线(我仍然只得到带有负斜率的任何直线的水平线,但这可能是算法问题)。

package project1;
import java.io.*;
import java.util.*;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GLContext.*;
import static org.lwjgl.opengl.GL30.*;
import static java.lang.Math.PI;
import org.lwjgl.opengl.GLContext;
public class project1 {
private ArrayList<String> shapeList = new ArrayList();
public static void main(String[]args){
project1 shapes = new project1();
shapes.readFile();
shapes.process();
}
public void readFile(){
try(BufferedReader br = new BufferedReader(new FileReader("coordinates.txt"))){
String line = br.readLine();
shapeList.add(line);
int n = 1;
while( line != null){
line = br.readLine();
if(line== null)
break;
shapeList.add(line);
//System.out.println(line);
n++;
}
}catch(Exception e){
System.out.println(e);
}
}
public void process(){
start();
while(!Display.isCloseRequested()){
try{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();               
glPointSize(10);         
glBegin(GL_POINTS);
for(String s: shapeList){
char ch = s.charAt(0);
switch(ch){
//lines
case 'l':
glColor3f(1.0f,0.0f,0.0f);
line(s);
//circle
case 'c':
glColor3f(0.0f,0.0f,1.0f);
circle(s);//if you comment this line out, program still makes lines
//ellipse
case 'e':
ellipse(s);
}
}
}catch(Exception e){
Display.destroy();
}         
glEnd();                
Display.update();
Display.sync(60);
}            
}
public void start(){
try{
createWindow();
initGL();
}catch(Exception e){
e.printStackTrace();
}
}
public void createWindow() throws Exception{
Display.setFullscreen(false);
Display.setTitle("Testing123");
Display.setDisplayMode(new DisplayMode(640,480));
Display.create();
//GLContext.loadOpenGLLibrary();
}
public void initGL(){
glClearColor(0.0f,0.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 0, 480, 1, -1);
glMatrixMode(GL_MODELVIEW);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
public void line(String s){
int[] points = new int[4];
//split into 2 strings by whitespace
String[] arr = s.split("\s+");
//get first coordinate
String[] first = arr[1].split(",");
///get second coordinate
String[] second = arr[2].split(",");
int x1 = Integer.parseInt(first[0]);
int y1 = Integer.parseInt(first[1]);
int x2 = Integer.parseInt(second[0]);
int y2 = Integer.parseInt(second[1]);        
drawLine(x1,y1,x2,y2);
}
public boolean drawLine(int x0, int y0, int x1, int y1){
//System.out.println(x0 + " " + y0 + " " + x1 + " " + y1);
x0 = 10;
y0 = 100;
x1 = 100;
y1 = 10;
/**while(!Display.isCloseRequested()){
try{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0f,0.0f,0.0f);
glPointSize(10);
glBegin(GL_POINTS);
//glVertex2f(350.0f,150.0f);
//glVertex2f(50.0f,50.0f);
**/
//change in y
int dx = x1 - x0;
//change in x
int dy = y1 - y0;
//distance to midpoint
//int d = 2*(dy) - dx;
int d = (dy-dx)/2;
System.out.println(d);
//int incrementRight = 2*(dy);
//int incrementUpRight = 2*(dy-dx);
//int x = x0, y=y0;
int y = y0;
//for(int i=0;x<x1;i++){
for (int x = x0;x<=x1;x++){
if(d>=0){
//x = x + 1;
y = y + 1;
//glVertex2f(x,y);
d = dy-dx;
//d = d+incrementUpRight;
}
else{
//x = x+1;
//y = y+0;
//glVertex2f(x,y);
d = d+dy;
//d = d+incrementRight;
}
glVertex2f(x,y);
}   
//glEnd();
//Display.update();
//Display.sync(60);
//}catch(Exception e){
//System.out.println("Exception occured");
//Display.destroy();
//} 
// }      
return true;
}
public void circle(String s){
int[] points = new int[3];
//split into 2 strings by whitespace
String[] arr = s.split("\s+");
//get first coordinate
String[] first = arr[1].split(",");
int x = Integer.parseInt(first[0]);
int y = Integer.parseInt(first[1]);
int rad = Integer.parseInt(arr[2]);
drawCircle(x,y,rad);
}
public boolean drawCircle(float radx, float rady, float rad){
/**        
int x = 0, y = 0;
int numSeg = 100;
//from theta to 2pi
// x = x+rcostheta;
// y = y+rsintheta;
System.out.println("test");
for(int i = 0;i<numSeg;i++){
float theta = (float)(2*Math.PI*(float)i)/(float)numSeg;
double cx = rad*Math.cos(theta);
double cy = rad*Math.sin(theta);
glVertex2f((float)cx + x0,(float)cy + y0);
}
**/
float x = 0;
float y = rad;
float d = (5 - rad*4)/4;
glVertex2f(112,400);
System.out.println(y);
/**
do{
glVertex2f( radx+x, rady+y);
glVertex2f( radx+x, rady-y);
glVertex2f(radx-x, rady+y);
glVertex2f(radx-x, rady-y);
glVertex2f(radx+x, rady+x);
glVertex2f(radx+x, rady-x);
glVertex2f(radx-y, rady+x);
glVertex2f(radx-y, rady-x);
if(d<0){
d += 2*x +1;
}else{
d += 2*(x-y)+1;
y--;
}
x++;
}while(x<=y);**/
return true;
}
public void ellipse(String s){
}
}

在drawCircle()中的这一点上,我只是想输出ANYTHING。我一直在从一个名为"坐标.txt"的文件中解析它,格式如下:

c 320,100 54
e 100,100 45,80
l 10,380 380,10
l 350,50 500,70
e 450,250 75,35
c 50,50 100

(c是圆,e是椭圆,l是线。)格式化/解析也很好。我正在研究算法,我真的只想知道为什么会发生这种错误?我已经犯了好几个小时这个错误了,我不明白,它让我发疯了。为什么circle()方法不起作用,而line()起作用?

OK SO。万一有人还在想办法。。。我忘了我的休息声明_<

最新更新