aboutsummaryrefslogtreecommitdiff
path: root/src/models.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/models.c')
-rw-r--r--src/models.c628
1 files changed, 314 insertions, 314 deletions
diff --git a/src/models.c b/src/models.c
index d66d2cce..95dbae7c 100644
--- a/src/models.c
+++ b/src/models.c
@@ -5,15 +5,15 @@
* Basic functions to draw 3d shapes and load/draw 3d models (.OBJ)
*
* Copyright (c) 2013 Ramon Santamaria (Ray San - raysan@raysanweb.com)
-*
-* This software is provided "as-is", without any express or implied warranty. In no event
+*
+* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
-* Permission is granted to anyone to use this software for any purpose, including commercial
+* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
-* 1. The origin of this software must not be misrepresented; you must not claim that you
-* wrote the original software. If you use this software in a product, an acknowledgment
+* 1. The origin of this software must not be misrepresented; you must not claim that you
+* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
@@ -72,60 +72,60 @@ void DrawCube(Vector3 position, float width, float height, float lenght, Color c
//rlTranslatef(0.0f, 0.0f, 0.0f);
//rlScalef(2.0f, 2.0f, 2.0f);
//rlRotatef(45, 0, 1, 0);
-
+
rlBegin(RL_TRIANGLES);
- rlColor4ub(color.r, color.g, color.b, color.a);
-
+ rlColor4ub(color.r, color.g, color.b, color.a);
+
// Front Face -----------------------------------------------------
rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Bottom Left
rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Right
rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left
-
+
rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Top Right
rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left
rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Right
-
+
// Back Face ------------------------------------------------------
rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Bottom Left
rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left
rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Bottom Right
-
+
rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Right
rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Bottom Right
rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left
-
+
// Top Face -------------------------------------------------------
rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left
rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Bottom Left
rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Bottom Right
-
+
rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Right
rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left
rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Bottom Right
-
+
// Bottom Face ----------------------------------------------------
rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Top Left
rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Right
rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Bottom Left
-
+
rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Top Right
rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Right
rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Top Left
-
+
// Right face -----------------------------------------------------
rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Bottom Right
rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Right
rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Top Left
-
+
rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Left
rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Bottom Right
rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Top Left
-
+
// Left Face ------------------------------------------------------
rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Bottom Right
rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left
rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Right
-
+
rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Bottom Left
rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left
rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Bottom Right
@@ -145,53 +145,53 @@ void DrawCubeWires(Vector3 position, float width, float height, float lenght, Co
float x = position.x;
float y = position.y;
float z = position.z;
-
+
rlPushMatrix();
//rlRotatef(45, 0, 1, 0);
-
+
rlBegin(RL_LINES);
- rlColor4ub(color.r, color.g, color.b, color.a);
-
+ rlColor4ub(color.r, color.g, color.b, color.a);
+
// Front Face -----------------------------------------------------
// Bottom Line
rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Bottom Left
rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Right
-
+
// Left Line
rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Bottom Right
rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Top Right
-
+
// Top Line
rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Top Right
rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left
-
+
// Right Line
rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left
rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Bottom Left
-
+
// Back Face ------------------------------------------------------
// Bottom Line
rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Bottom Left
rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Bottom Right
-
+
// Left Line
rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Bottom Right
rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Right
-
+
// Top Line
rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Right
rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left
-
+
// Right Line
rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left
rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Bottom Left
-
+
// Top Face -------------------------------------------------------
// Left Line
rlVertex3f(x-width/2, y+height/2, z+lenght/2); // Top Left Front
rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left Back
-
+
// Right Line
rlVertex3f(x+width/2, y+height/2, z+lenght/2); // Top Right Front
rlVertex3f(x+width/2, y+height/2, z-lenght/2); // Top Right Back
@@ -200,7 +200,7 @@ void DrawCubeWires(Vector3 position, float width, float height, float lenght, Co
// Left Line
rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Top Left Front
rlVertex3f(x-width/2, y-height/2, z-lenght/2); // Top Left Back
-
+
// Right Line
rlVertex3f(x+width/2, y-height/2, z+lenght/2); // Top Right Front
rlVertex3f(x+width/2, y-height/2, z-lenght/2); // Top Right Back
@@ -217,15 +217,15 @@ void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float hei
float z = position.z;
rlEnableTexture(texture.id);
-
- //rlPushMatrix();
+
+ //rlPushMatrix();
// NOTE: Be careful! Function order matters (scale, translate, rotate)
//rlScalef(2.0f, 2.0f, 2.0f);
//rlTranslatef(2.0f, 0.0f, 0.0f);
//rlRotatef(45, 0, 1, 0);
-
+
rlBegin(RL_QUADS);
- rlColor4ub(color.r, color.g, color.b, color.a);
+ rlColor4ub(color.r, color.g, color.b, color.a);
// Front Face
rlNormal3f(0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer
rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x-width/2, y-height/2, z+lenght/2); // Bottom Left Of The Texture and Quad
@@ -264,7 +264,7 @@ void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float hei
rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x-width/2, y+height/2, z-lenght/2); // Top Left Of The Texture and Quad
rlEnd();
//rlPopMatrix();
-
+
rlDisableTexture();
}
@@ -281,32 +281,32 @@ void DrawSphereEx(Vector3 centerPos, float radius, int rings, int slices, Color
rlTranslatef(centerPos.x, centerPos.y, centerPos.z);
rlScalef(radius, radius, radius);
//rlRotatef(rotation, 0, 1, 0);
-
+
rlBegin(RL_TRIANGLES);
rlColor4ub(color.r, color.g, color.b, color.a);
-
+
for(int i = 0; i < (rings + 2); i++)
{
for(int j = 0; j < slices; j++)
{
- rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*i)) * sin(DEG2RAD*(j*360/slices)),
- sin(DEG2RAD*(270+(180/(rings + 1))*i)),
+ rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*i)) * sin(DEG2RAD*(j*360/slices)),
+ sin(DEG2RAD*(270+(180/(rings + 1))*i)),
cos(DEG2RAD*(270+(180/(rings + 1))*i)) * cos(DEG2RAD*(j*360/slices)));
- rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * sin(DEG2RAD*((j+1)*360/slices)),
- sin(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
+ rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * sin(DEG2RAD*((j+1)*360/slices)),
+ sin(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * cos(DEG2RAD*((j+1)*360/slices)));
- rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * sin(DEG2RAD*(j*360/slices)),
- sin(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
+ rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * sin(DEG2RAD*(j*360/slices)),
+ sin(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * cos(DEG2RAD*(j*360/slices)));
- rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*i)) * sin(DEG2RAD*(j*360/slices)),
- sin(DEG2RAD*(270+(180/(rings + 1))*i)),
+ rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*i)) * sin(DEG2RAD*(j*360/slices)),
+ sin(DEG2RAD*(270+(180/(rings + 1))*i)),
cos(DEG2RAD*(270+(180/(rings + 1))*i)) * cos(DEG2RAD*(j*360/slices)));
- rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i))) * sin(DEG2RAD*((j+1)*360/slices)),
- sin(DEG2RAD*(270+(180/(rings + 1))*(i))),
+ rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i))) * sin(DEG2RAD*((j+1)*360/slices)),
+ sin(DEG2RAD*(270+(180/(rings + 1))*(i))),
cos(DEG2RAD*(270+(180/(rings + 1))*(i))) * cos(DEG2RAD*((j+1)*360/slices)));
- rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * sin(DEG2RAD*((j+1)*360/slices)),
- sin(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
+ rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * sin(DEG2RAD*((j+1)*360/slices)),
+ sin(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * cos(DEG2RAD*((j+1)*360/slices)));
}
}
@@ -321,33 +321,33 @@ void DrawSphereWires(Vector3 centerPos, float radius, int rings, int slices, Col
rlTranslatef(centerPos.x, centerPos.y, centerPos.z);
rlScalef(radius, radius, radius);
//rlRotatef(rotation, 0, 1, 0);
-
+
rlBegin(RL_LINES);
rlColor4ub(color.r, color.g, color.b, color.a);
-
+
for(int i = 0; i < (rings + 2); i++)
{
for(int j = 0; j < slices; j++)
{
- rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*i)) * sin(DEG2RAD*(j*360/slices)),
- sin(DEG2RAD*(270+(180/(rings + 1))*i)),
+ rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*i)) * sin(DEG2RAD*(j*360/slices)),
+ sin(DEG2RAD*(270+(180/(rings + 1))*i)),
cos(DEG2RAD*(270+(180/(rings + 1))*i)) * cos(DEG2RAD*(j*360/slices)));
- rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * sin(DEG2RAD*((j+1)*360/slices)),
- sin(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
+ rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * sin(DEG2RAD*((j+1)*360/slices)),
+ sin(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * cos(DEG2RAD*((j+1)*360/slices)));
-
- rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * sin(DEG2RAD*((j+1)*360/slices)),
- sin(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
+
+ rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * sin(DEG2RAD*((j+1)*360/slices)),
+ sin(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * cos(DEG2RAD*((j+1)*360/slices)));
- rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * sin(DEG2RAD*(j*360/slices)),
- sin(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
+ rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * sin(DEG2RAD*(j*360/slices)),
+ sin(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * cos(DEG2RAD*(j*360/slices)));
-
- rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * sin(DEG2RAD*(j*360/slices)),
- sin(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
+
+ rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * sin(DEG2RAD*(j*360/slices)),
+ sin(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
cos(DEG2RAD*(270+(180/(rings + 1))*(i+1))) * cos(DEG2RAD*(j*360/slices)));
- rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*i)) * sin(DEG2RAD*(j*360/slices)),
- sin(DEG2RAD*(270+(180/(rings + 1))*i)),
+ rlVertex3f(cos(DEG2RAD*(270+(180/(rings + 1))*i)) * sin(DEG2RAD*(j*360/slices)),
+ sin(DEG2RAD*(270+(180/(rings + 1))*i)),
cos(DEG2RAD*(270+(180/(rings + 1))*i)) * cos(DEG2RAD*(j*360/slices)));
}
}
@@ -360,7 +360,7 @@ void DrawSphereWires(Vector3 centerPos, float radius, int rings, int slices, Col
void DrawCylinder(Vector3 position, float radiusTop, float radiusBottom, float height, int sides, Color color)
{
if (sides < 3) sides = 3;
-
+
rlPushMatrix();
rlTranslatef(position.x, position.y, position.z);
@@ -375,12 +375,12 @@ void DrawCylinder(Vector3 position, float radiusTop, float radiusBottom, float h
rlVertex3f(sin(DEG2RAD*i) * radiusBottom, 0, cos(DEG2RAD*i) * radiusBottom); //Bottom Left
rlVertex3f(sin(DEG2RAD*(i+360/sides)) * radiusBottom, 0, cos(DEG2RAD*(i+360/sides)) * radiusBottom); //Bottom Right
rlVertex3f(sin(DEG2RAD*(i+360/sides)) * radiusTop, height, cos(DEG2RAD*(i+360/sides)) * radiusTop); //Top Right
-
+
rlVertex3f(sin(DEG2RAD*i) * radiusTop, height, cos(DEG2RAD*i) * radiusTop); //Top Left
rlVertex3f(sin(DEG2RAD*i) * radiusBottom, 0, cos(DEG2RAD*i) * radiusBottom); //Bottom Left
rlVertex3f(sin(DEG2RAD*(i+360/sides)) * radiusTop, height, cos(DEG2RAD*(i+360/sides)) * radiusTop); //Top Right
}
-
+
// Draw Cap --------------------------------------------------------------------------------------
for(int i = 0; i < 360; i += 360/sides)
{
@@ -399,7 +399,7 @@ void DrawCylinder(Vector3 position, float radiusTop, float radiusBottom, float h
rlVertex3f(sin(DEG2RAD*(i+360/sides)) * radiusBottom, 0, cos(DEG2RAD*(i+360/sides)) * radiusBottom);
}
}
-
+
// Draw Base -----------------------------------------------------------------------------------------
for(int i = 0; i < 360; i += 360/sides)
{
@@ -407,7 +407,7 @@ void DrawCylinder(Vector3 position, float radiusTop, float radiusBottom, float h
rlVertex3f(sin(DEG2RAD*(i+360/sides)) * radiusBottom, 0, cos(DEG2RAD*(i+360/sides)) * radiusBottom);
rlVertex3f(sin(DEG2RAD*i) * radiusBottom, 0, cos(DEG2RAD*i) * radiusBottom);
}
- rlEnd();
+ rlEnd();
rlPopMatrix();
}
@@ -416,24 +416,24 @@ void DrawCylinder(Vector3 position, float radiusTop, float radiusBottom, float h
void DrawCylinderWires(Vector3 position, float radiusTop, float radiusBottom, float height, int sides, Color color)
{
if(sides < 3) sides = 3;
-
+
rlPushMatrix();
rlTranslatef(position.x, position.y, position.z);
-
+
rlBegin(RL_LINES);
rlColor4ub(color.r, color.g, color.b, color.a);
-
+
for(int i = 0; i < 360; i += 360/sides)
{
rlVertex3f(sin(DEG2RAD*i) * radiusBottom, 0, cos(DEG2RAD*i) * radiusBottom);
rlVertex3f(sin(DEG2RAD*(i+360/sides)) * radiusBottom, 0, cos(DEG2RAD*(i+360/sides)) * radiusBottom);
-
+
rlVertex3f(sin(DEG2RAD*(i+360/sides)) * radiusBottom, 0, cos(DEG2RAD*(i+360/sides)) * radiusBottom);
rlVertex3f(sin(DEG2RAD*(i+360/sides)) * radiusTop, height, cos(DEG2RAD*(i+360/sides)) * radiusTop);
-
+
rlVertex3f(sin(DEG2RAD*(i+360/sides)) * radiusTop, height, cos(DEG2RAD*(i+360/sides)) * radiusTop);
rlVertex3f(sin(DEG2RAD*i) * radiusTop, height, cos(DEG2RAD*i) * radiusTop);
-
+
rlVertex3f(sin(DEG2RAD*i) * radiusTop, height, cos(DEG2RAD*i) * radiusTop);
rlVertex3f(sin(DEG2RAD*i) * radiusBottom, 0, cos(DEG2RAD*i) * radiusBottom);
}
@@ -449,15 +449,15 @@ void DrawPlane(Vector3 centerPos, Vector2 size, Vector3 rotation, Color color)
rlPushMatrix();
rlTranslatef(centerPos.x, centerPos.y, centerPos.z);
rlScalef(size.x, 1.0f, size.y);
-
+
// TODO: Review multiples rotations Gimbal-Lock... use matrix or quaternions...
rlRotatef(rotation.x, 1, 0, 0);
rlRotatef(rotation.y, 0, 1, 0);
rlRotatef(rotation.z, 0, 0, 1);
-
+
rlBegin(RL_QUADS);
rlColor4ub(color.r, color.g, color.b, color.a);
- rlNormal3f(0.0f, 1.0f, 0.0f);
+ rlNormal3f(0.0f, 1.0f, 0.0f);
rlTexCoord2f(0.0f, 0.0f); rlVertex3f(-0.5f, 0.0f, -0.5f);
rlTexCoord2f(1.0f, 0.0f); rlVertex3f(0.5f, 0.0f, -0.5f);
rlTexCoord2f(1.0f, 1.0f); rlVertex3f(0.5f, 0.0f, 0.5f);
@@ -472,7 +472,7 @@ void DrawPlaneEx(Vector3 centerPos, Vector2 size, Vector3 rotation, int slicesX,
{
float quadWidth = size.x / slicesX;
float quadLenght = size.y / slicesZ;
-
+
float texPieceW = 1 / size.x;
float texPieceH = 1 / size.y;
@@ -480,16 +480,16 @@ void DrawPlaneEx(Vector3 centerPos, Vector2 size, Vector3 rotation, int slicesX,
rlPushMatrix();
rlTranslatef(-size.x / 2, 0.0f, -size.y / 2);
rlTranslatef(centerPos.x, centerPos.y, centerPos.z);
-
+
// TODO: Review multiples rotations Gimbal-Lock... use matrix or quaternions...
rlRotatef(rotation.x, 1, 0, 0);
rlRotatef(rotation.y, 0, 1, 0);
rlRotatef(rotation.z, 0, 0, 1);
-
+
rlBegin(RL_QUADS);
rlColor4ub(color.r, color.g, color.b, color.a);
rlNormal3f(0.0f, 1.0f, 0.0f);
-
+
for (int z = 0; z < slicesZ; z++)
{
for (int x = 0; x < slicesX; x++)
@@ -497,13 +497,13 @@ void DrawPlaneEx(Vector3 centerPos, Vector2 size, Vector3 rotation, int slicesX,
// Draw the plane quad by quad (with textcoords)
rlTexCoord2f((float)x * texPieceW, (float)z * texPieceH);
rlVertex3f((float)x * quadWidth, 0.0f, (float)z * quadLenght);
-
+
rlTexCoord2f((float)x * texPieceW + texPieceW, (float)z * texPieceH);
rlVertex3f((float)x * quadWidth + quadWidth, 0.0f, (float)z * quadLenght);
-
+
rlTexCoord2f((float)x * texPieceW + texPieceW, (float)z * texPieceH + texPieceH);
rlVertex3f((float)x * quadWidth + quadWidth, 0.0f, (float)z * quadLenght + quadLenght);
-
+
rlTexCoord2f((float)x * texPieceW, (float)z * texPieceH + texPieceH);
rlVertex3f((float)x * quadWidth, 0.0f, (float)z * quadLenght + quadLenght);
}
@@ -535,7 +535,7 @@ void DrawGrid(int slices, float spacing)
rlColor3f(0.75f, 0.75f, 0.75f);
rlColor3f(0.75f, 0.75f, 0.75f);
}
-
+
rlVertex3f((float)i*spacing, 0.0f, (float)-halfSlices*spacing);
rlVertex3f((float)i*spacing, 0.0f, (float)halfSlices*spacing);
@@ -555,22 +555,22 @@ void DrawGizmo(Vector3 position)
rlTranslatef(position.x, position.y, position.z);
//rlRotatef(rotation, 0, 1, 0);
rlScalef(lenght, lenght, lenght);
-
+
rlBegin(RL_LINES);
rlColor3f(1.0f, 0.0f, 0.0f); rlVertex3f(0.0f, 0.0f, 0.0f);
rlColor3f(1.0f, 0.0f, 0.0f); rlVertex3f(1.0f, 0.0f, 0.0f);
-
+
rlColor3f(0.0f, 1.0f, 0.0f); rlVertex3f(0.0f, 0.0f, 0.0f);
rlColor3f(0.0f, 1.0f, 0.0f); rlVertex3f(0.0f, 1.0f, 0.0f);
-
+
rlColor3f(0.0f, 0.0f, 1.0f); rlVertex3f(0.0f, 0.0f, 0.0f);
rlColor3f(0.0f, 0.0f, 1.0f); rlVertex3f(0.0f, 0.0f, 1.0f);
- rlEnd();
+ rlEnd();
rlPopMatrix();
}
void DrawGizmoEx(Vector3 position, Vector3 rotation, float scale)
-{
+{
// NOTE: RGB = XYZ
rlPushMatrix();
rlTranslatef(position.x, position.y, position.z);
@@ -581,53 +581,53 @@ void DrawGizmoEx(Vector3 position, Vector3 rotation, float scale)
// X Axis
rlColor4ub(200, 0, 0, 255); rlVertex3f(position.x, position.y, position.z);
rlColor4ub(200, 0, 0, 255); rlVertex3f(position.x + 1, position.y, position.z);
-
+
// ArrowX
rlColor4ub(200, 0, 0, 255); rlVertex3f(position.x + 1.1, position.y, position.z);
rlColor4ub(200, 0, 0, 255); rlVertex3f(position.x + .9, position.y, position.z + .1);
-
+
rlColor4ub(200, 0, 0, 255); rlVertex3f(position.x + 1.1, position.y, position.z);
rlColor4ub(200, 0, 0, 255); rlVertex3f(position.x + .9, position.y, position.z - .1);
-
+
// Y Axis
rlColor4ub(0, 200, 0, 255); rlVertex3f(position.x, position.y, position.z);
rlColor4ub(0, 200, 0, 255); rlVertex3f(position.x, position.y + 1, position.z);
-
+
// ArrowY
rlColor4ub(0, 200, 0, 255); rlVertex3f(position.x, position.y + 1.1, position.z);
rlColor4ub(0, 200, 0, 255); rlVertex3f(position.x + .1, position.y + .9, position.z);
-
+
rlColor4ub(0, 200, 0, 255); rlVertex3f(position.x, position.y + 1.1, position.z);
rlColor4ub(0, 200, 0, 255); rlVertex3f(position.x - .1, position.y + .9, position.z);
-
+
// Z Axis
rlColor4ub(0, 0, 200, 255); rlVertex3f(position.x, position.y, position.z);
rlColor4ub(0, 0, 200, 255); rlVertex3f(position.x, position.y, position.z - 1);
-
+
// ArrowZ
rlColor4ub(0, 0, 200, 255); rlVertex3f(position.x, position.y, position.z - 1.1);
rlColor4ub(0, 0, 200, 255); rlVertex3f(position.x + .1, position.y, position.z - .9);
-
+
rlColor4ub(0, 0, 200, 255); rlVertex3f(position.x, position.y, position.z - 1.1);
rlColor4ub(0, 0, 200, 255); rlVertex3f(position.x - .1, position.y, position.z - .9);
-
+
// Extra
int n = 3;
-
+
// X Axis
for (int i=0; i < 360; i += 6)
{
rlColor4ub(200, 0, 0, 255); rlVertex3f(0, position.x + sin(DEG2RAD*i) * scale/n, position.y + cos(DEG2RAD*i) * scale/n);
rlColor4ub(200, 0, 0, 255); rlVertex3f(0, position.x + sin(DEG2RAD*(i+6)) * scale/n, position.y + cos(DEG2RAD*(i+6)) * scale/n);
}
-
+
// Y Axis
for (int i=0; i < 360; i += 6)
{
rlColor4ub(0, 200, 0, 255); rlVertex3f(position.x + sin(DEG2RAD*i) * scale/n, 0, position.y + cos(DEG2RAD*i) * scale/n);
rlColor4ub(0, 200, 0, 255); rlVertex3f(position.x + sin(DEG2RAD*(i+6)) * scale/n, 0, position.y + cos(DEG2RAD*(i+6)) * scale/n);
}
-
+
// Z Axis
for (int i=0; i < 360; i += 6)
{
@@ -639,18 +639,18 @@ void DrawGizmoEx(Vector3 position, Vector3 rotation, float scale)
}
// Load a 3d model
-Model LoadModel(const char *fileName)
+Model LoadModel(const char *fileName)
{
VertexData vData;
-
+
if (strcmp(GetExtension(fileName),"obj") == 0) vData = LoadOBJ(fileName);
- else TraceLog(WARNING, "[%s] Model extension not recognized, it can't be loaded", fileName);
+ else TraceLog(WARNING, "[%s] Model extension not recognized, it can't be loaded", fileName);
Model model;
model.mesh = vData; // Model mesh is vertex data
model.textureId = 0;
-
+
#if defined(USE_OPENGL_33) || defined(USE_OPENGL_ES2)
model.vaoId = rlglLoadModel(vData); // Use loaded data to generate VAO
model.textureId = 1; // Default whiteTexture
@@ -671,24 +671,24 @@ Model LoadHeightmap(Image heightmap, float maxHeight)
int mapX = heightmap.width;
int mapZ = heightmap.height;
-
+
// NOTE: One vertex per pixel
// TODO: Consider resolution when generating model data?
int numTriangles = (mapX-1)*(mapZ-1)*2; // One quad every four pixels
-
+
vData.vertexCount = numTriangles*3;
vData.vertices = (float *)malloc(vData.vertexCount * 3 * sizeof(float));
vData.normals = (float *)malloc(vData.vertexCount * 3 * sizeof(float));
vData.texcoords = (float *)malloc(vData.vertexCount * 2 * sizeof(float));
vData.colors = (unsigned char *)malloc(vData.vertexCount * 4 * sizeof(unsigned char));
-
+
int vCounter = 0; // Used to count vertices float by float
int tcCounter = 0; // Used to count texcoords float by float
int nCounter = 0; // Used to count normals float by float
-
+
int trisCounter = 0;
-
+
float scaleFactor = maxHeight/255; // TODO: Review scaleFactor calculation
for(int z = 0; z < mapZ-1; z++)
@@ -697,83 +697,83 @@ Model LoadHeightmap(Image heightmap, float maxHeight)
{
// Fill vertices array with data
//----------------------------------------------------------
-
+
// one triangle - 3 vertex
vData.vertices[vCounter] = x;
vData.vertices[vCounter + 1] = GetHeightValue(heightmap.pixels[x + z*mapX])*scaleFactor;
vData.vertices[vCounter + 2] = z;
-
+
vData.vertices[vCounter + 3] = x;
vData.vertices[vCounter + 4] = GetHeightValue(heightmap.pixels[x + (z+1)*mapX])*scaleFactor;
vData.vertices[vCounter + 5] = z+1;
-
+
vData.vertices[vCounter + 6] = x+1;
vData.vertices[vCounter + 7] = GetHeightValue(heightmap.pixels[(x+1) + z*mapX])*scaleFactor;
vData.vertices[vCounter + 8] = z;
-
+
// another triangle - 3 vertex
vData.vertices[vCounter + 9] = vData.vertices[vCounter + 6];
vData.vertices[vCounter + 10] = vData.vertices[vCounter + 7];
vData.vertices[vCounter + 11] = vData.vertices[vCounter + 8];
-
+
vData.vertices[vCounter + 12] = vData.vertices[vCounter + 3];
vData.vertices[vCounter + 13] = vData.vertices[vCounter + 4];
vData.vertices[vCounter + 14] = vData.vertices[vCounter + 5];
-
+
vData.vertices[vCounter + 15] = x+1;
vData.vertices[vCounter + 16] = GetHeightValue(heightmap.pixels[(x+1) + (z+1)*mapX])*scaleFactor;
vData.vertices[vCounter + 17] = z+1;
vCounter += 18; // 6 vertex, 18 floats
-
+
// Fill texcoords array with data
//--------------------------------------------------------------
vData.texcoords[tcCounter] = (float)x / (mapX-1);
vData.texcoords[tcCounter + 1] = (float)z / (mapZ-1);
-
+
vData.texcoords[tcCounter + 2] = (float)x / (mapX-1);
vData.texcoords[tcCounter + 3] = (float)(z+1) / (mapZ-1);
-
+
vData.texcoords[tcCounter + 4] = (float)(x+1) / (mapX-1);
vData.texcoords[tcCounter + 5] = (float)z / (mapZ-1);
-
+
vData.texcoords[tcCounter + 6] = vData.texcoords[tcCounter + 4];
vData.texcoords[tcCounter + 7] = vData.texcoords[tcCounter + 5];
-
+
vData.texcoords[tcCounter + 8] = vData.texcoords[tcCounter + 2];
vData.texcoords[tcCounter + 9] = vData.texcoords[tcCounter + 1];
-
+
vData.texcoords[tcCounter + 10] = (float)(x+1) / (mapX-1);
vData.texcoords[tcCounter + 11] = (float)(z+1) / (mapZ-1);
tcCounter += 12; // 6 texcoords, 12 floats
-
+
// Fill normals array with data
//--------------------------------------------------------------
- // NOTE: Current Model implementation doe not use normals!
+ // NOTE: Current Model implementation doe not use normals!
for (int i = 0; i < 18; i += 3)
{
vData.normals[nCounter + i] = 0.0f;
vData.normals[nCounter + i + 1] = 1.0f;
vData.normals[nCounter + i + 2] = 0.0f;
}
-
+
// TODO: Calculate normals in an efficient way
-
+
nCounter += 18; // 6 vertex, 18 floats
-
+
trisCounter += 2;
}
}
-
+
// NOTE: At this point we have all vertex, texcoord, normal data for the model in vData struct
// Fill color data
for (int i = 0; i < (4*vData.vertexCount); i++) vData.colors[i] = 255;
-
+
Model model;
model.mesh = vData; // Model mesh is vertex data
model.textureId = 0;
-
+
#if defined(USE_OPENGL_33) || defined(USE_OPENGL_ES2)
model.vaoId = rlglLoadModel(vData); // Use loaded data to generate VAO
model.textureId = 1; // Default whiteTexture
@@ -796,26 +796,26 @@ Model LoadCubesmap(Image cubesmap)
float mapCubeSide = 1.0f;
int mapWidth = cubesmap.width * (int)mapCubeSide;
int mapHeight = cubesmap.height * (int)mapCubeSide;
-
- // NOTE: Max possible number of triangles numCubes * (12 triangles by cube)
+
+ // NOTE: Max possible number of triangles numCubes * (12 triangles by cube)
int maxTriangles = cubesmap.width*cubesmap.height*12;
int vCounter = 0; // Used to count vertices
int tcCounter = 0; // Used to count texcoords
int nCounter = 0; // Used to count normals
-
+
float w = mapCubeSide;
float h = mapCubeSide;
float h2 = mapCubeSide;
-
+
Vector3 *mapVertices = (Vector3 *)malloc(maxTriangles * 3 * sizeof(Vector3));
Vector2 *mapTexcoords = (Vector2 *)malloc(maxTriangles * 3 * sizeof(Vector2));
- Vector3 *mapNormals = (Vector3 *)malloc(maxTriangles * 3 * sizeof(Vector3));
-
+ Vector3 *mapNormals = (Vector3 *)malloc(maxTriangles * 3 * sizeof(Vector3));
+
for (int z = 0; z < mapHeight; z += mapCubeSide)
{
for (int x = 0; x < mapWidth; x += mapCubeSide)
- {
+ {
// Define the 8 vertex of the cube, we will combine them accordingly later...
Vector3 v1 = { x - w/2, h2, z - h/2 };
Vector3 v2 = { x - w/2, h2, z + h/2 };
@@ -825,7 +825,7 @@ Model LoadCubesmap(Image cubesmap)
Vector3 v6 = { x - w/2, 0, z - h/2 };
Vector3 v7 = { x - w/2, 0, z + h/2 };
Vector3 v8 = { x + w/2, 0, z + h/2 };
-
+
// Define the 6 normals of the cube, we will combine them accordingly later...
Vector3 n1 = { 1.0f, 0.0f, 0.0f };
Vector3 n2 = { -1.0f, 0.0f, 0.0f };
@@ -833,14 +833,14 @@ Model LoadCubesmap(Image cubesmap)
Vector3 n4 = { 0.0f, -1.0f, 0.0f };
Vector3 n5 = { 0.0f, 0.0f, 1.0f };
Vector3 n6 = { 0.0f, 0.0f, -1.0f };
-
+
// Define the 4 texture coordinates of the cube, we will combine them accordingly later...
// TODO: Use texture rectangles to define different textures for top-bottom-front-back-right-left (6)
- Vector2 vt2 = { 0.0f, 0.0f };
+ Vector2 vt2 = { 0.0f, 0.0f };
Vector2 vt1 = { 0.0f, 1.0f };
Vector2 vt4 = { 1.0f, 0.0f };
Vector2 vt3 = { 1.0f, 1.0f };
-
+
// We check pixel color to be WHITE, we will full cubes
if ((cubesmap.pixels[z*cubesmap.width + x].r == 255) &&
(cubesmap.pixels[z*cubesmap.width + x].g == 255) &&
@@ -848,58 +848,58 @@ Model LoadCubesmap(Image cubesmap)
{
// Define triangles (Checking Collateral Cubes!)
//----------------------------------------------
-
+
// Define top triangles (2 tris, 6 vertex --> v1-v2-v3, v1-v3-v4)
mapVertices[vCounter] = v1;
- mapVertices[vCounter + 1] = v2;
- mapVertices[vCounter + 2] = v3;
- mapVertices[vCounter + 3] = v1;
- mapVertices[vCounter + 4] = v3;
+ mapVertices[vCounter + 1] = v2;
+ mapVertices[vCounter + 2] = v3;
+ mapVertices[vCounter + 3] = v1;
+ mapVertices[vCounter + 4] = v3;
mapVertices[vCounter + 5] = v4;
vCounter += 6;
-
+
mapNormals[nCounter] = n3;
- mapNormals[nCounter + 1] = n3;
- mapNormals[nCounter + 2] = n3;
- mapNormals[nCounter + 3] = n3;
- mapNormals[nCounter + 4] = n3;
+ mapNormals[nCounter + 1] = n3;
+ mapNormals[nCounter + 2] = n3;
+ mapNormals[nCounter + 3] = n3;
+ mapNormals[nCounter + 4] = n3;
mapNormals[nCounter + 5] = n3;
nCounter += 6;
-
+
mapTexcoords[tcCounter] = vt2;
- mapTexcoords[tcCounter + 1] = vt1;
- mapTexcoords[tcCounter + 2] = vt3;
- mapTexcoords[tcCounter + 3] = vt2;
- mapTexcoords[tcCounter + 4] = vt3;
+ mapTexcoords[tcCounter + 1] = vt1;
+ mapTexcoords[tcCounter + 2] = vt3;
+ mapTexcoords[tcCounter + 3] = vt2;
+ mapTexcoords[tcCounter + 4] = vt3;
mapTexcoords[tcCounter + 5] = vt4;
tcCounter += 6;
-
+
// Define bottom triangles (2 tris, 6 vertex --> v6-v8-v7, v6-v5-v8)
mapVertices[vCounter] = v6;
- mapVertices[vCounter + 1] = v8;
- mapVertices[vCounter + 2] = v7;
- mapVertices[vCounter + 3] = v6;
- mapVertices[vCounter + 4] = v5;
+ mapVertices[vCounter + 1] = v8;
+ mapVertices[vCounter + 2] = v7;
+ mapVertices[vCounter + 3] = v6;
+ mapVertices[vCounter + 4] = v5;
mapVertices[vCounter + 5] = v8;
vCounter += 6;
-
+
mapNormals[nCounter] = n4;
- mapNormals[nCounter + 1] = n4;
- mapNormals[nCounter + 2] = n4;
- mapNormals[nCounter + 3] = n4;
- mapNormals[nCounter + 4] = n4;
+ mapNormals[nCounter + 1] = n4;
+ mapNormals[nCounter + 2] = n4;
+ mapNormals[nCounter + 3] = n4;
+ mapNormals[nCounter + 4] = n4;
mapNormals[nCounter + 5] = n4;
nCounter += 6;
-
+
mapTexcoords[tcCounter] = vt4;
- mapTexcoords[tcCounter + 1] = vt1;
- mapTexcoords[tcCounter + 2] = vt3;
- mapTexcoords[tcCounter + 3] = vt4;
- mapTexcoords[tcCounter + 4] = vt2;
+ mapTexcoords[tcCounter + 1] = vt1;
+ mapTexcoords[tcCounter + 2] = vt3;
+ mapTexcoords[tcCounter + 3] = vt4;
+ mapTexcoords[tcCounter + 4] = vt2;
mapTexcoords[tcCounter + 5] = vt1;
tcCounter += 6;
-
- if (((z < cubesmap.height - 1) &&
+
+ if (((z < cubesmap.height - 1) &&
(cubesmap.pixels[(z + 1)*cubesmap.width + x].r == 0) &&
(cubesmap.pixels[(z + 1)*cubesmap.width + x].g == 0) &&
(cubesmap.pixels[(z + 1)*cubesmap.width + x].b == 0)) || (z == cubesmap.height - 1))
@@ -907,30 +907,30 @@ Model LoadCubesmap(Image cubesmap)
// Define front triangles (2 tris, 6 vertex) --> v2 v7 v3, v3 v7 v8
// NOTE: Collateral occluded faces are not generated
mapVertices[vCounter] = v2;
- mapVertices[vCounter + 1] = v7;
- mapVertices[vCounter + 2] = v3;
- mapVertices[vCounter + 3] = v3;
- mapVertices[vCounter + 4] = v7;
+ mapVertices[vCounter + 1] = v7;
+ mapVertices[vCounter + 2] = v3;
+ mapVertices[vCounter + 3] = v3;
+ mapVertices[vCounter + 4] = v7;
mapVertices[vCounter + 5] = v8;
vCounter += 6;
-
+
mapNormals[nCounter] = n6;
- mapNormals[nCounter + 1] = n6;
- mapNormals[nCounter + 2] = n6;
- mapNormals[nCounter + 3] = n6;
- mapNormals[nCounter + 4] = n6;
+ mapNormals[nCounter + 1] = n6;
+ mapNormals[nCounter + 2] = n6;
+ mapNormals[nCounter + 3] = n6;
+ mapNormals[nCounter + 4] = n6;
mapNormals[nCounter + 5] = n6;
nCounter += 6;
-
+
mapTexcoords[tcCounter] = vt2;
- mapTexcoords[tcCounter + 1] = vt1;
- mapTexcoords[tcCounter + 2] = vt4;
- mapTexcoords[tcCounter + 3] = vt4;
- mapTexcoords[tcCounter + 4] = vt1;
+ mapTexcoords[tcCounter + 1] = vt1;
+ mapTexcoords[tcCounter + 2] = vt4;
+ mapTexcoords[tcCounter + 3] = vt4;
+ mapTexcoords[tcCounter + 4] = vt1;
mapTexcoords[tcCounter + 5] = vt3;
tcCounter += 6;
}
-
+
if (((z > 0) &&
(cubesmap.pixels[(z - 1)*cubesmap.width + x].r == 0) &&
(cubesmap.pixels[(z - 1)*cubesmap.width + x].g == 0) &&
@@ -939,30 +939,30 @@ Model LoadCubesmap(Image cubesmap)
// Define back triangles (2 tris, 6 vertex) --> v1 v5 v6, v1 v4 v5
// NOTE: Collateral occluded faces are not generated
mapVertices[vCounter] = v1;
- mapVertices[vCounter + 1] = v5;
- mapVertices[vCounter + 2] = v6;
- mapVertices[vCounter + 3] = v1;
- mapVertices[vCounter + 4] = v4;
+ mapVertices[vCounter + 1] = v5;
+ mapVertices[vCounter + 2] = v6;
+ mapVertices[vCounter + 3] = v1;
+ mapVertices[vCounter + 4] = v4;
mapVertices[vCounter + 5] = v5;
vCounter += 6;
-
+
mapNormals[nCounter] = n5;
- mapNormals[nCounter + 1] = n5;
- mapNormals[nCounter + 2] = n5;
- mapNormals[nCounter + 3] = n5;
- mapNormals[nCounter + 4] = n5;
+ mapNormals[nCounter + 1] = n5;
+ mapNormals[nCounter + 2] = n5;
+ mapNormals[nCounter + 3] = n5;
+ mapNormals[nCounter + 4] = n5;
mapNormals[nCounter + 5] = n5;
nCounter += 6;
-
+
mapTexcoords[tcCounter] = vt4;
- mapTexcoords[tcCounter + 1] = vt1;
- mapTexcoords[tcCounter + 2] = vt3;
- mapTexcoords[tcCounter + 3] = vt4;
- mapTexcoords[tcCounter + 4] = vt2;
+ mapTexcoords[tcCounter + 1] = vt1;
+ mapTexcoords[tcCounter + 2] = vt3;
+ mapTexcoords[tcCounter + 3] = vt4;
+ mapTexcoords[tcCounter + 4] = vt2;
mapTexcoords[tcCounter + 5] = vt1;
tcCounter += 6;
}
-
+
if (((x < cubesmap.width - 1) &&
(cubesmap.pixels[z*cubesmap.width + (x + 1)].r == 0) &&
(cubesmap.pixels[z*cubesmap.width + (x + 1)].g == 0) &&
@@ -971,30 +971,30 @@ Model LoadCubesmap(Image cubesmap)
// Define right triangles (2 tris, 6 vertex) --> v3 v8 v4, v4 v8 v5
// NOTE: Collateral occluded faces are not generated
mapVertices[vCounter] = v3;
- mapVertices[vCounter + 1] = v8;
- mapVertices[vCounter + 2] = v4;
- mapVertices[vCounter + 3] = v4;
- mapVertices[vCounter + 4] = v8;
+ mapVertices[vCounter + 1] = v8;
+ mapVertices[vCounter + 2] = v4;
+ mapVertices[vCounter + 3] = v4;
+ mapVertices[vCounter + 4] = v8;
mapVertices[vCounter + 5] = v5;
vCounter += 6;
-
+
mapNormals[nCounter] = n1;
- mapNormals[nCounter + 1] = n1;
- mapNormals[nCounter + 2] = n1;
- mapNormals[nCounter + 3] = n1;
- mapNormals[nCounter + 4] = n1;
+ mapNormals[nCounter + 1] = n1;
+ mapNormals[nCounter + 2] = n1;
+ mapNormals[nCounter + 3] = n1;
+ mapNormals[nCounter + 4] = n1;
mapNormals[nCounter + 5] = n1;
nCounter += 6;
-
+
mapTexcoords[tcCounter] = vt2;
- mapTexcoords[tcCounter + 1] = vt1;
- mapTexcoords[tcCounter + 2] = vt4;
- mapTexcoords[tcCounter + 3] = vt4;
- mapTexcoords[tcCounter + 4] = vt1;
+ mapTexcoords[tcCounter + 1] = vt1;
+ mapTexcoords[tcCounter + 2] = vt4;
+ mapTexcoords[tcCounter + 3] = vt4;
+ mapTexcoords[tcCounter + 4] = vt1;
mapTexcoords[tcCounter + 5] = vt3;
tcCounter += 6;
}
-
+
if (((x > 0) &&
(cubesmap.pixels[z*cubesmap.width + (x - 1)].r == 0) &&
(cubesmap.pixels[z*cubesmap.width + (x - 1)].g == 0) &&
@@ -1003,26 +1003,26 @@ Model LoadCubesmap(Image cubesmap)
// Define left triangles (2 tris, 6 vertex) --> v1 v7 v2, v1 v6 v7
// NOTE: Collateral occluded faces are not generated
mapVertices[vCounter] = v1;
- mapVertices[vCounter + 1] = v7;
- mapVertices[vCounter + 2] = v2;
- mapVertices[vCounter + 3] = v1;
- mapVertices[vCounter + 4] = v6;
+ mapVertices[vCounter + 1] = v7;
+ mapVertices[vCounter + 2] = v2;
+ mapVertices[vCounter + 3] = v1;
+ mapVertices[vCounter + 4] = v6;
mapVertices[vCounter + 5] = v7;
vCounter += 6;
-
+
mapNormals[nCounter] = n2;
- mapNormals[nCounter + 1] = n2;
- mapNormals[nCounter + 2] = n2;
- mapNormals[nCounter + 3] = n2;
- mapNormals[nCounter + 4] = n2;
+ mapNormals[nCounter + 1] = n2;
+ mapNormals[nCounter + 2] = n2;
+ mapNormals[nCounter + 3] = n2;
+ mapNormals[nCounter + 4] = n2;
mapNormals[nCounter + 5] = n2;
nCounter += 6;
-
+
mapTexcoords[tcCounter] = vt2;
- mapTexcoords[tcCounter + 1] = vt3;
- mapTexcoords[tcCounter + 2] = vt4;
- mapTexcoords[tcCounter + 3] = vt2;
- mapTexcoords[tcCounter + 4] = vt1;
+ mapTexcoords[tcCounter + 1] = vt3;
+ mapTexcoords[tcCounter + 2] = vt4;
+ mapTexcoords[tcCounter + 3] = vt2;
+ mapTexcoords[tcCounter + 4] = vt1;
mapTexcoords[tcCounter + 5] = vt3;
tcCounter += 6;
}
@@ -1034,28 +1034,28 @@ Model LoadCubesmap(Image cubesmap)
{
// Define top triangles (2 tris, 6 vertex --> v1-v3-v2, v1-v4-v3)
// TODO: ...
-
- // Define bottom triangles (2 tris, 6 vertex --> v6-v7-v8, v6-v8-v5)
+
+ // Define bottom triangles (2 tris, 6 vertex --> v6-v7-v8, v6-v8-v5)
// TODO: ...
}
- }
+ }
}
// Move data from mapVertices temp arays to vertices float array
vData.vertexCount = vCounter;
-
+
printf("Vertex count: %i\n", vCounter);
vData.vertices = (float *)malloc(vData.vertexCount * 3 * sizeof(float));
vData.normals = (float *)malloc(vData.vertexCount * 3 * sizeof(float));
vData.texcoords = (float *)malloc(vData.vertexCount * 2 * sizeof(float));
vData.colors = (unsigned char *)malloc(vData.vertexCount * 4 * sizeof(unsigned char));
-
+
// Fill color data
for (int i = 0; i < (4*vData.vertexCount); i++) vData.colors[i] = 255;
-
+
int fCounter = 0;
-
+
// Move vertices data
for (int i = 0; i < vCounter; i++)
{
@@ -1064,9 +1064,9 @@ Model LoadCubesmap(Image cubesmap)
vData.vertices[fCounter + 2] = mapVertices[i].z;
fCounter += 3;
}
-
+
fCounter = 0;
-
+
// Move normals data
for (int i = 0; i < nCounter; i++)
{
@@ -1075,9 +1075,9 @@ Model LoadCubesmap(Image cubesmap)
vData.normals[fCounter + 2] = mapNormals[i].z;
fCounter += 3;
}
-
+
fCounter = 0;
-
+
// Move texcoords data
for (int i = 0; i < tcCounter; i++)
{
@@ -1085,18 +1085,18 @@ Model LoadCubesmap(Image cubesmap)
vData.texcoords[fCounter + 1] = mapTexcoords[i].y;
fCounter += 2;
}
-
+
free(mapVertices);
free(mapNormals);
free(mapTexcoords);
-
+
// NOTE: At this point we have all vertex, texcoord, normal data for the model in vData struct
-
+
Model model;
model.mesh = vData; // Model mesh is vertex data
model.textureId = 0;
-
+
#if defined(USE_OPENGL_33) || defined(USE_OPENGL_ES2)
model.vaoId = rlglLoadModel(vData); // Use loaded data to generate VAO
model.textureId = 1; // Default whiteTexture
@@ -1106,7 +1106,7 @@ Model LoadCubesmap(Image cubesmap)
//free(vData.texcoords);
//free(vData.normals);
#endif
-
+
return model;
}
@@ -1157,22 +1157,22 @@ void DrawBillboard(Camera camera, Texture2D texture, Vector3 center, float size,
{
// NOTE: Billboard size will maintain texture aspect ratio, size will be billboard width
Vector2 sizeRatio = { size, size * (float)texture.height/texture.width };
-
+
Matrix viewMatrix = MatrixLookAt(camera.position, camera.target, camera.up);
MatrixTranspose(&viewMatrix);
-
+
Vector3 right = { viewMatrix.m0, viewMatrix.m4, viewMatrix.m8 };
Vector3 up = { viewMatrix.m1, viewMatrix.m5, viewMatrix.m9 };
-/*
+/*
d-------c
| |
| * |
| |
a-------b
-*/
+*/
VectorScale(&right, sizeRatio.x/2);
VectorScale(&up, sizeRatio.y/2);
-
+
Vector3 p1 = VectorAdd(right, up);
Vector3 p2 = VectorSubtract(right, up);
@@ -1180,18 +1180,18 @@ void DrawBillboard(Camera camera, Texture2D texture, Vector3 center, float size,
Vector3 b = VectorAdd(center, p1);
Vector3 c = VectorAdd(center, p2);
Vector3 d = VectorSubtract(center, p1);
-
+
rlEnableTexture(texture.id);
-
+
rlBegin(RL_QUADS);
rlColor4ub(tint.r, tint.g, tint.b, tint.a);
- rlNormal3f(0.0f, 1.0f, 0.0f);
+ rlNormal3f(0.0f, 1.0f, 0.0f);
rlTexCoord2f(0.0f, 0.0f); rlVertex3f(a.x, a.y, a.z);
rlTexCoord2f(1.0f, 0.0f); rlVertex3f(b.x, b.y, b.z);
rlTexCoord2f(1.0f, 1.0f); rlVertex3f(c.x, c.y, c.z);
rlTexCoord2f(0.0f, 1.0f); rlVertex3f(d.x, d.y, d.z);
rlEnd();
-
+
rlDisableTexture();
}
@@ -1203,16 +1203,16 @@ void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vec
Matrix viewMatrix = MatrixLookAt(camera.position, camera.target, camera.up);
MatrixTranspose(&viewMatrix);
-
+
Vector3 right = { viewMatrix.m0, viewMatrix.m4, viewMatrix.m8 };
Vector3 up = { viewMatrix.m1, viewMatrix.m5, viewMatrix.m9 };
-/*
+/*
d-------c
| |
| * |
| |
a-------b
-*/
+*/
VectorScale(&right, sizeRatio.x/2);
VectorScale(&up, sizeRatio.y/2);
@@ -1223,29 +1223,29 @@ void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vec
Vector3 b = VectorAdd(center, p1);
Vector3 c = VectorAdd(center, p2);
Vector3 d = VectorSubtract(center, p1);
-
+
rlEnableTexture(texture.id);
-
+
rlBegin(RL_QUADS);
rlColor4ub(tint.r, tint.g, tint.b, tint.a);
-
+
// Bottom-left corner for texture and quad
- rlTexCoord2f((float)sourceRec.x / texture.width, (float)sourceRec.y / texture.height);
+ rlTexCoord2f((float)sourceRec.x / texture.width, (float)sourceRec.y / texture.height);
rlVertex3f(a.x, a.y, a.z);
-
+
// Bottom-right corner for texture and quad
rlTexCoord2f((float)(sourceRec.x + sourceRec.width) / texture.width, (float)sourceRec.y / texture.height);
rlVertex3f(b.x, b.y, b.z);
-
+
// Top-right corner for texture and quad
- rlTexCoord2f((float)(sourceRec.x + sourceRec.width) / texture.width, (float)(sourceRec.y + sourceRec.height) / texture.height);
+ rlTexCoord2f((float)(sourceRec.x + sourceRec.width) / texture.width, (float)(sourceRec.y + sourceRec.height) / texture.height);
rlVertex3f(c.x, c.y, c.z);
-
+
// Top-left corner for texture and quad
rlTexCoord2f((float)sourceRec.x / texture.width, (float)(sourceRec.y + sourceRec.height) / texture.height);
rlVertex3f(d.x, d.y, d.z);
rlEnd();
-
+
rlDisableTexture();
}
@@ -1259,10 +1259,10 @@ static float GetHeightValue(Color pixel)
static VertexData LoadOBJ(const char *fileName)
{
VertexData vData;
-
+
char dataType;
char comments[200];
-
+
int numVertex = 0;
int numNormals = 0;
int numTexCoords = 0;
@@ -1271,14 +1271,14 @@ static VertexData LoadOBJ(const char *fileName)
FILE* objFile;
objFile = fopen(fileName, "rt");
-
+
// First reading pass: Get numVertex, numNormals, numTexCoords, numTriangles
// NOTE: vertex, texcoords and normals could be optimized (to be used indexed on faces definition)
// NOTE: faces MUST be defined as TRIANGLES, not QUADS
while(!feof(objFile))
{
fscanf(objFile, "%c", &dataType);
-
+
switch(dataType)
{
case '#': // Comments
@@ -1288,12 +1288,12 @@ static VertexData LoadOBJ(const char *fileName)
case 'm': // mtllib [external .mtl file name]
case 'u': // usemtl [material name]
{
- fgets(comments, 200, objFile);
- } break;
+ fgets(comments, 200, objFile);
+ } break;
case 'v':
{
fscanf(objFile, "%c", &dataType);
-
+
if (dataType == 't') // Read texCoord
{
numTexCoords++;
@@ -1318,12 +1318,12 @@ static VertexData LoadOBJ(const char *fileName)
default: break;
}
}
-
+
TraceLog(DEBUG, "[%s] Model num vertices: %i", fileName, numVertex);
TraceLog(DEBUG, "[%s] Model num texcoords: %i", fileName, numTexCoords);
TraceLog(DEBUG, "[%s] Model num normals: %i", fileName, numNormals);
TraceLog(DEBUG, "[%s] Model num triangles: %i", fileName, numTriangles);
-
+
// Once we know the number of vertices to store, we create required arrays
Vector3 *midVertices = (Vector3 *)malloc(numVertex*sizeof(Vector3));
Vector3 *midNormals;
@@ -1336,86 +1336,86 @@ static VertexData LoadOBJ(const char *fileName)
int countTexCoords = 0;
rewind(objFile); // Return to the beginning of the file, to read again
-
+
// Second reading pass: Get vertex data to fill intermediate arrays
// NOTE: This second pass is required in case of multiple meshes defined in same OBJ
// TODO: Consider that diferent meshes can have different vertex data available (position, texcoords, normals)
while(!feof(objFile))
{
fscanf(objFile, "%c", &dataType);
-
+
switch(dataType)
{
- case '#': case 'o': case 'g': case 's': case 'm': case 'u': case 'f': fgets(comments, 200, objFile); break;
- case 'v':
+ case '#': case 'o': case 'g': case 's': case 'm': case 'u': case 'f': fgets(comments, 200, objFile); break;
+ case 'v':
{
fscanf(objFile, "%c", &dataType);
-
+
if (dataType == 't') // Read texCoord
{
float useless = 0;
-
+
fscanf(objFile, "%f %f %f", &midTexCoords[countTexCoords].x, &midTexCoords[countTexCoords].y, &useless);
countTexCoords++;
-
+
fscanf(objFile, "%c", &dataType);
}
else if (dataType == 'n') // Read normals
{
fscanf(objFile, "%f %f %f", &midNormals[countNormals].x, &midNormals[countNormals].y, &midNormals[countNormals].z );
countNormals++;
-
+
fscanf(objFile, "%c", &dataType);
}
else // Read vertex
{
fscanf(objFile, "%f %f %f", &midVertices[countVertex].x, &midVertices[countVertex].y, &midVertices[countVertex].z );
countVertex++;
-
+
fscanf(objFile, "%c", &dataType);
}
} break;
default: break;
}
}
-
+
// At this point all vertex data (v, vt, vn) has been gathered on midVertices, midTexCoords, midNormals
// Now we can organize that data into our VertexData struct
-
+
vData.vertexCount = numTriangles*3;
-
+
// Additional arrays to store vertex data as floats
vData.vertices = (float *)malloc(vData.vertexCount * 3 * sizeof(float));
vData.texcoords = (float *)malloc(vData.vertexCount * 2 * sizeof(float));
vData.normals = (float *)malloc(vData.vertexCount * 3 * sizeof(float));
vData.colors = (unsigned char *)malloc(vData.vertexCount * 4 * sizeof(unsigned char));
-
+
int vCounter = 0; // Used to count vertices float by float
int tcCounter = 0; // Used to count texcoords float by float
int nCounter = 0; // Used to count normals float by float
-
+
int vNum[3], vtNum[3], vnNum[3];
-
+
rewind(objFile); // Return to the beginning of the file, to read again
-
+
if (numNormals == 0) TraceLog(INFO, "[%s] No normals data on OBJ, normals will be generated from faces data", fileName);
-
+
// Third reading pass: Get faces (triangles) data and fill VertexArray
while(!feof(objFile))
{
fscanf(objFile, "%c", &dataType);
-
+
switch(dataType)
{
- case '#': case 'o': case 'g': case 's': case 'm': case 'u': case 'v': fgets(comments, 200, objFile); break;
+ case '#': case 'o': case 'g': case 's': case 'm': case 'u': case 'v': fgets(comments, 200, objFile); break;
case 'f':
{
// NOTE: It could be that OBJ does not have normals or texcoords defined!
-
+
if ((numNormals == 0) && (numTexCoords == 0)) fscanf(objFile, "%i %i %i", &vNum[0], &vNum[1], &vNum[2]);
else if (numNormals == 0) fscanf(objFile, "%i/%i %i/%i %i/%i", &vNum[0], &vtNum[0], &vNum[1], &vtNum[1], &vNum[2], &vtNum[2]);
else fscanf(objFile, "%i/%i/%i %i/%i/%i %i/%i/%i", &vNum[0], &vtNum[0], &vnNum[0], &vNum[1], &vtNum[1], &vnNum[1], &vNum[2], &vtNum[2], &vnNum[2]);
-
+
vData.vertices[vCounter] = midVertices[vNum[0]-1].x;
vData.vertices[vCounter + 1] = midVertices[vNum[0]-1].y;
vData.vertices[vCounter + 2] = midVertices[vNum[0]-1].z;
@@ -1428,7 +1428,7 @@ static VertexData LoadOBJ(const char *fileName)
vData.vertices[vCounter + 1] = midVertices[vNum[2]-1].y;
vData.vertices[vCounter + 2] = midVertices[vNum[2]-1].z;
vCounter += 3;
-
+
if (numNormals > 0)
{
vData.normals[nCounter] = midNormals[vnNum[0]-1].x;
@@ -1449,7 +1449,7 @@ static VertexData LoadOBJ(const char *fileName)
// If normals not defined, they are calculated from the 3 vertices [N = (V2 - V1) x (V3 - V1)]
Vector3 norm = VectorCrossProduct(VectorSubtract(midVertices[vNum[1]-1], midVertices[vNum[0]-1]), VectorSubtract(midVertices[vNum[2]-1], midVertices[vNum[0]-1]));
VectorNormalize(&norm);
-
+
vData.normals[nCounter] = norm.x;
vData.normals[nCounter + 1] = norm.y;
vData.normals[nCounter + 2] = norm.z;
@@ -1463,7 +1463,7 @@ static VertexData LoadOBJ(const char *fileName)
vData.normals[nCounter + 2] = norm.z;
nCounter += 3;
}
-
+
if (numTexCoords > 0)
{
vData.texcoords[tcCounter] = midTexCoords[vtNum[0]-1].x;
@@ -1480,22 +1480,22 @@ static VertexData LoadOBJ(const char *fileName)
default: break;
}
}
-
+
fclose(objFile);
-
+
// Security check, just in case no normals or no texcoords defined in OBJ
if (numTexCoords == 0) for (int i = 0; i < (2*vData.vertexCount); i++) vData.texcoords[i] = 0.0f;
-
+
// NOTE: We set all vertex colors to white
for (int i = 0; i < (4*vData.vertexCount); i++) vData.colors[i] = 255;
-
+
// Now we can free temp mid* arrays
free(midVertices);
free(midNormals);
free(midTexCoords);
-
+
// NOTE: At this point we have all vertex, texcoord, normal data for the model in vData struct
TraceLog(INFO, "[%s] Model loaded successfully in RAM (CPU)", fileName);
-
+
return vData;
} \ No newline at end of file