comparison TaskManager/Test/simple_render/viewer.cpp @ 51:c964110017c8

*** empty log message ***
author gongo
date Fri, 15 Feb 2008 19:44:23 +0900
parents 7927e00fb8e2
children 90523ccc4dff
comparison
equal deleted inserted replaced
50:7927e00fb8e2 51:c964110017c8
2 #include <SDL.h> 2 #include <SDL.h>
3 #include <SDL_opengl.h> 3 #include <SDL_opengl.h>
4 #include <math.h> 4 #include <math.h>
5 #include <unistd.h> 5 #include <unistd.h>
6 #include "polygon.h" 6 #include "polygon.h"
7 //#include "demonstration.h"
8 //#include "scene.h"
9 #include "viewer.h" 7 #include "viewer.h"
10 #include "sys.h" 8 #include "sys.h"
11 using namespace std; 9 using namespace std;
12 10
13 #define redMask 0x00ff0000 11 #define redMask 0x00ff0000
70 { 68 {
71 SDL_GL_SwapBuffers(); 69 SDL_GL_SwapBuffers();
72 } 70 }
73 71
74 72
75 void Viewer::write_pixel(int x, int y,float z, Uint32 rgb) { 73 void
74 Viewer::write_pixel(int x, int y,float z, Uint32 rgb)
75 {
76 SDL_PixelFormat *pf; 76 SDL_PixelFormat *pf;
77 pf = screen->format; 77 pf = screen->format;
78 //printf("x:%d y:%d z:%d\n",x,y,z);
79
80 //cout << "write_pixel" << endl;
81 //cout << x << " " << y << endl;
82
83 //cout << SDL_MapRGB(pf,0,0,150) << endl;
84
85 x += width/2; 78 x += width/2;
86 y += height/2; 79 y += height/2;
87 80
88 static int diffz,diffz1; 81 static int diffz,diffz1;
82
89 diffz1 = diffz; 83 diffz1 = diffz;
90 diffz = (zRow[x][y]>z); 84 diffz = (zRow[x][y]>z);
91 if(diffz != diffz1) { 85 if (z < zRow[x][y]) {
92 //printf("diffz :%d zRow[%d][%d] = %f z = %f\n",diffz,x,y,zRow[x][y],z); 86 if (x < width && x > 0 && y > 0 && y < height) {
93 }
94 //printf("x:%d,y:%d,z:%f,zRow:%f\n",x,y,z,zRow[x][y]);
95 if(z < zRow[x][y]) {
96 // printf("x:%d,y:%d,z:%d\n",x,y,z,zRow);
97 if(x < width && x > 0 && y > 0 && y < height)
98 {
99 //pixels[width*y + x] = SDL_MapRGB(pf,70,70,71);
100 zRow[x][y] = z; 87 zRow[x][y] = z;
101 y = height - y; 88 y = height - y;
102 pixels[width*y + x] = rgb; 89 pixels[width*y + x] = rgb;
103 } 90 }
104 } 91 }
105 } 92 }
106 93
107 void Viewer::write_line(float x1, float y1, float x2, float y2, Uint32 rgb) 94 void
108 { 95 Viewer::write_line(float x1, float y1, float x2, float y2, Uint32 rgb)
109 //cout << "write_line("<< x1 << "," << y1 << "," << x2 << "," << y2 << ")"<< endl; 96 {
110 97 if (x1 > x2) {
111 //Uint32 rgb = 9830400;
112
113 if(x1 > x2)
114 {
115 float x=0; 98 float x=0;
116 float y=0; 99 float y=0;
117 x=x1; 100 x=x1;
118 y=y1; 101 y=y1;
119 x1 = x2; 102 x1 = x2;
120 y1 = y2; 103 y1 = y2;
121 x2 = x; 104 x2 = x;
122 y2 = y; 105 y2 = y;
123 } 106 }
107
124 float s = y1; 108 float s = y1;
125 109
126 if((int)x1 == (int)x2) 110 if ((int)x1 == (int)x2) {
127 { 111 if (y1 > y2) {
128 if(y1 > y2)
129 {
130 float y=0; 112 float y=0;
131 y = y1; 113 y = y1;
132 y1 = y2; 114 y1 = y2;
133 y2 = y; 115 y2 = y;
134 } 116 }
135 for(float i=y1; i<y2; i++) 117
136 { 118 for (float i=y1; i<y2; i++) {
137 //write_pixel((int)x1,(int)i); 119 //write_pixel((int)x1,(int)i);
138 write_pixel((int)x1,(int)i,0,rgb); 120 write_pixel((int)x1,(int)i,0,rgb);
139 } 121 }
140 } 122 } else {
141 else
142 {
143 float t = (y2 - y1)/(x2 - x1); 123 float t = (y2 - y1)/(x2 - x1);
144 if(t < -1) 124 if (t < -1) {
145 {
146 float f = 0; 125 float f = 0;
147 for(float i=x1; i<x2; i++) 126 for (float i=x1; i<x2; i++) {
148 { 127 for (float a=(int)t; a<0; a++) {
149 for(float a=(int)t; a<0; a++)
150 {
151 //write_pixel((int)i,(int)s);
152 write_pixel((int)i,(int)s,0,rgb); 128 write_pixel((int)i,(int)s,0,rgb);
153 s--; 129 s--;
154 } 130 }
131
155 f += t-(int)t; 132 f += t-(int)t;
156 if(f <= -1) 133
157 { 134 if (f <= -1) {
158 //write_pixel((int)i,(int)s);
159 write_pixel((int)i,(int)s,0,rgb); 135 write_pixel((int)i,(int)s,0,rgb);
160 f = 0; 136 f = 0;
161 s--; 137 s--;
162 } 138 }
163 } 139 }
164 } 140 } else if (t <= 1) {
165 else if(t <= 1) 141 for(float i=x1; i<x2; i++) {
166 {
167 for(float i=x1; i<x2; i++)
168 {
169 //write_pixel((int)i,(int)s); 142 //write_pixel((int)i,(int)s);
170 write_pixel((int)i,(int)s,0,rgb); 143 write_pixel((int)i,(int)s,0,rgb);
171 s += t; 144 s += t;
172 } 145 }
173 } 146 } else {
174 else
175 {
176 float f = 0; 147 float f = 0;
177 for(float i=x1; i<x2; i++) 148 for (float i=x1; i<x2; i++) {
178 { 149 for (float a=0; a<(int)t; a++) {
179 for(float a=0; a<(int)t; a++)
180 {
181 //write_pixel((int)i,(int)s);
182 write_pixel((int)i,(int)s,0,rgb); 150 write_pixel((int)i,(int)s,0,rgb);
183 s++; 151 s++;
184 } 152 }
153
185 f += t-(int)t; 154 f += t-(int)t;
186 if(f >= 1) 155
187 { 156 if (f >= 1) {
188 //write_pixel((int)i,(int)s);
189 write_pixel((int)i,(int)s,0,rgb); 157 write_pixel((int)i,(int)s,0,rgb);
190 f = 0; 158 f = 0;
191 s++; 159 s++;
192 } 160 }
193 } 161 }
253 arg = new run_arg_t; 221 arg = new run_arg_t;
254 HTaskPtr task; 222 HTaskPtr task;
255 int fd; 223 int fd;
256 224
257 arg->start_time = get_ticks(); 225 arg->start_time = get_ticks();
226 arg->this_time = 0;
227 arg->frames = 0;
228
258 arg->pf = screen->format; 229 arg->pf = screen->format;
259 arg->background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00); 230 arg->background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
260 arg->p = new Polygon; 231 arg->p = new Polygon;
261 arg->p->set_data("cube.xml"); 232 arg->p->set_data("cube.xml");
262 arg->p->viewer = this; 233 arg->p->viewer = this;
335 arg->p->draw(arg->pp); // test draw of PolygonPack 306 arg->p->draw(arg->pp); // test draw of PolygonPack
336 307
337 SDL_BlitSurface(arg->bitmap, NULL, screen, NULL); 308 SDL_BlitSurface(arg->bitmap, NULL, screen, NULL);
338 SDL_UpdateRect(screen, 0, 0, 0, 0); 309 SDL_UpdateRect(screen, 0, 0, 0, 0);
339 310
340 //swap_buffers();
341 arg->frames++; 311 arg->frames++;
342 312
343 fd = manager->open("ViewerRunLoop"); 313 fd = manager->open("ViewerRunLoop");
344 task = manager->create_task(fd, 0, 0, 0, NULL); 314 task = manager->create_task(fd, 0, 0, 0, NULL);
345 manager->spawn_task(task); 315 manager->spawn_task(task);