0

我尝试使用 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);
}

我究竟做错了什么?

4

1 回答 1

2

糟糕,抱歉,我忘记调用 glViewport(0, 0, w, h);

于 2013-09-17T16:35:48.340 回答