963eb06b44d261f1232151cb7140805947f5e5df
[mesa.git] / src / mesa / main / drawpix.c
1 /* $Id: drawpix.c,v 1.54 2001/06/18 17:26:08 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 x = IROUND(ctx->Current.RasterPos[0]);
68 y = IROUND(ctx->Current.RasterPos[1]);
69
70 ctx->OcclusionResult = GL_TRUE;
71 ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
72 &ctx->Unpack, pixels);
73 }
74 else if (ctx->RenderMode==GL_FEEDBACK) {
75 if (ctx->Current.RasterPosValid) {
76 GLfloat texcoord[4], invq;
77
78 FLUSH_CURRENT(ctx, 0);
79 invq = 1.0F / ctx->Current.Texcoord[0][3];
80 texcoord[0] = ctx->Current.Texcoord[0][0] * invq;
81 texcoord[1] = ctx->Current.Texcoord[0][1] * invq;
82 texcoord[2] = ctx->Current.Texcoord[0][2] * invq;
83 texcoord[3] = ctx->Current.Texcoord[0][3];
84 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
85 _mesa_feedback_vertex( ctx,
86 ctx->Current.RasterPos,
87 ctx->Current.Color,
88 ctx->Current.Index,
89 texcoord );
90 }
91 }
92 else if (ctx->RenderMode==GL_SELECT) {
93 if (ctx->Current.RasterPosValid) {
94 _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
95 }
96 }
97 }
98
99
100
101 void
102 _mesa_ReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
103 GLenum format, GLenum type, GLvoid *pixels )
104 {
105 GET_CURRENT_CONTEXT(ctx);
106 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
107
108 if (!pixels) {
109 _mesa_error( ctx, GL_INVALID_VALUE, "glReadPixels(pixels)" );
110 return;
111 }
112
113 if (ctx->NewState)
114 _mesa_update_state(ctx);
115
116 ctx->Driver.ReadPixels(ctx, x, y, width, height,
117 format, type, &ctx->Pack, pixels);
118
119 }
120
121
122
123 void
124 _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
125 GLenum type )
126 {
127 GET_CURRENT_CONTEXT(ctx);
128 GLint destx, desty;
129 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
130
131 if (width < 0 || height < 0) {
132 _mesa_error( ctx, GL_INVALID_VALUE, "glCopyPixels" );
133 return;
134 }
135
136 if (ctx->NewState) {
137 _mesa_update_state(ctx);
138 }
139
140 if (ctx->RenderMode==GL_RENDER) {
141 /* Destination of copy: */
142 if (!ctx->Current.RasterPosValid) {
143 return;
144 }
145 destx = IROUND(ctx->Current.RasterPos[0]);
146 desty = IROUND(ctx->Current.RasterPos[1]);
147
148 ctx->OcclusionResult = GL_TRUE;
149
150 ctx->Driver.CopyPixels( ctx, srcx, srcy, width, height, destx, desty,
151 type );
152 }
153 else if (ctx->RenderMode == GL_FEEDBACK) {
154 FLUSH_CURRENT( ctx, 0 );
155 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_COPY_PIXEL_TOKEN );
156 _mesa_feedback_vertex( ctx,
157 ctx->Current.RasterPos,
158 ctx->Current.Color,
159 ctx->Current.Index,
160 ctx->Current.Texcoord[0] );
161 }
162 else if (ctx->RenderMode == GL_SELECT) {
163 _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
164 }
165 }
166
167
168
169 void
170 _mesa_Bitmap( GLsizei width, GLsizei height,
171 GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove,
172 const GLubyte *bitmap )
173 {
174 GET_CURRENT_CONTEXT(ctx);
175 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
176
177
178 /* Error checking */
179 if (width < 0 || height < 0) {
180 _mesa_error( ctx, GL_INVALID_VALUE, "glBitmap" );
181 return;
182 }
183
184 if (ctx->Current.RasterPosValid == GL_FALSE) {
185 return; /* do nothing */
186 }
187
188 if (ctx->RenderMode==GL_RENDER) {
189 if (bitmap) {
190 GLint x = (GLint) ( (ctx->Current.RasterPos[0] - xorig) + 0.0F );
191 GLint y = (GLint) ( (ctx->Current.RasterPos[1] - yorig) + 0.0F );
192
193 if (ctx->NewState) {
194 _mesa_update_state(ctx);
195 }
196
197 ctx->OcclusionResult = GL_TRUE;
198 ctx->Driver.Bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap );
199 }
200 }
201 else if (ctx->RenderMode==GL_FEEDBACK) {
202 GLfloat color[4], texcoord[4], invq;
203
204 color[0] = ctx->Current.RasterColor[0];
205 color[1] = ctx->Current.RasterColor[1];
206 color[2] = ctx->Current.RasterColor[2];
207 color[3] = ctx->Current.RasterColor[3];
208 if (ctx->Current.Texcoord[0][3] == 0.0)
209 invq = 1.0F;
210 else
211 invq = 1.0F / ctx->Current.RasterTexCoord[3];
212 texcoord[0] = ctx->Current.RasterTexCoord[0] * invq;
213 texcoord[1] = ctx->Current.RasterTexCoord[1] * invq;
214 texcoord[2] = ctx->Current.RasterTexCoord[2] * invq;
215 texcoord[3] = ctx->Current.RasterTexCoord[3];
216 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN );
217 _mesa_feedback_vertex( ctx,
218 ctx->Current.RasterPos,
219 color, ctx->Current.RasterIndex, texcoord );
220 }
221 else if (ctx->RenderMode==GL_SELECT) {
222 /* Bitmaps don't generate selection hits. See appendix B of 1.1 spec. */
223 }
224
225 /* update raster position */
226 ctx->Current.RasterPos[0] += xmove;
227 ctx->Current.RasterPos[1] += ymove;
228 }