ac1c68af5ed3020a87389223f41c5ee1585855c1
[mesa.git] / src / mesa / main / drawpix.c
1 /* $Id: drawpix.c,v 1.58 2001/12/14 02:55:08 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
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 * Execute glDrawPixels
47 */
48 void
49 _mesa_DrawPixels( GLsizei width, GLsizei height,
50 GLenum format, GLenum type, const GLvoid *pixels )
51 {
52 GET_CURRENT_CONTEXT(ctx);
53 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
54
55 if (ctx->RenderMode==GL_RENDER) {
56 GLint x, y;
57 if (!pixels || !ctx->Current.RasterPosValid) {
58 return;
59 }
60
61 if (ctx->NewState) {
62 _mesa_update_state(ctx);
63 }
64
65 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
66 x = IROUND(ctx->Current.RasterPos[0]);
67 y = IROUND(ctx->Current.RasterPos[1]);
68
69 ctx->OcclusionResult = GL_TRUE;
70 ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
71 &ctx->Unpack, pixels);
72 }
73 else if (ctx->RenderMode==GL_FEEDBACK) {
74 /* Feedback the current raster pos info */
75 if (ctx->Current.RasterPosValid) {
76 FLUSH_CURRENT( ctx, 0 );
77 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
78 _mesa_feedback_vertex( ctx,
79 ctx->Current.RasterPos,
80 ctx->Current.RasterColor,
81 ctx->Current.RasterIndex,
82 ctx->Current.RasterTexCoord );
83 }
84 }
85 else if (ctx->RenderMode==GL_SELECT) {
86 if (ctx->Current.RasterPosValid) {
87 _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
88 }
89 }
90 }
91
92
93
94 void
95 _mesa_ReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
96 GLenum format, GLenum type, GLvoid *pixels )
97 {
98 GET_CURRENT_CONTEXT(ctx);
99 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
100
101 if (!pixels) {
102 _mesa_error( ctx, GL_INVALID_VALUE, "glReadPixels(pixels)" );
103 return;
104 }
105
106 if (ctx->NewState)
107 _mesa_update_state(ctx);
108
109 ctx->Driver.ReadPixels(ctx, x, y, width, height,
110 format, type, &ctx->Pack, pixels);
111
112 }
113
114
115
116 void
117 _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
118 GLenum type )
119 {
120 GET_CURRENT_CONTEXT(ctx);
121 GLint destx, desty;
122 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
123
124 if (width < 0 || height < 0) {
125 _mesa_error( ctx, GL_INVALID_VALUE, "glCopyPixels" );
126 return;
127 }
128
129 if (ctx->NewState) {
130 _mesa_update_state(ctx);
131 }
132
133 if (ctx->RenderMode==GL_RENDER) {
134 /* Destination of copy: */
135 if (!ctx->Current.RasterPosValid) {
136 return;
137 }
138
139 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
140 destx = IROUND(ctx->Current.RasterPos[0]);
141 desty = IROUND(ctx->Current.RasterPos[1]);
142
143 ctx->OcclusionResult = GL_TRUE;
144
145 ctx->Driver.CopyPixels( ctx, srcx, srcy, width, height, destx, desty,
146 type );
147 }
148 else if (ctx->RenderMode == GL_FEEDBACK) {
149 if (ctx->Current.RasterPosValid) {
150 FLUSH_CURRENT( ctx, 0 );
151 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_COPY_PIXEL_TOKEN );
152 _mesa_feedback_vertex( ctx,
153 ctx->Current.RasterPos,
154 ctx->Current.RasterColor,
155 ctx->Current.RasterIndex,
156 ctx->Current.RasterTexCoord );
157 }
158 }
159 else if (ctx->RenderMode == GL_SELECT) {
160 _mesa_update_hitflag( ctx, ctx->Current.RasterPos[2] );
161 }
162 }
163
164
165
166 void
167 _mesa_Bitmap( GLsizei width, GLsizei height,
168 GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove,
169 const GLubyte *bitmap )
170 {
171 GET_CURRENT_CONTEXT(ctx);
172 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
173
174 /* Error checking */
175 if (width < 0 || height < 0) {
176 _mesa_error( ctx, GL_INVALID_VALUE, "glBitmap" );
177 return;
178 }
179
180 if (ctx->Current.RasterPosValid == GL_FALSE) {
181 return; /* do nothing */
182 }
183
184 if (ctx->RenderMode==GL_RENDER) {
185 if (bitmap) {
186 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
187 GLint x = IFLOOR(ctx->Current.RasterPos[0] - xorig);
188 GLint y = IFLOOR(ctx->Current.RasterPos[1] - yorig);
189
190 if (ctx->NewState) {
191 _mesa_update_state(ctx);
192 }
193
194 ctx->OcclusionResult = GL_TRUE;
195 ctx->Driver.Bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap );
196 }
197 }
198 else if (ctx->RenderMode==GL_FEEDBACK) {
199 if (ctx->Current.RasterPosValid) {
200 FLUSH_CURRENT(ctx, 0);
201 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN );
202 _mesa_feedback_vertex( ctx,
203 ctx->Current.RasterPos,
204 ctx->Current.RasterColor,
205 ctx->Current.RasterIndex,
206 ctx->Current.RasterTexCoord );
207 }
208 }
209 else if (ctx->RenderMode==GL_SELECT) {
210 /* Bitmaps don't generate selection hits. See appendix B of 1.1 spec. */
211 }
212
213 /* update raster position */
214 ctx->Current.RasterPos[0] += xmove;
215 ctx->Current.RasterPos[1] += ymove;
216 }