Пример #1
0
void gl_setup_program(gl_args *arg, bool gen_buffers, LPCWSTR shader_file, bool transpose_shader)
{
    
    GLuint program = glCreateProgram();
    GLuint shader = glCreateShader(GL_COMPUTE_SHADER);
    GLint length;
    arg->shader_src = gl_prepare_source(arg, shader_file, &length, transpose_shader);
    glShaderSource(shader, 1, &arg->shader_src, &length);
    glCompileShader(shader);
    int rvalue;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &rvalue);
    if (!rvalue) {
        gl_log(shader, "Compiler", "Error in compiling the compute shader\n");
    }
    glAttachShader(program, shader);
    glLinkProgram(program);
    glGetProgramiv(program, GL_LINK_STATUS, &rvalue);
    if (!rvalue) {
        gl_log(shader, "Linker", "Error in linking compute shader program\n");
    }
    if (gen_buffers) {
        GLuint buffers[2];
        glGenBuffers(2, buffers);
        arg->buf_in = buffers[0];
        arg->buf_out = buffers[1];
    }

    glValidateProgram(program);

    arg->program = program;
    arg->shader = shader;
}
Пример #2
0
/*--------------------------------GLFW3 and GLEW------------------------------*/
bool start_gl () {
	gl_log ("starting GLFW %s\n", glfwGetVersionString ());
	
	glfwSetErrorCallback (glfw_error_callback);
	if (!glfwInit ()) {
		fprintf (stderr, "ERROR: could not start GLFW3\n");
		return false;
	}

	// uncomment these lines if on Apple OS X
	glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
	glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

//	glfwWindowHint (GLFW_SAMPLES, 32);
	
	/*GLFWmonitor* mon = glfwGetPrimaryMonitor ();
	const GLFWvidmode* vmode = glfwGetVideoMode (mon);
	g_window = glfwCreateWindow (
		vmode->width, vmode->height, "Extended GL Init", mon, NULL
	);*/

	g_window = glfwCreateWindow (
		g_gl_width, g_gl_height, "Extended Init.", NULL, NULL
	);
	if (!g_window) {
		fprintf (stderr, "ERROR: could not open window with GLFW3\n");
		glfwTerminate();
		return false;
	}
	glfwSetWindowSizeCallback (g_window, glfw_window_size_callback);
	glfwMakeContextCurrent (g_window);
	
	// start GLEW extension handler
	glewExperimental = GL_TRUE;
	glewInit ();
	
	if (GLEW_KHR_debug) {
		int param = -1;
		printf ("KHR_debug extension found\n");
		glDebugMessageCallback ((GLDEBUGPROC)debug_gl_callback, &param);
		glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS);
		printf ("debug callback engaged\n");
	} else {
		printf ("KHR_debug extension NOT found\n");
	}

	// get version info
	const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
	const GLubyte* version = glGetString (GL_VERSION); // version as a string
	printf ("Renderer: %s\n", renderer);
	printf ("OpenGL version supported %s\n", version);
	gl_log ("renderer: %s\nversion: %s\n", renderer, version);
	//vysnch
//	glfwSwapInterval (1);
	return true;
}
Пример #3
0
//writes the graphics card parameters in the log file
void Logger::log_gl_params()
{
 GLenum params[] = {
     GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS,
     GL_MAX_CUBE_MAP_TEXTURE_SIZE,
     GL_MAX_DRAW_BUFFERS,
     GL_MAX_FRAGMENT_UNIFORM_COMPONENTS,
     GL_MAX_TEXTURE_IMAGE_UNITS,
     GL_MAX_TEXTURE_SIZE,
     GL_MAX_VARYING_FLOATS,
     GL_MAX_VERTEX_ATTRIBS,
     GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS,
     GL_MAX_VERTEX_UNIFORM_COMPONENTS,
     GL_MAX_VIEWPORT_DIMS,
     GL_STEREO
    };

 const char* names[] = {
     "GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS",
     "GL_MAX_CUBE_MAP_TEXTURE_SIZE",
     "GL_MAX_DRAW_BUFFERS",
     "GL_MAX_FRAGMENT_UNIFORM_COMPONENTS",
     "GL_MAX_TEXTURE_IMAGE_UNITS",
     "GL_MAX_TEXTURE_SIZE",
     "GL_MAX_VARYING_FLOATS",
     "GL_MAX_VERTEX_ATTRIBS",
     "GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS",
     "GL_MAX_VERTEX_UNIFORM_COMPONENTS",
     "GL_MAX_VIEWPORT_DIMS",
     "GL_STEREO"
    };

    gl_log("GL Context Parameters:\n");

    //integers - only works if the orderis 0-10 integer return types
    for(int i=0; i<10; i++)
    {
     int v = 0;
     glGetIntegerv(params[i], &v);
     gl_log("%s %i\n", names[i], v);
    }

    //others
    int v[2];
    v[0]=v[1]=0;
    glGetIntegerv(params[10], v);
    gl_log("%s %i %i\n", names[10], v[0], v[1]);

    unsigned char s = 0;
    glGetBooleanv(params[11], &s);
    gl_log("%s %u\n", names[11], (unsigned int)s);
    gl_log("------------------------------------------\n");
}
/*--------------------------------GLFW3 and GLEW-----------------------------*/
bool start_gl() {
	const GLubyte *renderer;
	const GLubyte *version;

	gl_log( "starting GLFW %s", glfwGetVersionString() );

	glfwSetErrorCallback( glfw_error_callback );
	if ( !glfwInit() ) {
		fprintf( stderr, "ERROR: could not start GLFW3\n" );
		return false;
	}

/* We must specify 3.2 core if on Apple OS X -- other O/S can specify
 anything here. I defined 'APPLE' in the makefile for OS X */
#ifdef APPLE
	glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
	glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 2 );
	glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
	glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
