Initial commit
[opengl.git] / assignment1.cpp
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fstream>
4 #include <sstream>
5 #ifdef __APPLE__
6 #include <GL/glew.h>
7 #else
8 #include <OpenGL/glew.h>
9 #endif
10 #include <GLUT/glut.h>
11
12 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
13
14 using namespace std;
15
16 void display() {
17         glClear(GL_COLOR_BUFFER_BIT);
18         glDrawArrays(GL_TRIANGLES, 0, 6);
19         glutSwapBuffers();
20 }
21
22 void attachShader(GLuint progId, const char* filePath, GLenum type) {
23         GLuint shader = glCreateShader(type);
24
25         if (!shader) {
26                 fprintf(stderr, "error creating shader\n");
27                 exit(1);
28         }
29
30         ifstream file(filePath);
31         stringstream buffer;
32         buffer << file.rdbuf();
33         const char* contents = buffer.str().c_str();
34
35         glShaderSource(shader, 1, (const GLchar**)&contents, NULL);
36         glCompileShader(shader);
37         GLint success;
38         glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
39         if (!success) {
40                 GLchar log[1024];
41                 glGetShaderInfoLog(shader, 1024, NULL, log);
42                 fprintf(stderr, "error: %s\n", log);
43                 exit(1);
44         }
45         glAttachShader(progId, shader);
46 }
47
48 GLuint compileShaders() {
49         GLuint progId = glCreateProgram();
50
51         attachShader(progId, "vertex.glsl", GL_VERTEX_SHADER);
52         attachShader(progId, "fragment.glsl", GL_FRAGMENT_SHADER);
53
54         glLinkProgram(progId);
55         GLint success = 0;
56         glGetProgramiv(progId, GL_LINK_STATUS, &success);
57         if (!success) {
58                 GLchar log[1024];
59                 glGetProgramInfoLog(progId, sizeof(log), NULL, log);
60                 fprintf(stderr, "error linking: %s\n", log);
61                 exit(1);
62         }
63
64         glUseProgram(progId);
65         return progId;
66 }
67
68 void generateObjectBuffers() {
69         GLfloat vertices[] = {
70                 0.0f, -1.0f, 0.0f,
71                 1.0f, -1.0f, 0.0f,
72                 0.5f, 1.0f, 0.0f,
73
74                 -1.0f, -1.0f, 0.0f,
75                 0.0f, -1.0f, 0.0f,
76                 -0.5f, 1.0f, 0.0f
77         };
78
79         GLfloat colors[] = {
80                 0, 1, 0, 1,
81                 1, 0, 0, 1,
82                 0, 0, 1, 1,
83                 
84                 0, 1, 0, 1,
85                 1, 0, 0, 1,
86                 0, 0, 1, 1                      
87         };
88
89         GLuint numVerts = 6;
90
91         GLuint VBO;
92         glGenBuffers(1, &VBO);
93         glBindBuffer(GL_ARRAY_BUFFER, VBO);
94         glBufferData(GL_ARRAY_BUFFER, numVerts * 7 * sizeof(GLfloat), NULL, GL_STATIC_DRAW);
95
96         GLuint vertsLen = numVerts * 3 * sizeof(GLfloat);
97         GLuint colorsLen = numVerts * 4 * sizeof(GLfloat);
98         glBufferSubData(GL_ARRAY_BUFFER, 0, vertsLen, vertices);
99         glBufferSubData(GL_ARRAY_BUFFER, vertsLen, colorsLen, colors);
100 }
101
102 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
103
104 void linkBuffers(GLuint progId) {
105         GLuint numVerts = 6;
106         GLuint posId = glGetAttribLocation(progId, "vPosition");
107         GLuint colorId = glGetAttribLocation(progId, "vColor");
108
109         GLuint vao = 0;
110         glGenVertexArrays(1, &vao);
111         glBindVertexArray(vao);
112
113         glEnableVertexAttribArray(posId);
114         glVertexAttribPointer(posId, 3, GL_FLOAT, GL_FALSE, 0, 0);
115
116         glEnableVertexAttribArray(colorId);
117         glVertexAttribPointer(colorId, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVerts * 3 * sizeof(GLfloat)));
118
119 }
120
121 void init() {
122         GLuint progId = compileShaders();
123         generateObjectBuffers();
124         linkBuffers(progId);
125         
126         glValidateProgram(progId);
127         
128         GLint success;
129         glGetProgramiv(progId, GL_VALIDATE_STATUS, &success);
130         if (!success) {
131                 GLchar log[1024];
132                 glGetProgramInfoLog(progId, sizeof(log), NULL, log);
133                 fprintf(stderr, "error: %s\n", log);
134                 exit(1);
135         }
136 }
137
138 int main(int argc, char** argv) {
139         glutInit(&argc, argv);
140         glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE);
141         glutInitWindowSize(800, 600);
142         glutCreateWindow("Hello Triangle");
143         glutDisplayFunc(display);
144
145         glewInit();
146         
147         init();
148
149         glutMainLoop();
150
151         return 0;
152 }
153