draw-elements-base-vertex-neg.c 3.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Copyright © 2010 Marek Olšák <maraeo@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Marek Olšák <maraeo@gmail.com>
 */

#include "piglit-util.h"

int piglit_width = 320, piglit_height = 80;
int piglit_window_mode = GLUT_RGB | GLUT_DOUBLE;

32 33
GLboolean user_va = GL_FALSE;

34 35
void piglit_init(int argc, char **argv)
{
36 37 38 39 40 41 42 43 44
	unsigned i;

	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "user_varrays")) {
			user_va = GL_TRUE;
			puts("Testing user vertex arrays.");
		}
	}

45 46
	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

Paul Berry's avatar
Paul Berry committed
47
	if (piglit_get_gl_version() < 15) {
48 49 50 51
		printf("Requires OpenGL 1.5\n");
		piglit_report_result(PIGLIT_SKIP);
	}

52 53
	piglit_require_extension("GL_ARB_draw_elements_base_vertex");

54 55 56 57 58 59 60
	glShadeModel(GL_FLAT);
	glClearColor(0.2, 0.2, 0.2, 1.0);
}

static GLuint vboVertexPointer(GLint size, GLenum type, GLsizei stride,
                               const GLvoid *buf, GLsizei bufSize, intptr_t bufOffset)
{
61
	GLuint id;
62
	if (user_va) {
63
		glVertexPointer(size, type, stride, (char*)buf + bufOffset);
64 65
		return 0;
	}
66 67 68 69 70 71 72 73 74
	glGenBuffers(1, &id);
	glBindBuffer(GL_ARRAY_BUFFER, id);
	glBufferData(GL_ARRAY_BUFFER, bufSize, buf, GL_STATIC_DRAW);
	glVertexPointer(size, type, stride, (void*)bufOffset);
	return id;
}

static GLuint vboElementPointer(const GLvoid *buf, GLsizei bufSize)
{
75
	GLuint id;
76 77 78
	if (user_va) {
		return 0;
	}
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	glGenBuffers(1, &id);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, bufSize, buf, GL_STATIC_DRAW);
	return id;
}

static void test_negative_index_offset(float x1, float y1, float x2, float y2, int index)
{
	float v2[] = {
		x1, y1,
		x1, y2,
		x2, y1
	};
	int indices[] = {
		index, index+1, index+2
	};
	GLuint vbo, ib;

	vbo = vboVertexPointer(2, GL_FLOAT, 0, v2, sizeof(v2), 0);
	ib = vboElementPointer(indices, sizeof(indices));
99 100 101 102 103
	glDrawElementsBaseVertex(GL_TRIANGLES, 3, GL_UNSIGNED_INT, user_va ? indices : NULL, -index);
	if (vbo)
		glDeleteBuffers(1, &vbo);
	if (ib)
		glDeleteBuffers(1, &ib);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
}

enum piglit_result piglit_display(void)
{
	GLboolean pass = GL_TRUE;
	unsigned i;
	float x = 0, y = 0;
	float expected[] = {1,1,1};

	glClear(GL_COLOR_BUFFER_BIT);
	glEnableClientState(GL_VERTEX_ARRAY);

	for (i = 0; i < 63; i++) {
		int index = (int)pow(i, 5.2)+1;
		glBindBuffer(GL_ARRAY_BUFFER, 0);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

		printf("BaseVertex = -%i\n", index);
		test_negative_index_offset(x, y, x+20, y+20, index);
		assert(glGetError() == 0);
		pass = piglit_probe_pixel_rgb(x+5, y+5, expected) && pass;

		x += 20;
		if (x > 300) {
			x = 0;
			y += 20;
		}
	}

	glFinish();
	glutSwapBuffers();

136
	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
137
}