#endif
	/*GLFWmonitor* mon = glfwGetPrimaryMonitor ();
	const GLFWvidmode* vmode = glfwGetVideoMode (mon);
	g_window = glfwCreateWindow (
		vmode->width, vmode->height, "Extended GL Init", mon, NULL
	);*/

	g_window = glfwCreateWindow( g_gl_width, g_gl_height, "Shaders", NULL, NULL );
	if ( !g_window ) {
		fprintf( stderr, "ERROR: could not open window with GLFW3\n" );
		glfwTerminate();
		return false;
	}
	glfwSetFramebufferSizeCallback( g_window, glfw_framebuffer_size_callback );
	glfwMakeContextCurrent( g_window );

	glfwWindowHint( GLFW_SAMPLES, 4 );

	/* start GLEW extension handler */
	glewExperimental = GL_TRUE;
	glewInit();

	/* get version info */
	renderer = glGetString( GL_RENDERER ); /* get renderer string */
	version = glGetString( GL_VERSION );	 /* version as a string */
	printf( "Renderer: %s\n", renderer );
	printf( "OpenGL version supported %s\n", version );
	gl_log( "renderer: %s\nversion: %s\n", renderer, version );

	previous_seconds = glfwGetTime();

	return true;
}
Пример #5
0
Model* create_model(char* filename, GLint shader_program) {
    struct aiScene* scene = aiImportFile(filename, aiProcess_Triangulate);
    if (!scene) {
        gl_log(ERROR, "Cannot open scene file: %s", filename);
        return 0;
    }

    gl_log(INFO, "Loading model: %s", filename);
    gl_log(INFO, "%d animations", scene->mNumAnimations);
    gl_log(INFO, "%d cameras", scene->mNumCameras);
    gl_log(INFO, "%d lights", scene->mNumLights);
    gl_log(INFO, "%d materials", scene->mNumMaterials);
    gl_log(INFO, "%d meshes", scene->mNumMeshes);
    gl_log(INFO, "%d textures", scene->mNumTextures);

    GLint light_position = glGetUniformLocation(shader_program, "light.position");
    GLint light_ambient = glGetUniformLocation(shader_program, "light.ambient_color");
    GLint light_diffuse = glGetUniformLocation(shader_program, "light.diffuse_color");
    GLint light_specular = glGetUniformLocation(shader_program, "light.specular_color");

    glUniform3f(light_position, 0.0, 100.0, 10.0);
    glUniform3f(light_ambient, 1.0f, 1.0f, 1.0f);
    glUniform3f(light_diffuse, 1.0f, 1.0f, 1.0f);
    glUniform3f(light_specular, 1.0f, 1.0f, 1.0f);

    Model* model = malloc(sizeof(Model));
    model->num_meshes = scene->mNumMeshes;
    model->meshes = malloc(sizeof(Mesh*) * scene->mNumMeshes);

    process_node(model, scene->mRootNode, scene);

    model->shader_program = shader_program;

    return model;
}
/*--------------------------------GLFW3 and GLEW------------------------------*/
bool start_gl () {
	gl_log ("starting GLFW %s\n", glfwGetVersionString ());
	
	glfwSetErrorCallback (glfw_error_callback);
	if (!glfwInit ()) {
		fprintf (stderr, "ERROR: could not start GLFW3\n");
		return false;
	}

	// uncomment these lines if on Apple OS X
	/*glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2);
	glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
	glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
*/
	
	/*GLFWmonitor* mon = glfwGetPrimaryMonitor ();
	const GLFWvidmode* vmode = glfwGetVideoMode (mon);
	g_window = glfwCreateWindow (
		vmode->width, vmode->height, "Extended GL Init", mon, NULL
	);*/

	g_window = glfwCreateWindow (
		g_gl_width, g_gl_height, "Extended Init.", NULL, NULL
	);
	if (!g_window) {
		fprintf (stderr, "ERROR: could not open window with GLFW3\n");
		glfwTerminate();
		return false;
	}
	glfwSetWindowSizeCallback (g_window, glfw_window_size_callback);
	glfwMakeContextCurrent (g_window);
	
	glfwWindowHint (GLFW_SAMPLES, 4);
	
	// start GLEW extension handler
	glewExperimental = GL_TRUE;
	glewInit ();

	// get version info
	const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
	const GLubyte* version = glGetString (GL_VERSION); // version as a string
	printf ("Renderer: %s\n", renderer);
	printf ("OpenGL version supported %s\n", version);
	gl_log ("renderer: %s\nversion: %s\n", renderer, version);
	
	return true;
}
bool create_programme (GLuint vert, GLuint frag, GLuint* programme) {
	*programme = glCreateProgram ();
	gl_log (
		"created programme %u. attaching shaders %u and %u...\n",
		*programme,
		vert,
		frag
	);
	glAttachShader (*programme, vert);
	glAttachShader (*programme, frag);
	// link the shader programme. if binding input attributes do that before link
	glLinkProgram (*programme);
	GLint params = -1;
	glGetProgramiv (*programme, GL_LINK_STATUS, &params);
	if (GL_TRUE != params) {
		gl_log_err (
			"ERROR: could not link shader programme GL index %u\n",
			*programme
		);
		print_programme_info_log (*programme);
		return false;
	}
	assert (is_programme_valid (*programme));
	// delete shaders here to free memory
	glDeleteShader (vert);
	glDeleteShader (frag);
	return true;
}
Пример #8
0
/*--------------------------------GLFW3 and GLEW------------------------------*/
bool start_gl () {
	gl_log ("starting GLFW %s\n", glfwGetVersionString ());
	
	glfwSetErrorCallback (glfw_error_callback);
	if (!glfwInit ()) {
		fprintf (stderr, "ERROR: could not start GLFW3\n");
		return false;
	}
	
	/* We must specify 3.2 core if on Apple OS X -- other O/S can specify
	 anything here. I defined 'APPLE' in the makefile for OS X */

	glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 2);
	glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1);


	/*GLFWmonitor* mon = glfwGetPrimaryMonitor ();
	const GLFWvidmode* vmode = glfwGetVideoMode (mon);
	g_window = glfwCreateWindow (
		vmode->width, vmode->height, "Extended GL Init", mon, NULL
	);*/

	g_window = glfwCreateWindow (g_gl_width, g_gl_height, "Extended Init.",
		NULL, NULL);
	if (!g_window) {
		fprintf (stderr, "ERROR: could not open window with GLFW3\n");
		glfwTerminate();
		return false;
	}
	glfwSetWindowSizeCallback (g_window, glfw_window_size_callback);
	glfwMakeContextCurrent (g_window);
	
	glfwWindowHint (GLFW_SAMPLES, 16);
	
	// start GLEW extension handler
	glewExperimental = GL_TRUE;
	glewInit ();

	// get version info
	const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
	const GLubyte* version = glGetString (GL_VERSION); // version as a string
	printf ("renderer: %s\n", renderer);
	printf ("OpenGL version supported %s\n", version);
	gl_log ("renderer: %s\nversion: %s\n", renderer, version);
	
	return true;
}
void print_shader_info_log (GLuint shader_index) {
	int max_length = 2048;
	int actual_length = 0;
	char log[2048];
	glGetShaderInfoLog (shader_index, max_length, &actual_length, log);
	printf ("shader info log for GL index %i:\n%s\n", shader_index, log);
	gl_log ("shader info log for GL index %i:\n%s\n", shader_index, log);
}
void print_programme_info_log (GLuint sp) {
	int max_length = 2048;
	int actual_length = 0;
	char log[2048];
	glGetProgramInfoLog (sp, max_length, &actual_length, log);
	printf ("program info log for GL index %u:\n%s", sp, log);
	gl_log ("program info log for GL index %u:\n%s", sp, log);
}
bool create_shader (const char* file_name, GLuint* shader, GLenum type) {
	gl_log ("creating shader from %s...\n", file_name);
	char shader_string[MAX_SHADER_LENGTH];
	assert (parse_file_into_str (file_name, shader_string, MAX_SHADER_LENGTH));
	*shader = glCreateShader (type);
	const GLchar* p = (const GLchar*)shader_string;
	glShaderSource (*shader, 1, &p, NULL);
	glCompileShader (*shader);
	// check for compile errors
	int params = -1;
	glGetShaderiv (*shader, GL_COMPILE_STATUS, &params);
	if (GL_TRUE != params) {
		gl_log_err ("ERROR: GL shader index %i did not compile\n", *shader);
		print_shader_info_log (*shader);
		return false; // or exit or something
	}
	gl_log ("shader compiled. index %i\n", *shader);
	return true;
}
bool is_programme_valid (GLuint sp) {
	glValidateProgram (sp);
	GLint params = -1;
	glGetProgramiv (sp, GL_VALIDATE_STATUS, &params);
	if (GL_TRUE != params) {
		gl_log_err ("program %i GL_VALIDATE_STATUS = GL_FALSE\n", sp);
		print_programme_info_log (sp);
		return false;
	}
	gl_log ("program %i GL_VALIDATE_STATUS = GL_TRUE\n", sp);
	return true;
}
Пример #13
0
bool Skybox::loadCubeMapSide(
	GLuint texture, GLenum side_target, const char* file_name
	)
{
	glBindTexture(GL_TEXTURE_CUBE_MAP, texture);

	int x, y, n;
	int force_channels = 4;
	unsigned char*  image_data = stbi_load(
		file_name, &x, &y, &n, force_channels);
	if (!image_data)
	{
		gl_log("ERROR: could not load %s\n", file_name);
		return false;
	}
	// non-power-of-2 dimensions check
	if ((x & (x - 1)) != 0 || (y & (y - 1)) != 0)
	{
		gl_log("WARNING: image %s is not power-of-2 dimensions\n", file_name);
	}

	// copy image data into 'target' side of cube map
	glTexImage2D(
		side_target,
		0,
		GL_RGBA,
		x,
		y,
		0,
		GL_RGBA,
		GL_UNSIGNED_BYTE,
		image_data
		);
	free(image_data);
	gl_log("Skybox: loaded %s \n", file_name);
	std::cout << "SKYBOY LOADCUBEMAPSIDE " << side_target << "\n";
	return true;
}
Пример #14
0
lval* lval_copy(lval *v) {
    lval* x;
    gl_log(L_DEBUG, "Copying lval of type: %s", ltype_name(v->type));
    switch (v->type) {
        case LVAL_FUN:
            x = new_lval(v->type, 0);
            if (v->builtin) {
                x->builtin = v->builtin;
            } else {
                x->builtin = NULL;
                x->env = lenv_copy(v->env);
                x->formals = lval_copy(v->formals);
                x->body = lval_copy(v->body);
            }
            break;
        case LVAL_NUM:
            x = new_lval(v->type, 0);
            x->num = v->num;
            break;
        case LVAL_ERR:
            x = new_lval(v->type, strlen(v->err) + 1);
            x->err = malloc(strlen(v->err) + 1);
            strcpy(x->err, v->err);
            break;
        case LVAL_SYM:
            x = new_lval(v->type, strlen(v->sym) + 1);
            x->sym = malloc(strlen(v->sym) + 1);
            strcpy(x->sym, v->sym);
            break;
        case LVAL_STR:
            x = new_lval(v->type, strlen(v->str) + 1);
            x->str = malloc(strlen(v->str) + 1);
            strcpy(x->str, v->str);
            break;
        case LVAL_SEXPR:
        case LVAL_QEXPR:
            x = new_lval(v->type, sizeof(lval*) * v->count);
            x->count = v->count;
            x->cell = malloc(sizeof(lval*) * x->count);
            for (int i = 0; i < x->count; i++) {
                x->cell[i] = lval_copy(v->cell[i]);
            }
        break;
    }
    x->type = v->type;
    return x;
}
Пример #15
0
int main()
{


	// open an OS window using GLFW
	if(!glfwInit())
	{
		fprintf (stderr, "ERROR: could not start GLFW3\n");
		return 1;
	}
	char message[256];
	sprintf (message, "starting GLFW %s", glfwGetVersionString());
	assert (gl_log (message, __FILE__, __LINE__));
	glfwSetErrorCallback (glfw_error_callback);

	// Anti-Aliasing
	glfwWindowHint (GLFW_SAMPLES, 4);

	// get the primary monitor
	GLFWmonitor* mon = glfwGetPrimaryMonitor();

	// this lets us use the video mode for the monitor we pass
	const GLFWvidmode* vmode = glfwGetVideoMode(mon);
		
	//GLFWwindow* window2 = glfwCreateWindow(vmode->width, vmode->height, "Extended GL Init",mon/* mon*/, NULL);
	//	if (!window2)
	//		{
	//			fprintf (stderr, "ERROR: could not open window with GLFW3\n");
	//			glfwTerminate();
	//			return 1;
	//		}

	//glfwSetWindowSize (window2, vmode->width, vmode->height);
	//glfwMakeContextCurrent (window2);

	GLFWwindow * window = glfwCreateWindow(640,480,"Hello Triangle", NULL, NULL);
		if (!window)
		{
			fprintf (stderr, "ERROR: could not open window with GLFW3\n");
			glfwTerminate();
			return 1;
		}

	glfwMakeContextCurrent(window);	// set as active window

	// start GLEW extension handler
	glewExperimental = GL_TRUE;
	glewInit();

	// get version info
	const GLubyte* renderer = glGetString (GL_RENDERER);	// get renderer string
	const GLubyte* version = glGetString (GL_VERSION);		// version as a string
	printf ("Renderer: %s\n", renderer);
	printf ("OpenGL version supported %s\n", version);

	// tell GL to only draw onto a pixel if the shape is closer to the viewer
	glEnable (GL_DEPTH_TEST); // enable depth-testing
	glDepthFunc (GL_LESS);
	// depth-testing interprets a smaller value as "closer"

	//const char* vertex_shader = 
	//"#version 330\n"
	//"in vec3 vp;"
	//"void main () {"
	//" gl_Position = vec4 (vp, 1.0);"
	//"}";

	//const char* fragment_shader = 
	//"#version 330\n"
	//"out vec4 frag_colour;"
	//"void main () {" 
	//" float lerpValue = gl_FragCoord.y / 480.0f;"
	//" frag_colour = mix(vec4 (1.0f, 1.0f, 1.0f, 1.0f), vec4 (0.5, 0.0, 0.5, 1.0), lerpValue);"
	//"}";

	//// Create a shader
	//GLuint vs = glCreateShader (GL_VERTEX_SHADER);
	//// put actual char* into the shader
	//glShaderSource (vs, 1, &vertex_shader, NULL);
	//// compile the shader
	//glCompileShader(vs);
	//printShaderInfoLog(vs);

	//GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
	//glShaderSource (fs, 1, &fragment_shader, NULL);
	//glCompileShader (fs);
	//printShaderInfoLog(fs);

	//// create the container that holds your shaders
	//GLuint shaderProgram = glCreateProgram ();
	//// attach the shaders you compiled
	//glAttachShader (shaderProgram, fs);
	//glAttachShader (shaderProgram, vs);

	//// this links the shaders together, its kinda like a compile
	//glLinkProgram (shaderProgram);
	//printProgramInfoLog(shaderProgram);

	//std::vector<Quad> v_quads;

	//Quad * tester = new Quad();
	Sprite * richard = new Sprite("simmons.png",384,324,Vector4(1,1,1,1),window);
	//v_quads.push_back(initQuad);

	while (!glfwWindowShouldClose (window)) 
	{
		_update_fps_counter (window);
		// wipe the drawing surface clear
		glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glViewport (0,0, g_gl_width, g_gl_height);
		glClearColor(0,1,1,1);
		//glUseProgram (shaderProgram);

		//// if key is pressed
		//if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
		//{
		//// make new quad object
		//	Quad newQuad;
		//	v_quads.push_back(newQuad);
		//}

		/*for (int i=0;i<7;i++)
		{
			Quad newQuad;
			v_quads.push_back(newQuad);
		}*/

		// for each quad in vector,  bind its VAO and draw
		/*for(auto quad= v_quads.begin(); quad != v_quads.end(); ++quad)
		{
			glBindVertexArray (quad->m_VAO);
 			glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
		}*/
		
		richard->Draw();
		// glBindVertexArray (0);

		// put the stuff we've been drawing onto the display
		glfwSwapBuffers (window);	
	}



	// close GL context and any other GLFW resources
	glfwTerminate();


	//char* pause = "not";
	//std::cin>> pause;

	return 0;
}
Пример #16
0
static Mesh* process_mesh(struct aiMesh* mesh, struct aiScene* scene) {
    Vertex **vertices = malloc(sizeof(Vertex *) * mesh->mNumVertices);
    for (int i = 0; i < mesh->mNumVertices; i++) {
        vertices[i] = malloc(sizeof(Vertex));
    }

    // Process vertices
    if (mesh->mVertices && mesh->mNumVertices > 0) {
        for (int i = 0; i < mesh->mNumVertices; i++) {
            struct aiVector3D position = mesh->mVertices[i];
            vertices[i]->position = create_vec(position.x, position.y, position.z, 1.0);
        }
    } else {
        gl_log(INFO, "A mesh was processed with no vertices.");
    }

    // Process normals
    if (mesh->mNormals) {
        for (int i = 0; i < mesh->mNumVertices; i++) {
            struct aiVector3D normal = mesh->mNormals[i];
            vertices[i]->normal = create_vec(normal.x, normal.y, normal.z, 1.0);
        }
    } else {
        gl_log(INFO, "A mesh was processed with no normals.");
    }

    // Process texture coords
    if (mesh->mTextureCoords[0]) {
        for (int i = 0; i < mesh->mNumVertices; i++) {
            struct aiVector3D texture = mesh->mTextureCoords[0][i];
            vertices[i]->texture_coords = create_vec(texture.x, texture.y, texture.z, 1.0);
        }
    } else {
        for (int i = 0; i < mesh->mNumVertices; i++) {
            vertices[i]->texture_coords = 0;
        }
        gl_log(INFO, "A mesh was processed with no texture coordinates.");
    }

    // Process material
    Texture* mesh_texture = malloc(sizeof(Texture));
    Material* mesh_material = malloc(sizeof(Material));
    if (mesh->mMaterialIndex >= 0) {
        struct aiString path;
        struct aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];
        aiGetMaterialTexture(material, aiTextureType_DIFFUSE, 0, &path, 0, 0, 0, 0, 0, 0);
        mesh_texture->id = create_texture(path.data);

        struct aiColor4D color;
        aiGetMaterialColor(material, AI_MATKEY_COLOR_DIFFUSE, &color);
        mesh_material->diffuse_color = create_vec(color.r, color.g, color.b, color.a);

        aiGetMaterialColor(material, AI_MATKEY_COLOR_AMBIENT, &color);
        mesh_material->ambient_color = create_vec(color.r, color.g, color.b, color.a);

        aiGetMaterialColor(material, AI_MATKEY_COLOR_SPECULAR, &color);
        mesh_material->specular_color = create_vec(color.r, color.g, color.b, color.a);
    } else {
        gl_log(INFO, "A mesh was processed with no material.");
    }

    return create_mesh(vertices, mesh->mNumVertices, mesh_texture, mesh_material);
}
Пример #17
0
	void glfw_error_callback(int error, const char * description)
	{
		fputs(description, stderr);
		gl_log(description, __FILE__, __LINE__);
		//update matrices below
	}
