Rube Goldberg Machine 1.0
This is the base code for Rube Goldberg designed for the CS296 Software Systems Lab
src/render.cpp
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
00003 *
00004 * This software is provided 'as-is', without any express or implied
00005 * warranty.  In no event will the authors be held liable for any damages
00006 * arising from the use of this software.
00007 * Permission is granted to anyone to use this software for any purpose,
00008 * including commercial applications, and to alter it and redistribute it
00009 * freely, subject to the following restrictions:
00010 * 1. The origin of this software must not be misrepresented; you must not
00011 * claim that you wrote the original software. If you use this software
00012 * in a product, an acknowledgment in the product documentation would be
00013 * appreciated but is not required.
00014 * 2. Altered source versions must be plainly marked as such, and must not be
00015 * misrepresented as being the original software.
00016 * 3. This notice may not be removed or altered from any source distribution.
00017 */
00018 
00019 /* 
00020  * Base code for CS 296 Software Systems Lab 
00021  * Department of Computer Science and Engineering, IIT Bombay
00022  * Instructor: Parag Chaudhuri
00023  */
00024 
00025 #include "render.hpp"
00026 
00027 #ifdef __APPLE__
00028 #include <GLUT/glut.h>
00029 #else
00030 #include <GL/freeglut.h>
00031 #endif
00032 
00033 #include <cstdio>
00034 #include <cstdarg>
00035 #include <cstring>
00036 
00037 using namespace std;
00038 
00039 void debug_draw_t::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
00040 {
00041     glColor3f(color.r, color.g, color.b);
00042     glBegin(GL_LINE_LOOP);
00043     for (int32 i = 0; i < vertexCount; ++i)
00044     {
00045         glVertex2f(vertices[i].x, vertices[i].y);
00046     }
00047     glEnd();
00048 }
00049 
00050 void debug_draw_t::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
00051 {
00052     glEnable(GL_BLEND);
00053     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00054     glColor4f(0.5f * color.r, 0.5f * color.g, 0.5f * color.b, 0.5f);
00055     glBegin(GL_TRIANGLE_FAN);
00056     for (int32 i = 0; i < vertexCount; ++i)
00057     {
00058         glVertex2f(vertices[i].x, vertices[i].y);
00059     }
00060     glEnd();
00061     glDisable(GL_BLEND);
00062 
00063     glColor4f(color.r, color.g, color.b, 1.0f);
00064     glBegin(GL_LINE_LOOP);
00065     for (int32 i = 0; i < vertexCount; ++i)
00066     {
00067         glVertex2f(vertices[i].x, vertices[i].y);
00068     }
00069     glEnd();
00070 }
00071 
00072 void debug_draw_t::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
00073 {
00074     const float32 k_segments = 16.0f;
00075     const float32 k_increment = 2.0f * b2_pi / k_segments;
00076     float32 theta = 0.0f;
00077     glColor3f(color.r, color.g, color.b);
00078     glBegin(GL_LINE_LOOP);
00079     for (int32 i = 0; i < k_segments; ++i)
00080     {
00081         b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
00082         glVertex2f(v.x, v.y);
00083         theta += k_increment;
00084     }
00085     glEnd();
00086 }
00087 
00088 void debug_draw_t::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
00089 {
00090     const float32 k_segments = 16.0f;
00091     const float32 k_increment = 2.0f * b2_pi / k_segments;
00092     float32 theta = 0.0f;
00093     glEnable(GL_BLEND);
00094     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00095     glColor4f(0.5f * color.r, 0.5f * color.g, 0.5f * color.b, 0.5f);
00096     glBegin(GL_TRIANGLE_FAN);
00097     for (int32 i = 0; i < k_segments; ++i)
00098     {
00099         b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
00100         glVertex2f(v.x, v.y);
00101         theta += k_increment;
00102     }
00103     glEnd();
00104     glDisable(GL_BLEND);
00105 
00106     theta = 0.0f;
00107     glColor4f(color.r, color.g, color.b, 1.0f);
00108     glBegin(GL_LINE_LOOP);
00109     for (int32 i = 0; i < k_segments; ++i)
00110     {
00111         b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
00112         glVertex2f(v.x, v.y);
00113         theta += k_increment;
00114     }
00115     glEnd();
00116 
00117     b2Vec2 p = center + radius * axis;
00118     glBegin(GL_LINES);
00119     glVertex2f(center.x, center.y);
00120     glVertex2f(p.x, p.y);
00121     glEnd();
00122 }
00123 
00124 void debug_draw_t::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
00125 {
00126     glColor3f(color.r, color.g, color.b);
00127     glBegin(GL_LINES);
00128     glVertex2f(p1.x, p1.y);
00129     glVertex2f(p2.x, p2.y);
00130     glEnd();
00131 }
00132 
00133 void debug_draw_t::DrawTransform(const b2Transform& xf)
00134 {
00135     b2Vec2 p1 = xf.p, p2;
00136     const float32 k_axisScale = 0.4f;
00137     glBegin(GL_LINES);
00138     
00139     glColor3f(1.0f, 0.0f, 0.0f);
00140     glVertex2f(p1.x, p1.y);
00141     p2 = p1 + k_axisScale * xf.q.GetXAxis();
00142     glVertex2f(p2.x, p2.y);
00143 
00144     glColor3f(0.0f, 1.0f, 0.0f);
00145     glVertex2f(p1.x, p1.y);
00146     p2 = p1 + k_axisScale * xf.q.GetYAxis();
00147     glVertex2f(p2.x, p2.y);
00148 
00149     glEnd();
00150 }
00151 
00152 void debug_draw_t::DrawPoint(const b2Vec2& p, float32 size, const b2Color& color)
00153 {
00154     glPointSize(size);
00155     glBegin(GL_POINTS);
00156     glColor3f(color.r, color.g, color.b);
00157     glVertex2f(p.x, p.y);
00158     glEnd();
00159     glPointSize(1.0f);
00160 }
00161 
00162 void debug_draw_t::DrawString(int x, int y, const char *string, ...)
00163 {
00164     char buffer[128];
00165 
00166     va_list arg;
00167     va_start(arg, string);
00168     vsprintf(buffer, string, arg);
00169     va_end(arg);
00170 
00171     glMatrixMode(GL_PROJECTION);
00172     glPushMatrix();
00173     glLoadIdentity();
00174     int w = glutGet(GLUT_WINDOW_WIDTH);
00175     int h = glutGet(GLUT_WINDOW_HEIGHT);
00176     gluOrtho2D(0, w, h, 0);
00177     glMatrixMode(GL_MODELVIEW);
00178     glPushMatrix();
00179     glLoadIdentity();
00180 
00181     glColor3f(0.9f, 0.6f, 0.6f);
00182     glRasterPos2i(x, y);
00183     int32 length = (int32)strlen(buffer);
00184     for (int32 i = 0; i < length; ++i)
00185     {
00186         glutBitmapCharacter(GLUT_BITMAP_8_BY_13, buffer[i]);
00187     }
00188 
00189     glPopMatrix();
00190     glMatrixMode(GL_PROJECTION);
00191     glPopMatrix();
00192     glMatrixMode(GL_MODELVIEW);
00193 }
00194 
00195 void debug_draw_t::DrawAABB(b2AABB* aabb, const b2Color& c)
00196 {
00197     glColor3f(c.r, c.g, c.b);
00198     glBegin(GL_LINE_LOOP);
00199     glVertex2f(aabb->lowerBound.x, aabb->lowerBound.y);
00200     glVertex2f(aabb->upperBound.x, aabb->lowerBound.y);
00201     glVertex2f(aabb->upperBound.x, aabb->upperBound.y);
00202     glVertex2f(aabb->lowerBound.x, aabb->upperBound.y);
00203     glEnd();
00204 }
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines