Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/gl.h>
- #include <GL/glut.h>
- #include <stdlib.h>
- #include <stddef.h>
- #include <math.h>
- #include <stdio.h>
- GLchar *file_contents(const char *filename, GLint *length)
- {
- FILE *f = fopen(filename, "r");
- GLchar *buffer;
- if (!f) {
- fprintf(stderr, "Unable to open %s for reading\n", filename);
- return NULL;
- }
- fseek(f, 0, SEEK_END);
- *length = ftell(f);
- fseek(f, 0, SEEK_SET);
- buffer = (GLchar*) malloc(*length+1);
- *length = fread(buffer, 1, *length, f);
- fclose(f);
- ((char*)buffer)[*length] = '\0';
- return buffer;
- }
- GLuint make_shader(GLenum type, const char *filename)
- {
- GLint length;
- GLchar *source = file_contents(filename, &length);
- GLuint shader;
- GLint shader_ok;
- if (!source)
- return 0;
- shader = glCreateShader(type);
- glShaderSource(shader, 1, (const GLchar**)&source, &length);
- free(source);
- glCompileShader(shader);
- glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok);
- if (!shader_ok) {
- fprintf(stderr, "Failed to compile %s:\n", filename);
- glDeleteShader(shader);
- return 0;
- }
- return shader;
- }
- GLuint make_program(GLuint vertex_shader, GLuint fragment_shader)
- {
- GLint program_ok;
- GLuint program = glCreateProgram();
- glAttachShader(program, vertex_shader);
- glAttachShader(program, fragment_shader);
- glLinkProgram(program);
- glGetProgramiv(program, GL_LINK_STATUS, &program_ok);
- if (!program_ok) {
- fprintf(stderr, "Failed to link shader program:\n");
- glDeleteProgram(program);
- return 0;
- }
- return program;
- }
- static const GLfloat verts[] = {
- -1.0f, -1.0f,
- 1.0f, -1.0f,
- -1.0f, 1.0f,
- 1.0f, 1.0f
- };
- static const GLushort polys[] = { 0, 1, 2, 2,1,3 };
- GLuint program, posidx, vbuf, ebuf;
- void init_gl() {
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glClearColor(0.5,0.5,0.5,1);
- glClearDepth(1.0);
- glDepthFunc(GL_LESS);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_TEXTURE_2D);
- glViewport(0,0,800,600);
- program = make_program(make_shader(GL_VERTEX_SHADER, "stupid.v.glsl"), make_shader(GL_FRAGMENT_SHADER, "stupid.f.glsl"));
- posidx = glGetAttribLocation(program, "position");
- glGenBuffers(1, &vbuf);
- glGenBuffers(1, &ebuf);
- glBindBuffer(GL_ARRAY_BUFFER, vbuf);
- glBufferData(GL_ARRAY_BUFFER, 4*8, verts, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebuf);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2*6, polys, GL_STATIC_DRAW);
- }
- void render() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glUseProgram(program);
- glEnableVertexAttribArray(posidx);
- glBindBuffer(GL_ARRAY_BUFFER, vbuf);
- glVertexAttribPointer( posidx, 2, GL_FLOAT, GL_FALSE, 4*2, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebuf);
- glDrawElements(
- GL_TRIANGLES,
- 6,
- GL_UNSIGNED_SHORT,
- 0
- );
- glDisableVertexAttribArray(posidx);
- glutSwapBuffers();
- }
- int main(int argc, char* argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
- glutInitWindowSize(800, 600);
- glutCreateWindow("Flag");
- glutDisplayFunc(&render);
- init_gl();
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement