Created
February 24, 2016 22:15
-
-
Save fschutt/b74c06027bb6e81af648 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define GLEW_STATIC | |
#include <glew.h> | |
#include <glfw3.h> | |
#include <iostream> | |
#include <string> | |
#include <fstream> | |
#include "main.h" | |
using namespace std; | |
//Window dimensions | |
const GLuint WIDTH = 800, HEIGHT = 600; | |
int main() | |
{ | |
//Set up GLFW to make a window | |
glfwInit(); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); | |
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | |
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); | |
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL", nullptr, nullptr); | |
if (window == nullptr) | |
{ | |
cout << "GLFW failed to initialize. Please check your OpenGL version and update your drivers." << endl; | |
glfwTerminate(); | |
return -1; | |
} | |
glfwMakeContextCurrent(window); | |
// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions | |
glewExperimental = GL_TRUE; | |
// Initialize GLEW to setup the OpenGL Function pointers | |
if (glewInit() != GLEW_OK) | |
{ | |
std::cout << "Failed to initialize GLEW" << std::endl; | |
return -1; | |
} | |
glViewport(0, 0, WIDTH, HEIGHT); | |
//-------------------------------------------TUTORIAL 2------------------------------------------------// | |
//Now we have a window which we can draw in | |
//Vertices | |
GLfloat vertices[] = { | |
-0.5f, -0.5f, 0.0f, | |
0.5f, -0.5f, 0.0f, | |
0.0f, 0.5f, 0.0f | |
}; | |
//Compile vertex shader | |
const char* vertexShader = getGLSLCode("..\\shaders\\shader.vert"); | |
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER); | |
glShaderSource(vertShader, 1, &vertexShader, NULL); | |
glCompileShader(vertShader); | |
// Check for compile time errors | |
GLint success; | |
GLchar infoLog[512]; | |
glGetShaderiv(vertShader, GL_COMPILE_STATUS, &success); | |
if (!success) | |
{ | |
glGetShaderInfoLog(vertShader, 512, NULL, infoLog); | |
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; | |
} | |
//Compile fragment shader | |
const char* fragmentShader = getGLSLCode("..\\shaders\\shader.frag"); | |
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(fragShader, 1, &fragmentShader, NULL); | |
glCompileShader(fragShader); | |
// Check for compile time errors | |
glGetShaderiv(fragShader, GL_COMPILE_STATUS, &success); | |
if (!success) | |
{ | |
glGetShaderInfoLog(fragShader, 512, NULL, infoLog); | |
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; | |
} | |
//Create shader program | |
GLuint shaderProgram = glCreateProgram(); | |
glAttachShader(shaderProgram, vertShader); | |
glAttachShader(shaderProgram, fragShader); | |
glLinkProgram(shaderProgram); | |
//OK, now comes the tricky part | |
//VBO (contains many VAOs), container for data to send to the GPU | |
GLuint VBO; | |
GLuint VAO; | |
glGenBuffers(1, &VBO); | |
glGenVertexArrays(1, &VAO); | |
//Bind the VAO first, then set vertex buffers and attribute pointers | |
glBindVertexArray(VAO); | |
glBindBuffer(GL_ARRAY_BUFFER, VBO); | |
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW); //Copy the vertices into the VBO | |
//OpenGL should interpret the data as an array of GL_FLOATS | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); | |
glEnableVertexAttribArray(0); | |
//Unbind buffers | |
glBindBuffer(GL_ARRAY_BUFFER, 0); | |
glBindVertexArray(0); | |
//-------------------------Game loop-----------------------------------// | |
while (!glfwWindowShouldClose(window)) | |
{ | |
//Poll input | |
glfwPollEvents(); | |
glfwSetKeyCallback(window, key_callback); | |
//Clear color buffer | |
glClearColor(0.1f, 0.2f, 0.3f, 0.5f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
//Render triangle | |
glUseProgram(shaderProgram); | |
glBindVertexArray(VAO); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
glBindVertexArray(0); | |
glfwSwapBuffers(window); | |
} | |
glDeleteShader(vertShader); | |
glDeleteShader(fragShader); | |
glfwTerminate(); | |
return 0; | |
} | |
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) | |
{ | |
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) | |
{ | |
glfwSetWindowShouldClose(window, GL_TRUE); | |
} | |
} | |
//Reads file | |
const char* getGLSLCode(string fname) { | |
string ret; | |
ifstream ifs(fname.c_str()); | |
if (ifs) { | |
string line; | |
while (getline(ifs, line)) { | |
ret = ret + line + "\n"; | |
} | |
ifs.close(); | |
} | |
else { | |
return ""; | |
} | |
return ret.c_str(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#ifndef _COMMON_H | |
#define _COMMON_H | |
using namespace std; | |
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode); | |
const char* getGLSLCode(string fname); | |
#endif // !_COMMON_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 core | |
out vec4 color; | |
void main() | |
{ | |
color = vec4(1.0f, 0.5f, 0.2f, 1.0f); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 core | |
layout (location = 0) in vec3 position; | |
void main() | |
{ | |
gl.Position = vec4(position.x, position.y, position.z, 1.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//For future reference:
The getGLSL function returns a pointer, but at the time it returns it, the string stored at the memory adress is already destroyed. So it returns garbage. It is smarter to simply return a string, and then convert the string into the const char* later. This way, I avoid juggling with pointers.
Also, it is gl_Position, not gl.Position, sorry.