For future reference, here's the relevant information in a single post without attachments.
The teapot data is a version of freeglut_teapot_data.h. Below is the first couple of triangles. The scale member is apparently 1.0 for all of them.
teapot_data.h
Code:
#define TEAPOT_NUMTRIANGLES 1092
triangle_t teapot_model[] = {
{
.x1=-160,
.y1=-142,
.x2=-160,
.y2=-142,
.x3=-199,
.y3=-142,
.fillcolor=0x41a7,
.scale=1.0
},
{
.x1=-160,
.y1=-142,
.x2=-199,
.y2=-142,
.x3=-176,
.y3=-128,
.fillcolor=0xd63af1,
.scale=1.0
},
drawline.h
Code:
#ifndef DRAWLINE_H_
#define DRAWLINE_H_
// Draw a line on the screen from x1,y1 to x2,y2 with a given pen color
void DrawLine(SDL_Surface *screen, int x1, int y1, int x2, int y2, unsigned int color);
// Read pixel x,y on the screen
unsigned int GetPixel(SDL_Surface *screen, int x, int y);
// Set pixel x,y on the screen
void SetPixel(SDL_Surface *screen, int x, int y, unsigned int color);
#endif /* DRAWLINE_H_ */
triangle.h
Code:
#ifndef TRIANGLE_H_
#define TRIANGLE_H_
typedef struct triangle triangle_t;
struct triangle {
// Model coordinates
int x1, y1;
int x2, y2;
int x3, y3;
// Fill color
unsigned int fillcolor;
// Scale factor
float scale;
// Translate to these coordinates before drawing on screen
int tx, ty;
// Bounding box of on-screen coordinates
int bx, by; // upper left corner of box
int bw, bh; // Width and height of box
// On-screen coordinates
int sx1, sy1;
int sx2, sy2;
int sx3, sy3;
};
// Draw triangle on screen.
void DrawTriangle(SDL_Surface *screen, triangle_t *triangle);
#endif /*TRIANGLE_H_*/
triangle.c
Code:
#include <stdlib.h>
#include <stdio.h>
#include "SDL.h"
#include "triangle.h"
#include "drawline.h"
#define TRIANGLE_PENCOLOR 0xBBBB0000
// Print triangle coordinates along with a message
void PrintTriangle(triangle_t *triangle, char *msg)
{
printf("%s: %d,%d - %d,%d - %d,%d\n",
msg,
triangle->x1, triangle->y1,
triangle->x2, triangle->y2,
triangle->x3, triangle->y3);
}
// Return 0 if triangle coordinates are outside the screen boundary. 1 otherwise.
int SanityCheckTriangle(SDL_Surface *screen, triangle_t *triangle)
{
if (triangle->sx1 < 0 || triangle->sx1 >= screen->w ||
triangle->sx2 < 0 || triangle->sx2 >= screen->w ||
triangle->sx3 < 0 || triangle->sx3 >= screen->w ||
triangle->sy1 < 0 || triangle->sy1 >= screen->h ||
triangle->sy2 < 0 || triangle->sy2 >= screen->h ||
triangle->sy3 < 0 || triangle->sy3 >= screen->h) {
return 0;
} else {
return 1;
}
}
// Scale triangle
void ScaleTriangle(triangle_t *triangle)
{
// TODO: Replace the code below with code that scales each triangle coordinate.
// The scaling factor is specified in triangle->scale.
// Remember that this function MUST write to the on-screen coordinates.
// Do not alter the model coordinates.
triangle->sx1 = triangle->x1;
triangle->sx2 = triangle->x2;
triangle->sx3 = triangle->x3;
triangle->sy1 = triangle->y1;
triangle->sy2 = triangle->y2;
triangle->sy3 = triangle->y3;
}
// Move triangle to its screen position
void TranslateTriangle(triangle_t *triangle)
{
// TODO: Insert code that moves the triangle on the screen.
// The translation coordinates are specified in triangle->tx and triangle->ty.
// Remember to use the on-screen coordinates (triangle->sx1, etc.)
}
// Calculate triangle bounding box
void CalculateTriangleBoundingBox(triangle_t *triangle)
{
// TODO: Insert code that calculates the bounding box of a triangle.
// Remember to use the on-screen coordinates (triangle->sx1, etc.)
// The bounding box coordinates should be written to
// triangle->bx, triangle->by, triangle->bw, triangle->bh
}
// Fill triangle with a color
void FillTriangle(SDL_Surface *screen, triangle_t *triangle)
{
// TODO: Insert code that fills the triangle with the color specified in triangle->fillcolor.
// Hint: Draw the triangle with color TRIANGLE_PENCOLOR (this color can not
// occur in e.g. the teapot or the example triangles). Thus, if your
// approach to filling the triangle relies on looking for the edges of
// the triangle on the screen (via the GetPixel function), you will find those
// edges even if the triangle overlaps with a triangle that has already
// been drawn on the screen.
}
// Draw triangle on screen
void DrawTriangle(SDL_Surface *screen, triangle_t *triangle)
{
int isOK;
// Scale.
ScaleTriangle(triangle);
// Translate.
TranslateTriangle(triangle);
// Determine bounding box
CalculateTriangleBoundingBox(triangle);
// Sanity check that triangle is within screen boundaries.
isOK = SanityCheckTriangle(screen, triangle);
if (isOK == 0) {
PrintTriangle(triangle, "Triangle outside screen boundaries");
return;
}
// TODO: Insert calls to DrawLine to draw the triangle.
// Remember to use the on-screen coordinates (triangle->sx1, etc.)
// The following 3 lines are the OP's code so far.
DrawLine(screen, triangle->sx1, triangle->sy1, triangle->sx2, triangle->sy2, TRIANGLE_PENCOLOR);
DrawLine(screen, triangle->sx2, triangle->sy2, triangle->sx3, triangle->sy3, TRIANGLE_PENCOLOR);
DrawLine(screen, triangle->sx1, triangle->sy1, triangle->sx3, triangle->sy3, TRIANGLE_PENCOLOR);
// Fill triangle
FillTriangle(screen, triangle);
// Force screen update.
//SDL_UpdateRect(screen, triangle->bx, triangle->by, triangle->bw, triangle->bh);
// Force update of entire screen. Comment/remove this call and uncomment the above call
// when your CalculateTriangleBoundingBox function has been implemented.
SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
}
main.c
Code:
#include <stdlib.h>
#include <stdio.h>
#include "SDL.h"
#include "drawline.h"
#include "triangle.h"
#include "teapot_data.h"
triangle_t exampletriangle1 = {
.x1 = 100,
.y1 = 200,
.x2 = 200,
.y2 = 100,
.x3 = 300,
.y3 = 300,
.fillcolor = 0xffff0000,
.scale = 1.0
};
triangle_t exampletriangle2 = {
.x1 = 50,
.y1 = 150,
.x2 = 150,
.y2 = 50,
.x3 = 250,
.y3 = 250,
.fillcolor = 0xffffff00,
.scale = 1.0
};
triangle_t exampletriangle3 = {
.x1 = 350,
.y1 = 350,
.x2 = 460,
.y2 = 300,
.x3 = 500,
.y3 = 400,
.fillcolor = 0xff00ff00,
.scale = 1.0
};
triangle_t exampletriangle4 = {
.x1 = 350,
.y1 = 100,
.x2 = 450,
.y2 = 50,
.x3 = 500,
.y3 = 200,
.fillcolor = 0xff0000ff,
.scale = 1.0
};
// First function run in your program
int main(int argc, char **argv)
{
int retval, done;
SDL_Surface *screen;
SDL_Event event;
// Initialize SDL
retval = SDL_Init(SDL_INIT_VIDEO);
if (retval == -1) {
printf("Unable to initialize SDL\n");
exit(1);
}
//Create a 1024x768x32 window
screen = SDL_SetVideoMode(1024, 768, 32, 0);
if (screen == NULL) {
printf("Unable to get video surface: %s\n", SDL_GetError());
exit(1);
}
// The teapot is represented as an array of triangle data structures.
// To draw it on the screen you need to traverse the 'teapot_model' array
// and call DrawTriangle for each triangle (teapot_data.h contains the array).
// The definition TEAPOT_NUMTRIANGLES specifies the number of triangles in the array.
// The teapot model is contained within a 1000x1000 box (coordinates
// from -500 to 500 on the x and y axis). Remember to translate the
// model to the middle of the screen before drawing it (initialize
// triangle->tx and triangle->ty with the appropriate coordinates).
// Draw some example triangles on the screen.
// Use these examples in the beginning.
DrawTriangle(screen, &exampletriangle1);
DrawTriangle(screen, &exampletriangle2);
DrawTriangle(screen, &exampletriangle3);
DrawTriangle(screen, &exampletriangle4);
// Wait for ctrl-c from user
done = 0;
while (done == 0) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
done = 1;
break;
}
}
}
SDL_Quit();
return 0;
}