Unheavly _GLBuffer and create new fanction initVertexArray
This commit is contained in:
parent
5460918bcc
commit
b6ae3369a1
2 changed files with 24 additions and 9 deletions
30
opengl.c
30
opengl.c
|
@ -91,6 +91,24 @@ void createProgram ( GLuint *shaders, int len )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void initVertexArray ( void )
|
||||||
|
{
|
||||||
|
glGenVertexArrays(1, &sp.vao);
|
||||||
|
glBindVertexArray(sp.vao);
|
||||||
|
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, sp.vbo);
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sp.ibo);
|
||||||
|
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
|
||||||
|
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void *) (4*4*sizeof(float)));
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sp.ibo);
|
||||||
|
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
|
||||||
void createBuffer ( void )
|
void createBuffer ( void )
|
||||||
{
|
{
|
||||||
GLushort indices[] = {
|
GLushort indices[] = {
|
||||||
|
@ -128,6 +146,7 @@ void setUniformLocation ( void )
|
||||||
void startShaderProg ( void )
|
void startShaderProg ( void )
|
||||||
{
|
{
|
||||||
createBuffer();
|
createBuffer();
|
||||||
|
initVertexArray();
|
||||||
|
|
||||||
GLuint shaders[] = {
|
GLuint shaders[] = {
|
||||||
createShader("vertex.glsl", GL_VERTEX_SHADER),
|
createShader("vertex.glsl", GL_VERTEX_SHADER),
|
||||||
|
@ -152,16 +171,11 @@ void _GLBuffer( void )
|
||||||
glClearDepth(1.0f);
|
glClearDepth(1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
glUseProgram(sp.program);
|
glUseProgram(sp.program);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
|
||||||
glEnableVertexAttribArray(0);
|
glBindVertexArray(sp.vao);
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, sp.vbo);
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sp.ibo);
|
|
||||||
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
|
|
||||||
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void *) (4*4*sizeof(float)));
|
|
||||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
|
||||||
glDisableVertexAttribArray(0);
|
|
||||||
glDisableVertexAttribArray(1);
|
|
||||||
|
|
||||||
|
glBindVertexArray(0);
|
||||||
glUseProgram(0);
|
glUseProgram(0);
|
||||||
}
|
}
|
||||||
|
|
3
opengl.h
3
opengl.h
|
@ -5,9 +5,10 @@ typedef struct {
|
||||||
SDL_Window* window;
|
SDL_Window* window;
|
||||||
SDL_GLContext glcontext;
|
SDL_GLContext glcontext;
|
||||||
} _glWindows_t;
|
} _glWindows_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
GLuint program;
|
GLuint program;
|
||||||
|
GLuint vao;
|
||||||
GLuint vbo;
|
GLuint vbo;
|
||||||
GLuint ibo;
|
GLuint ibo;
|
||||||
GLuint projMatrLoc;
|
GLuint projMatrLoc;
|
||||||
|
|
Loading…
Add table
Reference in a new issue