1 /*
2  * Copyright (C) 2016 Daniel Scharrer <daniel@constexpr.org>
3  *
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  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
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.
14  *
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 NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 /** @file spot-light.c
25  *
26  * This is a simple sanity test for fixed function spot lights in OpenGL.
27  *
28  * It tests that vertices directly in front of the spot light are lit with
29  * full intensity and that lighting of vertices beyond the spot cutoff,
30  * and especially of those behind the spot light, is not affected by the
31  * spot light. This is done for three spot lights with different exponents.
32  */
33 
34 #include "piglit-util-gl.h"
35 
36 PIGLIT_GL_TEST_CONFIG_BEGIN
37 
38 	config.supports_gl_compat_version = 10;
39 
40 	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;
41 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
42 
43 PIGLIT_GL_TEST_CONFIG_END
44 
45 static const GLfloat pos[4] = {15.0, 15.0, 0.0, 1.0}; /* center */
46 static const GLfloat light0_dir[3] = {-1.0, 0.0, 0.0}; /* left */
47 static const GLfloat light1_dir[3] = {0.0, 1.0, 0.0}; /* up */
48 static const GLfloat light2_dir[3] = {1.0, 0.0, 0.0}; /* right */
49 static const GLfloat light0_ambient[4] = {1.0, 0.0, 0.0, 1.0};
50 static const GLfloat light1_ambient[4] = {0.0, 1.0, 0.0, 1.0};
51 static const GLfloat light2_ambient[4] = {0.0, 0.0, 1.0, 1.0};
52 static const GLfloat global_ambient[4] = {0.2, 0.2, 0.2, 1.0};
53 
54 static const GLfloat expected_left[4] = {1.0, 0.2, 0.2, 1.0};
55 static const GLfloat expected_bottom[4] = {0.2, 0.2, 0.2, 1.0};
56 static const GLfloat expected_right[4] = {0.2, 0.2, 1.0, 1.0};
57 static const GLfloat expected_top[4] = {0.2, 1.0, 0.2, 1.0};
58 static const GLfloat expected_bottom_left[4] = {0.2, 0.2, 0.2, 1.0};
59 static const GLfloat expected_top_left[4] = {0.2, 1.0, 0.2, 1.0};
60 static const GLfloat expected_bottom_right[4] = {0.2, 0.2, 0.2, 1.0};
61 static const GLfloat expected_top_right[4] = {0.2, 1.0, 0.2, 1.0};
62 
63 enum piglit_result
piglit_display(void)64 piglit_display(void)
65 {
66 	bool pass = true;
67 
68 	glClear(GL_COLOR_BUFFER_BIT);
69 
70 	glPointSize(3);
71 	glBegin(GL_POINTS);
72 	for (int x = 0; x < 11; x++) {
73 		for (int y = 0; y < 11; y++) {
74 			glVertex2f(3 * x, 3 * y);
75 		}
76 	}
77 	glEnd();
78 
79 	pass = piglit_probe_pixel_rgba( 0, 15, expected_left) && pass;
80 	pass = piglit_probe_pixel_rgba(15,  0, expected_bottom) && pass;
81 	pass = piglit_probe_pixel_rgba(30, 15, expected_right) && pass;
82 	pass = piglit_probe_pixel_rgba(15, 30, expected_top) && pass;
83 	pass = piglit_probe_pixel_rgba( 0,  0, expected_bottom_left) && pass;
84 	pass = piglit_probe_pixel_rgba( 0, 30, expected_top_left) && pass;
85 	pass = piglit_probe_pixel_rgba(30,  0, expected_bottom_right) && pass;
86 	pass = piglit_probe_pixel_rgba(30, 30, expected_top_right) && pass;
87 
88 	piglit_present_results();
89 
90 	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
91 }
92 
93 
94 void
piglit_init(int argc,char ** argv)95 piglit_init(int argc, char **argv)
96 {
97 	GLfloat zero[4] = {0.0, 0.0, 0.0, 1.0};
98 	GLfloat one[4] = {1.0, 1.0, 1.0, 1.0};
99 
100 	glClearColor(0.0, 0.0, 0.0, 1.0);
101 
102 	glEnable(GL_LIGHT0);
103 	glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 44.0);
104 	glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 1.0);
105 	glLightfv(GL_LIGHT0, GL_POSITION, pos);
106 	glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light0_dir);
107 	glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
108 
109 	glEnable(GL_LIGHT1);
110 	glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 60.0);
111 	glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 0.0);
112 	glLightfv(GL_LIGHT1, GL_POSITION, pos);
113 	glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, light1_dir);
114 	glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
115 
116 	glEnable(GL_LIGHT2);
117 	glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 44.0);
118 	glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 5.0);
119 	glLightfv(GL_LIGHT2, GL_POSITION, pos);
120 	glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, light2_dir);
121 	glLightfv(GL_LIGHT2, GL_AMBIENT, light2_ambient);
122 
123 	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
124 	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 0);
125 
126 	/* We are not interested in testing diffuse lighting, enable only the ambient term. */
127 	glMaterialfv(GL_FRONT, GL_DIFFUSE, zero);
128 	glMaterialfv(GL_FRONT, GL_AMBIENT, one);
129 
130 	glEnable(GL_LIGHTING);
131 
132 	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
133 }
134