redraw on a timer; rotate quad
This commit is contained in:
parent
9be9f3fc69
commit
17edc0a9d6
32
app.cc
32
app.cc
@ -67,7 +67,6 @@ bool init(void)
|
|||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(3 * sizeof(GLfloat)));
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(3 * sizeof(GLfloat)));
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(6 * sizeof(GLfloat)));
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void *)(6 * sizeof(GLfloat)));
|
||||||
|
|
||||||
modelview = glm::translate(glm::mat4(1.0), glm::vec3(0, 0, -2));
|
|
||||||
projection = glm::perspective(60.0f, (float)WIDTH / (float)HEIGHT, 0.1f, 1000.0f);
|
projection = glm::perspective(60.0f, (float)WIDTH / (float)HEIGHT, 0.1f, 1000.0f);
|
||||||
|
|
||||||
for (int i = 0; i < 16; i++)
|
for (int i = 0; i < 16; i++)
|
||||||
@ -106,12 +105,13 @@ bool init(void)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void display(SDL_Window * window)
|
static void draw_cube()
|
||||||
{
|
{
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glm::mat4 modelview_backup = modelview;
|
||||||
|
|
||||||
program->use();
|
program->use();
|
||||||
cube_array->bind();
|
cube_array->bind();
|
||||||
|
|
||||||
glUniform4f(uniforms.ambient, 1.0, 1.0, 1.0, 1.0);
|
glUniform4f(uniforms.ambient, 1.0, 1.0, 1.0, 1.0);
|
||||||
glUniform4f(uniforms.specular, 1.0, 1.0, 1.0, 1.0);
|
glUniform4f(uniforms.specular, 1.0, 1.0, 1.0, 1.0);
|
||||||
glUniform1f(uniforms.shininess, 150.0);
|
glUniform1f(uniforms.shininess, 150.0);
|
||||||
@ -119,13 +119,30 @@ void display(SDL_Window * window)
|
|||||||
glUniformMatrix4fv(uniforms.modelview, 1, GL_FALSE, &modelview[0][0]);
|
glUniformMatrix4fv(uniforms.modelview, 1, GL_FALSE, &modelview[0][0]);
|
||||||
glUniformMatrix4fv(uniforms.projection, 1, GL_FALSE, &projection[0][0]);
|
glUniformMatrix4fv(uniforms.projection, 1, GL_FALSE, &projection[0][0]);
|
||||||
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
void display(SDL_Window * window)
|
||||||
|
{
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
modelview = glm::translate(glm::mat4(1.0), glm::vec3(0, 0, -2));
|
||||||
|
modelview = glm::rotate(modelview, (float)(SDL_GetTicks() / 1000.0), glm::vec3(0, 1, 0));
|
||||||
|
|
||||||
|
draw_cube();
|
||||||
|
|
||||||
SDL_GL_SwapWindow(window);
|
SDL_GL_SwapWindow(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static SDL_Event UpdateEvent;
|
||||||
|
|
||||||
|
static Uint32 UpdateCallback(Uint32 interval, void * param)
|
||||||
|
{
|
||||||
|
SDL_PushEvent(&UpdateEvent);
|
||||||
|
return interval;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (SDL_Init(SDL_INIT_VIDEO))
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER))
|
||||||
{
|
{
|
||||||
printf("Failed to initialize SDL!\n");
|
printf("Failed to initialize SDL!\n");
|
||||||
return 1;
|
return 1;
|
||||||
@ -164,6 +181,9 @@ int main(int argc, char *argv[])
|
|||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UpdateEvent.type = SDL_USEREVENT;
|
||||||
|
UpdateEvent.user.code = 0;
|
||||||
|
SDL_AddTimer(25, UpdateCallback, NULL);
|
||||||
display(window);
|
display(window);
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_WaitEvent(&event))
|
while (SDL_WaitEvent(&event))
|
||||||
@ -177,5 +197,9 @@ int main(int argc, char *argv[])
|
|||||||
if (event.key.keysym.sym == SDLK_RETURN)
|
if (event.key.keysym.sym == SDLK_RETURN)
|
||||||
display(window);
|
display(window);
|
||||||
}
|
}
|
||||||
|
else if (event.type == SDL_USEREVENT)
|
||||||
|
{
|
||||||
|
display(window);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user