我试图用OpenGL 2.0绘制矩形,但我得到了黑屏,这是我的代码:
public FloatBuffer buf;
public int program;
@Override public void init()
{
program = glCreateProgram();
int vs = glCreateShader(GL_VERTEX_SHADER);
int fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vs, ... code of vertex shader ...);
glShaderSource(fs, ... code of fragment shader ...);
glCompileShader(vs);
glCompileShader(fs);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glUseProgram(program);
buf = BufferUtils.createFloatBuffer(6);
buf.put(-.5F); //LEFT
buf.put(-.5F);
buf.put(0);// CENTER
buf.put(.5F);
buf.put(.5F);
buf.put(-.5F).flip();// RIGHT
glClearColor(0, 0, 0, 1);
}
@Override public void render()
{
glClear(GL_COLOR_BUFFER_BIT);
int ptr = glGetAttribLocation(program, "pos");
glEnableVertexAttribArray(ptr);
glVertexAttribPointer(ptr, 2, false, 0, buf);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
顶点着色器代码:
#version 110
attribute vec4 pos;
void main()
{
gl_Position = pos;
}
片段着色器代码:
#version 110
void main()
{
gl_FragColor = vec4(1,0,0,1);
}
我做错了什么?
答案 0 :(得分:2)
Ooooops,对不起,我忘了打电话glViewport(0,0,w,h);