int main () {
	GLFWwindow* window;
	const GLubyte* renderer;
	const GLubyte* version;
	GLfloat points[] = {
		 0.0f,	0.5f,	0.0f,
		 0.5f, -0.5f,	0.0f,
		-0.5f, -0.5f,	0.0f
	};
	GLuint vbo;
	GLuint vao;
	const char* vertex_shader =
	"#version 400\n"
	"in vec3 vp;"
	"void main () {"
	"	gl_Position = vec4 (vp, 1.0);"
	"}";
	
	const char* fragment_shader =
	"#version 400\n"
	"out vec4 frag_colour;"
	"void main () {"
	"	frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"
	"}";
	GLuint shader_programme, vs, fs;

	assert (restart_gl_log ());
	// start GL context and O/S window using the GLFW helper library
	gl_log ("starting GLFW\n%s\n", glfwGetVersionString ());
	// register the error call-back function that we wrote, above
	glfwSetErrorCallback (glfw_error_callback);
	if (!glfwInit ()) {
		fprintf (stderr, "ERROR: could not start GLFW3\n");
		return 1;
	}

	// uncomment these lines if on Apple OS X
	/*glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2);
	glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
	glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
*/
	/* we can run a full-screen window here */
	
	/*GLFWmonitor* mon = glfwGetPrimaryMonitor ();
	const GLFWvidmode* vmode = glfwGetVideoMode (mon);
	GLFWwindow* window = glfwCreateWindow (
		vmode->width, vmode->height, "Extended GL Init", mon, NULL
	);*/

	window = glfwCreateWindow (
		g_gl_width, g_gl_height, "Extended Init.", NULL, NULL
	);
	if (!window) {
		fprintf (stderr, "ERROR: could not open window with GLFW3\n");
		glfwTerminate();
		return 1;
	}
	glfwSetWindowSizeCallback (window, glfw_window_size_callback);
	glfwMakeContextCurrent (window);
	
	glfwWindowHint (GLFW_SAMPLES, 4);
	// start GLEW extension handler
	glewExperimental = GL_TRUE;
	glewInit ();

	// get version info
	renderer = glGetString (GL_RENDERER); // get renderer string
	version = glGetString (GL_VERSION); // version as a string
	printf ("Renderer: %s\n", renderer);
	printf ("OpenGL version supported %s\n", version);
	gl_log ("renderer: %s\nversion: %s\n", renderer, version);
	log_gl_params ();
	// tell GL to only draw onto a pixel if the shape is closer to the viewer
	glEnable (GL_DEPTH_TEST); // enable depth-testing
	glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"
	
	glGenBuffers (1, &vbo);
	glBindBuffer (GL_ARRAY_BUFFER, vbo);
	glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (GLfloat), points, GL_STATIC_DRAW);
	
	glGenVertexArrays (1, &vao);
	glBindVertexArray (vao);
	glEnableVertexAttribArray (0);
	glBindBuffer (GL_ARRAY_BUFFER, vbo);
	glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
	
	vs = glCreateShader (GL_VERTEX_SHADER);
	glShaderSource (vs, 1, &vertex_shader, NULL);
	glCompileShader (vs);
	fs = glCreateShader (GL_FRAGMENT_SHADER);
	glShaderSource (fs, 1, &fragment_shader, NULL);
	glCompileShader (fs);
	shader_programme = glCreateProgram ();
	glAttachShader (shader_programme, fs);
	glAttachShader (shader_programme, vs);
	glLinkProgram (shader_programme);
	
	while (!glfwWindowShouldClose (window)) {
		_update_fps_counter (window);
		// wipe the drawing surface clear
		glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glViewport (0, 0, g_gl_width, g_gl_height);
		
		glUseProgram (shader_programme);
		glBindVertexArray (vao);
		// draw points 0-3 from the currently bound VAO with current in-use shader
		glDrawArrays (GL_TRIANGLES, 0, 3);
		// update other events like input handling 
		glfwPollEvents ();
		if (GLFW_PRESS == glfwGetKey (window, GLFW_KEY_ESCAPE)) {
			glfwSetWindowShouldClose (window, 1);
		}
		// put the stuff we've been drawing onto the display
		glfwSwapBuffers (window);
	}
	
	// close GL context and any other GLFW resources
	glfwTerminate();
	return 0;
}
Пример #19
0
int main()
{
	//setup to log some GLFW stuff

	char message[256];
	sprintf (message, "starting GLFW %s", glfwGetVersionString ());
	assert (gl_log (message, __FILE__, __LINE__));
	glfwSetErrorCallback (glfw_error_callback);

	//open an OS window using GLFW
	if(!glfwInit())
	{
		fprintf (stderr,"ERROR: could not start GLFW3\n");
		return 1;
	}

	// uncomment these lines if on Apple OS X
	/*	glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2);
	glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
	glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	*/	
	//Anti-Aliasing
	glfwWindowHint (GLFW_SAMPLES, 4);

	//get the primary monitor
	GLFWmonitor* mon = glfwGetPrimaryMonitor ();
	//this lets us the the video mode for the monitor we pass
	const GLFWvidmode* vmode = glfwGetVideoMode (mon);
	GLFWwindow* window = glfwCreateWindow (
		vmode->width, vmode->height, "Extended GL Init",NULL/* mon*/, NULL
		);
	glfwSetWindowSize(window, g_gl_width, g_gl_height);

	if (!window) {
		fprintf (stderr, "ERROR: could not open window with GLFW3\n");
		glfwTerminate();
		return 1;
	}
	//not sure if this works
	//log_gl_params ();

	glfwMakeContextCurrent(window);

	//start GLEW extension handler
	glewExperimental = GL_TRUE;
	glewInit();

	// get version info
	const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
	const GLubyte* version = glGetString (GL_VERSION); // version as a string
	printf ("Renderer: %s\n", renderer);
	printf ("OpenGL version supported %s\n", version);

	// tell GL to only draw onto a pixel if the shape is closer to the viewer
	glEnable (GL_DEPTH_TEST); // enable depth-testing
	glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"

	float speed = 1.0f; // move at 1 unit per second
	float last_position = 0.0f;

	//Quad * tester = new Quad();

	Sprite * richard = new Sprite("./resources/simmons.png", 384, 324, Vector4(1,1,1,1), window); 


	//int matrix_location = glGetUniformLocation (shaderProgram, "matrix");
	//glUniform1i(glGetUniformLocation(shaderProgram, "Texture"), 0);

	Ortho = new Matrix4();
	Orthographic(0, g_gl_width, g_gl_height, 0, 0, -1, Ortho);

	while (!glfwWindowShouldClose (window)) {

		/* add a timer for doing animation
  static double previous_seconds = glfwGetTime ();
  double current_seconds = glfwGetTime ();
  double elapsed_seconds = current_seconds - previous_seconds;
  previous_seconds = current_seconds;
   // reverse direction when going to far left or right
  if (fabs(last_position) > 1.0f) {
    speed = -speed;
  }
  */

		glEnable (GL_CULL_FACE); // cull face
		glCullFace (GL_BACK); // cull back face
		glFrontFace (GL_CW); // GL_CCW for counter clock-wise

		// wipe the drawing surface clear
		glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		//resize window
		glViewport (0, 0, g_gl_width, g_gl_height);

	
		
		  // update the matrix
//  matrix[12] = elapsed_seconds * speed + last_position;
//  last_position = matrix[12];
		//set the shader for this VAO
//		glUseProgram (shaderProgram);
		//Here is where we attach the marix
//		glUniformMatrix4fv (matrix_location, 1, GL_FALSE, matrix);
		//bind the VAO to be drawn
//		glBindVertexArray (VAO);
		// draw points 0-3 from the currently bound VAO with current in-use shader
//		glDrawArrays (GL_TRIANGLES, 0, 3);

		richard->Input();
		richard->Draw();

		// update other events like input handling 
		glfwPollEvents ();
		// put the stuff we've been drawing onto the display
		glfwSwapBuffers (window);


		//When do i exit?
		if (GLFW_PRESS == glfwGetKey (window, GLFW_KEY_ESCAPE)) {
			glfwSetWindowShouldClose (window, 1);
		}
	}

	// close GL context and any other GLFW resources
	glfwTerminate();
	return 0;

}
Пример #20
0
int notmain2()
{
	char message[256];
sprintf (message, "starting GLFW %s", glfwGetVersionString ());
assert (gl_log (message, __FILE__, __LINE__));
glfwSetErrorCallback (glfw_error_callback);

    //open an OS window using GLFW
    if(!glfwInit())
    {
        fprintf (stderr,"ERROR: could not start GLFW3\n");
        return 1;
    }

    // uncomment these lines if on Apple OS X
    /*glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);*/

	glfwWindowHint (GLFW_SAMPLES, 4);

    GLFWwindow * window = glfwCreateWindow(640,480,"Hello Triangle",NULL,NULL);
    if (!window) {
        fprintf (stderr, "ERROR: could not open window with GLFW3\n");
        glfwTerminate();
        return 1;
    }

    glfwMakeContextCurrent(window);

    //start GLEW extension handler
    glewExperimental = GL_TRUE;
    glewInit();

    // get version info
    const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
    const GLubyte* version = glGetString (GL_VERSION); // version as a string
    printf ("Renderer: %s\n", renderer);
    printf ("OpenGL version supported %s\n", version);

    // tell GL to only draw onto a pixel if the shape is closer to the viewer
    glEnable (GL_DEPTH_TEST); // enable depth-testing
    glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"

    /* OTHER STUFF GOES HERE NEXT */
	
  float points[] = 
    {
        0.0f,0.5f,0.0f,
        0.5f,-0.5f,0.0f,
        -0.5f,-0.5f,0.0f,

    };
    GLuint VBO = 0;
    glGenBuffers (1, &VBO);
    glBindBuffer (GL_ARRAY_BUFFER, VBO);
    glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW);

	GLuint VAO = 0;
