Skip to main content
added 318 characters in body
Source Link
LongLT
  • 117
  • 6

I'm new and trying to draw a very simple quad with VAO and GLSL.

My definitions:

typedef struct SPos
{
    float x;
    float y;
} SPos;
SPos mVertices[6];
SPos mIndicies[6];
GLuint mVao;
GLuint mVertexVbo;
GLuint mIbo;

These are my steps:

  1. Load Vertex, Fragment code (I'm sure that no mistake in this step) and get programID

  2. Create VBO

    glGenBuffers(1, &mVertexVbo);
    glBindBuffer(GL_ARRAY_BUFFER, mVertexVbo);
    glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(SPos), mVertices, GL_STATIC_DRAW);
    
  3. Create IBO.

    mIndices[0] = 0;
    mIndices[1] = 1;
    mIndices[2] = 2;
    mIndices[3] = 3;
    glGenBuffers(1, &mIbo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIbo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(GLuint), (GLuint*) mIndices,\
        GL_STATIC_DRAW);
    
  4. Create VAO

    glGenVertexArrays(1, &mVao);
    glBindVertexArray(mVao);
    glUseProgram(mProgram);
    
    GLint posLoc = glGetAttribLocation(mShader.program, "a_posL");
    glEnableVertexAttribArray(posLoc);
    glBindBuffer( GL_ARRAY_BUFFER, mVertexVbo );
    glVertexAttribPointer( posLoc, 2, GL_FLOAT, GL_FALSE, 0, 0 );
    
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mIbo );
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    
  5. Render loop.

    mVertices[0].x = -50.0f;
    mVertices[0].y = -50.0f;
    
    mVertices[1].x = 50.0f;
    mVertices[1].y = -50.0f;
    
    mVertices[2].x = 50.0f;
    mVertices[2].y = 50.0f;
    
    mVertices[3].x = -50.0f;
    mVertices[3].y = 50.0f;
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBindVertexArray(mVao);
    glDrawElements( GL_TRIANGLE_FAN, 4, GL_UNSIGNED_INT, 0 );
    // GLES2 swap function.
    eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
    

These are my shaders

fragment:

#version 300 es
precision mediump float;

out vec4 oColor;
uniform vec4 unifColor;

void main()
{
    oColor = vec4(1);
}

vertex:

#version 300 es

in vec2 a_posL;

void main()
{
    gl_Position = vec4(a_posL, 1.0, 1.0);
}

Nothing is draw in the screen. What's wrong?

All above is follows a VAO tutorial on the internet.

I spend 1 day to debug and scan it very carefully but not work.

EDIT:

Thanks GOD. The problem is that I setted the data of mVertices in render step, have to set it before create VBO.

I'm still confuse that glBufferData need a pointer for data but Why I cannot pass the address of data then assign its value in later? Why I have to assign its value before bind it to VBO?

I'm new and trying to draw a very simple quad with VAO and GLSL.

My definitions:

typedef struct SPos
{
    float x;
    float y;
} SPos;
SPos mVertices[6];
SPos mIndicies[6];
GLuint mVao;
GLuint mVertexVbo;
GLuint mIbo;

These are my steps:

  1. Load Vertex, Fragment code (I'm sure that no mistake in this step) and get programID

  2. Create VBO

    glGenBuffers(1, &mVertexVbo);
    glBindBuffer(GL_ARRAY_BUFFER, mVertexVbo);
    glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(SPos), mVertices, GL_STATIC_DRAW);
    
  3. Create IBO.

    mIndices[0] = 0;
    mIndices[1] = 1;
    mIndices[2] = 2;
    mIndices[3] = 3;
    glGenBuffers(1, &mIbo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIbo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(GLuint), (GLuint*) mIndices,\
        GL_STATIC_DRAW);
    
  4. Create VAO

    glGenVertexArrays(1, &mVao);
    glBindVertexArray(mVao);
    glUseProgram(mProgram);
    
    GLint posLoc = glGetAttribLocation(mShader.program, "a_posL");
    glEnableVertexAttribArray(posLoc);
    glBindBuffer( GL_ARRAY_BUFFER, mVertexVbo );
    glVertexAttribPointer( posLoc, 2, GL_FLOAT, GL_FALSE, 0, 0 );
    
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mIbo );
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    
  5. Render loop.

    mVertices[0].x = -50.0f;
    mVertices[0].y = -50.0f;
    
    mVertices[1].x = 50.0f;
    mVertices[1].y = -50.0f;
    
    mVertices[2].x = 50.0f;
    mVertices[2].y = 50.0f;
    
    mVertices[3].x = -50.0f;
    mVertices[3].y = 50.0f;
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBindVertexArray(mVao);
    glDrawElements( GL_TRIANGLE_FAN, 4, GL_UNSIGNED_INT, 0 );
    // GLES2 swap function.
    eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
    

These are my shaders

fragment:

#version 300 es
precision mediump float;

out vec4 oColor;
uniform vec4 unifColor;

void main()
{
    oColor = vec4(1);
}

vertex:

#version 300 es

in vec2 a_posL;

void main()
{
    gl_Position = vec4(a_posL, 1.0, 1.0);
}

Nothing is draw in the screen. What's wrong?

All above is follows a VAO tutorial on the internet.

I spend 1 day to debug and scan it very carefully but not work.

