Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JH456 committed Sep 12, 2016
0 parents commit df0469d
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 3dExample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// g++ 3dExample.cpp -lglut -lGL -lGLU -lSDL -o ddd
// g++ 3dExample.cpp -lglut -lGL -o ddd also works
// dependencies: mesa-common-dev and freeglut3-dev

#include "lib3d.cpp"

int main(int argc, char** argv) {
square = new polygon;
polygonColor(square, 1.0f, 0.0f, 0.0f);
addVertex(square, 0.0f, 10.0f, 0.0f);
addVertex(square, 0.6f, 10.0f, 0.0f);
addVertex(square, 0.6f, 10.0f, 0.6f);
addVertex(square, 0.0f, 10.0f, 0.6f);
square2 = new polygon;
polygonColor(square2, 0.0f, 0.0f, 1.0f);
addVertex(square2, 0.0f, 10.6f, 0.0f);
addVertex(square2, 0.6f, 10.6f, 0.0f);
addVertex(square2, 0.6f, 10.6f, 0.6f);
addVertex(square2, 0.0f, 10.6f, 0.6f);
glutInit(&argc, argv);
initGL();
glutMainLoop();
destroyPolygon(square);
destroyPolygon(square2);
return 0;
}
112 changes: 112 additions & 0 deletions lib3d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "shaped3d.cpp"

void MyKeyboardFunc(unsigned char key, int x, int y);
//void MySpecialFunc(int key, int x, int y);
void MyUpFunc(unsigned char key, int x, int y);
//void MySpecialUpFunc(int key, int x, int y);
void paintAgain(int i);
void display();
void drawPolygon(polygon* poly);

polygon* square;
polygon* square2;

/* Initialize OpenGL Graphics */
void initGL() {
glutCreateWindow("Vertex, Primitive & Color");
glutInitWindowSize(320, 320);
glutInitWindowPosition(50, 50);
glutDisplayFunc(display);
glutIgnoreKeyRepeat(1);
// Set "clearing" or background color
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glutKeyboardFunc(MyKeyboardFunc);
//glutSpecialFunc(MySpecialFunc);
glutKeyboardUpFunc(MyUpFunc);
//glutSpecialUpFunc(MySpecialUpFunc);
//glutIdleFunc(update) // IMPLEMENT THIS ONCE WE ARE NETWORKING
}

void MyKeyboardFunc(unsigned char key, int x, int y)
{
switch(key) {
case 'w':
vertical += 1.0f;
break;
case 's':
vertical -= 1.0f;
break;
case 'a':
thetaVel += -rotVel;
break;
case 'd':
thetaVel += rotVel;
break;
case 'q':
horizontal -= 1.0f;
break;
case 'e':
horizontal += 1.0f;
break;
};
}

void MyUpFunc(unsigned char key, int x, int y) {
switch(key) {
case 'w':
vertical -= 1.0f;
break;
case 's':
vertical += 1.0f;
break;
case 'a':
thetaVel -= -rotVel;
break;
case 'd':
thetaVel -= rotVel;
break;
case 'q':
horizontal += 1.0f;
break;
case 'e':
horizontal -= 1.0f;
break;
};
}

void paintAgain(int i) {
glutPostRedisplay();
}