glGenVertexArrays (1, &VAO);
glBindVertexArray (VAO);
glEnableVertexAttribArray (0);
glBindBuffer (GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);

const char* vertex_shader =
"#version 400\n"
"in vec3 vp;"
"void main () {"
"  gl_Position = vec4 (vp, 1.0);"
"}";

const char* fragment_shader =
"#version 400\n"
"out vec4 frag_colour;"
"void main () {"
"  frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"
"}";

//Create a shader
GLuint vs = glCreateShader (GL_VERTEX_SHADER);
//put the actual char* into the shader 
glShaderSource (vs, 1, &vertex_shader, NULL);
//Compile the shader 
glCompileShader (vs);


GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (fs, 1, &fragment_shader, NULL);
glCompileShader (fs);


//Create the container that holds your shaders
GLuint shaderProgram = glCreateProgram ();
//attach the shaders you compiled
glAttachShader (shaderProgram, fs);
glAttachShader (shaderProgram, vs);
//this is links the shaders together, its kinda like a compile
glLinkProgram (shaderProgram);

while (!glfwWindowShouldClose (window)) {
  // wipe the drawing surface clear
  glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glUseProgram (shaderProgram);
  glBindVertexArray (VAO);
  // draw points 0-3 from the currently bound VAO with current in-use shader
  glDrawArrays (GL_TRIANGLES, 0, 3);
  // update other events like input handling 
  glfwPollEvents ();
  // put the stuff we've been drawing onto the display
  glfwSwapBuffers (window);
}

    // close GL context and any other GLFW resources
    glfwTerminate();
    return 0;


}