Example #1
0
bool GLShaderImpl::CompileFromSourceImpl(const char *source_text) {
    glShaderSource(m_shader_id, 1, &source_text, NULL);
    glerr();

    glCompileShader(m_shader_id);
    glerr();

    GLint compiled;
    glGetShaderiv(m_shader_id, GL_COMPILE_STATUS, &compiled);
    glerr();

    if (!compiled) {
        GLint info_len;
        glGetShaderiv(m_shader_id, GL_INFO_LOG_LENGTH, &info_len);
        glerr();

        m_last_log.clear();
        m_last_log.resize(info_len, '\0');
        glGetShaderInfoLog(m_shader_id, info_len, NULL, &m_last_log[0]);
        glerr();

    } else {
        m_is_compiled = true;
    }

    return compiled != 0;
}
Example #2
0
void gameLoop(XStuff* xs, GameState* gs, InputState* is) {
	
	checkResize(xs,gs);
	
	preFrame(gs);
	
	handleInput(gs, is);
	
	setUpView(gs);
	
	// update world state
	
	
	// depth and picking pre-pass
	glDepthFunc(GL_LESS);
	glBindFramebuffer(GL_FRAMEBUFFER, gs->framebuffer);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	
	depthPrepass(xs, gs, is);
	
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	glBindFramebuffer(GL_READ_FRAMEBUFFER, gs->framebuffer);
	
	checkCursor(gs, is);
	
	glBindFramebuffer(GL_FRAMEBUFFER, gs->framebuffer);
	
	
	// clear color buffer for actual rendering
	//glClear(GL_COLOR_BUFFER_BIT);
	glerr("pre shader create 1d");
	glDepthFunc(GL_LEQUAL);
	glerr("pre shader create e");
	
	renderFrame(xs, gs, is);

	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	glBindFramebuffer(GL_READ_FRAMEBUFFER, gs->framebuffer);
	
	
	// draw to the screen
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	
	shadingPass(gs);
	
	renderUI(xs, gs);
	
	gs->screen.resized = 0;
	
	glXSwapBuffers(xs->display, xs->clientWin);

}
Example #3
0
GLShaderImpl::GLShaderImpl(GLenum shader_type) {
  m_is_compiled = false;
  m_need_recompile = false;
  m_shader_id = glCreateShader(static_cast<GLenum>(shader_type));
  glerr();

  m_watcher.RegisterObserver(this);
}
Example #4
0
static int make_shader(struct shader *shader)
{
	GLint res = GL_FALSE;
	GLsizei len = 0;
	GLchar log[4096] = "nil";/* FIXME: some drivers return wrong values for
				 * GL_INFO_LOG_LENGTH use a fixed size instead
				 */
	timeinit();

	if (!shader) {
		errno = EFAULT;
		return -1;
	}

	timestart();

	shader->id = glCreateShader(shader->type);
	if (shader->id == 0) {
		glerr("glCreateShader(%s) failed", shader->name);
		goto error;
	}

	glShaderSource(shader->id, 1, &shader->code, NULL);
	glCompileShader(shader->id);
	glGetShaderiv(shader->id, GL_COMPILE_STATUS, &res);
	if (res != GL_TRUE) {
		glerr("glCompileShader(%s) failed", shader->name);
		goto error;
	}

	timestop("compiled shader %s, id %d type 0x%04x\n", shader->name,
		 shader->id, shader->type);
	return 0;

error:
	if (shader->id > 0) {
		glGetShaderInfoLog(shader->id, sizeof(log), &len, log);
		_msg("len: %d, log: %s\n", len, log);
		glDeleteShader(shader->id);
		shader->id = 0;
	}
	return -1;
}
Example #5
0
int program_make(struct program *prog, struct shader **shader, int num)
{
	int i;
	GLchar log[256] = "nil";
	GLsizei len = 0;
	GLint res = GL_FALSE;

	if (!prog) {
		errno = EFAULT;
		return -1;
	}

	timeinit();

	prog->id = glCreateProgram();
	if (prog->id == 0) {
		glerr("glCreateProgram() failed");
		return -1;
	}

	prog->shader = shader;
	prog->shader_num = num;

	for (i = 0; i < num; i++) {
		if (make_shader(shader[i]) < 0) {
			_err("shader %s failed\n", shader[i]->name);
			goto err;
		}
		glAttachShader(prog->id, shader[i]->id);
	}

	timestart();
	glLinkProgram(prog->id);
	glGetProgramiv(prog->id, GL_LINK_STATUS, &res);
	if (res != GL_TRUE) {
		_err("failed to link program\n");
		goto err;
	}

	timestop("program %d linked\n", prog->id);
	return prog->id;

err:
	if (prog->id > 0) {
		glGetProgramInfoLog(prog->id, sizeof (log), &len, log);
		_msg("len: %d, log: %s\n", len, log);
	}

	program_clean(prog);
	return -1;
}
Example #6
0
void initGame(XStuff* xs, GameState* gs) {
	int ww, wh;
	
	glerr("left over error on game init");
	
	
	gs->activeTool = 0;
	
	gs->debugMode = 0;
	gs->sunSpeed = 0;
	
	
	ww = xs->winAttr.width;
	wh = xs->winAttr.height;
	
	gs->screen.wh.x = (float)ww;
	gs->screen.wh.y = (float)wh;
	
	gs->screen.aspect = gs->screen.wh.x / gs->screen.wh.y;
	gs->screen.resized = 0;
	
	
	printf("w: %d, h: %d\n", ww, wh);
	
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	
	initTexBuffers(gs, 0);

	glBindTexture(GL_TEXTURE_2D, 0);
	
	glGenFramebuffers(1, &gs->framebuffer);
	glBindFramebuffer(GL_FRAMEBUFFER, gs->framebuffer);
	glexit("fbo creation");
	
	// The depth buffer
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gs->diffuseTexBuffer, 0);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, gs->normalTexBuffer, 0);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, gs->selectionTexBuffer, 0);
 	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, gs->depthTexBuffer, 0);
	glexit("fb tex2d");
	
//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, gs->depthTexBuffer, 0);
	
	GLenum DrawBuffers[] = {
		GL_COLOR_ATTACHMENT0,
		GL_COLOR_ATTACHMENT1,
		GL_COLOR_ATTACHMENT2
	};
	glDrawBuffers(3, DrawBuffers);
	glexit("drawbuffers");
	
	GLenum status;
	status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if(status != GL_FRAMEBUFFER_COMPLETE) {
		printf("fbo status invalid\n");
		exit(1);
	}
	printf("FBO created.\n");
	

	shadingProg = loadCombinedProgram("shading");
	initFSQuad();
	
	// check some prerequisites
// 	GLint MaxDrawBuffers = 0;
// 	glGetIntegerv(GL_MAX_DRAW_BUFFERS, &MaxDrawBuffers);
// 	printf("MAX_DRAW_BUFFERS: %d\n", MaxDrawBuffers);
//  	if(MaxDrawBuffers < 4) {
// 		fprintf(stderr, "FATAL: GL_MAX_DRAW_BUFFERS is too low: %d. Minimum required value is 4.\n", MaxDrawBuffers);
// 		exit(3);
// 	};

	getPrintGLEnum(GL_MAX_COLOR_ATTACHMENTS, "meh");
	getPrintGLEnum(GL_MAX_DRAW_BUFFERS, "meh");
	getPrintGLEnum(GL_MAX_FRAMEBUFFER_WIDTH, "meh");
	getPrintGLEnum(GL_MAX_FRAMEBUFFER_HEIGHT, "meh");
	getPrintGLEnum(GL_MAX_FRAMEBUFFER_LAYERS, "meh");
	getPrintGLEnum(GL_MAX_FRAMEBUFFER_SAMPLES, "meh");
	getPrintGLEnum(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, "meh");
	getPrintGLEnum(GL_MAX_TESS_EVALUATION_TEXTURE_IMAGE_UNITS, "meh");
	getPrintGLEnum(GL_MAX_GEOMETRY_TEXTURE_IMAGE_UNITS, "meh");
	getPrintGLEnum(GL_MAX_TEXTURE_IMAGE_UNITS, "meh");
	getPrintGLEnum(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, "meh");
	getPrintGLEnum(GL_MAX_ARRAY_TEXTURE_LAYERS, "meh");
	getPrintGLEnum(GL_MAX_SAMPLES, "meh");
	
	
	// set up matrix stacks
	MatrixStack* view, *proj;
	
	view = &gs->view;
	proj = &gs->proj;
	
	msAlloc(2, view);
	msAlloc(2, proj);

	msIdent(view);
	msIdent(proj);
	

	
	
	// perspective matrix, pretty standard
// 	msPerspective(60, 1.0, 01000.0f, 100000.0f, proj);
// 		msOrtho(0, 1, 0, 1, .01, 100000, proj);

	gs->zoom = -760.0;
	gs->direction = 0.0f;
	gs->lookCenter.x = 128;
	gs->lookCenter.y = 128;
	
	
	// initialize all those magic globals
	initMap(&gs->map);
	
	initUI(gs);
	initMarker();
	
	// text rendering stuff
	arial = LoadFont("Arial", 64, NULL);
	glerr("clearing before text program load");
	textProg = loadCombinedProgram("text");
	
	unsigned int colors[] = {
		0xFF0000FF, 2,
		0x00FF00FF, 4,
		0x0000FFFF, INT_MAX
	};
	
	strRI = prepareText(arial, "FPS: --", -1, colors);
	/*
	OBJContents cube;
	loadOBJFile("assets/models/untitled.obj", 0, &cube);
	Mesh* cubem = OBJtoMesh(&cube);
	*/
	
}
Example #7
0
GLShaderImpl::~GLShaderImpl() {
  glDeleteShader(m_shader_id);
  glerr();

  m_watcher.UnregisterObserver(this);
}
Example #8
0
static bool
borisgl_initialize(int width, int height, const char *title)
{
	int screen;
	Window root;
	XVisualInfo *xvi;
	int fbcount;
	GLXFBConfig *fbconfiglist, fbconfig;
	int i;
	const int glx_attribs[] = {
		GLX_X_RENDERABLE, True,
		GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
		GLX_RENDER_TYPE, GLX_RGBA_BIT,
		GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
		GLX_ALPHA_SIZE, 8,
		GLX_DEPTH_SIZE, 1,
		GLX_DOUBLEBUFFER, True,
		None
	};
	static int ctx_attribs[] = {
#if USE_GLES2
	        GLX_CONTEXT_MAJOR_VERSION_ARB, 2,
	        GLX_CONTEXT_MINOR_VERSION_ARB, 0,
		GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_ES2_PROFILE_BIT_EXT,
#else
	        GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
	        GLX_CONTEXT_MINOR_VERSION_ARB, 0,
		GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
		/* GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, */
#endif
		None
	};

	dpy = XOpenDisplay(NULL);
	if (!dpy)
		return false;

	wm_protocols = XInternAtom(dpy, "WM_PROTOCOLS", False);
	wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", 0);

	screen = DefaultScreen(dpy);
	root = RootWindow(dpy, screen);

	fbconfiglist = glXChooseFBConfig(dpy, screen, glx_attribs, &fbcount);
	if (fbcount <= 0) {
		pr_err("No valid FB configurations");
		return 1;
	}
	for (i = 0; i < fbcount; i++) {
		xvi = glXGetVisualFromFBConfig(dpy, fbconfiglist[i]);
		if (!xvi)
			continue;
		fbconfig = fbconfiglist[i];
		goto found_fbconfig;
	}
	pr_err("No valid FB configurations");
	return 1;
found_fbconfig:
	XFree(fbconfiglist);

	XSetWindowAttributes wattr;
	wattr.event_mask = StructureNotifyMask | KeyPressMask;
	wattr.background_pixmap = None;
	wattr.background_pixel = 0;
	wattr.border_pixel = 0;
	wattr.colormap = XCreateColormap(dpy, root, xvi->visual, AllocNone);

	win = XCreateWindow(dpy, root,
			(DisplayWidth(dpy, screen) - width) / 2, (DisplayHeight(dpy, screen) - height) / 2,
			width, height, 0, xvi->depth, InputOutput, xvi->visual,
			CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask, &wattr);

	if (!win) {
		pr_err("Unable to create window");
		return false;
	}

	/* setup various window manager(WM) metadata */

	if (title)
		XStoreName(dpy, win, title);

	XSizeHints sizehints;
	sizehints.flags = PPosition | PBaseSize | PMinSize | PMaxSize | PResizeInc | PAspect;
	sizehints.x = 200;
	sizehints.y = 100;
	sizehints.base_width = width;
	sizehints.base_height = height;
	sizehints.min_width = width / 8;
	sizehints.min_height = height / 8;
	sizehints.max_width = width * 8 ;
	sizehints.max_height = height * 8;
	sizehints.width_inc = 16;
	sizehints.height_inc = 16;
	sizehints.min_aspect.x = width;
	sizehints.min_aspect.y = height;
	sizehints.max_aspect.x = width;
	sizehints.max_aspect.y = height;
	XSetWMNormalHints(dpy, win, &sizehints );

	XSetWMProtocols(dpy, win, &wm_delete_window, 1);

	/* make a temporary context to find glXCreateContextAttribsARB() */
	GLXContext ctx_tmp = glXCreateContext(dpy, xvi, 0, True);
	if (!ctx_tmp) {
		pr_err("Failed to allocate legacy GL context!");
		goto failed;
	}
	glXMakeCurrent(dpy, win, ctx_tmp);
	PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB =
		(PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");
	if (!glXCreateContextAttribsARB) {
		pr_err("Failed to bind glXCreateContextAttribsARB!");
		glXMakeCurrent(dpy, None, 0);
		glXDestroyContext(dpy, ctx_tmp);
		goto failed_destroy_context;
	}
	glXMakeCurrent(dpy, None, 0);
	glXDestroyContext(dpy, ctx_tmp);

	/* now that we have glXCreateContextAttribsARB, we can make the real context */
	ctx = glXCreateContextAttribsARB(dpy, fbconfig, 0, True, ctx_attribs);
	if (!ctx) {
		pr_err("Failed to allocate GL context!");
		goto failed;
	}
	XSync(dpy, False);
	glXMakeCurrent(dpy, win, ctx);

	pr_info("GL_VERSION=%s", glGetString(GL_VERSION));
	pr_info("GL_RENDERER=%s", glGetString(GL_RENDERER));
#if 0 // not supported/broken on one of my systems
	GLint gl_major = 0, gl_minor = 0;
	glGetIntegerv(GL_MAJOR_VERSION, &gl_major);
	glerr("GL_MAJOR_VERSION");
	glGetIntegerv(GL_MINOR_VERSION, &gl_minor);
	inf("GL_VERSION_MAJOR=%d GL_VERSION_MINOR=%d", (int)gl_major, (int)gl_minor);
#endif

	PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddress((const GLubyte*)"glXSwapIntervalEXT");
	if (!glXSwapIntervalEXT) {
		pr_err("Failed to bind glXCreateContextAttribsARB!");
		goto failed_destroy_context;
	}
	glXSwapIntervalEXT(dpy, win, 1);

	/* done - we can show the window */
	XMapRaised(dpy, win);
	XFlush(dpy);

	/* report to the game that we are ready */
	game_initialize();

	return true;
failed_destroy_context:
	glXMakeCurrent(dpy, None, 0);
	glXDestroyContext(dpy, ctx);
failed:
	XDestroyWindow(dpy, win);
	return false;
}