我正在使用 Java 中的 LWJGL 和 OpenGL 制作一款简单的 2D 游戏,但我似乎无法渲染多个四边形。
我的主类中有一个 Squares
的数组列表,如果列表中有多个 Square,它只会渲染列表中的最后一个,而无法渲染其他的(我调试并打印了 Square 的名称,这表明它正在正确地迭代它们,但只是出于某种原因决定不渲染其中任何一个,而是渲染最后一个)。所以我唯一能想到的是,要么我的 Square 类 Draw 方法有问题(因为我仅使用该方法来渲染我的四边形),要么我的 OpenGL 有问题设置代码。
如果有帮助的话,我的代码看起来非常类似于此视频中的内容,因为我一直在很大程度上关注此视频和 lwjgl 上的 wiki 页面:http://www.youtube.com/watch?v=EjbOjio_pC4
方形类:
package dasting;
import org.lwjgl.*; //lwjgl engine
import org.lwjgl.opengl.*; //opengl
import static org.lwjgl.opengl.GL11.*; //Dunno yet, youtube said so
import org.lwjgl.LWJGLException; //Allows tries and catches with exception handling for LWJGL (IMPORTANT SHIT)
import java.util.Random;
public class Square {
private int x1, x2, y1, y2, roomHeight, roomWidth;
//constructor takes the position values and width and height of room for boundary checks
Square(int X1, int X2, int Y2, int Y1, int rmWidth, int rmHeight) {
x1 = X1; //initialising the point values
x2 = X2;
y2 = Y2;
y1 = Y1;
roomHeight = rmHeight;
roomWidth = rmWidth;
}
public void draw() { //draw method
//Rendering random stuff example code. Also moves the square
glClear(GL_COLOR_BUFFER_BIT);
//Render quad
glBegin(GL_QUADS);
glVertex2i(x1, y1);
glVertex2i(x2, y1);
glVertex2i(x2, y2);
glVertex2i(x1, y2);
glEnd();
}
}
这是我认为在我的主类中可能有问题的 openGl 设置:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, roomWidth, roomHeight, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
最佳答案
您应该每帧调用 glClear
,而不是每次绘制 Square
时调用。将对 glClear
的调用移出 draw
函数,并将其放在绘图循环的开头。
关于java - opengl仅从java中的许多列表中绘制最终的四边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15508181/