我给了Archsynthetics'引物和 Hello三角章节,并决定使用LWJGL。当我第一次尝试后没有在屏幕上显示任何内容时,我再次尝试从另一个GL 3.x tutorial移植一些C ++代码,但无济于事。
据我所知,我将所有部件放在一起,但屏幕仍然是黑色的。我理解这些概念,但我确信我在这里缺少一些简单的东西。
我尽可能地减少了这一点。请注意,以下类使用this shader helper,并且我可以告诉它按预期工作(除了缺少错误检查 - 但是,我确保着色器编译)。
public class HelloTriangle31 {
public static void main(String[] args) throws LWJGLException, InterruptedException, IOException
{
// Setup display mode (size)
Display.setDisplayMode(new DisplayMode(800, 600));
// Set context settings
// Basically forces 3.1
ContextAttribs contextAttributes = new ContextAttribs(3, 2)
.withForwardCompatible(true)
.withProfileCompatibility(false)
.withProfileCore(true);
Display.create(new PixelFormat(), contextAttributes);
Display.setResizable(false);
Display.setVSyncEnabled(true);
// Log some stuff
System.out.println("OpenGL version: " + GL11.glGetString(GL11.GL_VERSION));
// Setup
String vertexStr = readEntireFile(new File("vertex32.gl"));
int vertexID = ShaderUtils.makeShader(vertexStr, GL20.GL_VERTEX_SHADER);
String fragStr = readEntireFile(new File("fragment32.gl"));
int fragID = ShaderUtils.makeShader(fragStr, GL20.GL_FRAGMENT_SHADER);
int program = GL20.glCreateProgram();
GL20.glAttachShader(program, vertexID);
GL20.glAttachShader(program, fragID);
GL20.glBindAttribLocation(program, 0, "in_Position");
GL20.glBindAttribLocation(program, 1, "in_Color");
GL20.glLinkProgram(program);
FloatBuffer vertexFloats = BufferUtils.createFloatBuffer(9);
assert(vertexFloats.capacity() == 9);
vertexFloats.put(new float[]{
-0.3f, 0.5f, 0f,
-0.8f, -0.5f, 0f,
0.2f, -0.5f, 0f
});
FloatBuffer colorFloats = BufferUtils.createFloatBuffer(9);
assert(colorFloats.capacity() == 9);
colorFloats.put(new float[]{
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
});
IntBuffer vertexArrayInts = BufferUtils.createIntBuffer(1);
assert(vertexArrayInts.capacity() == 1);
GL30.glGenVertexArrays(vertexArrayInts);
GL30.glBindVertexArray(vertexArrayInts.get(0));
IntBuffer vertexBufferInts = BufferUtils.createIntBuffer(2);
assert(vertexBufferInts.capacity() == 2);
GL15.glGenBuffers(vertexBufferInts);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferInts.get(0));
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexFloats, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
GL20.glEnableVertexAttribArray(0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBufferInts.get(1));
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, colorFloats, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(1, 3, GL11.GL_FLOAT, false, 0, 0);
GL20.glEnableVertexAttribArray(1);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL30.glBindVertexArray(0);
GL11.glViewport(0, 0, 800, 600);
GL20.glUseProgram(program);
// Main loop
while(!Display.isCloseRequested())
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL30.glBindVertexArray(vertexArrayInts.get(0));
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);
GL30.glBindVertexArray(0);
Display.update();
}
Display.destroy();
}
private static String readEntireFile(File file) throws IOException
{
// Open input stream
FileInputStream fis = new FileInputStream(file);
try
{
byte[] buffer = new byte[(int) file.length()];
int len = fis.read(buffer);
return new String(buffer, 0, len);
}finally{
if(fis != null) fis.close();
}
}
}
vertex32.gl :
#version 140
in vec3 in_Position;
in vec3 in_Color;
out vec3 ex_Color;
void main(void)
{
gl_Position = vec4(in_Position, 1.0);
ex_Color = in_Color;
}
fragment32.gl :
#version 140
precision highp float; // needed only for version 1.30
in vec3 ex_Color;
out vec4 out_Color;
void main(void)
{
out_Color = vec4(ex_Color,1.0);
}
我没有看到我出错的地方。没有错误,唯一的输出是版本字符串(正确显示OpenGL 3.2 - 是,我尝试过使用和不使用任何类型的显式上下文属性)。
在我所遵循的所有教程中,让我感到奇怪的是,没有使用例如glOrtho
设置投影矩阵。我在LWJGL(使用GL 1.1功能)中有应用程序可以正常使用glOrtho
,但现在我正在尝试升级/重新基础/改进我的GL知识我回到了第一个方向。
我错过了什么?
编辑:定义VM参数-Dorg.lwjgl.util.Debug=true
会产生:
[LWJGL] Initial mode: 1920 x 1080 x 32 @60Hz
[LWJGL] MemoryUtil Accessor: AccessorUnsafe
[LWJGL] GL_ARB_gpu_shader_fp64 was reported as available but an entry point is missing
[LWJGL] GL_ARB_shader_subroutine was reported as available but an entry point is missing
[LWJGL] GL_ARB_vertex_attrib_64bit was reported as available but an entry point is missing
OpenGL version: 3.2.0
答案 0 :(得分:0)
我没有看到告诉OpenGL的部分in_Position
是属性0而in_Color
是属性1.有问题的教程使用layout(location)
语法进行着色器。
如果您不能或不会使用该语法,则需要在链接着色器之前使用glBindAttribLocation
calls 进行此操作。