I am fairly new at using the C language and OpenGL language...
I have searched the internet, I have got books both on C and OpenGL but I still can not get this working
What I am attempting to do is to load a .x model, would prefer to load .obj loader but gave up with that a while back
The .x object loader is written in C code but I am getting 2 errors. I think it maybe due to not calling the function correctly (or at all) or maybe the way I have structured my code
Main.c
//Header Files
#include <windows.h> // windows
#include <GL/gl.h>
#include <GL/glu.h>
#include "glut.h" // GL utility Toolkit (GLUT)
#include <stdio.h>
//Global Variables
boolean g_gamemode; // GLUT GameMode ON/OFF
boolean g_fullscreen; // Fullscreen Mode ON/OFF (When g_gamemode is OFF)
//************************************************************************************************************************
// Strucutres
//************************************************************************************************************************
typedef struct
{
GLfloat x, y, z;
} vertex;
typedef struct
{
int howMany, one, two, three, four;
} facet;
typedef struct
{
GLfloat x, y;
} textureCoord;
typedef struct
{
GLfloat x, y, z;
} normal;
//************************************************************************************************************************
//************************************************************************************************************************
// global variables that the file reading procedure uses.
//************************************************************************************************************************
vertex * vertices;
facet * facets;
textureCoord * textureCoords;
normal * normals;
GLfloat ambCoefficients[4];
GLfloat shininess[1];
GLfloat diffCoefficients[3];
GLfloat specCoefficients[3];
int numOfVerts, numOfFacets, numOfTexCoords, numOfNormals;
//************************************************************************************************************************
//************************************************************************************************************************
// File Loader Procedure
//************************************************************************************************************************
void readInModel(void)
{
char filename1[20];
char filename2[20];
int i;
FILE * in_file;
printf("Please enter the filename to read from\n");
gets(filename1);
in_file = fopen(filename1, "r");
printf("File %s opened\n\n", &filename1);
fscanf(in_file, "%d", &numOfVerts);
printf("Number of vertices: %d\n", numOfVerts);
vertices = calloc(numOfVerts, sizeof(vertex));
for (i = 0; i < numOfVerts; i++)
{
fscanf(in_file, "%f%f%f", &vertices[i].x, &vertices[i].y, &vertices[i].z);
// printf("X: %f\nY: %f\nZ: %f\n\n", vertices[i].x, vertices[i].y, vertices[i].z);
}
printf("Vertices read in\n\n");
fscanf(in_file, "%d", &numOfFacets);
printf("Number of facets: %d\n", numOfFacets);
facets = calloc(numOfFacets, sizeof(facet));
for (i = 0; i < numOfFacets; i++)
{
fscanf(in_file, "%d%d%d%d", &facets[i].howMany, &facets[i].one, &facets[i].two, &facets[i].three);
// printf("%d %d %d", facets[i].one, facets[i].two, facets[i].three);
if (facets[i].howMany == 4)
{
fscanf(in_file, "%d", &facets[i].four);
// printf(" %d\n", facets[i].four);
}
else
{
// printf("\n");
}
}
printf("Facets read in\n\n");
fscanf(in_file, "%f%f%f%f", &ambCoefficients[0], &ambCoefficients[1], &ambCoefficients[2], &ambCoefficients[3]);
printf("Ambient coefficients read in\n\n");
fscanf(in_file, "%f", &shininess[0]);
printf("Model shininess read in\n\n");
fscanf(in_file, "%f%f%f", &diffCoefficients[0], &diffCoefficients[1], &diffCoefficients[2]);
printf("Diffuse coefficients read in\n\n");
fscanf(in_file, "%f%f%f", &specCoefficients[0], &specCoefficients[1], &specCoefficients[2]);
printf("Specular coefficients read in\n\n");
fscanf(in_file, "%s", &filename2);
printf("Texture file: %s\n\n", filename2);
fscanf(in_file, "%d", &numOfTexCoords);
printf("Number of texture co-ordinates: %d\n", numOfTexCoords);
textureCoords = calloc(numOfTexCoords, sizeof(textureCoord));
for (i = 0; i < numOfTexCoords; i++)
{
fscanf(in_file, "%f%f", &textureCoords[i].x, &textureCoords[i].y);
// printf("%f %f\n", textureCoords[i].x, textureCoords[i].y);
}
printf("Texture co-ordinates read in\n\n");
fscanf(in_file, "%d", &numOfNormals);
printf("Number of vertex normals: %d\n", numOfNormals);
normals = calloc(numOfNormals, sizeof(normal));
for (i = 0; i < numOfNormals; i++)
{
fscanf(in_file, "%f%f%f", &normals[i].x, &normals[i].y, &normals[i].z);
// printf("%f %f %f\n", normals[i].x, normals[i].y, normals[i].z);
}
printf("Vertex normals read in\n\n");
fclose(in_file);
readInTexture(filename2);
printf("Texture read in from %s\n\n", &filename2);
}
//************************************************************************************************************************
//Code GL Specific Initialisations
boolean init(void)
{
// Enable Smooth shading (Shades Colours across a Polygon, and smoothes out lighting)
glShadeModel(GL_SMOOTH);
// Black background (Sets the colour of the screen when it clears)
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
// Depth Buffer (Layers in screen (How deep Objects are into screen))
// Sorts which OBJECTS to draw first (So a square you draw behind a circle
// does not end up on top of the circle)
glClearDepth(1.0f); // Depth Buffer setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The type of Depth Test to do;
// OpenGL best perspective to be done
// (Slight performance hit but perspective view will look a little better)
// Really Nice Perspective Calculations
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
// Return True (If need to see if initialisation went ok,
// add a check to see if TRUE or False was returned (FALSE if error occurs)
return TRUE; // Initialisation went ok
}
//Code Rendering is done here
void render(void)
{
// Clear Screen And Depth Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // Reset The Current Modelview Matrix
// Moves to the centre of the screen (0.0f)
//Code *****All code for drawing starts here*****
// Examples
// glTranslatef(x,y,z); Sets drawing position
// so glTranslatef(-1.5f,0,0f,-6.0f);
// Would move Left on X axis 1.5 units, does not move on y axis(0.0)
// and moves into the screen 6.0 units
// Left of centre (X axis) would be a negative number, right would be positive
// Towards the top of screen (Y) would be a positive, bottom Negative
// Deeper into screen (z) is negative, towards the viewer (you) positive
// **NOTE** When you translate, you are not moving from the centre of the screen
// you are moving from WHEREVER YOU CURRENTLY WERE
// glBegin(); Draw using Triangles(GL_TRIANGLES), Quads(GL_QUADS) etc.....
// glVertex3f(); Coordinates for drawing..
// Triangle would need 3..Top, Bottom Left, Bottom Right
// Quads would need 4..Top Left, Top Right, Bottom Left, Bottom Right
// For more than 4 points use GL_POLYGON
// **NOTE** You can draw multiple objects within this section e.g. two triangles or two quads
// within the glBegin(); and glEnd(); just not 1 triangle and one quad
// glEnd(); Ends the drawing of the current object
//Code *****All code for drawing ends here*****
glutSwapBuffers ( ); // Swap The Buffers To Become Our Rendering Visible
// Using Double buffer enables smooth flicker free animation
// Double buffer draws everything to a hidden screen (We can't see)
// When we swap the buffer, the screen we see becomes the hidden screen
// the hidden screen becomes visible
// This way we do not see the Scene being drawn out it just instantly appears
}
//Code Our Reshaping Handler (Required Even In Fullscreen-Only Modes)
void reshape(int w, int h)
{
glViewport(0, 0, w, h); // Reset the current Viewport
glMatrixMode(GL_PROJECTION);// Select The Projection Matrix
glLoadIdentity(); // Moves to the centre of the screen (0.0f)
// Reset The Projection Matrix (Restores the Matrix to it's original state)
// Calculate The Aspect Ratio And Set The Clipping Volume
if (h == 0) h = 1; // Prevent a divide by 0 and make height equal to 1
// Set up screen for perspective view (Things in distance get smaller)
// Calculate the Aspect Ratio of the window (80 degree angle based on the width and height)
gluPerspective(80, (float)w/(float)h, 1.0, 5000.0);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix (Object information stored here)
glLoadIdentity(); // Moves to the centre of the screen (0.0f)
// Reset The Modelview Matrix (Restores the Matrix to it's original state)
}
//Code Our Keyboard Handler (Normal Keys)
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: // When Escape Is Pressed...
exit(0); // Exit The Program
break; // Ready For Next Case
default: // Now Wrap It Up
break;
}
}
//Code Our Keyboard Handler For Special Keys (Like Arrow Keys And Function Keys)
void special_keys(int a_keys, int x, int y)
{
switch (a_keys) {
case GLUT_KEY_F1:
// We Can Switch Between Windowed Mode And Fullscreen Mode Only
if (!g_gamemode) {
// Toggle g_fullscreen Flag
g_fullscreen = !g_fullscreen;
// We Went In Fullscreen Mode
if (g_fullscreen) glutFullScreen();
// We Went In Windowed Mode
else glutReshapeWindow(500, 500);
}
break;
default:
break;
}
}
//Code Main Function For Bringing It All Together.
int main(int argc, char** argv)
{
glutInit(&argc, argv); // GLUT Initialistion
// Display Mode (Rgb And Double Buffered)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
if (g_gamemode) {
// Select The 640x480 In 16bpp Mode
glutGameModeString("640x480:16");
if (glutGameModeGet(GLUT_GAME_MODE_POSSIBLE))
glutEnterGameMode();// Enter Full Screen
else g_gamemode = FALSE;// Cannot Enter Game Mode, Switch To Windowed
}
if (!g_gamemode) {
// Window Size If We Start In Windowed Mode
glutInitWindowSize(500, 500);
// Window Title
glutCreateWindow("BERT'S DEFAULT WINDOW");
}
init(); // Our Initialisation
glutDisplayFunc(render); // Register The Display Function
glutReshapeFunc(reshape); // Register The Reshape Handler
glutKeyboardFunc(keyboard); // Register The Keyboard Handler
// Register Special Keys Handler
glutSpecialFunc(special_keys);
glutMainLoop(); // Go To GLUT Main Loop
return 0;
}
Anything to do with the C code (.x file loader) is surrounded by the //************************************* for clarity
Error messages I get:
Error 1 error LNK2019: unresolved external symbol _readInTexture referenced in function _readInModel Main.obj
Error 2 fatal error LNK1120: 1 unresolved externals
Like I said I am new to both Languages, have tried looking on-line in lot's of places but they all give me examples in C++ and use code with separate header files (.h)
If anyone can help with this I would be so grateful or if anyone can point me in the direction to someone's .obj loader or .3ds loader but written in C not C++ that would be fantastic! I am willing to just use someone else's and source them in my project

New Topic/Question
Reply




MultiQuote




|