|
64
|
1 |
/*
|
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
3 |
* All rights reserved.
|
|
|
4 |
* This component and the accompanying materials are made available
|
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
|
6 |
* which accompanies this distribution, and is available
|
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
|
8 |
*
|
|
|
9 |
* Initial Contributors:
|
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
|
11 |
*
|
|
|
12 |
* Contributors:
|
|
|
13 |
*
|
|
|
14 |
* Description:
|
|
|
15 |
*
|
|
|
16 |
*/
|
|
|
17 |
|
|
|
18 |
#include <stdio.h>
|
|
|
19 |
#include <stdlib.h>
|
|
|
20 |
#include <string.h>
|
|
|
21 |
#include <GLES2/gl2.h>
|
|
|
22 |
#include <sys/syslimits.h>
|
|
|
23 |
|
|
|
24 |
#include "shader.h"
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
GLuint LoadAndCompileShader(char* path, char* shadername, GLenum type)
|
|
|
28 |
{
|
|
|
29 |
char filename[PATH_MAX];
|
|
|
30 |
int len = 0;
|
|
|
31 |
GLuint shader = 0;
|
|
|
32 |
GLint compiled = 0;
|
|
|
33 |
char* shaderdata = NULL;
|
|
|
34 |
FILE* file;
|
|
|
35 |
|
|
|
36 |
strcpy(filename,path);
|
|
|
37 |
strcat(filename,"\\");
|
|
|
38 |
strcat(filename,shadername);
|
|
|
39 |
file = fopen(filename,"rb");
|
|
|
40 |
if (!file)
|
|
|
41 |
{
|
|
|
42 |
return 0;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
fseek (file, 0, SEEK_END);
|
|
|
47 |
len = ftell (file);
|
|
|
48 |
fseek(file, 0, SEEK_SET);
|
|
|
49 |
if (len<0)
|
|
|
50 |
{
|
|
|
51 |
fclose(file);
|
|
|
52 |
return 0;
|
|
|
53 |
}
|
|
|
54 |
shaderdata = (char*)malloc(len+1);
|
|
|
55 |
if (!shaderdata)
|
|
|
56 |
{
|
|
|
57 |
fclose(file);
|
|
|
58 |
return 0;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
fread(shaderdata, 1, len, file);
|
|
|
62 |
fclose(file);
|
|
|
63 |
shaderdata[len] = 0;
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
shader = glCreateShader(type);
|
|
|
67 |
glShaderSource(shader, 1, (const char**)&shaderdata,NULL);
|
|
|
68 |
glCompileShader(shader);
|
|
|
69 |
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
|
|
|
70 |
|
|
|
71 |
if (!compiled)
|
|
|
72 |
{
|
|
|
73 |
char infobuf[ 256 ];
|
|
|
74 |
|
|
|
75 |
glGetShaderInfoLog( shader, 256, NULL, infobuf );
|
|
|
76 |
|
|
|
77 |
glDeleteShader(shader);
|
|
|
78 |
shader = 0;
|
|
|
79 |
}
|
|
|
80 |
free(shaderdata);
|
|
|
81 |
return shader;
|
|
|
82 |
}
|