File: 3Dcontext_common.c

package info (click to toggle)
pdp 1%3A0.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,904 kB
  • ctags: 4,405
  • sloc: ansic: 22,321; asm: 2,088; makefile: 551; perl: 145; sh: 93; cpp: 4
file content (180 lines) | stat: -rw-r--r-- 4,489 bytes parent folder | download | duplicates (4)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

/*
 *   zl - pbuffer packet implementation
 *   Copyright (c) by Tom Schouten <tom@zwizwa.be>
 *
 *   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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*
  this code uses glx. i don't know if it is worth to take into
  account portabiliy. since it will take a while until pdp runs
  on anything else than linux. but in any case, providing a windows/osx
  implementation here should not be too difficult..
*/

#include "zl/3Dcontext.h"
#include <GL/gl.h>
#include <GL/glu.h>

#define D if (0)

/* constructor */

/* pbuf operators */

uint32_t zl_3Dcontext_width(zl_3Dcontext *c) {
    if (c) return c->width;
    else return 0;
}

uint32_t zl_3Dcontext_height(zl_3Dcontext *c) {
    if (c) return c->height;
    else return 0;
}

uint32_t zl_3Dcontext_subwidth(zl_3Dcontext *c) {
    if (c) return c->sub_width;
    else return 0;
}


uint32_t zl_3Dcontext_subheight(zl_3Dcontext *c) {
    if (c) return c->sub_height;
    else return 0;
}


void  zl_3Dcontext_set_subwidth(zl_3Dcontext *c, uint32_t w) {
    if (c) c->sub_width = w;
}


void zl_3Dcontext_set_subheight(zl_3Dcontext *c, uint32_t h) {
    if (c)  c->sub_height = h;
}


float zl_3Dcontext_subaspect(zl_3Dcontext *c) {
    if (c) return (float)c->sub_width/c->sub_height;
    else return 0;
}


void zl_3Dcontext_snap_to_bitmap(zl_3Dcontext *c, int w, int h, void *_data) {
    int x, y;
    char *data = _data;

    x = zl_3Dcontext_subwidth(c);
    y = zl_3Dcontext_subheight(c);

    // ???
    x = (x - w) >> 1;
    y = (y - h) >> 1;
    x = (x < 0 ) ? 0 : x;
    y = (y < 0 ) ? 0 : y;

    zl_3Dcontext_set_rendering_context(c);

    glReadPixels(x,y, w ,h,GL_RGB,GL_UNSIGNED_BYTE, data);
}




/* setup for 2d operation from pbuf dimensions */
void zl_3Dcontext_setup_2d_context(zl_3Dcontext *c) {
    uint32_t w;
    uint32_t h;
    float asp;
    w = zl_3Dcontext_subwidth(c);
    h = zl_3Dcontext_subheight(c);
    asp = zl_3Dcontext_subaspect(c);


    /* set the viewport to the size of the sub frame */
    glViewport(0, 0, w, h);

    /* set orthogonal projection, with a relative frame size of (2asp x 2) */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, 2*asp, 0, 2);

    /* set the center of view */
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(asp, 1, 0);
    glScalef(1,-1,1);


}

/* setup for 3d operation from pbuf dimensions */
void zl_3Dcontext_setup_3d_context(zl_3Dcontext *c) {
    uint32_t w;
    uint32_t h;
    int i;
    float asp;
    float m_perspect[] = {-1.f, /* left */
			  1.f,  /* right */
			  -1.f, /* bottom */
			  1.f,  /* top */
			  1.f,  /* front */
			  20.f};/* back */

    w = zl_3Dcontext_subwidth(c);
    h = zl_3Dcontext_subheight(c);
    asp = zl_3Dcontext_subaspect(c);


    /* set the viewport to the size of the sub frame */
    glViewport(0, 0, w, h);

    /* set orthogonal projection, with a relative frame size of (2asp x 2) */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(m_perspect[0] * asp, m_perspect[1] * asp,       // left, right
	      m_perspect[2], m_perspect[3],                   // bottom, top
	      m_perspect[4], m_perspect[5]);                  // front, back
    
    /* reset texture matrix */
    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();


    /* set the center of view */
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 0, 4, 0, 0, 0, 0, 1, 0);
    //glTranslatef(asp, 1, 0);


    glEnable(GL_DEPTH_TEST);
    glEnable(GL_AUTO_NORMAL);
    glEnable(GL_NORMALIZE);
    glShadeModel(GL_SMOOTH);
    //glShadeModel(GL_FLAT);


    /* disable everything that is enabled in other modules
     this resets the ogl state to its initial conditions */
    glDisable(GL_LIGHTING);
    for (i=0; i<8; i++) glDisable(GL_LIGHT0 + i);
    glDisable(GL_COLOR_MATERIAL);


}