/*
Copyright 2010 Etay Meiri
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
Tutorial 01 - Create a window
*/
#include
#include
#include
static const char *vert_shader_text =
"uniform mat4 rotation;\n"
"attribute vec4 pos;\n"
"attribute vec4 color;\n"
"void main() {\n"
" gl_Position = rotation * pos;\n"
" gl_FrontColor = color;\n"
"}\n";
static GLuint rotation_uniform;
static GLuint pos, col;
static int first_render = 1;
static void _resize_viewport(void)
{
static int i = 1;
int size;
i = i * 2;
size = i * 100;
printf(">>> resizing to %dx%d\n", size, size);
glutReshapeWindow(size, size);
glViewport(0, 0, size, size);
// glClear(GL_COLOR_BUFFER_BIT);
glutPostRedisplay();
}
static void
handle_keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'i':
_resize_viewport();
break;
default:
break;
}
}
static void RenderSceneCB()
{
static const GLfloat verts[3][2] = {
{ -0.5, -0.5 },
{ 0.5, -0.5 },
{ 0, 0.5 }
};
static const GLfloat colors[3][3] = {
{ 1, 0, 0 },
{ 0, 1, 0 },
{ 0, 0, 1 }
};
GLfloat angle;
GLfloat rotation[4][4] = {
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
};
printf(">>> rendering\n");
if (first_render)
glClear(GL_COLOR_BUFFER_BIT);
angle = 0;
rotation[0][0] = cos(angle);
rotation[0][2] = sin(angle);
rotation[2][0] = -sin(angle);
rotation[2][2] = cos(angle);
// glViewport(0, 0, window->geometry.width, window->geometry.height);
glUniformMatrix4fv(rotation_uniform, 1, GL_FALSE,
(GLfloat *) rotation);
glVertexAttribPointer(pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
glVertexAttribPointer(col, 3, GL_FLOAT, GL_FALSE, 0, colors);
glEnableVertexAttribArray(pos);
glEnableVertexAttribArray(col);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(pos);
glDisableVertexAttribArray(col);
first_render = 0;
glutSwapBuffers();
}
static GLuint
create_shader(const char *source, GLenum shader_type)
{
GLuint shader;
GLint status;
shader = glCreateShader(shader_type);
glShaderSource(shader, 1, (const char **) &source, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (!status) {
char log[1000];
GLsizei len;
glGetShaderInfoLog(shader, 1000, &len, log);
fprintf(stderr, "Error: compiling %s: %*s\n",
shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
len, log);
exit(1);
}
return shader;
}
static void InitializeGlutCallbacks()
{
GLuint vert;
GLuint program;
GLint status;
pos = 0;
col = 1;
glutDisplayFunc(RenderSceneCB);
glutKeyboardFunc(handle_keyboard);
vert = create_shader(vert_shader_text, GL_VERTEX_SHADER);
program = glCreateProgram();
glAttachShader(program, vert);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (!status) {
char log[1000];
GLsizei len;
glGetProgramInfoLog(program, 1000, &len, log);
fprintf(stderr, "Error: linking:\n%*s\n", len, log);
exit(1);
}
glUseProgram(program);
glBindAttribLocation(program, pos, "pos");
glBindAttribLocation(program, col, "color");
glLinkProgram(program);
rotation_uniform = glGetUniformLocation(program, "rotation");
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(100, 100);
glutInitWindowPosition(100, 100);
glutCreateWindow("Tutorial 01");
InitializeGlutCallbacks();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glutMainLoop();
return 0;
}