-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add triangle render code from http://en.wikibooks.org/wiki/OpenGL_Pro…
- Loading branch information
dff180
committed
Jun 22, 2012
1 parent
b24e740
commit 2b88be6
Showing
8 changed files
with
233 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* From the OpenGL Programming wikibook: http://en.wikibooks.org/wiki/OpenGL_Programming | ||
* This file is in the public domain. | ||
* Contributors: Sylvain Beucler | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "shader_utils.h" | ||
|
||
|
||
/** | ||
* Store all the file's contents in memory, useful to pass shaders | ||
* source code to OpenGL | ||
*/ | ||
char* file_read(const char* filename) | ||
{ | ||
FILE* in = fopen(filename, "rb"); | ||
if (in == NULL) return NULL; | ||
|
||
int res_size = BUFSIZ; | ||
char* res = (char*)malloc(res_size); | ||
int nb_read_total = 0; | ||
|
||
while (!feof(in) && !ferror(in)) { | ||
if (nb_read_total + BUFSIZ > res_size) { | ||
if (res_size > 10*1024*1024) break; | ||
res_size = res_size * 2; | ||
res = (char*)realloc(res, res_size); | ||
} | ||
char* p_res = res + nb_read_total; | ||
nb_read_total += fread(p_res, 1, BUFSIZ, in); | ||
} | ||
|
||
fclose(in); | ||
res = (char*)realloc(res, nb_read_total + 1); | ||
res[nb_read_total] = '\0'; | ||
return res; | ||
} | ||
|
||
/** | ||
* Display compilation errors from the OpenGL shader compiler | ||
*/ | ||
void print_log(GLuint object) | ||
{ | ||
GLint log_length = 0; | ||
if (glIsShader(object)) | ||
glGetShaderiv(object, GL_INFO_LOG_LENGTH, &log_length); | ||
else if (glIsProgram(object)) | ||
glGetProgramiv(object, GL_INFO_LOG_LENGTH, &log_length); | ||
else { | ||
fprintf(stderr, "printlog: Not a shader or a program\n"); | ||
return; | ||
} | ||
|
||
char* log = (char*)malloc(log_length); | ||
|
||
if (glIsShader(object)) | ||
glGetShaderInfoLog(object, log_length, NULL, log); | ||
else if (glIsProgram(object)) | ||
glGetProgramInfoLog(object, log_length, NULL, log); | ||
|
||
fprintf(stderr, "%s", log); | ||
free(log); | ||
} | ||
|
||
/** | ||
* Compile the shader from file 'filename', with error handling | ||
*/ | ||
GLuint create_shader(const char* filename, GLenum type) | ||
{ | ||
const GLchar* source = file_read(filename); | ||
if (source == NULL) { | ||
fprintf(stderr, "Error opening %s: ", filename); perror(""); | ||
return 0; | ||
} | ||
GLuint res = glCreateShader(type); | ||
const GLchar* sources[] = { | ||
// Define GLSL version | ||
#ifdef GL_ES_VERSION_2_0 | ||
"#version 100\n" | ||
#else | ||
"#version 120\n" | ||
#endif | ||
, | ||
// GLES2 precision specifiers | ||
#ifdef GL_ES_VERSION_2_0 | ||
// Define default float precision for fragment shaders: | ||
(type == GL_FRAGMENT_SHADER) ? | ||
"#ifdef GL_FRAGMENT_PRECISION_HIGH\n" | ||
"precision highp float; \n" | ||
"#else \n" | ||
"precision mediump float; \n" | ||
"#endif \n" | ||
: "" | ||
// Note: OpenGL ES automatically defines this: | ||
// #define GL_ES | ||
#else | ||
// Ignore GLES 2 precision specifiers: | ||
"#define lowp \n" | ||
"#define mediump\n" | ||
"#define highp \n" | ||
#endif | ||
, | ||
source }; | ||
glShaderSource(res, 3, sources, NULL); | ||
free((void*)source); | ||
|
||
glCompileShader(res); | ||
GLint compile_ok = GL_FALSE; | ||
glGetShaderiv(res, GL_COMPILE_STATUS, &compile_ok); | ||
if (compile_ok == GL_FALSE) { | ||
fprintf(stderr, "%s:", filename); | ||
print_log(res); | ||
glDeleteShader(res); | ||
return 0; | ||
} | ||
|
||
return res; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* From the OpenGL Programming wikibook: http://en.wikibooks.org/wiki/OpenGL_Programming | ||
* This file is in the public domain. | ||
* Contributors: Sylvain Beucler | ||
*/ | ||
#ifndef _CREATE_SHADER_H | ||
#define _CREATE_SHADER_H | ||
//#include <GL/glew.h> | ||
#include <EGL/egl.h> | ||
#include <EGL/eglext.h> | ||
#include <GLES2/gl2.h> | ||
|
||
char* file_read(const char* filename); | ||
void print_log(GLuint object); | ||
GLuint create_shader(const char* filename, GLenum type); | ||
#endif |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
void main(void) { | ||
gl_FragColor[0] = gl_FragCoord.x / 640.0; | ||
gl_FragColor[1] = gl_FragCoord.y / 480.0; | ||
gl_FragColor[2] = 0.5; | ||
gl_FragColor[3] = 1.0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
attribute vec2 coord2d; | ||
|
||
void main(void) { | ||
gl_Position = vec4(coord2d, 0.0, 1.0); | ||
} |