Last active
July 14, 2016 10:54
-
-
Save lygstate/40f5d9e42cb48163406718a51eee3fc0 to your computer and use it in GitHub Desktop.
glfw with gis
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
#include <GLFW/glfw3.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <stdio.h> | |
#define esLogMessage printf | |
typedef struct | |
{ | |
// Handle to a program object | |
GLuint programObject; | |
} UserData; | |
typedef struct | |
{ | |
UserData* userData; | |
// Handle to a program object | |
GLsizei width; | |
GLsizei height; | |
} ESContext; | |
/// | |
// Create a shader object, load the shader source, and | |
// compile the shader. | |
// | |
GLuint LoadShader(GLenum type, const GLchar *shaderSrc) | |
{ | |
GLuint shader; | |
GLint compiled; | |
// Create the shader object | |
shader = glCreateShader(type); | |
if (shader == 0) | |
return 0; | |
// Load the shader source | |
glShaderSource(shader, 1, &shaderSrc, NULL); | |
// Compile the shader | |
glCompileShader(shader); | |
// Check the compile status | |
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); | |
if (!compiled) | |
{ | |
GLint infoLen = 0; | |
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); | |
if (infoLen > 1) | |
{ | |
GLchar* infoLog = (GLchar*)malloc(sizeof(char) * infoLen); | |
glGetShaderInfoLog(shader, infoLen, NULL, infoLog); | |
free(infoLog); | |
} | |
glDeleteShader(shader); | |
return 0; | |
} | |
return shader; | |
} | |
/// | |
// Initialize the shader and program object | |
// | |
int Init(ESContext *esContext) | |
{ | |
UserData *userData = esContext->userData; | |
GLchar vShaderStr[] = | |
"attribute vec4 vPosition; \n" | |
"void main() \n" | |
"{ \n" | |
" gl_Position = vPosition; \n" | |
"} \n"; | |
GLchar fShaderStr[] = | |
"precision mediump float;\n"\ | |
"void main() \n" | |
"{ \n" | |
" gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n" | |
"} \n"; | |
GLuint vertexShader; | |
GLuint fragmentShader; | |
GLuint programObject; | |
GLint linked; | |
// Load the vertex/fragment shaders | |
vertexShader = LoadShader(GL_VERTEX_SHADER, vShaderStr); | |
fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fShaderStr); | |
// Create the program object | |
programObject = glCreateProgram(); | |
if (programObject == 0) | |
return 0; | |
glAttachShader(programObject, vertexShader); | |
glAttachShader(programObject, fragmentShader); | |
// Bind vPosition to attribute 0 | |
glBindAttribLocation(programObject, 0, "vPosition"); | |
// Link the program | |
glLinkProgram(programObject); | |
// Check the link status | |
glGetProgramiv(programObject, GL_LINK_STATUS, &linked); | |
if (!linked) | |
{ | |
GLint infoLen = 0; | |
glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen); | |
if (infoLen > 1) | |
{ | |
GLchar* infoLog = (GLchar*)malloc(sizeof(char) * infoLen); | |
glGetProgramInfoLog(programObject, infoLen, NULL, infoLog); | |
esLogMessage("Error linking program:\n%s\n", infoLog); | |
free(infoLog); | |
} | |
glDeleteProgram(programObject); | |
return false; | |
} | |
// Store the program object | |
userData->programObject = programObject; | |
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); | |
return true; | |
} | |
/// | |
// Draw a triangle using the shader pair created in Init() | |
// | |
void Draw(ESContext *esContext) | |
{ | |
UserData *userData = esContext->userData; | |
GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f, | |
-0.5f, -0.5f, 0.0f, | |
0.5f, -0.5f, 0.0f }; | |
// Set the viewport | |
glViewport(0, 0, esContext->width, esContext->height); | |
// Clear the color buffer | |
glClear(GL_COLOR_BUFFER_BIT); | |
// Use the program object | |
glUseProgram(userData->programObject); | |
// Load the vertex data | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices); | |
glEnableVertexAttribArray(0); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
} | |
int main(void) | |
{ | |
ESContext context; | |
UserData userData; | |
GLFWwindow* window; | |
context.userData = &userData; | |
/* Initialize the library */ | |
if (!glfwInit()) | |
return -1; | |
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); | |
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); | |
/* Create a windowed mode window and its OpenGL context */ | |
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); | |
if (!window) | |
{ | |
glfwTerminate(); | |
return -1; | |
} | |
context.width = 640; | |
context.height = 480; | |
/* Make the window's context current */ | |
glfwMakeContextCurrent(window); | |
Init(&context); | |
/* Loop until the user closes the window */ | |
while (!glfwWindowShouldClose(window)) | |
{ | |
Draw(&context); | |
/* Swap front and back buffers */ | |
glfwSwapBuffers(window); | |
/* Poll for and process events */ | |
glfwPollEvents(); | |
} | |
glfwTerminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment