我正在尝试使用自定义投影矩阵实现与处理投影映射相关的项目。我找到了一个可能给我一个线索的例子,但它太旧了,openGL和Processing同时改变了很多。我对着色器和openGL不是很熟悉,但到目前为止我可以将旧代码更新为我在下面显示的版本,所以你也可以与原版进行比较。
我仍然得到:
GLException:不是gl2实现
我也有点混淆使用PGL,GL,PG2同时。我觉得这不是一个好习惯。
Processing 1.0论坛的原始版本代码为here。
这是我到目前为止尝试更新的代码:
import com.jogamp.opengl.*;
import java.nio.FloatBuffer;
float[] modelview = {
0.670984f, 0.250691f, 0.674993f, 0, -0.288247f,
0.966749f, -0.137371f, 0f, -0.68315f, -0.0505059f, 0.720934f, 0f,
0.164808f, 2.1425f, 32.9616f, 1f };
float[] proj = {
0.78125f, 0, 0, 0, 0, 1.04167f, 0, 0, 0, 0, -1.0002f, -1, 0,
0, -2.0002f, 0 };
FloatBuffer mvbuf;
FloatBuffer projbuf;
void setup() {
size(1024, 768, P3D);
PJOGL.profile = 2; //not sure if needed
mvbuf = FloatBuffer.wrap(modelview);
projbuf= FloatBuffer.wrap(proj);
GLProfile glp = GLProfile.get(GLProfile.GL2);
GLCapabilitiesImmutable glcaps = (GLCapabilitiesImmutable) new GLCapabilities(glp);
GLCapabilities tGLCapabilities = new GLCapabilities(glp);
println("System Capabilities:" + glcaps.toString());
println("Profile Details: " + glp.toString());
println("Is GL2 Supported?: " + glp.isGL2());
}
void draw() {
background(0);
PGL pgl = (PJOGL) beginPGL();
GL gl = ((PJOGL) pgl).gl;
GL2 gl2 = gl.getGL2(); //GLException: not a GL2 implemantation
gl2.glMatrixMode(GL2.GL_PROJECTION);
gl2.glLoadIdentity();
gl2.glLoadMatrixf(projbuf);
gl2.glMatrixMode(GL2.GL_MODELVIEW);
gl2.glLoadIdentity();
gl2.glLoadMatrixf(mvbuf);
drawGrid(100, 10, gl2);
endPGL(); //not sure if this is closing what it supposed to
}
void drawGrid(float len, float offset, GL2 g) {
int nr_lines = (int)(len/offset);
g.glColor3f(1, 1, 1);
g.glBegin(g.GL_LINES);
for (int i=-nr_lines; i<nr_lines; i++) {
g.glVertex3f(i*offset, 0, -nr_lines*offset);
g.glVertex3f(i*offset, 0, nr_lines*offset);
}
for (int i=-nr_lines; i<nr_lines; i++) {
g.glVertex3f(-nr_lines*offset, 0, i*offset);
g.glVertex3f(nr_lines*offset, 0, i*offset);
}
g.glEnd();
}
答案 0 :(得分:1)
首先尝试这个:
PGraphicsOpenGL pg = (PGraphicsOpenGL)g;
println(pg.OPENGL_VERSION);
输出什么?对我来说,这将输出:
4.5.0 NVIDIA 376.51
因此调用gl.getGL2()
失败,因为OpenGL 4.5核心上下文不向后兼容OpenGL 2.x上下文。
如果我没记错,那么您必须使用PJOGL.profile
并将其设置为1
,以获得向后兼容的上下文:
PJOGL.profile = 1;
请注意,如果您使用的是Processing 3.0,则可能必须使用settings()
:
void settings() {
size(1024, 768, P3D);
PJOGL.profile = 1;
}