u_draw_quad.c

Go to the documentation of this file.
00001 /**************************************************************************
00002  *
00003  * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
00004  * All Rights Reserved.
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  *
00014  * The above copyright notice and this permission notice (including the
00015  * next paragraph) shall be included in all copies or substantial portions
00016  * of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00020  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
00021  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
00022  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00023  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00024  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  *
00026  **************************************************************************/
00027 
00028 
00029 #include "pipe/p_context.h"
00030 #include "pipe/p_defines.h"
00031 #include "pipe/p_inlines.h"
00032 #include "pipe/p_winsys.h"
00033 #include "util/u_draw_quad.h"
00034 
00035 
00040 void 
00041 util_draw_vertex_buffer(struct pipe_context *pipe,
00042                         struct pipe_buffer *vbuf,
00043                         uint offset,
00044                         uint prim_type,
00045                         uint num_verts,
00046                         uint num_attribs)
00047 {
00048    struct pipe_vertex_buffer vbuffer;
00049    struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
00050    uint i;
00051 
00052    assert(num_attribs <= PIPE_MAX_ATTRIBS);
00053 
00054    /* tell pipe about the vertex buffer */
00055    vbuffer.buffer = vbuf;
00056    vbuffer.pitch = num_attribs * 4 * sizeof(float);  /* vertex size */
00057    vbuffer.buffer_offset = offset;
00058    pipe->set_vertex_buffers(pipe, 1, &vbuffer);
00059 
00060    /* tell pipe about the vertex attributes */
00061    for (i = 0; i < num_attribs; i++) {
00062       velements[i].src_offset = i * 4 * sizeof(float);
00063       velements[i].vertex_buffer_index = 0;
00064       velements[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
00065       velements[i].nr_components = 4;
00066    }
00067    pipe->set_vertex_elements(pipe, num_attribs, velements);
00068 
00069    /* draw */
00070    pipe->draw_arrays(pipe, prim_type, 0, num_verts);
00071 }
00072 
00073 
00074 
00080 void 
00081 util_draw_texquad(struct pipe_context *pipe,
00082                   float x0, float y0, float x1, float y1, float z)
00083 {
00084    struct pipe_buffer *vbuf;
00085    uint numAttribs = 2, vertexBytes, i, j;
00086 
00087    vertexBytes = 4 * (4 * numAttribs * sizeof(float));
00088 
00089    /* XXX create one-time */
00090    vbuf = pipe_buffer_create(pipe->screen, 32,
00091                              PIPE_BUFFER_USAGE_VERTEX, vertexBytes);
00092    if (vbuf) {
00093       float *v = (float *) pipe_buffer_map(pipe->screen, vbuf,
00094                                            PIPE_BUFFER_USAGE_CPU_WRITE);
00095       if (v) {
00096          /*
00097           * Load vertex buffer
00098           */
00099          for (i = j = 0; i < 4; i++) {
00100             v[j + 2] = z;   /* z */
00101             v[j + 3] = 1.0; /* w */
00102             v[j + 6] = 0.0; /* r */
00103             v[j + 7] = 1.0; /* q */
00104             j += 8;
00105          }
00106 
00107          v[0] = x0;
00108          v[1] = y0;
00109          v[4] = 0.0; /*s*/
00110          v[5] = 0.0; /*t*/
00111 
00112          v[8] = x1;
00113          v[9] = y0;
00114          v[12] = 1.0;
00115          v[13] = 0.0;
00116 
00117          v[16] = x1;
00118          v[17] = y1;
00119          v[20] = 1.0;
00120          v[21] = 1.0;
00121 
00122          v[24] = x0;
00123          v[25] = y1;
00124          v[28] = 0.0;
00125          v[29] = 1.0;
00126 
00127          pipe_buffer_unmap(pipe->screen, vbuf);
00128          util_draw_vertex_buffer(pipe, vbuf, 0, PIPE_PRIM_TRIANGLE_FAN, 4, 2);
00129       }
00130 
00131       pipe_buffer_reference(pipe->screen, &vbuf, NULL);
00132    }
00133 }

Generated on Tue Sep 29 06:25:15 2009 for Gallium3D by  doxygen 1.5.4