#include
//Link the library glew32s.lib also. Put this linker at the top of linker list.
//#define GLEW_STATIC before the include of GLEW.#define GLEW_STATIC
#define GLEW_STATIC
#include // the opengl library wrapped by extended features
//#include // the opengl library wrapped by extended features
//#include // the opengl library wrapped by extended features
#ifdef __APPLE__
# include
#else
//# include
# include
//#include // library cross-platform toolkit windows and managing input operations
#endif
#include
#include
#include
#include "util.h"
/*
* Global data used by our render callback:
*/
//#define GLEW_STATIC
static struct {
// buffer objects
GLuint vertex_buffer, element_buffer , normal_buffer , texcoord_buffer ;
//texture objects
GLuint textures[2];
//shader objects
GLuint vertex_shader, fragment_shader, program;
// struct flag_mesh flag, background;
// struct flag_vertex *flag_vertex_array;
// uniforms
struct {
GLint fade_factor;
GLint p_matrix, mv_matrix , uSamplers[2] ; //textures[2];
} uniforms;
//attributes
struct {
GLint position , normal, texcoord, shininess, specular;
} attributes;
GLfloat fade_factor;
GLfloat p_matrix[16], mv_matrix[16];
GLfloat eye_offset[2];
GLsizei window_size[2];
} g_resources;
// Functions for creating OpenGL objects:
static GLuint make_buffer(
GLenum target,
const void *buffer_data,
GLsizei buffer_size
) {
GLuint buffer;
glGenBuffers(1 , &buffer) ;
glBindBuffer(target, buffer);
glBufferData(target, buffer_size, buffer_data, GL_STATIC_DRAW);
return buffer;
}
static GLuint make_texture(const char *filename)
{
int width, height;
void *pixels = read_tga(filename, &width, &height);
GLuint texture;
if (!pixels)
return 0;
////////////////////
///////////////////
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(
GL_TEXTURE_2D, 0, // target, level
GL_RGB8, // internal format
width, height, 0, // width, height, border
GL_BGR, GL_UNSIGNED_BYTE, // external format, type
pixels // pixels
);
free(pixels);
return texture;
}
static void show_info_log(
GLuint object,
PFNGLGETSHADERIVPROC glGet__iv,
PFNGLGETSHADERINFOLOGPROC glGet__InfoLog
)
{
GLint log_length;
char *log;
glGet__iv(object, GL_INFO_LOG_LENGTH, &log_length);
log = malloc(log_length);
glGet__InfoLog(object, log_length, NULL, log);
fprintf(stderr, "%s", log);
free(log);
}
static GLuint make_shader(GLenum type, const char *filename)
{
GLint length;
GLchar *source = file_contents(filename, &length);
GLuint shader;
GLint shader_ok;
if (!source)
return 0;
shader = glCreateShader(type);
printf("\nshader pass\n");
glShaderSource(shader, 1, (const GLchar**)&source, &length);
free(source);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok);
printf( "shader_ok %d:\n", shader_ok);
if (!shader_ok) {
printf( "Failed to compile:\n");
fprintf(stderr, "Failed to compile %s:\n", filename);
show_info_log(shader, glGetShaderiv, glGetShaderInfoLog);
glDeleteShader(shader);
return 0;
}
return shader;
}
static GLuint make_program(GLuint vertex_shader, GLuint fragment_shader)
{
GLint program_ok;
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &program_ok);
if (!program_ok) {
fprintf(stderr, "Failed to link shader program:\n");
show_info_log(program, glGetProgramiv, glGetProgramInfoLog);
glDeleteProgram(program);
return 0;
}
return program;
}
/*
* Data used to seed our vertex array and element array buffers:
*/
//static const GLfloat g_vertex_buffer_data[] = {
static const GLfloat g_vertex_buffer_position[] = {
/*
-0.5f, -0.5f, //-1.0f, -1.0f,
0.5f, -0.5f, //1.0f, -1.0f,
-0.5f, 0.5f, //-1.0f, 1.0f,
0.5f, 0.5f //1.0f, 1.0f
*/
// Front face
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
// Back face
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
// Top face
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
// Bottom face
1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
// Right face
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f, //1BOT
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
// Left face
-1.0f, -1.0f, -1.0f, //3BOT
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f
/*
// Front face
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// Back face
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
// Top face
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
// Bottom face
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
// Right face
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
// Left face
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0
*/
};
///////////////////normal,
static const GLushort g_vertex_buffer_normal[] = { //0, 1, 2, 3
// Front
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
0.0, 0.0, 1.0,
// Back
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
0.0, 0.0, -1.0,
// Top
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 1.0, 0.0,
// Bottom
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
0.0, -1.0, 0.0,
// Right
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
// Left
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0,
-1.0, 0.0, 0.0
};
static const GLushort g_vertex_buffer_texcoord[] = { //0, 1, 2, 3
// Front
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// Back
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// Top
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// Bottom
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// Right
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// Left
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0
};
///////////////////////
static const GLushort g_element_buffer_data[] = { //0, 1, 2, 3
0, 1, 2, 0, 2, 3 , // front
4, 5, 6, 4, 6, 7 , // back
8, 9, 10, 8, 10, 11, // top
12, 13, 14, 12, 14, 15, // bottom
16, 17, 18, 16, 18, 19, // right
20, 21, 22, 20, 22, 23 // left
};
/*
static void update_flag_program(void)
{
printf("reloading program\n");
GLuint vertex_shader, fragment_shader, program;
if (make_flag_program(&vertex_shader, &fragment_shader, &program)) {
delete_flag_program();
enact_flag_program(vertex_shader, fragment_shader, program);
}
}
*/
/*
mat4 my_frustum(
float angle_of_view,
float aspect_ratio,
float z_near,
float z_far
) {
return mat4(
vec4(1.0/tan(angle_of_view), 0.0, 0.0, 0.0),
vec4(0.0, aspect_ratio/tan(angle_of_view), 0.0, 0.0),
vec4(0.0, 0.0, (z_far+z_near)/(z_far-z_near), 1.0),
vec4(0.0, 0.0, -2.0*z_far*z_near/(z_far-z_near), 0.0)
);
}
//const mat4 projection = mat4(
// vec4(3.0/4.0, 0.0, 0.0, 0.0),
// vec4( 0.0, 1.0, 0.0, 0.0),
// vec4( 0.0, 0.0, 0.5, 0.5),
// vec4( 0.0, 0.0, 0.0, 1.0)
// );
mat4 identity = mat4(
vec4(1.0, 0.0, 0.0, 0.0),
vec4(0.0, 1.0, 0.0, 0.0),
vec4(0.0, 0.0, 1.0, 0.0),
vec4(0.0, 0.0, 0.0, 1.0)
);
mat4 scale(float x, float y, float z)
{
return mat4(
vec4(x, 0.0, 0.0, 0.0),
vec4(0.0, y, 0.0, 0.0),
vec4(0.0, 0.0, z, 0.0),
vec4(0.0, 0.0, 0.0, 1.0)
);
}
mat4 translate(float x, float y, float z)
{
return mat4(
vec4(1.0, 0.0, 0.0, 0.0),
vec4(0.0, 1.0, 0.0, 0.0),
vec4(0.0, 0.0, 1.0, 0.0),
vec4(x, y, z, 1.0)
);
}
*/
#define PROJECTION_FOV_RATIO 0.9f //0.7f
#define PROJECTION_NEAR_PLANE 0.0625f
#define PROJECTION_FAR_PLANE 256.0f
static void update_p_matrix(GLfloat *matrix, int w, int h)
{
GLfloat wf = (GLfloat)w, hf = (GLfloat)h;
// GLfloat wf = (GLfloat)400, hf = (GLfloat)400;
GLfloat
r_xy_factor = fminf(wf, hf) * 1.0f/PROJECTION_FOV_RATIO,
r_x = r_xy_factor/wf,
r_y = r_xy_factor/hf,
r_zw_factor = 1.0f/(PROJECTION_FAR_PLANE - PROJECTION_NEAR_PLANE),
r_z = (PROJECTION_NEAR_PLANE + PROJECTION_FAR_PLANE)*r_zw_factor,
r_w = -2.0f*PROJECTION_NEAR_PLANE*PROJECTION_FAR_PLANE*r_zw_factor;
matrix[ 0] = r_x; matrix[ 1] = 0.0f; matrix[ 2] = 0.0f; matrix[ 3] = 0.0f;
matrix[ 4] = 0.0f; matrix[ 5] = r_y; matrix[ 6] = 0.0f; matrix[ 7] = 0.0f;
matrix[ 8] = 0.0f; matrix[ 9] = 0.0f; matrix[10] = r_z; matrix[11] = 1.0f;
matrix[12] = 0.0f; matrix[13] = 0.0f; matrix[14] = r_w; matrix[15] = 0.0f;
}
/*
vec4(1.0/tan(angle_of_view), 0.0, 0.0, 0.0),
vec4(0.0, aspect_ratio/tan(angle_of_view), 0.0, 0.0),
vec4(0.0, 0.0, (z_far+z_near)/(z_far-z_near), 1.0),
vec4(0.0, 0.0, -2.0*z_far*z_near/(z_far-z_near), 0.0)
*/
GLfloat ROTATION_ANGLE = 0.0f ;
static void update_mv_matrix(GLfloat *matrix, GLfloat *eye_offset)
{
static const GLfloat BASE_EYE_POSITION[3] = { 0.0f, 0.0f, -300.0f }; //{ 0.0f, -0.0f, -1.0f }; ////{ 0.5f, -0.25f, -1.25f };
/*
matrix[ 0] = 1.0f; matrix[ 1] = 0.0f; matrix[ 2] = 0.0f; matrix[ 3] = 0.0f;
matrix[ 4] = 0.0f; matrix[ 5] = 1.0f; matrix[ 6] = 0.0f; matrix[ 7] = 0.0f;
matrix[ 8] = 0.0f; matrix[ 9] = 0.0f; matrix[10] = 1.0f; matrix[11] = 0.0f;
matrix[12] = -BASE_EYE_POSITION[0] - eye_offset[0];
matrix[13] = -BASE_EYE_POSITION[1] - eye_offset[1];
matrix[14] = -BASE_EYE_POSITION[2];
matrix[15] = 1.0f;
*/
ROTATION_ANGLE = ROTATION_ANGLE + 0.001 * g_resources.fade_factor ; // 1.0 ; // *g_resources.fade_factor ;
//printf("ROTATION_ANGLE = %f" , ROTATION_ANGLE);
//z axis
matrix[ 0] = cos(ROTATION_ANGLE ) ; matrix[ 1] = - sin(ROTATION_ANGLE); matrix[ 2] = 0.0f; matrix[ 3] = 0.0f;
matrix[ 4] = sin(ROTATION_ANGLE ) ; matrix[ 5] = cos(ROTATION_ANGLE); matrix[ 6] = 0.0f; matrix[ 7] = 0.0f;
matrix[ 8] = 0.0f; matrix[ 9] = 0.0f; matrix[10] = 1.0f; matrix[11] = 0.0f;
matrix[12] = -BASE_EYE_POSITION[0] - eye_offset[0];
matrix[13] = -BASE_EYE_POSITION[1] - eye_offset[1];
matrix[14] = -BASE_EYE_POSITION[2];
matrix[15] = 1.0f;
/*
// y axis
matrix[ 0] = cos(ROTATION_ANGLE ) ; matrix[1] = 0.0f; matrix[ 2] = sin(ROTATION_ANGLE); matrix[ 3] = 0.0f;
matrix[ 4] = 0.0f; matrix[ 5] = 1.0f; matrix[6] = 0.0f; matrix[7] = 0.0f;
matrix[ 8] = - sin(ROTATION_ANGLE ) ; matrix[ 9] = 0.0f ; matrix[ 10] = cos(ROTATION_ANGLE); matrix[11] = 0.0f;
matrix[12] = -BASE_EYE_POSITION[0] - eye_offset[0];
matrix[13] = -BASE_EYE_POSITION[1] - eye_offset[1];
matrix[14] = -BASE_EYE_POSITION[2];
matrix[15] = 1.0f;
// x axis
matrix[0] = 1.0f; matrix[1] = 1.0f; matrix[2] = 0.0f; matrix[3] = 0.0f;
matrix[4] = 0.0f; matrix[5] = cos(ROTATION_ANGLE ) ; matrix[6] = -sin(ROTATION_ANGLE); matrix[7] = 0.0f;
matrix[8] = 0.0f; matrix[9] = sin(ROTATION_ANGLE ) ; matrix[ 10] = cos(ROTATION_ANGLE); matrix[11] = 0.0f;
matrix[12] = -BASE_EYE_POSITION[0] - eye_offset[0];
matrix[13] = -BASE_EYE_POSITION[1] - eye_offset[1];
matrix[14] = -BASE_EYE_POSITION[2];
matrix[15] = 1.0f;
*/
}
/*
* Load and create all of our resources:
*/
#define INITIAL_WINDOW_WIDTH 640
#define INITIAL_WINDOW_HEIGHT 480
static int make_resources(void){
//add
g_resources.eye_offset[0] = 0.0f ; // 400.0f ; -50.0f;
g_resources.eye_offset[1] = 0.0f ; // 400.0f ; -50.0f;
g_resources.window_size[0] = INITIAL_WINDOW_WIDTH;
g_resources.window_size[1] = INITIAL_WINDOW_HEIGHT;
update_p_matrix( g_resources.p_matrix,
INITIAL_WINDOW_WIDTH,
INITIAL_WINDOW_HEIGHT
);
update_mv_matrix( g_resources.mv_matrix,
g_resources.eye_offset);
// buffers
g_resources.vertex_buffer = make_buffer(
GL_ARRAY_BUFFER,
g_vertex_buffer_position , //g_vertex_buffer_data,
sizeof(g_vertex_buffer_position ) //g_vertex_buffer_data)
);
g_resources.normal_buffer = make_buffer(
GL_ARRAY_BUFFER,
g_vertex_buffer_normal,
sizeof(g_vertex_buffer_normal)
);
g_resources.texcoord_buffer = make_buffer(
GL_ARRAY_BUFFER,
g_vertex_buffer_texcoord,
sizeof(g_vertex_buffer_texcoord)
);
g_resources.element_buffer = make_buffer(
GL_ELEMENT_ARRAY_BUFFER,
g_element_buffer_data,
sizeof(g_element_buffer_data)
);
g_resources.textures[0] = make_texture("hello1.tga");
printf("Ok1 texture1 loaded\n");
g_resources.textures[1] = make_texture("hello2.tga");
printf("Ok2 texture2 loaded\n");
if (g_resources.textures[0] == 0 || g_resources.textures[1] == 0)
return 0;
printf("Ok1");
g_resources.vertex_shader = make_shader(
GL_VERTEX_SHADER ,
"lightTest.v.glsl" );
if (g_resources.vertex_shader == 0)
return 0;
printf("Ok2");
g_resources.fragment_shader = make_shader(
GL_FRAGMENT_SHADER ,
"lightTest.f.glsl" );
if (g_resources.fragment_shader == 0)
return 0;
printf("Ok3");
g_resources.program = make_program(g_resources.vertex_shader, g_resources.fragment_shader);
if (g_resources.program == 0)
return 0;
// uniforms
g_resources.uniforms.p_matrix
= glGetUniformLocation(g_resources.program, "p_matrix");
g_resources.uniforms.mv_matrix
= glGetUniformLocation(g_resources.program, "mv_matrix");
// g_resources.uniforms.textures[0]
g_resources.uniforms.uSamplers[0]
= glGetUniformLocation(g_resources.program, "textures[0]");
// g_resources.uniforms.textures[1]
g_resources.uniforms.uSamplers[1]
= glGetUniformLocation(g_resources.program, "textures[1]");
g_resources.uniforms.fade_factor
= glGetUniformLocation(g_resources.program, "fade_factor");
///////////////
// attributes
g_resources.attributes.position
= glGetAttribLocation(g_resources.program, "position");
/*
*/
//////////////////
g_resources.attributes.normal
= glGetAttribLocation(g_resources.program, "normal");
g_resources.attributes.texcoord
= glGetAttribLocation(g_resources.program, "texcoord");
g_resources.attributes.shininess
= glGetAttribLocation(g_resources.program, "shininess");
g_resources.attributes.specular
= glGetAttribLocation(g_resources.program, "specular");
//////////////////////////
return 1;
}
/*
* GLUT callbacks:
*/
static void update_fade_factor(void)
{
int milliseconds = glutGet(GLUT_ELAPSED_TIME);
// g_resources.fade_factor = g_resources.fade_factor + 0.005 ; //sinf((float)milliseconds * 0.001f) * 0.5f + 0.5f;
g_resources.fade_factor = sinf((float)milliseconds * 0.001f) * 0.5f + 0.5f;
//printf("fade_factor = %f" , g_resources.fade_factor);
///////// added by djafer //////////
g_resources.eye_offset[0] = 0.0f ; // 400.0f ; -50.0f;
g_resources.eye_offset[1] = 0.0f ; // 400.0f ; -50.0f;
g_resources.window_size[0] = INITIAL_WINDOW_WIDTH;
g_resources.window_size[1] = INITIAL_WINDOW_HEIGHT;
update_p_matrix( g_resources.p_matrix,
INITIAL_WINDOW_WIDTH,
INITIAL_WINDOW_HEIGHT
);
update_mv_matrix( g_resources.mv_matrix,
g_resources.eye_offset);
///////// end /////////////////
glutPostRedisplay();
}
/*
static void delete_flag_program(void)
{
glDetachShader(
g_resources.flag_program.program,
g_resources.flag_program.vertex_shader
);
glDetachShader(
g_resources.flag_program.program,
g_resources.flag_program.fragment_shader
);
glDeleteProgram(g_resources.flag_program.program);
glDeleteShader(g_resources.flag_program.vertex_shader);
glDeleteShader(g_resources.flag_program.fragment_shader);
}
*/
static void render(void){
glClearColor( 0.0, 0.0, 0.0, 1.0); // Clear to black, fully opaque
glClearDepth(1.0); // Clear everything
glEnable(GL_DEPTH_TEST); // Enable depth testing
// glDisable(GL_DEPTH_TEST); // Enable depth testing
glDepthFunc(GL_LESS) ; //GL_LEQUAL); // Near things obscure far things
// Clear the canvas before we start drawing on it.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(g_resources.program);
//uniforms
glUniform1f(g_resources.uniforms.fade_factor, g_resources.fade_factor);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, g_resources.textures[0]);
glUniform1i(g_resources.uniforms.uSamplers[0], 0);
// glUniform1i(g_resources.uniforms.textures[0], 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, g_resources.textures[1]);
glUniform1i(g_resources.uniforms.uSamplers[1], 1);
// glUniform1i(g_resources.uniforms.textures[1], 1);
//////////
glUniformMatrix4fv(
g_resources.uniforms.p_matrix,
1, GL_FALSE,
g_resources.p_matrix
);
glUniformMatrix4fv(
g_resources.uniforms.mv_matrix,
1, GL_FALSE,
g_resources.mv_matrix
);
///////////
glBindBuffer(GL_ARRAY_BUFFER, g_resources.vertex_buffer);
glVertexAttribPointer(
g_resources.attributes.position, // attribute
3 , //2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
sizeof(GLfloat)*0,//2, // stride
(void*)0 // array buffer offset
);
glEnableVertexAttribArray(g_resources.attributes.position);
//djafer
// glBindBuffer(GL_ARRAY_BUFFER, g_resources.normal_buffer);
glVertexAttribPointer(
g_resources.attributes.texcoord,
2, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*0,//sizeof(struct flag_vertex),
(void*)0 //offsetof(struct flag_vertex, texcoord)
);
glEnableVertexAttribArray(g_resources.attributes.texcoord);
//djafer
// glBindBuffer(GL_ARRAY_BUFFER, g_resources.texcoord_buffer);
glVertexAttribPointer(
g_resources.attributes.normal,
3, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*0,//sizeof(struct flag_vertex),
(void*)0 // offsetof(struct flag_vertex, normal)
);
glEnableVertexAttribArray(g_resources.attributes.normal);
glVertexAttribPointer(
g_resources.attributes.shininess,
1, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*0,//struct flag_vertex),
(void*)0 //offsetof(struct flag_vertex, shininess)
);
glEnableVertexAttribArray(g_resources.attributes.shininess);
glVertexAttribPointer(
g_resources.attributes.specular,
4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(GLfloat)*0,//struct flag_vertex),
(void*)0 //offsetof(struct flag_vertex, specular)
);
glEnableVertexAttribArray(g_resources.attributes.specular);
/////////////////
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_resources.element_buffer);
glDrawElements(
// GL_TRIANGLE_STRIP , // mode
GL_TRIANGLES ,
36, // count
GL_UNSIGNED_SHORT, // type
(void*)0 // element array buffer offset
);
//////////////////
glDisableVertexAttribArray(g_resources.attributes.position);
glDisableVertexAttribArray(g_resources.attributes.normal);
glDisableVertexAttribArray(g_resources.attributes.texcoord);
glDisableVertexAttribArray(g_resources.attributes.shininess);
glDisableVertexAttribArray(g_resources.attributes.specular);
glutSwapBuffers();
}
void supportingOpenGLversion( )
{
char *versionGL = "\0";
char *versionGLEWmicro = "\0";
char *versionGLEWmajor = "\0";
// GLint versionFreeGlutInt = 0;
versionGL = (char *)(glGetString(GL_VERSION));
printf("OpenGL version: %s\n", versionGL);
versionGLEWmicro = (char *)(glGetString(GLEW_VERSION_MICRO));
printf("GLEW version Micro: %s \n" , versionGLEWmicro ) ;
versionGLEWmajor = (char *)(glGetString(GLEW_VERSION_MAJOR));
printf("GLEW version Major: %s \n" , versionGLEWmajor ) ;
/*
// string versionFreeGlutString = makeMeString(versionFreeGlutInt);
// formatMe(&versionFreeGlutString);
cout << endl;
cout << "OpenGL version: " << versionGL << endl << endl;
// cout << "FreeGLUT version: " << versionFreeGlutString << endl << endl;
cout << "GLEW version: " <<
GLEW_VERSION << "." << GLEW_VERSION_MAJOR << "." <<
GLEW_VERSION_MINOR << "." << GLEW_VERSION_MICRO << endl;
// if (!GLEW_VERSION_2_0) {
if (!GLEW_VERSION_1_5) {
// if (!GL_VERSION_2_0) {
// if (!GL_VERSION_3_3) {
// if (!GL_VERSION_4_0) {
// fprintf(stderr, "OpenGL 2.0 not available\n");
printf("OpenGL 2.0 not available\n");
return 1;
}
else
// printf("OpenGL 2.0 available\n");
printf("OpenGL 4.0 available\n");
*/
if (glewIsSupported("GL_VERSION_2_0"))
//if (glewIsSupported("GL_VERSION_4_0"))
printf("Ready for OpenGL 2.0\n");
else {
printf("OpenGL 2.0 not supported\n");
}
}
// Entry point
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(600, 600);
glutCreateWindow("Teste Eclairage ");
glutIdleFunc(&update_fade_factor);
glutDisplayFunc(&render);
// g_resources.fade_factor = 0.0 ;
glewInit();
supportingOpenGLversion( );
printf("Ok!");
if (!make_resources()) {
// fprintf(stderr, "Failed to load resources\n");
printf("Failed to load resources\n");
return 1;
}
glutMainLoop();
return 0;
}
/////////// shaders
#version 110
//uniforms
uniform mat4 p_matrix, mv_matrix;
uniform sampler2D uSamplers[2] ; // textures[2];
//uniform sampler2D texture;
uniform float fade_factor;
//attributes
//attribute vec2 position;
attribute vec3 position, normal;
attribute vec2 texcoord;
attribute float shininess;
attribute vec4 specular;
varying vec3 frag_position, frag_normal;
varying vec2 frag_texcoord;
varying float frag_shininess;
varying vec4 frag_specular;
//attribute vec4 aVertexColor ;
//varying vec4 vColor;
//varying vec2 texcoord;
/*
//uniforms
uniform mat4 uNormalMatrix;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
//attribute
attribute vec4 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aTextureCoord;
//varying
varying highp vec2 vTextureCoord;
varying highp vec3 vLighting;
void main(void) {
gl_Position = p_matrix * mv_matrix * aVertexPosition;
frag_texcoord = texcoord;
// Apply lighting effect
highp vec3 ambientLight = vec3(0.3, 0.3, 0.3);
highp vec3 directionalLightColor = vec3(1, 1, 1);
highp vec3 directionalVector = normalize(vec3(0.85, 0.8, 0.75));
highp vec4 transformedNormal = uNormalMatrix * vec4(aVertexNormal, 1.0);
highp float directional = max(dot(transformedNormal.xyz, directionalVector), 0.0);
vLighting = ambientLight + (directionalLightColor * directional);
}
*/
void main(){
// gl_Position = fade_factor + p_matrix * eye_position;
// gl_Position = vec4(position, 0.0, 1.0);
vec4 eye_position = mv_matrix * vec4(100.0*position, 1.0);
// vec4 eye_position = mv_matrix * vec4(100.0*fade_factor*position, 1.0);
gl_Position = p_matrix * eye_position;
// gl_Position = p_matrix * mv_matrix * vec4(position, 1.0);
frag_position = 0.74 +
// fade_factor +
eye_position.xyz;
frag_normal = 0.74 +
// fade_factor +
(mv_matrix * vec4(normal, 0.0)).xyz;
frag_texcoord = 0.74 +
// fade_factor +
texcoord ;
frag_shininess = fade_factor -
shininess;
frag_specular = fade_factor -
specular;
// texcoord = position * vec2(0.5) + vec2(0.5);
// vColor = vec4(1.0 , 1.0 , 1.0 ,1.0); //
// aVertexColor = vec4(1.0 , 1.0 , 1.0 ,1.0);
}
//////////// shaders
#version 110
uniform mat4 p_matrix, mv_matrix;
uniform sampler2D uSamplers[2] ; // textures[2];
//uniform sampler2D texture;
uniform float fade_factor;
varying vec3 frag_position, frag_normal;
varying vec2 frag_texcoord;
varying float frag_shininess;
varying vec4 frag_specular;
//varying vec2 texcoord;
//precision mediump float ;
//varying vec4 vColor;
const vec3 light_direction = vec3(0.9, -0.9, 0.9); //vec3(0.408248, -0.816497, 0.408248);
const vec4 light_diffuse = vec4(0.8, 0.8, 0.8, 0.0);
const vec4 light_ambient = vec4(0.2, 0.2, 0.2, 1.0);
const vec4 light_specular = vec4(1.0, 1.0, 1.0, 1.0); //vec4(1.0, 1.0, 1.0, 1.0);
/*
varying highp vec2 vTextureCoord;
varying highp vec3 vLighting;
void main() {
// gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
highp vec4 texelColor = texture2D(uSampler, vTextureCoord);
gl_FragColor = vec4(texelColor.rgb * vLighting, texelColor.a);
}`;
*/
void main(){
vec3 mv_light_direction = (mv_matrix * vec4(2.0*light_direction, 0.0)).xyz,
normal = normalize(frag_normal),
eye = normalize(frag_position),
reflection = reflect(mv_light_direction, normal)
// +
// fade_factor
;
///////////
// gl_FragColor =
vec4 frag_diffuse =
mix(
// texture2D(textures[0], frag_texcoord),
// texture2D(textures[1], frag_texcoord),
texture2D(uSamplers[0], frag_texcoord),
texture2D(uSamplers[1], frag_texcoord),
fade_factor
);
// fade_factor = fade_factor + 100.0 ;
// vec4 frag_diffuse = texture2D(texture, frag_texcoord);
///////////
vec4 diffuse_factor
= max(-dot(normal, mv_light_direction), 0.0) * light_diffuse;
vec4 ambient_diffuse_factor
= diffuse_factor + light_ambient;
vec4 specular_factor =
max(
pow(
-dot(
-mv_light_direction ,
//reflection,
eye
),
frag_shininess
),
0.0
)
*
light_specular;
// gl_FragColor = specular_factor * frag_specular ;
// gl_FragColor = ambient_diffuse_factor * frag_diffuse;
gl_FragColor = specular_factor * frag_specular
+
ambient_diffuse_factor * frag_diffuse;
}