Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Perspective correct texture-mapped triangle routine  (Read 1791 times)
0 Members and 1 Guest are viewing this topic.
Offline mlaux

Junior Member


Medals: 3



« Posted 2011-12-12 02:18:01 »

Here's a method I wrote to fill in textured triangles. (x1, y1, z1) to (x3, y3, z3) are the 3 points of the triangle, (u1, v1) to (u3, v3) are the UV texture coordinates, texture[] is the texture image, sidelen is the length of the sides of the texture (should be a power of 2), pixels[] is the buffer to draw the triangle into, and width/height are the buffer dimensions.

UV coordinates are specified in texture image pixels, not 0.0 to 1.0 like in OpenGL. I didn't see too much software rendering code posted so I thought I'd put something out there! There's probably a lot of room for improvement; I'm not sure if floats/doubles are still dismally slow but perhaps a fixed-point implementation would be better.

Here's a screenshot of the code in action:



Have fun with it.

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  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
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  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
public void perspectiveTriangle(int x1, int y1, double z1, int x2, int y2,
      double z2, int x3, int y3, double z3, int u1, int v1, int u2,
      int v2, int u3, int v3, int[] texture, int sidelen, int[] pixels,
      int width, int height) {
   
   double startx, endx, startu, endu, startv, endv, startz, endz;
   double slope21, slope31, slope32;
   double u1i, v1i, z1r, u2i, v2i, z2r, u3i, v3i, z3r;
   double du21, dv21, dz21, du31, dv31, dz31, du32, dv32, dz32, dx, dy, du, dv, dz, u, v, z;
   double slopeleft, sloperight, duleft, duright, dvleft, dvright, dzleft, dzright, tempz;
   int temp, off, start, end, uu, vv;
   
   if(y2 < y1) {
      temp = x2; x2 = x1; x1 = temp;
      temp = y2; y2 = y1; y1 = temp;
      temp = u2; u2 = u1; u1 = temp;
      temp = v2; v2 = v1; v1 = temp;
      tempz = z2; z2 = z1; z1 = tempz;
   }
   if(y3 < y1) {
      temp = x3; x3 = x1; x1 = temp;
      temp = y3; y3 = y1; y1 = temp;
      temp = u3; u3 = u1; u1 = temp;
      temp = v3; v3 = v1; v1 = temp;
      tempz = z3; z3 = z1; z1 = tempz;
   }
   if(y3 < y2) {
      temp = x3; x3 = x2; x2 = temp;
      temp = y3; y3 = y2; y2 = temp;
      temp = u3; u3 = u2; u2 = temp;
      temp = v3; v3 = v2; v2 = temp;
      tempz = z3; z3 = z2; z2 = tempz;
   }
   
   if(y1 == y3)
      return;
   
   u1i = u1 / z1; u2i = u2 / z2; u3i = u3 / z3;
   v1i = v1 / z1; v2i = v2 / z2; v3i = v3 / z3;
   z1r = 1.0d / z1; z2r = 1.0d / z2; z3r = 1.0d / z3;
   
   dy      = 1.0d /  (y2 - y1);
   slope21 = (double) (x2 - x1) * dy;
   du21    = (double) (u2i - u1i) * dy;
   dv21    = (double) (v2i - v1i) * dy;
   dz21    = (double) (z2r - z1r) * dy;
   
   dy      = 1.0d /  (y3 - y1);
   slope31 = (double) (x3 - x1) * dy;
   du31    = (double) (u3i - u1i) * dy;
   dv31    = (double) (v3i - v1i) * dy;
   dz31    = (double) (z3r - z1r) * dy;
   
   dy      = 1.0d /  (y3 - y2);
   slope32 = (double) (x3 - x2) * dy;
   du32    = (double) (u3i - u2i) * dy;
   dv32    = (double) (v3i - v2i) * dy;
   dz32    = (double) (z3r - z2r) * dy;
   
   startx = endx = x1;
   startu = endu = u1i;
   startv = endv = v1i;
   startz = endz = z1r;
   
   if(y1 != y2) {
      if(slope21 > slope31) {
         slopeleft = slope31;
         sloperight = slope21;
         
         duleft = du31;
         duright = du21;
         
         dvleft = dv31;
         dvright = dv21;
         
         dzleft = dz31;
         dzright = dz21;
      } else {
         slopeleft = slope21;
         sloperight = slope31;
         
         duleft = du21;
         duright = du31;
         
         dvleft = dv21;
         dvright = dv31;
         
         dzleft = dz21;
         dzright = dz31;
      }
     
      for(int y = y1; y != y2; y++) {
         if(y > 0 && y < height) {
            dx = endx - startx;
            if(dx != 0) {
               du = (endu - startu) / dx;
               dv = (endv - startv) / dx;
               dz = (endz - startz) / dx;
            } else {
               du = endu - startu;
               dv = endv - startv;
               dz = endz - startz;
            }
            u = startu;
            v = startv;
            z = startz;
           
            off = y * width;
            start = off + (int) startx;
            end = off + (int) endx;
            if(start < off) {
               dx = -startx;
               u += dx * du;
               v += dx * dv;
               z += dx * dz;
               start = off;
            }
            if(end > off + width - 1)
               end = off + width - 1;
         
            while(start < end) {
               uu = ((int) (u / z)) & (sidelen - 1);
               vv = ((int) (v / z)) & (sidelen - 1);
               pixels[start++] = texture[vv * sidelen + uu];
               u += du;
               v += dv;
               z += dz;
            }
         }

         startx += slopeleft;
         endx += sloperight;
         
         startu += duleft;
         endu += duright;
         
         startv += dvleft;
         endv += dvright;
         
         startz += dzleft;
         endz += dzright;
      }
   } else {
      if (x1 > x2) {
         startx = x2;
         endx = x1;
         
         startu = u2i;
         endu = u1i;
         
         startv = v2i;
         endv = v1i;
         
         startz = z2r;
         endz = z1r;
      } else {
         startx = x1;
         endx = x2;
         
         startu = u1i;
         endu = u2i;
         
         startv = v1i;
         endv = v2i;
         
         startz = z1r;
         endz = z2r;
      }
   }
   
   if(y2 != y3) {
      if(slope32 > slope31) {
         slopeleft  = slope32;
         sloperight = slope31;
         
         duleft = du32;
         duright = du31;
         
         dvleft = dv32;
         dvright = dv31;
         
         dzleft = dz32;
         dzright = dz31;
      } else {
         slopeleft  = slope31;
         sloperight = slope32;
         
         duleft = du31;
         duright = du32;
         
         dvleft = dv31;
         dvright = dv32;
         
         dzleft = dz31;
         dzright = dz32;
      }
     
      for(int y = y2; y != y3; y++) {
         if(y > 0 && y < height) {
            dx = endx - startx;
            if(dx != 0) {
               du = (endu - startu) / dx;
               dv = (endv - startv) / dx;
               dz = (endz - startz) / dx;
            } else {
               du = endu - startu;
               dv = endv - startv;
               dz = endz - startz;
            }
            u = startu;
            v = startv;
            z = startz;

            off = y * width;
            start = off + (int) startx;
            end = off + (int) endx;
            if(start < off) {
               dx = -startx;
               u += dx * du;
               v += dx * dv;
               z += dx * dz;
               start = off;
            }
            if(end > off + width - 1)
               end = off + width - 1;
         
            while(start < end) {
               uu = ((int) (u / z)) & (sidelen - 1);
               vv = ((int) (v / z)) & (sidelen - 1);
               pixels[start++] = texture[vv * sidelen + uu];
               u += du;
               v += dv;
               z += dz;
            }
         }
         
         startx += slopeleft;
         endx += sloperight;
         
         startu += duleft;
         endu += duright;
         
         startv += dvleft;
         endv += dvright;
         
         startz += dzleft;
         endz += dzright;
      }
   }
}

Offline theagentd
« Reply #1 - Posted 2011-12-12 09:42:11 »

Very interesting! Bookmarked in case I want to do something that isn't supported in hardware yet! Anything on the performance of it?

Why are the texture coordinates and vertex coordinates ints? I want doubles, or at least floats! xD

Myomyomyo.
Offline mlaux

Junior Member


Medals: 3



« Reply #2 - Posted 2011-12-13 00:05:40 »

Very interesting! Bookmarked in case I want to do something that isn't supported in hardware yet! Anything on the performance of it?

Why are the texture coordinates and vertex coordinates ints? I want doubles, or at least floats! xD

Well, the idea is that by this point you will have already projected your points in 3d to screen coordinates, which are assumed by this method to be whole number pixels. My routine doesn't do any fancy rounding stuff to make fractional pixels look nicer, so floats/doubles aren't really necessary.

About performance: it's probably not too great but I haven't tested it extensively. I got 500fps with 2 triangles at 320x240, but that dropped down to 50 fps at 1600x900. And that's only 2 triangles. I guess I've got some optimization to do. Smiley

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (10 views)
2014-04-15 03:46:01

UprightPath (23 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (107 views)
2014-04-08 23:06:04

tom_mai78101 (207 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!