Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /examples/iso2d-spatial/utils/utils.c
ViewVC logotype

View of /examples/iso2d-spatial/utils/utils.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2950 - (download) (as text) (annotate)
Wed Mar 4 20:52:35 2015 UTC (4 years, 7 months ago) by lamonts
File size: 3753 byte(s)
Adding the iso2d demo with particle interaction
#include "utils.h"

static GLchar * utils_readFile (const char *file); 
static const char * utils_shaderName (GLenum kind);

void reportError (GLenum err, const char *file, int line, const char *msg){
  const char *error;

    switch (err) {
      case GL_INVALID_ENUM: error = "invalid enum"; break;
      case GL_INVALID_VALUE: error = "invalid value"; break;
      case GL_INVALID_OPERATION: error = "invalid operation"; break;
      case GL_INVALID_FRAMEBUFFER_OPERATION: error = "invalid framebuffer operation"; break;
      case GL_OUT_OF_MEMORY: error = "out of memory"; break;
      default: error = "unknown"; break;
    }

    fprintf(stderr, "[%s:%d] %s; %s\n\n",file,line,error,msg); 

    exit (EXIT_FAILURE);
}

void checkError (const char *file, int line, const char *msg)
{
    GLenum sts = glGetError();
    if (sts != GL_NO_ERROR)
        reportError (sts, file, line, msg);
}


GLuint utils_loadShadersFromFile(GLenum kind, const char * file){

	GLuint id = 0; 

	id = glCreateShader(kind); 

	if(id == 0){
		fprintf(stderr,"Error creating shade: kind = %s, error = %d",utils_shaderName(kind),glGetError()); 
		exit(EXIT_FAILURE);
	}

    GLint sts;

    const GLchar *src = utils_readFile (file);

    if (src == NULL)
		return id;

    glShaderSource (id, 1, &src, 0);

    free((void *)src);

    glCompileShader (id);

  /* check for errors */
    glGetShaderiv (id, GL_COMPILE_STATUS, &sts);
    if (sts != GL_TRUE) {
      /* the compile failed, so report an error */
	glGetShaderiv (id, GL_INFO_LOG_LENGTH, &sts);
	if (sts != 0) {
	    GLchar *log = malloc(sizeof(GLchar) * sts);
	    glGetShaderInfoLog (id, sts, 0, log);
	    fprintf(stderr,"Error compiling %s shader \" %s \" :\n  no log\n", utils_shaderName(kind), log); 
	    free(log);
	}
	else
	    fprintf(stderr,"Error compiling %s shader \" %s \" :\n  no log\n" , utils_shaderName(kind), file); 
    }

    return id;

}
/* Create GL program */ 
GLuint utils_createGLProgram(GLuint vId, GLuint fId) 
{
	GLuint id  = glCreateProgram(); 
	if(id == 0){
		fprintf(stderr, "Error creating shader program\n");
		exit(EXIT_FAILURE); 
	}
	//attach shaders 
	glAttachShader(id, vId); 
	glAttachShader(id, fId); 

	//Link program 
	GLint	sts;

    glLinkProgram (id);

    /* check for errors in the linking */
    glGetProgramiv(id, GL_LINK_STATUS, &sts);
    if (sts != GL_TRUE) {
      /* the link failed, so report an error */
	glGetProgramiv (id, GL_INFO_LOG_LENGTH, &sts);
	if (sts != 0) {
	    GLchar *log = malloc(sizeof(GLchar) * sts);
	    glGetProgramInfoLog (id, sts, 0, log);
	    fprintf(stderr,"Error linking program:%s\n\n\n",log); 
	    fprintf(stderr,"Shader ID = %d\n", id); 
	    free(log);
	}
	else
	  fprintf(stderr,"Error linking program:\n  no log\n\n"); 
	  exit (EXIT_FAILURE);
    } 
    return id; 
}

/* Local functions */ 
static GLchar * utils_readFile (const char *file)
{
    struct stat	st;

  /* get the size of the file */
    if (stat(file, &st) < 0) {
	fprintf(stderr, "error reading the size of \" %s \"\n",file); 
	return 0;
    }
    size_t sz = st.st_size;

  /* open the file */
    FILE *strm = fopen(file, "r");
    if (strm == NULL) {
	fprintf(stderr, "error openning \" %s \"\n",file); 
	return 0;
    }

  /* allocate the buffer */
    GLchar *buf = malloc(sizeof(GLchar) * (sz+1));

  /* read the file */
    if (fread(buf, 1, sz, strm) != sz) {
	fprintf(stderr, "error reading \" %s \"\n",file); 
	free (buf);
    }
    buf[sz] = '\0';

    fclose (strm);

    return buf;

}
static const char *utils_shaderName (GLenum kind)
{
    if (kind == GL_FRAGMENT_SHADER) return "fragment";
    else if (kind == GL_VERTEX_SHADER) return "vertex";
    else if (kind == GL_GEOMETRY_SHADER) return "geometry";
    else return "unknown";
}

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0