7d29257c4979b5eb477fc7a3eaf1d102e773d592
[mesa.git] / src / mesa / main / drawpix.c
1 /* $Id: drawpix.c,v 1.55 2001/06/26 01:32:48 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 #ifdef PC_HEADER
29 #include "all.h"
30 #else
31 #include "glheader.h"
32 #include "colormac.h"
33 #include "context.h"
34 #include "drawpix.h"
35 #include "feedback.h"
36 #include "macros.h"
37 #include "mem.h"
38 #include "mmath.h"
39 #include "state.h"
40 #include "mtypes.h"
41 #endif
42
43
44
45
46
47 /*
48 * Execute glDrawPixels
49 */
50 void
51 _mesa_DrawPixels( GLsizei width, GLsizei height,
52 GLenum format, GLenum type, const GLvoid *pixels )
53 {
54 GET_CURRENT_CONTEXT(ctx);
55 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
56
57 if (ctx->RenderMode==GL_RENDER) {
58 GLint x, y;
59 if (!pixels || !ctx->Current.RasterPosValid) {
60 return;
61 }
62
63 if (ctx->NewState) {
64 _mesa_update_state(ctx);
65 }
66
67 #if 1
68 x = IROUND(ctx->Current.RasterPos[0]);
69 y = IROUND(ctx->Current.RasterPos[1]);
70 #else
71 x = IFLOOR(ctx->Current.RasterPos[0]);
72 y = IFLOOR(ctx->Current.RasterPos[1]);
73 #endif
74 ctx->OcclusionResult = GL_TRUE;
75 ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
76 &ctx->Unpack, pixels);
77 }
78 else if (ctx->RenderMode==GL_FEEDBACK) {
79 if (ctx->Current.RasterPosValid) {
80 GLfloat texcoord[4], invq;
81
82 FLUSH_CURRENT(ctx, 0);
83 invq = 1.0F / ctx->Current.Texcoord[0][3];
84 texcoord[0] = ctx->Current.Texcoord[0][0] * invq;
85 texcoord[1] = ctx->Current.Texcoord[0][1] * invq;
86 texcoord[2] = ctx->Current.Texcoord[0][2] * invq;
87 texcoord[3] = ctx->Current.Texcoord[0][3];
88 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
89 _mesa_feedback_vertex( ctx,
90 ctx->Current.RasterPos,
91 ctx->Current.Color,
92 ctx->Current.Index,
93 texcoord );
94 }
95 }
96 else if (ctx->RenderMode==GL_SELECT) {
97 if (ctx->Current.RasterPosValid) {
98 _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
99 }
100 }
101 }
102
103
104
105 void
106 _mesa_ReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
107 GLenum format, GLenum type, GLvoid *pixels )
108 {
109 GET_CURRENT_CONTEXT(ctx);
110 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
111
112 if (!pixels) {
113 _mesa_error( ctx, GL_INVALID_VALUE, "glReadPixels(pixels)" );
114 return;
115 }
116
117 if (ctx->NewState)
118 _mesa_update_state(ctx);
119
120 ctx->Driver.ReadPixels(ctx, x, y, width, height,
121 format, type, &ctx->Pack, pixels);
122
123 }
124
125
126
127 void
128 _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
129 GLenum type )
130 {
131 GET_CURRENT_CONTEXT(ctx);
132 GLint destx, desty;
133 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
134
135 if (width < 0 || height < 0) {
136 _mesa_error( ctx, GL_INVALID_VALUE, "glCopyPixels" );
137 return;
138 }
139
140 if (ctx->NewState) {
141 _mesa_update_state(ctx);
142 }
143
144 if (ctx->RenderMode==GL_RENDER) {
145 /* Destination of copy: */
146 if (!ctx->Current.RasterPosValid) {
147 return;
148 }
149 #if 1
150 destx = IROUND(ctx->Current.RasterPos[0]);
151 desty = IROUND(ctx->Current.RasterPos[1]);
152 #else
153 destx = IFLOOR(ctx->Current.RasterPos[0]);
154 desty = IFLOOR(ctx->Current.RasterPos[1]);
155 #endif
156 ctx->OcclusionResult = GL_TRUE;
157
158 ctx->Driver.CopyPixels( ctx, srcx, srcy, width, height, destx, desty,
159 type );
160 }
161 else if (ctx->RenderMode == GL_FEEDBACK) {
162 FLUSH_CURRENT( ctx, 0 );
163 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_COPY_PIXEL_TOKEN );
164 _mesa_feedback_vertex( ctx,
165 ctx->Current.RasterPos,
166 ctx->Current.Color,
167 ctx->Current.Index,
168 ctx->Current.Texcoord[0] );
169 }
170 else if (ctx->RenderMode == GL_SELECT) {
171 _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
172 }
173 }
174
175
176
177 void
178 _mesa_Bitmap( GLsizei width, GLsizei height,
179 GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove,
180 const GLubyte *bitmap )
181 {
182 GET_CURRENT_CONTEXT(ctx);
183 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
184
185
186 /* Error checking */
187 if (width < 0 || height < 0) {
188 _mesa_error( ctx, GL_INVALID_VALUE, "glBitmap" );
189 return;
190 }
191
192 if (ctx->Current.RasterPosValid == GL_FALSE) {
193 return; /* do nothing */
194 }
195
196 if (ctx->RenderMode==GL_RENDER) {
197 if (bitmap) {
198 #if 0
199 GLint x = (GLint) ( (ctx->Current.RasterPos[0] - xorig) + 0.0F );
200 GLint y = (GLint) ( (ctx->Current.RasterPos[1] - yorig) + 0.0F );
201 #else
202 GLint x = IFLOOR(ctx->Current.RasterPos[0] - xorig);
203 GLint y = IFLOOR(ctx->Current.RasterPos[1] - yorig);
204 #endif
205 if (ctx->NewState) {
206 _mesa_update_state(ctx);
207 }
208
209 ctx->OcclusionResult = GL_TRUE;
210 ctx->Driver.Bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap );
211 }
212 }
213 else if (ctx->RenderMode==GL_FEEDBACK) {
214 GLfloat color[4], texcoord[4], invq;
215
216 color[0] = ctx->Current.RasterColor[0];
217 color[1] = ctx->Current.RasterColor[1];
218 color[2] = ctx->Current.RasterColor[2];
219 color[3] = ctx->Current.RasterColor[3];
220 if (ctx->Current.Texcoord[0][3] == 0.0)
221 invq = 1.0F;
222 else
223 invq = 1.0F / ctx->Current.RasterTexCoord[3];
224 texcoord[0] = ctx->Current.RasterTexCoord[0] * invq;
225 texcoord[1] = ctx->Current.RasterTexCoord[1] * invq;
226 texcoord[2] = ctx->Current.RasterTexCoord[2] * invq;
227 texcoord[3] = ctx->Current.RasterTexCoord[3];
228 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN );
229 _mesa_feedback_vertex( ctx,
230 ctx->Current.RasterPos,
231 color, ctx->Current.RasterIndex, texcoord );
232 }
233 else if (ctx->RenderMode==GL_SELECT) {
234 /* Bitmaps don't generate selection hits. See appendix B of 1.1 spec. */
235 }
236
237 /* update raster position */
238 ctx->Current.RasterPos[0] += xmove;
239 ctx->Current.RasterPos[1] += ymove;
240 }