tavli

changeset 16:d6209903454b

opengl capabilities
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 28 Jun 2015 08:48:25 +0300
parents b1a195c3ee16
children 16a420432aa3
files src/game.cc src/mesh.cc src/object.cc src/opengl.c src/opengl.h
diffstat 5 files changed, 64 insertions(+), 17 deletions(-) [+]
line diff
     1.1 --- a/src/game.cc	Sun Jun 28 08:34:24 2015 +0300
     1.2 +++ b/src/game.cc	Sun Jun 28 08:48:25 2015 +0300
     1.3 @@ -1,5 +1,5 @@
     1.4  #include <stdio.h>
     1.5 -#include <GL/glew.h>
     1.6 +#include "opengl.h"
     1.7  #include "game.h"
     1.8  #include "board.h"
     1.9  #include "scenery.h"
    1.10 @@ -21,23 +21,32 @@
    1.11  
    1.12  bool game_init()
    1.13  {
    1.14 +	if(init_opengl() == -1) {
    1.15 +		return false;
    1.16 +	}
    1.17 +
    1.18  	glEnable(GL_DEPTH_TEST);
    1.19  	glEnable(GL_CULL_FACE);
    1.20  	glEnable(GL_NORMALIZE);
    1.21  	glEnable(GL_LIGHTING);
    1.22  	glEnable(GL_LIGHT0);
    1.23  
    1.24 -	if(GLEW_ARB_multisample) {
    1.25 +	if(glcaps.sep_spec) {
    1.26 +		glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
    1.27 +	}
    1.28 +
    1.29 +	if(glcaps.fsaa) {
    1.30  		glEnable(GL_MULTISAMPLE);
    1.31  	}
    1.32  
    1.33 -	Mesh::use_custom_sdr_attr = false;
    1.34 -
    1.35 -	if(!(sdr_phong = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl"))) {
    1.36 -		return false;
    1.37 -	}
    1.38 -	if(!(sdr_phong_notex = create_program_load("sdr/phong.v.glsl", "sdr/phong-notex.p.glsl"))) {
    1.39 -		return false;
    1.40 +	if(glcaps.shaders) {
    1.41 +		Mesh::use_custom_sdr_attr = false;
    1.42 +		if(!(sdr_phong = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl"))) {
    1.43 +			return false;
    1.44 +		}
    1.45 +		if(!(sdr_phong_notex = create_program_load("sdr/phong.v.glsl", "sdr/phong-notex.p.glsl"))) {
    1.46 +			return false;
    1.47 +		}
    1.48  	}
    1.49  
    1.50  	if(!board.init()) {
     2.1 --- a/src/mesh.cc	Sun Jun 28 08:34:24 2015 +0300
     2.2 +++ b/src/mesh.cc	Sun Jun 28 08:48:25 2015 +0300
     2.3 @@ -564,9 +564,10 @@
     2.4  
     2.5  void Mesh::draw() const
     2.6  {
     2.7 -	int cur_sdr;
     2.8 -	glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
     2.9 -
    2.10 +	int cur_sdr = 0;
    2.11 +	if(glcaps.shaders) {
    2.12 +		glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
    2.13 +	}
    2.14  
    2.15  	((Mesh*)this)->update_buffers();
    2.16  
    2.17 @@ -720,8 +721,10 @@
    2.18  void Mesh::draw_normals() const
    2.19  {
    2.20  #ifdef USE_OLDGL
    2.21 -	int cur_sdr;
    2.22 -	glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
    2.23 +	int cur_sdr = 0;
    2.24 +	if(glcaps.shaders) {
    2.25 +		glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
    2.26 +	}
    2.27  
    2.28  	Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
    2.29  	Vector3 *norm = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL);
    2.30 @@ -756,8 +759,10 @@
    2.31  void Mesh::draw_tangents() const
    2.32  {
    2.33  #ifdef USE_OLDGL
    2.34 -	int cur_sdr;
    2.35 -	glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
    2.36 +	int cur_sdr = 0;
    2.37 +	if(glcaps.shaders) {
    2.38 +		glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
    2.39 +	}
    2.40  
    2.41  	Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX);
    2.42  	Vector3 *tang = (Vector3*)get_attrib_data(MESH_ATTR_TANGENT);
     3.1 --- a/src/object.cc	Sun Jun 28 08:34:24 2015 +0300
     3.2 +++ b/src/object.cc	Sun Jun 28 08:48:25 2015 +0300
     3.3 @@ -69,7 +69,9 @@
     3.4  
     3.5  void Object::set_shader(unsigned int sdr)
     3.6  {
     3.7 -	this->sdr = sdr;
     3.8 +	if(GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader) {
     3.9 +		this->sdr = sdr;
    3.10 +	}
    3.11  }
    3.12  
    3.13  void Object::draw() const
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/opengl.c	Sun Jun 28 08:48:25 2015 +0300
     4.3 @@ -0,0 +1,14 @@
     4.4 +#include "opengl.h"
     4.5 +
     4.6 +struct GLCaps glcaps;
     4.7 +
     4.8 +int init_opengl()
     4.9 +{
    4.10 +	glewInit();
    4.11 +
    4.12 +	glcaps.shaders = GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader;
    4.13 +	glcaps.fsaa = GLEW_ARB_multisample;
    4.14 +	glcaps.sep_spec = GLEW_EXT_separate_specular_color;
    4.15 +
    4.16 +	return 0;
    4.17 +}
     5.1 --- a/src/opengl.h	Sun Jun 28 08:34:24 2015 +0300
     5.2 +++ b/src/opengl.h	Sun Jun 28 08:48:25 2015 +0300
     5.3 @@ -3,4 +3,21 @@
     5.4  
     5.5  #include <GL/glew.h>
     5.6  
     5.7 +struct GLCaps {
     5.8 +	int shaders;
     5.9 +	int fsaa;
    5.10 +	int sep_spec;
    5.11 +};
    5.12 +extern struct GLCaps glcaps;
    5.13 +
    5.14 +#ifdef __cplusplus
    5.15 +extern "C" {
    5.16 +#endif
    5.17 +
    5.18 +int init_opengl();
    5.19 +
    5.20 +#ifdef __cplusplus
    5.21 +}
    5.22 +#endif
    5.23 +
    5.24  #endif	/* OPENGL_H_ */