/* Handler for window-repaint event. Call back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
// Clear the color buffer with current clearing color
glClear(GL_COLOR_BUFFER_BIT);

//Move these to some other thread once we get networking
if (vertical != 0 || horizontal != 0) {
float movementAngle = horizontal == 0
? vertical >= 0
? 0
: PI
: atan(vertical / horizontal);
if (horizontal > 0) movementAngle = (PI / 2.0f) - movementAngle;
else if (horizontal < 0) movementAngle = (3.0f * PI / 2.0f) - movementAngle;
movementAngle += theta;
screeny += vel * sin(movementAngle);
screenx += vel * cos(movementAngle);
}
theta += thetaVel;
if (theta < 0) {
theta += (PI * 2.0);
}
theta = std::fmod(theta, 2.0 * PI);

drawPolygon(square);
drawPolygon(square2);

// Render now
glFlush();
glutTimerFunc(1000.0 / 60.0, paintAgain, 0);
}
111 changes: 111 additions & 0 deletions shaped3d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <GL/glut.h>
#include <iostream>
#include <math.h>
#include <cmath>

const float vel = .1f;
const float rotVel = .01f;
const float PI = 3.14159f;
float screenx = 0.0f;
float screenz = 0.0f;
float screeny = 0.0f;
float d = -2.0f;
float theta = 1.57f;
float xVel = 0.0f;
float yVel = 0.0f;
float zVel = 0.0f;
float thetaVel = 0.0f;
float vertical = 0.0f;
float horizontal = 0.0f;

struct vertex;
struct polygon;

struct vertex {
float v_x;
float v_y;
float v_z;
vertex* v_next = NULL;
};

struct polygon {
float p_red;
float p_green;
float p_blue;
int p_numVertices;
vertex* p_vertexRoot;
};

void addVertex(polygon* poly, float x, float y, float z) {
if (poly->p_vertexRoot == NULL) {
poly->p_vertexRoot = new vertex;
poly->p_vertexRoot->v_x = x;
poly->p_vertexRoot->v_y = y;
poly->p_vertexRoot->v_z = z;
} else {
vertex* lastVertex = poly->p_vertexRoot;
while (lastVertex->v_next != NULL) {
lastVertex = lastVertex->v_next;
}
lastVertex->v_next = new vertex;
lastVertex = lastVertex->v_next;
lastVertex->v_x = x;
lastVertex->v_y = y;
lastVertex->v_z = z;
}
poly->p_numVertices++;
}

void polygonColor(polygon* poly, float red, float green, float blue) {
poly->p_red = red;
poly->p_green = green;
poly->p_blue = blue;
}

void drawPolygon(polygon* poly) {
vertex* currentVertex = poly->p_vertexRoot;
glBegin(GL_POLYGON);
glColor3f(poly->p_red, poly->p_green, poly->p_blue);
while (currentVertex != NULL) {
float deltaX = currentVertex->v_x - screenx;
float deltaY = currentVertex->v_y - screeny;
float deltaZ = currentVertex->v_z - screenz;
float phi = deltaY == 0
? deltaX >= 0
? 0
: PI
: atan(deltaX / deltaY);

if (deltaY > 0) phi = (PI / 2.0f) - phi;
else if (deltaY < 0) phi = (3.0f * PI / 2.0f) - phi;
// phi is the vertex's angle to the camera coords (lower left hand corner)
float angle = phi - theta;
if ((theta >= phi && (angle >= -PI / 2.0f || angle <= -3.0f * PI / 2.0f)) || (theta < phi && (angle <= PI
/ 2.0f || angle >= 3.0f * PI / 2.0f))) {
float deltaX2 = sqrt((deltaX * deltaX) + (deltaY * deltaY)) * cos((PI / 2.0f) + theta - phi);
float deltaY2 = sqrt((deltaX * deltaX) + (deltaY * deltaY)) * sin((PI / 2.0f) + theta - phi);
glVertex2f(d * deltaX2 / (d - deltaY2), d * deltaZ / (d - deltaY2));
}
std::cout<< "PHI " << phi << " THETA " << theta << std::endl;
currentVertex = currentVertex->v_next;
}
glEnd();
}

void listVertices(polygon* poly) {
vertex* currentVertex = poly->p_vertexRoot;
while (currentVertex != NULL) {
std::cout << currentVertex->v_x << " " << currentVertex->v_y << " " << currentVertex->v_z << " " << std::endl;
currentVertex = currentVertex->v_next;
}
}

void destroyVertex(vertex* vert) {
if (vert->v_next != NULL) destroyVertex(vert->v_next);
delete vert;
}

void destroyPolygon(polygon* poly) {
if (poly->p_vertexRoot != NULL) destroyVertex(poly->p_vertexRoot);
delete poly;
}

0 comments on commit df0469d

Please sign in to comment.