Add a negative test for bitwise not used in a larger expression.
[piglit/hramrach.git] / tests / general / linestipple.c
blob6d0b385e6e2cdca5b5753c30c5f80408c685d2a6
1 /*
2 * Copyright (c) The Piglit project 2008
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 /**
25 * @file
26 * Test basic line stippling functionality.
29 #include "piglit-util.h"
31 int piglit_width = 128, piglit_height = 128;
32 int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;
34 static void probe_pixel(int x, int y, const float* expected)
36 if (!piglit_probe_pixel_rgb(x, y, expected)) {
37 if (piglit_automatic)
38 piglit_report_result(PIGLIT_FAILURE);
42 struct vertex {
43 GLuint x;
44 GLuint y;
47 struct stipple_line {
48 GLint factor;
49 GLuint pattern;
50 GLfloat color[3];
51 GLuint primitive;
52 GLuint nvertices;
53 struct vertex *vertices;
56 static const int basex = 10;
57 static const int basey = 10;
59 static float background[3] = { 0, 0, 0 };
61 /**
62 * @note Only horizontal and vertical lines supported right now.
64 static GLuint probe_line(const struct stipple_line *line, const struct vertex *v1, const struct vertex *v2, GLuint fragment)
66 int x = v1->x;
67 int y = v1->y;
68 int length, dx = 0, dy = 0;
70 if (v2->x != v1->x) {
71 if (v2->x > v1->x)
72 dx = 1;
73 else
74 dx = -1;
75 length = (v2->x - v1->x)*dx;
76 } else {
77 if (v2->y > v1->y)
78 dy = 1;
79 else
80 dy = -1;
81 length = (v2->y - v1->y)*dy;
84 while(length) {
85 GLuint s = (fragment/line->factor) & 15;
86 if (line->pattern & (1 << s))
87 probe_pixel(basex+x, basey+y, line->color);
88 else
89 probe_pixel(basex+x, basey+y, background);
91 length--;
92 fragment++;
93 x += dx;
94 y += dy;
97 return fragment;
100 static void test_line(const struct stipple_line *line)
102 GLuint i;
104 glLineStipple(line->factor, line->pattern);
105 glColor3f(line->color[0], line->color[1], line->color[2]);
106 glBegin(line->primitive);
107 for(i = 0; i < line->nvertices; ++i)
108 glVertex2f(line->vertices[i].x + 0.5, line->vertices[i].y + 0.5);
109 glEnd();
111 glReadBuffer(GL_BACK);
112 if (line->primitive == GL_LINES) {
113 for(i = 0; i+1 < line->nvertices; i += 2)
114 probe_line(line, &line->vertices[i], &line->vertices[i+1], 0);
115 } else {
116 GLuint fragment = 0;
117 for(i = 0; i+1 < line->nvertices; ++i)
118 fragment = probe_line(line, &line->vertices[i], &line->vertices[i+1], fragment);
119 if (line->primitive == GL_LINE_LOOP)
120 probe_line(line, &line->vertices[i], &line->vertices[0], fragment);
124 static struct vertex BaselineVertices[] = { { 0, 0 }, { 24, 0 } };
125 static struct vertex RestartVertices[] = { { 0, 2 }, { 24, 2 }, { 0, 4 }, { 24, 4 } };
126 static struct vertex LinestripVertices[] = { { 0, 6 }, { 24, 6 }, { 24, 30 } };
127 static struct vertex LineloopVertices[] = { { 26, 0 }, { 46, 0 }, { 46, 20 }, { 26, 20 } };
128 static struct vertex Factor2Vertices[] = { { 0, 32 }, { 32, 32 }, { 32, 33 }, { 0, 33 } };
129 static struct vertex Factor3Vertices[] = { { 0, 35 }, { 63, 35 }, { 63, 36 }, { 0, 36 } };
130 static struct stipple_line Lines[] = {
131 { /* Baseline */
132 1, 0xffff, { 1.0, 1.0, 1.0 },
133 GL_LINES, 2,
134 BaselineVertices
136 { /* Restarting lines within a single Begin/End block */
137 1, 0x00ff, { 1.0, 0.0, 0.0 },
138 GL_LINES, 4,
139 RestartVertices
141 { /* Line strip */
142 1, 0x0f8f, { 1.0, 1.0, 0.0 },
143 GL_LINE_STRIP, 3,
144 LinestripVertices
146 { /* Line loop */
147 1, 0x8cef, { 0.0, 1.0, 0.0 },
148 GL_LINE_LOOP, 4,
149 LineloopVertices
151 { /* Factor 2x */
152 2, 0x838f, { 0.0, 0.0, 1.0 },
153 GL_LINE_LOOP, 4,
154 Factor2Vertices
156 { /* Factor 3x */
157 3, 0xf731, { 0.0, 1.0, 1.0 },
158 GL_LINE_LOOP, 4,
159 Factor3Vertices
163 static void test(void)
165 int i;
167 glClearColor(0.0, 0.0, 0.0, 1.0);
168 glClear(GL_COLOR_BUFFER_BIT);
170 glEnable(GL_LINE_STIPPLE);
172 glPushMatrix();
173 glTranslatef(basex, basey, 0.0);
175 for(i = 0; i < sizeof(Lines)/sizeof(Lines[0]); ++i)
176 test_line(&Lines[i]);
177 glPopMatrix();
179 glutSwapBuffers();
183 enum piglit_result
184 piglit_display(void)
186 test();
188 return PIGLIT_SUCCESS;
192 static void Reshape(int width, int height)
194 piglit_width = width;
195 piglit_height = height;
196 glViewport(0, 0, width, height);
197 piglit_ortho_projection(width, height, GL_FALSE);
201 void piglit_init(int argc, char **argv)
203 glutReshapeFunc(Reshape);
204 Reshape(piglit_width, piglit_height);