Initial commit
authorLuke Lau <luke_lau@icloud.com>
Sun, 16 Sep 2018 01:04:20 +0000 (02:04 +0100)
committerLuke Lau <luke_lau@icloud.com>
Sun, 16 Sep 2018 01:04:20 +0000 (02:04 +0100)
.gitignore [new file with mode: 0644]
CMakeLists.txt [new file with mode: 0644]
assignment1.cpp [new file with mode: 0644]
fragment.glsl [new file with mode: 0644]
vertex.glsl [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..0505cd2
--- /dev/null
@@ -0,0 +1,12 @@
+.DS_Store
+*.swp
+CMakeCache.txt
+CMakeFiles
+CMakeScripts
+Testing
+Makefile
+cmake_install.cmake
+install_manifest.txt
+compile_commands.json
+CTestTestfile.cmake
+bin
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644 (file)
index 0000000..10ab545
--- /dev/null
@@ -0,0 +1,24 @@
+cmake_minimum_required(VERSION 2.6)
+project(assignment-1)
+set(CMAKE_BINARY_DIR "bin")
+set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
+add_executable(assignment-1 assignment1.cpp)
+
+find_package(OpenGL REQUIRED)
+include_directories(${OPENGL_INCLUDE_DIRS})
+target_link_libraries(assignment-1 ${OPENGL_LIBRARIES})
+
+find_package(GLUT REQUIRED)
+if(NOT GLUT_FOUND)
+       message(ERROR "GLUT not found")
+endif(NOT GLUT_FOUND)
+include_directories(${GLUT_INCLUDE_DIR})
+target_link_libraries(assignment-1 ${GLUT_LIBRARIES})
+
+find_package(GLEW REQUIRED)
+if(NOT GLEW_FOUND)
+       message(ERROR "GLEW not found")
+endif(NOT GLEW_FOUND)
+include_directories(${GLEW_INCLUDE_DIR})
+target_link_libraries(assignment-1 ${GLEW_LIBRARIES})
+
diff --git a/assignment1.cpp b/assignment1.cpp
new file mode 100644 (file)
index 0000000..43cd626
--- /dev/null
@@ -0,0 +1,153 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <fstream>
+#include <sstream>
+#ifdef __APPLE__
+#include <GL/glew.h>
+#else
+#include <OpenGL/glew.h>
+#endif
+#include <GLUT/glut.h>
+
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+
+using namespace std;
+
+void display() {
+       glClear(GL_COLOR_BUFFER_BIT);
+       glDrawArrays(GL_TRIANGLES, 0, 6);
+       glutSwapBuffers();
+}
+
+void attachShader(GLuint progId, const char* filePath, GLenum type) {
+       GLuint shader = glCreateShader(type);
+
+       if (!shader) {
+               fprintf(stderr, "error creating shader\n");
+               exit(1);
+       }
+
+       ifstream file(filePath);
+       stringstream buffer;
+       buffer << file.rdbuf();
+       const char* contents = buffer.str().c_str();
+
+       glShaderSource(shader, 1, (const GLchar**)&contents, NULL);
+       glCompileShader(shader);
+       GLint success;
+       glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
+       if (!success) {
+               GLchar log[1024];
+               glGetShaderInfoLog(shader, 1024, NULL, log);
+               fprintf(stderr, "error: %s\n", log);
+               exit(1);
+       }
+       glAttachShader(progId, shader);
+}
+
+GLuint compileShaders() {
+       GLuint progId = glCreateProgram();
+
+       attachShader(progId, "vertex.glsl", GL_VERTEX_SHADER);
+       attachShader(progId, "fragment.glsl", GL_FRAGMENT_SHADER);
+
+       glLinkProgram(progId);
+       GLint success = 0;
+       glGetProgramiv(progId, GL_LINK_STATUS, &success);
+       if (!success) {
+               GLchar log[1024];
+               glGetProgramInfoLog(progId, sizeof(log), NULL, log);
+               fprintf(stderr, "error linking: %s\n", log);
+               exit(1);
+       }
+
+       glUseProgram(progId);
+       return progId;
+}
+
+void generateObjectBuffers() {
+       GLfloat vertices[] = {
+               0.0f, -1.0f, 0.0f,
+               1.0f, -1.0f, 0.0f,
+               0.5f, 1.0f, 0.0f,
+
+               -1.0f, -1.0f, 0.0f,
+               0.0f, -1.0f, 0.0f,
+               -0.5f, 1.0f, 0.0f
+       };
+
+       GLfloat colors[] = {
+               0, 1, 0, 1,
+               1, 0, 0, 1,
+               0, 0, 1, 1,
+               
+               0, 1, 0, 1,
+               1, 0, 0, 1,
+               0, 0, 1, 1                      
+       };
+
+       GLuint numVerts = 6;
+
+       GLuint VBO;
+       glGenBuffers(1, &VBO);
+       glBindBuffer(GL_ARRAY_BUFFER, VBO);
+       glBufferData(GL_ARRAY_BUFFER, numVerts * 7 * sizeof(GLfloat), NULL, GL_STATIC_DRAW);
+
+       GLuint vertsLen = numVerts * 3 * sizeof(GLfloat);
+       GLuint colorsLen = numVerts * 4 * sizeof(GLfloat);
+       glBufferSubData(GL_ARRAY_BUFFER, 0, vertsLen, vertices);
+       glBufferSubData(GL_ARRAY_BUFFER, vertsLen, colorsLen, colors);
+}
+
+#define BUFFER_OFFSET(i) ((char *)NULL + (i))
+
+void linkBuffers(GLuint progId) {
+       GLuint numVerts = 6;
+       GLuint posId = glGetAttribLocation(progId, "vPosition");
+       GLuint colorId = glGetAttribLocation(progId, "vColor");
+
+       GLuint vao = 0;
+       glGenVertexArrays(1, &vao);
+       glBindVertexArray(vao);
+
+       glEnableVertexAttribArray(posId);
+       glVertexAttribPointer(posId, 3, GL_FLOAT, GL_FALSE, 0, 0);
+
+       glEnableVertexAttribArray(colorId);
+       glVertexAttribPointer(colorId, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVerts * 3 * sizeof(GLfloat)));
+
+}
+
+void init() {
+       GLuint progId = compileShaders();
+       generateObjectBuffers();
+       linkBuffers(progId);
+       
+       glValidateProgram(progId);
+       
+       GLint success;
+       glGetProgramiv(progId, GL_VALIDATE_STATUS, &success);
+       if (!success) {
+               GLchar log[1024];
+               glGetProgramInfoLog(progId, sizeof(log), NULL, log);
+               fprintf(stderr, "error: %s\n", log);
+               exit(1);
+       }
+}
+
+int main(int argc, char** argv) {
+       glutInit(&argc, argv);
+       glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE);
+       glutInitWindowSize(800, 600);
+       glutCreateWindow("Hello Triangle");
+       glutDisplayFunc(display);
+
+       glewInit();
+       
+       init();
+
+       glutMainLoop();
+
+       return 0;
+}
+
diff --git a/fragment.glsl b/fragment.glsl
new file mode 100644 (file)
index 0000000..edc4f63
--- /dev/null
@@ -0,0 +1,7 @@
+#version 330
+in vec4 color;
+out vec4 FragColor;
+
+void main() {
+       FragColor = color;
+}
diff --git a/vertex.glsl b/vertex.glsl
new file mode 100644 (file)
index 0000000..6333f54
--- /dev/null
@@ -0,0 +1,9 @@
+#version 330
+in vec3 vPosition;
+in vec4 vColor;
+out vec4 color;
+
+void main() {
+       gl_Position = vec4(vPosition, 1.0);
+       color = vColor;
+}