Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Triangle.h"
- Triangle::Triangle(float scale, float position)
- : Model()
- {
- mVertices[0][0]= -5.0f; mVertices[0][1]= 0.0f; mVertices[0][2]= 5.0f;
- mVertices[1][0]= 5.0f; mVertices[1][1]= 0.0f; mVertices[1][2]= 0.0f;
- mVertices[2][0]= 0.0f; mVertices[2][1]= 0.0f; mVertices[2][2]= -5.0f;
- mScaleFactor = scale;
- mYPosition = position;
- mFloorShadow = true;
- }
- Triangle::~Triangle(void)
- {
- }
- void Triangle::draw()
- {
- glColor3f(0.0f,0.0f,0.0f);
- glPushMatrix();
- glTranslatef(0.0f, mYPosition, 0.0f);
- glBegin( GL_TRIANGLES );
- //Want to avoid culling
- for ( int i = 0; i < 3; ++i )
- {
- glVertex3f(mVertices[i][0], mVertices[i][1], mVertices[i][2]);
- }
- for ( int i = 2; i > -1; --i )
- {
- glVertex3f(mVertices[i][0], mVertices[i][1], mVertices[i][2]);
- }
- glEnd();
- glPopMatrix();
- }
- void Triangle::draw_bottom()
- {
- glColor3f(1.0f,1.0f,1.0f);
- glPushMatrix();
- glTranslatef(0.0f, mYPosition, 0.0f);
- glBegin(GL_TRIANGLES);
- for ( int i = 0; i < 3; ++i )
- {
- glVertex3f(mBottomVertices[i][0], mBottomVertices[i][1], mBottomVertices[i][2]);
- }
- for ( int i = 2; i > -1; --i )
- {
- glVertex3f(mBottomVertices[i][0], mBottomVertices[i][1], mBottomVertices[i][2]);
- }
- glEnd();
- glPopMatrix();
- }
- void Triangle::build_volume(GLfloat* light_pos, float* camera_pos)
- {
- static float volume_size = 10.0f; //Some big number that will for sure cover our needs
- //Initialize our bottom vertices to our top ones....
- /*
- for ( int i = 0; i < 3; ++i )
- {
- mBottomVertices[i][0] = light_pos[0];
- mBottomVertices[i][1] = light_pos[1];
- mBottomVertices[i][2] = light_pos[2];
- }
- */
- // Given out light point at L, and our three vertices: A,B,C
- // the direction vectors for our volume can be generated as following.
- // v1 = A - L. This direction vector can then be scaled with out volume_size
- // maginute and added on to our initial A to give us out bottom vertice:
- // A' = A + volume_size * v1
- if (light_pos[1] > mYPosition)
- {
- mFloorShadow = true;
- for ( int i = 0; i < 3; ++i )
- {
- mBottomVertices[i][0] = light_pos[0] + (mVertices[i][0] - light_pos[0]) * volume_size;
- mBottomVertices[i][1] = light_pos[1] + (mVertices[i][1] - light_pos[1]) * volume_size;
- mBottomVertices[i][2] = light_pos[2]+ (mVertices[i][2] - light_pos[2]) * volume_size;
- }
- }
- else
- {
- mFloorShadow = false;
- for ( int i = 0; i < 3; ++i )
- {
- mBottomVertices[i][0] = light_pos[0] + (mVertices[i][0] + light_pos[0]) * volume_size;
- mBottomVertices[i][1] = light_pos[1] + (mVertices[i][1] + light_pos[1]) * volume_size;
- mBottomVertices[i][2] = light_pos[2] + (mVertices[i][2] + light_pos[2]) * volume_size;
- }
- }
- }
- void Triangle::draw_volume()
- {
- //glEnable(GL_CULL_FACE);
- //glCullFace(GL_FRONT);
- glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glPushMatrix();
- glTranslatef(0.0f, mYPosition, 0.0f);
- //Draw top cap...
- glBegin( GL_TRIANGLES );
- glColor4f(0.0f,0.0f,0.0f,0.3f);
- for ( int i = 2; i > -1; --i )
- {
- glVertex3f(mVertices[i][0], mVertices[i][1], mVertices[i][2]);
- }
- glEnd();
- glColor4f(1.0f,1.0f,0.0f,0.3f);
- draw_quad(0,0,1,1);
- glColor4f(1.0f,0.0f,0.0f,0.3f);
- draw_quad(1,1,2,2);
- glColor4f(0.0f,1.0f,0.0f,0.3f);
- draw_quad(2,2,0,0);
- //Draw bottom cap...
- glBegin( GL_TRIANGLES );
- glColor4f(0.0f,0.0f,0.0f,0.3f);
- for ( int i = 0; i < 3; ++i )
- {
- glVertex3f(mBottomVertices[i][0], mBottomVertices[i][1], mBottomVertices[i][2]);
- }
- glEnd();
- glPopMatrix();
- glDisable (GL_BLEND);
- //glDisable(GL_CULL_FACE);
- }
- void Triangle::draw_quad(int first_top, int second_bottom, int third_bottom, int fourth_top)
- {
- /*cout << "Drawing quad from: ("<<mVertices[first_top][0]<<","<<mVertices[first_top][1]<<","<<mVertices[first_top][2]<<"), "
- << "("<<mBottomVertices[second_bottom][0]<<","<<mBottomVertices[second_bottom][1]<<","<<mBottomVertices[second_bottom][2]<<"), "
- << "("<<mBottomVertices[third_bottom][0]<<","<<mBottomVertices[third_bottom][1]<<","<<mBottomVertices[third_bottom][2]<<"), "
- << "("<<mVertices[fourth_top][0]<<","<<mVertices[fourth_top][1]<<","<<mVertices[fourth_top][2]<<"), " << endl;*/
- if (mFloorShadow)
- {
- glBegin(GL_QUADS);
- glVertex3f(mVertices[first_top][0], mVertices[first_top][1], mVertices[first_top][2]);
- glVertex3f(mBottomVertices[second_bottom][0], mBottomVertices[second_bottom][1],mBottomVertices[second_bottom][2]);
- glVertex3f(mBottomVertices[third_bottom][0], mBottomVertices[third_bottom][1],mBottomVertices[third_bottom][2]);
- glVertex3f(mVertices[fourth_top][0], mVertices[fourth_top][1], mVertices[fourth_top][2]);
- glEnd();
- }
- else
- //Reverse order to fix culling
- {
- glBegin(GL_QUADS);
- glVertex3f(mVertices[first_top][0], mVertices[first_top][1], mVertices[first_top][2]);
- glVertex3f(mVertices[fourth_top][0], mVertices[fourth_top][1], mVertices[fourth_top][2]);
- glVertex3f(mBottomVertices[third_bottom][0], mBottomVertices[third_bottom][1],mBottomVertices[third_bottom][2]);
- glVertex3f(mBottomVertices[second_bottom][0], mBottomVertices[second_bottom][1],mBottomVertices[second_bottom][2]);
- glEnd();
- }
- }
Add Comment
Please, Sign In to add comment