Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <cstdio>
#include <cstdlib>
#include "glew.h"
#include "glfw3.h"
typedef short int16;
typedef int int32;
typedef unsigned short uint16;
typedef unsigned int uint32;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
GLuint getShaderProgramId(const char *vertexFile, const char *fragmentFile);
GLuint compileShader(const GLchar *source, GLuint shaderType);
GLFWwindow* window;
const uint32
WINDOW_WIDTH = 1440,
WINDOW_HEIGHT = 810;
GLuint shaderProgramId, vao, vbo;
const char *vertexShader =
"#version 330\n"
"layout (location = 0) in vec2 vert;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(vert.x / 720.0 - 1.0, vert.y / 405.0 - 1.0, 0.0, 1.0);\n"
"}\n";
const char *fragmentShader =
"#version 330\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
" color = vec4(1.0, 0.0, 1.0, 1.0);\n"
"}\n";
struct Rect
{
uint16 x, y, width, height;
};
Rect rectangles[3] = { { 100, 200, 200, 300 }, { 400, 450, 500, 200 }, { 600, 100, 600, 300 } };
static int16 vertices[36];
void render()
{
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 36);
glfwSwapBuffers(window);
}
int32 main()
{
// initialize GLFW
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_FALSE);
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "", 0, 0);
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
// initialize GLEW
glewExperimental = GL_TRUE;
glewInit();
// OpenGL setup
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisable(GL_SCISSOR_TEST);
// viewport setup
{
int32 width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
}
// initialize shader
shaderProgramId = getShaderProgramId(vertexShader, fragmentShader);
for (uint32 i = 0; i < 3; i++)
{
// top right
vertices[i * 12] = rectangles[i].x + rectangles[i].width;
vertices[i * 12 + 1] = rectangles[i].y;
// bottom right
vertices[i * 12 + 2] = rectangles[i].x + rectangles[i].width;
vertices[i * 12 + 3] = rectangles[i].y + rectangles[i].height;
// top left
vertices[i * 12 + 4] = rectangles[i].x;
vertices[i * 12 + 5] = rectangles[i].y;
// bottom right
vertices[i * 12 + 6] = rectangles[i].x + rectangles[i].width;
vertices[i * 12 + 7] = rectangles[i].y + rectangles[i].height;
// bottom left
vertices[i * 12 + 8] = rectangles[i].x;
vertices[i * 12 + 9] = rectangles[i].y + rectangles[i].height;
// top left
vertices[i * 12 + 10] = rectangles[i].x;
vertices[i * 12 + 11] = rectangles[i].y;
}
// initialize OpenGL buffers
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 2, GL_SHORT, GL_FALSE, 2 * sizeof(int16), 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glUseProgram(shaderProgramId);
glBindVertexArray(vao);
double t = 0.0;
const double dt = 0.01;
double currentTime = glfwGetTime();
double accumulator = 0.0;
while (!glfwWindowShouldClose(window))
{
double newTime = glfwGetTime();
double frameTime = newTime - currentTime;
currentTime = newTime;
accumulator += frameTime;
while (accumulator >= dt)
{
glfwPollEvents();
accumulator -= dt;
t += dt;
}
render();
}
// cleanup
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(1, &vbo);
glDeleteProgram(shaderProgramId);
glfwTerminate();
return 0;
}
void key_callback(GLFWwindow* window, int key, int32 scancode, int32 action, int32 mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
GLuint getShaderProgramId(const char *vertexFile, const char *fragmentFile)
{
GLuint programId, vertexHandler, fragmentHandler;
vertexHandler = compileShader(vertexFile, GL_VERTEX_SHADER);
fragmentHandler = compileShader(fragmentFile, GL_FRAGMENT_SHADER);
programId = glCreateProgram();
glAttachShader(programId, vertexHandler);
glAttachShader(programId, fragmentHandler);
glLinkProgram(programId);
GLint success;
GLchar infoLog[512];
glGetProgramiv(programId, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(programId, 512, 0, infoLog);
printf("Error in linking of shaders:\n%s\n", infoLog);
system("pause");
exit(1);
}
glDeleteShader(vertexHandler);
glDeleteShader(fragmentHandler);
return programId;
}
GLuint compileShader(const GLchar *source, GLuint shaderType)
{
GLuint shaderHandler;
shaderHandler = glCreateShader(shaderType);
glShaderSource(shaderHandler, 1, &source, 0);
glCompileShader(shaderHandler);
GLint success;
GLchar infoLog[512];
glGetShaderiv(shaderHandler, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(shaderHandler, 512, 0, infoLog);
printf("Error in compilation of shader:\n%s\n", infoLog);
system("pause");
exit(1);
};
return shaderHandler;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment