Skip to content

Commit

Permalink
pad out vertex array to four-element vectors, and pass timer instead …
Browse files Browse the repository at this point in the history
…of fade_factor as uniform
  • Loading branch information
jckarter committed Mar 29, 2010
1 parent 2408792 commit 08d480f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
28 changes: 14 additions & 14 deletions hello-gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ static struct {
GLuint vertex_shader, fragment_shader, program;

struct {
GLint fade_factor;
GLint timer;
GLint textures[2];
} uniforms;

struct {
GLint position;
} attributes;

GLfloat fade_factor;
GLfloat timer;
} g_resources;

/*
Expand Down Expand Up @@ -136,10 +136,10 @@ static GLuint make_program(GLuint vertex_shader, GLuint fragment_shader)
* Data used to seed our vertex array and element array buffers:
*/
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f
-1.0f, -1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
};
static const GLuint g_element_buffer_data[] = { 0, 1, 2, 3 };

Expand Down Expand Up @@ -183,8 +183,8 @@ static int make_resources(void)
if (g_resources.program == 0)
return 0;

g_resources.uniforms.fade_factor
= glGetUniformLocation(g_resources.program, "fade_factor");
g_resources.uniforms.timer
= glGetUniformLocation(g_resources.program, "timer");
g_resources.uniforms.textures[0]
= glGetUniformLocation(g_resources.program, "textures[0]");
g_resources.uniforms.textures[1]
Expand All @@ -199,18 +199,18 @@ static int make_resources(void)
/*
* GLUT callbacks:
*/
static void update_fade_factor(void)
static void update_timer(void)
{
int milliseconds = glutGet(GLUT_ELAPSED_TIME);
g_resources.fade_factor = sinf((float)milliseconds * 0.001f) * 0.5f + 0.5f;
g_resources.timer = (float)milliseconds * 0.001f;
glutPostRedisplay();
}

static void render(void)
{
glUseProgram(g_resources.program);

glUniform1f(g_resources.uniforms.fade_factor, g_resources.fade_factor);
glUniform1f(g_resources.uniforms.timer, g_resources.timer);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, g_resources.textures[0]);
Expand All @@ -223,10 +223,10 @@ static void render(void)
glBindBuffer(GL_ARRAY_BUFFER, g_resources.vertex_buffer);
glVertexAttribPointer(
g_resources.attributes.position, /* attribute */
2, /* size */
4, /* size */
GL_FLOAT, /* type */
GL_FALSE, /* normalized? */
sizeof(GLfloat)*2, /* stride */
sizeof(GLfloat)*4, /* stride */
(void*)0 /* array buffer offset */
);
glEnableVertexAttribArray(g_resources.attributes.position);
Expand All @@ -252,7 +252,7 @@ int main(int argc, char** argv)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(400, 300);
glutCreateWindow("Hello World");
glutIdleFunc(&update_fade_factor);
glutIdleFunc(&update_timer);
glutDisplayFunc(&render);

glewInit();
Expand Down
2 changes: 1 addition & 1 deletion hello-gl.f.glsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#version 110

uniform float fade_factor;
uniform sampler2D textures[2];

varying float fade_factor;
varying vec2 texcoord;

void main()
Expand Down
10 changes: 7 additions & 3 deletions hello-gl.v.glsl
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#version 110

attribute vec2 position;
uniform float timer;

attribute vec4 position;

varying vec2 texcoord;
varying float fade_factor;

void main()
{
gl_Position = vec4(position, 0.0, 1.0);
texcoord = position * vec2(0.5) + vec2(0.5);
gl_Position = position;
texcoord = position.xy * vec2(0.5) + vec2(0.5);
fade_factor = sin(timer * 0.5 + 0.5);
}

0 comments on commit 08d480f

Please sign in to comment.