Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <iostream>
- #include <fstream>
- #include <assert.h>
- #include <string.h>
- #include "teapot.h"
- #include <glm/glm.hpp>
- int gl_width = 800;
- int gl_height = 800;
- std::string readShaderFile(std::string filename) {
- std::ifstream ifile(filename);
- std::string text;
- while( ifile.good() ) {
- std::string line;
- std::getline(ifile, line);
- text.append(line + "\n");
- }
- text += '\0';
- return text;
- }
- void printShaderStatus(GLuint shader) {
- GLint status;
- glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
- if (status == GL_FALSE) {
- GLint infoLogLength;
- glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
- GLchar *strInfoLog = (char *)malloc(infoLogLength + 1);
- glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);
- fprintf(stderr, "Compilation error in shader: %s\n", strInfoLog);
- free(strInfoLog);
- glDeleteShader(shader);
- }
- }
- int main() {
- /* pointer to an OS window created by GLFW */
- GLFWwindow *window = NULL;
- /* pointers to strings that GL will return, giving use the version being run */
- const GLubyte *renderer;
- const GLubyte *version;
- GLuint shader_programme;
- GLuint vao;
- /* start GL context and O/S window using the GLFW helper library */
- if (!glfwInit()) {
- fprintf (stderr, "ERROR: could not start GLFW3\n");
- return 1;
- }
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- /* ask GLFW library to create an OS window */
- window = glfwCreateWindow(gl_width, gl_height, "Assignment 2", NULL, NULL);
- if (!window) {
- fprintf (stderr, "ERROR: opening OS window\n");
- return 1;
- }
- glfwMakeContextCurrent(window);
- glewExperimental = GL_TRUE;
- glewInit();
- /* get version info */
- renderer = glGetString(GL_RENDERER); /* get renderer string */
- version = glGetString(GL_VERSION); /* version as a string */
- printf("Renderer: %s\n", renderer);
- printf("OpenGL version supported %s\n", version);
- //
- // Set up vertex buffers and vertex array object
- // --------------------------------------------------------------------------
- {
- GLuint points_vbo, normals_vbo;
- glGenBuffers(1, &points_vbo);
- glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
- // copy our points from the header file into our VBO on graphics hardware
- glBufferData(GL_ARRAY_BUFFER, sizeof (teapot_vertex_points),
- teapot_vertex_points, GL_STATIC_DRAW);
- // and grab the normals
- glGenBuffers(1, &normals_vbo);
- glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof (teapot_normals),
- teapot_normals, GL_STATIC_DRAW);
- glGenVertexArrays(1, &vao);
- glBindVertexArray(vao);
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
- glEnableVertexAttribArray(1);
- glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
- }
- //
- // Load shaders from files
- // --------------------------------------------------------------------------
- {
- std::string vertex_shader_str = readShaderFile("/Users/mattdonnelly/Documents/College/4th Year/Computer Graphics/Assignment 2/Assignment 2/teapot.vert");
- GLchar const *vertex_shader_source = vertex_shader_str.c_str();
- std::string fragment_shader_str = readShaderFile("/Users/mattdonnelly/Documents/College/4th Year/Computer Graphics/Assignment 2/Assignment 2/teapot.frag");
- GLchar const *fragment_shader_source = fragment_shader_str.c_str();
- GLuint vs, fs;
- vs = glCreateShader(GL_VERTEX_SHADER);
- fs = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(vs, 1, &vertex_shader_source, NULL);
- glShaderSource(fs, 1, &fragment_shader_source, NULL);
- glCompileShader(vs);
- printShaderStatus(vs);
- glCompileShader(fs);
- printShaderStatus(fs);
- shader_programme = glCreateProgram ();
- glAttachShader(shader_programme, fs);
- glAttachShader(shader_programme, vs);
- glLinkProgram (shader_programme);
- }
- //
- // Create some matrices
- // --------------------------------------------------------------------------
- // a model matrix
- // can you see what affine transformation this does?
- float M[] = {
- 0.05f, 0.0f, 0.0f, 0.0f,
- 0.0f, 0.05f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.05f, 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f
- };
- // location of "M" in vertex shader
- int M_loc = glGetUniformLocation (shader_programme, "M");
- assert(M_loc > -1);
- // send matrix values to shader immediately
- glUseProgram(shader_programme);
- glUniformMatrix4fv(M_loc, 1, GL_FALSE, M);
- //
- // Start rendering
- // --------------------------------------------------------------------------
- // tell GL to only draw onto a pixel if the fragment is closer to the viewer
- glEnable(GL_DEPTH_TEST); // enable depth-testing
- glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as "closer"
- glClearColor(0.5, 0.5, 0.5, 1.0);
- while (!glfwWindowShouldClose (window)) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glUseProgram(shader_programme);
- glBindVertexArray(vao);
- glViewport(0, 0, gl_width, gl_height);
- glDrawArrays(GL_TRIANGLES, 0, teapot_vertex_count);
- /* this just updates window events and keyboard input events (not used yet) */
- glfwPollEvents();
- glfwSwapBuffers(window);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement