aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/g3dviewer-0.2.99.4/thumbnailer/g3d-thumbnailer.c
blob: 92acd0b4576162da129f86faed8999ad07bdcbb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* $Id: g3d-thumbnailer.c 61 2006-11-09 15:31:12Z mmmaddd $ */

/*
	G3DViewer - 3D object viewer

	Copyright (C) 2005, 2006  Markus Dahms <mad@automagically.de>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdio.h>
#include <stdlib.h>

#include <X11/Xlib.h>
#include <GL/glx.h>

#include <gtk/gtk.h>
#include <glib.h>
#include <g3d/g3d.h>

#include "gl.h"
#include "trackball.h"
#include "screenshot.h"
#include "texture.h"

static gboolean setup_gl(guint32 width, guint32 height)
{
	Display *display;
	XVisualInfo *visinfo;
	Pixmap pixmap;
	GLXPixmap glxpixmap;
	GLXContext glxctx;
	char *dpyname;
	int attrlist_dbl[] = {
		GLX_RGBA,
		GLX_DOUBLEBUFFER,
		GLX_RED_SIZE, 1,
		GLX_GREEN_SIZE, 1,
		GLX_BLUE_SIZE, 1,
		None};
	int attrlist_sng[] = {
		GLX_RGBA,
		GLX_RED_SIZE, 1,
		GLX_GREEN_SIZE, 1,
		GLX_BLUE_SIZE, 1,
		None};

	dpyname = getenv("DISPLAY");

	display = XOpenDisplay(dpyname);
	if(display == NULL)
	{
		g_printerr("ERROR: could not open display '%s'\n",
			dpyname ? dpyname : "(null)");
		return FALSE;
	}

	visinfo = glXChooseVisual(display, DefaultScreen(display), attrlist_sng);
	if(visinfo == NULL)
	{
		/* try with double buffering */
		visinfo = glXChooseVisual(display, DefaultScreen(display),
			attrlist_dbl);
		if(visinfo == NULL)
		{
			g_printerr("ERROR: could not get a supported visual\n");
			return FALSE;
		}
	}

	glxctx = glXCreateContext(display, visinfo, 0, GL_FALSE);
	if(glxctx == NULL)
	{
		g_printerr("ERROR: could not create GLX context\n");
		return FALSE;
	}

	pixmap = XCreatePixmap(display, RootWindow(display, visinfo->screen),
		width, height, visinfo->depth);
	if(pixmap <= 0)
	{
		g_printerr("ERROR: could not create pixmap\n");
		return FALSE;
	}

	glxpixmap = glXCreateGLXPixmap(display, visinfo, pixmap);
	if(glxpixmap <= 0)
	{
		g_printerr("ERROR: could not create GLX pixmap\n");
		return FALSE;
	}

	glXMakeCurrent(display, glxpixmap, glxctx);

	return TRUE;
}

int main(int argc, char *argv[])
{
	G3DContext *context;
	G3DModel *model;
	gfloat bgcolor[4] = { 1.0, 1.0, 1.0, 0.0 };
	gfloat quat[4] = { 0.10, -0.31, -0.87, 0.38 };
	guint32 width = 128;
	guint32 height = 128;

	gtk_init(&argc, &argv);

	setup_gl(width, height);

	if(argc < 3)
	{
		g_print("usage: %s <input file: model> <output file: image> "
			"[<width in px>]\n",
			argv[0]);
		return EXIT_FAILURE;
	}

	if(argc > 3)
	{
		/* size */
		width = atoi(argv[3]);
		/* height = width / 4 * 3; */
		height = width;
	}

	context = g3d_context_new();
	model = g3d_model_load(context, argv[1]);

	if(model)
	{
		texture_load_all_textures(model);

		gl_draw(
			G3D_FLAG_GL_SHININESS | G3D_FLAG_GL_ALLTWOSIDE |
			G3D_FLAG_GL_TEXTURES,
			45 /* zoom */,
			(gfloat)width / (gfloat)height,
			bgcolor,
			quat,
			model);

		glXWaitGL();

		if(screenshot_save(argv[2], width, height))
		{
			return EXIT_SUCCESS;
		}
	}

	return EXIT_FAILURE;
}