I'm new and trying to draw a very simple quad with VAO and GLSL.

My definitions:

typedef struct SPos
{
    float x;
    float y;
} SPos;
SPos mVertices[6];
SPos mIndicies[6];
GLuint mVao;
GLuint mVertexVbo;
GLuint mIbo;

These are my steps:

  1. Load Vertex, Fragment code (I'm sure that no mistake in this step) and get programID

  2. Create VBO

    glGenBuffers(1, &mVertexVbo);
    glBindBuffer(GL_ARRAY_BUFFER, mVertexVbo);
    glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(SPos), mVertices, GL_STATIC_DRAW);
    
  3. Create IBO.

    mIndices[0] = 0;
    mIndices[1] = 1;
    mIndices[2] = 2;
    mIndices[3] = 3;
    glGenBuffers(1, &mIbo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIbo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(GLuint), (GLuint*) mIndices,\
        GL_STATIC_DRAW);
    
  4. Create VAO

    glGenVertexArrays(1, &mVao);
    glBindVertexArray(mVao);
    glUseProgram(mProgram);
    
    GLint posLoc = glGetAttribLocation(mShader.program, "a_posL");
    glEnableVertexAttribArray(posLoc);
    glBindBuffer( GL_ARRAY_BUFFER, mVertexVbo );
    glVertexAttribPointer( posLoc, 2, GL_FLOAT, GL_FALSE, 0, 0 );
    
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mIbo );
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    
  5. Render loop.

    mVertices[0].x = -50.0f;
    mVertices[0].y = -50.0f;
    
    mVertices[1].x = 50.0f;
    mVertices[1].y = -50.0f;
    
    mVertices[2].x = 50.0f;
    mVertices[2].y = 50.0f;
    
    mVertices[3].x = -50.0f;
    mVertices[3].y = 50.0f;
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBindVertexArray(mVao);
    glDrawElements( GL_TRIANGLE_FAN, 4, GL_UNSIGNED_INT, 0 );
    // GLES2 swap function.
    eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
    

These are my shaders

fragment:

#version 300 es
precision mediump float;

out vec4 oColor;
uniform vec4 unifColor;

void main()
{
    oColor = vec4(1);
}

vertex:

#version 300 es

in vec2 a_posL;

void main()
{
    gl_Position = vec4(a_posL, 1.0, 1.0);
}

Nothing is draw in the screen. What's wrong?

All above is follows a VAO tutorial on the internet.

I spend 1 day to debug and scan it very carefully but not work.

EDIT:

Thanks GOD. The problem is that I setted the data of mVertices in render step, have to set it before create VBO.

I'm still confuse that glBufferData need a pointer for data but Why I cannot pass the address of data then assign its value in later? Why I have to assign its value before bind it to VBO?

Source Link
LongLT
  • 117
  • 6

How to combine VAO and shader language?

I'm new and trying to draw a very simple quad with VAO and GLSL.

My definitions:

typedef struct SPos
{
    float x;
    float y;
} SPos;
SPos mVertices[6];
SPos mIndicies[6];
GLuint mVao;
GLuint mVertexVbo;
GLuint mIbo;

These are my steps:

  1. Load Vertex, Fragment code (I'm sure that no mistake in this step) and get programID

  2. Create VBO

    glGenBuffers(1, &mVertexVbo);
    glBindBuffer(GL_ARRAY_BUFFER, mVertexVbo);
    glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(SPos), mVertices, GL_STATIC_DRAW);
    
  3. Create IBO.

    mIndices[0] = 0;
    mIndices[1] = 1;
    mIndices[2] = 2;
    mIndices[3] = 3;
    glGenBuffers(1, &mIbo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIbo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(GLuint), (GLuint*) mIndices,\
        GL_STATIC_DRAW);
    
  4. Create VAO

    glGenVertexArrays(1, &mVao);
    glBindVertexArray(mVao);
    glUseProgram(mProgram);
    
    GLint posLoc = glGetAttribLocation(mShader.program, "a_posL");
    glEnableVertexAttribArray(posLoc);
    glBindBuffer( GL_ARRAY_BUFFER, mVertexVbo );
    glVertexAttribPointer( posLoc, 2, GL_FLOAT, GL_FALSE, 0, 0 );
    
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mIbo );
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    
  5. Render loop.

    mVertices[0].x = -50.0f;
    mVertices[0].y = -50.0f;
    
    mVertices[1].x = 50.0f;
    mVertices[1].y = -50.0f;
    
    mVertices[2].x = 50.0f;
    mVertices[2].y = 50.0f;
    
    mVertices[3].x = -50.0f;
    mVertices[3].y = 50.0f;
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBindVertexArray(mVao);
    glDrawElements( GL_TRIANGLE_FAN, 4, GL_UNSIGNED_INT, 0 );
    // GLES2 swap function.
    eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
    

These are my shaders

fragment:

#version 300 es
precision mediump float;

out vec4 oColor;
uniform vec4 unifColor;

void main()
{
    oColor = vec4(1);
}

vertex:

#version 300 es

in vec2 a_posL;

void main()
{
    gl_Position = vec4(a_posL, 1.0, 1.0);
}

Nothing is draw in the screen. What's wrong?

All above is follows a VAO tutorial on the internet.

I spend 1 day to debug and scan it very carefully but not work.