graphene

changeset 1:f85a59195206

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 24 Jul 2015 01:28:00 +0300
parents 8ab44b19895e
children fb032d88839f
files .hgignore Makefile src/opengl.cc src/opengl.h test/Makefile test/src/main.cc
diffstat 6 files changed, 252 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/.hgignore	Mon Jul 20 07:08:25 2015 +0300
     1.2 +++ b/.hgignore	Fri Jul 24 01:28:00 2015 +0300
     1.3 @@ -1,3 +1,7 @@
     1.4  \.o$
     1.5  \.d$
     1.6  \.swp$
     1.7 +\.a$
     1.8 +\.so\.
     1.9 +\.so$
    1.10 +test/test$
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Makefile	Fri Jul 24 01:28:00 2015 +0300
     2.3 @@ -0,0 +1,62 @@
     2.4 +ccsrc = $(wildcard src/*.cc) \
     2.5 +		$(wildcard gmath/*.cc)
     2.6 +csrc = $(wildcard src/*.c) \
     2.7 +	   $(wildcard gmath/*.c)
     2.8 +obj = $(ccsrc:.cc=.o) $(csrc:.c=.o)
     2.9 +dep = $(obj:.o=.d)
    2.10 +
    2.11 +name = graphene3d
    2.12 +somajor = 0
    2.13 +sominor = 1
    2.14 +
    2.15 +sys = $(shell uname -s)
    2.16 +
    2.17 +ifeq ($(sys), Darwin)
    2.18 +	libgl = -framework OpenGL -lGLEW
    2.19 +
    2.20 +	shared = -dynamiclib
    2.21 +	solib = lib$(name).dylib
    2.22 +else
    2.23 +	libgl = -lGL -lGLU -lGLEW
    2.24 +
    2.25 +	ldname = lib$(name).so
    2.26 +	soname = $(ldname).$(somajor)
    2.27 +	solib = $(soname).$(sominor)
    2.28 +
    2.29 +	shared = -shared -Wl,-soname=$(soname)
    2.30 +	pic = -fPIC
    2.31 +endif
    2.32 +
    2.33 +inc = -Ilib
    2.34 +opt =
    2.35 +dbg = -g
    2.36 +
    2.37 +CFLAGS = -pedantic -Wall $(opt) $(dbg) $(pic) $(inc)
    2.38 +CXXFLAGS = $(CFLAGS)
    2.39 +LDFLAGS = $(libgl)
    2.40 +
    2.41 +.PHONY: shared
    2.42 +shared: $(solib)
    2.43 +
    2.44 +$(solib): $(obj)
    2.45 +	$(CXX) -o $@ $(shared) $(obj) $(LDFLAGS)
    2.46 +	[ -n "$(soname)" ] && \
    2.47 +		rm -f $(soname) $(ldname) && \
    2.48 +		ln -s $(solib) $(soname) && \
    2.49 +		ln -s $(soname) $(ldname) || true
    2.50 +
    2.51 +-include $(dep)
    2.52 +
    2.53 +%.d: %.c
    2.54 +	@$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@
    2.55 +
    2.56 +%.d: %.cc
    2.57 +	@$(CPP) $(CXXFLAGS) $< -MM -MT $(@:.d=.o) >$@
    2.58 +
    2.59 +.PHONY: clean
    2.60 +clean:
    2.61 +	rm -f $(obj) $(solib)
    2.62 +
    2.63 +.PHONY: cleandep
    2.64 +cleandep:
    2.65 +	rm -f $(dep)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/opengl.cc	Fri Jul 24 01:28:00 2015 +0300
     3.3 @@ -0,0 +1,79 @@
     3.4 +#include <stdio.h>
     3.5 +#include <stdlib.h>
     3.6 +#include <string.h>
     3.7 +#include "opengl.h"
     3.8 +
     3.9 +namespace gph {
    3.10 +
    3.11 +GLCaps glcaps;
    3.12 +
    3.13 +static void fill_glcaps();
    3.14 +static void print_glcaps(GLCaps *caps);
    3.15 +
    3.16 +bool init_opengl()
    3.17 +{
    3.18 +	glewInit();
    3.19 +	fill_glcaps();
    3.20 +	return true;
    3.21 +}
    3.22 +
    3.23 +static void fill_glcaps()
    3.24 +{
    3.25 +	memset(&glcaps, 0, sizeof glcaps);
    3.26 +
    3.27 +	glcaps.version = strdup((const char*)glGetString(GL_VERSION));
    3.28 +	glcaps.renderer = strdup((const char*)glGetString(GL_RENDERER));
    3.29 +	glcaps.vendor = strdup((const char*)glGetString(GL_VENDOR));
    3.30 +
    3.31 +	glcaps.sdr = GLEW_ARB_shading_language_100;
    3.32 +	if(glcaps.sdr) {
    3.33 +		glcaps.sdr_ver = strdup((const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
    3.34 +	}
    3.35 +
    3.36 +	glcaps.fbo = GLEW_ARB_framebuffer_object || GLEW_EXT_framebuffer_object;
    3.37 +	glcaps.tex_float = GLEW_ARB_texture_float;
    3.38 +	glcaps.npow2_tex = GLEW_ARB_texture_non_power_of_two;
    3.39 +	glcaps.fsaa = GLEW_ARB_multisample;
    3.40 +
    3.41 +	glGetIntegerv(GL_MAX_TEXTURE_UNITS, &glcaps.max_tex_units);
    3.42 +	if(glcaps.fbo) {
    3.43 +		glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &glcaps.max_mrt);
    3.44 +	}
    3.45 +	if(glcaps.fsaa) {
    3.46 +		glGetIntegerv(GL_MAX_SAMPLES, &glcaps.max_samples);
    3.47 +	} else {
    3.48 +		glcaps.max_samples = 1;
    3.49 +	}
    3.50 +	if(GLEW_EXT_texture_filter_anisotropic) {
    3.51 +		glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &glcaps.max_aniso);
    3.52 +	} else {
    3.53 +		glcaps.max_aniso = 1.0f;
    3.54 +	}
    3.55 +
    3.56 +	print_glcaps(&glcaps);
    3.57 +}
    3.58 +
    3.59 +static void print_glcaps(GLCaps *caps)
    3.60 +{
    3.61 +	printf("OpenGL %s - %s - %s\n", caps->version, caps->vendor, caps->renderer);
    3.62 +
    3.63 +	printf("--- capabilities ---\n");
    3.64 +	printf("       GLSL shaders: %s\n", caps->sdr ? "yes" : "no");
    3.65 +	if(caps->sdr) {
    3.66 +		printf("       GLSL version: %s\n", caps->sdr_ver);
    3.67 +	}
    3.68 +	printf("framebuffer objects: %s\n", caps->fbo ? "yes" : "no");
    3.69 +	if(caps->fbo) {
    3.70 +		printf(" max render targets: %d\n", caps->max_mrt);
    3.71 +	}
    3.72 +	printf("       antialiasing: %s\n", caps->fsaa ? "yes" : "no");
    3.73 +	if(caps->fsaa) {
    3.74 +		printf("        max samples: %d\n", caps->max_samples);
    3.75 +	}
    3.76 +	printf("     float textures: %s\n", caps->tex_float ? "yes" : "no");
    3.77 +	printf("  non-pow2 textures: %s\n", caps->npow2_tex ? "yes" : "no");
    3.78 +	printf("      texture units: %d\n", caps->max_tex_units);
    3.79 +	printf("--------------------\n");
    3.80 +}
    3.81 +
    3.82 +}	// namespace gph
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/opengl.h	Fri Jul 24 01:28:00 2015 +0300
     4.3 @@ -0,0 +1,27 @@
     4.4 +#ifndef GPH_OPENGL_H_
     4.5 +#define GPH_OPENGL_H_
     4.6 +
     4.7 +#include <GL/glew.h>
     4.8 +
     4.9 +namespace gph {
    4.10 +
    4.11 +extern struct GLCaps {
    4.12 +	char *version, *renderer, *vendor;
    4.13 +	int max_tex_units;
    4.14 +	int max_tex_size;
    4.15 +	int max_samples;
    4.16 +	float max_aniso;
    4.17 +	int max_mrt;
    4.18 +	bool sdr;
    4.19 +	char *sdr_ver;
    4.20 +	bool fbo;
    4.21 +	bool tex_float;
    4.22 +	bool npow2_tex;
    4.23 +	bool fsaa;
    4.24 +} glcaps;
    4.25 +
    4.26 +bool init_opengl();
    4.27 +
    4.28 +}	// namespace gph
    4.29 +
    4.30 +#endif	/* GPH_OPENGL_H_ */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/Makefile	Fri Jul 24 01:28:00 2015 +0300
     5.3 @@ -0,0 +1,21 @@
     5.4 +src = $(wildcard src/*.cc)
     5.5 +obj = $(src:.cc=.o)
     5.6 +bin = test
     5.7 +
     5.8 +gphroot = ..
     5.9 +
    5.10 +CXXFLAGS = -pedantic -Wall -g -I$(gphroot)/src
    5.11 +LDFLAGS = -L$(gphroot) -Wl,-rpath=$(gphroot) -lgraphene3d $(libgl)
    5.12 +
    5.13 +ifeq ($(shell uname -s), Darwin)
    5.14 +	libgl = -framework OpenGL -framework GLUT -lGLEW
    5.15 +else
    5.16 +	libgl = -lGL -lGLU -lglut -lGLEW
    5.17 +endif
    5.18 +
    5.19 +$(bin): $(obj)
    5.20 +	$(CXX) -o $@ $(obj) $(LDFLAGS)
    5.21 +
    5.22 +.PHONY: clean
    5.23 +clean:
    5.24 +	rm -f $(obj) $(bin)
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/test/src/main.cc	Fri Jul 24 01:28:00 2015 +0300
     6.3 @@ -0,0 +1,59 @@
     6.4 +#include <stdio.h>
     6.5 +#include <stdlib.h>
     6.6 +#include "opengl.h"
     6.7 +#include <GL/glut.h>
     6.8 +
     6.9 +void display();
    6.10 +void reshape(int x, int y);
    6.11 +void keydown(unsigned char key, int x, int y);
    6.12 +void mouse(int bn, int st, int x, int y);
    6.13 +void motion(int x, int y);
    6.14 +
    6.15 +int main(int argc, char **argv)
    6.16 +{
    6.17 +	glutInit(&argc, argv);
    6.18 +	glutInitWindowSize(1280, 800);
    6.19 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    6.20 +	glutCreateWindow("graphene3d test");
    6.21 +
    6.22 +	glutDisplayFunc(display);
    6.23 +	glutReshapeFunc(reshape);
    6.24 +	glutKeyboardFunc(keydown);
    6.25 +	glutMouseFunc(mouse);
    6.26 +	glutMotionFunc(motion);
    6.27 +
    6.28 +	if(!gph::init_opengl()) {
    6.29 +		return 1;
    6.30 +	}
    6.31 +
    6.32 +	glutMainLoop();
    6.33 +	return 0;
    6.34 +}
    6.35 +
    6.36 +void display()
    6.37 +{
    6.38 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    6.39 +
    6.40 +	glutSwapBuffers();
    6.41 +}
    6.42 +
    6.43 +void reshape(int x, int y)
    6.44 +{
    6.45 +	glViewport(0, 0, x, y);
    6.46 +}
    6.47 +
    6.48 +void keydown(unsigned char key, int x, int y)
    6.49 +{
    6.50 +	switch(key) {
    6.51 +	case 27:
    6.52 +		exit(0);
    6.53 +	}
    6.54 +}
    6.55 +
    6.56 +void mouse(int bn, int st, int x, int y)
    6.57 +{
    6.58 +}
    6.59 +
    6.60 +void motion(int x, int y)
    6.61 +{
    6.62 +}