scanf/fscanf problem with an OBJ reader
Is there any good reason why you are using C over C++?
Hmmm. C seems easier at first glance... But then again, maybe C++ is right for me. Do you think so? Should I start to learn C++? At some point we'll all have to go Cocoa. Is C++ more compliant, or should I go to ObjC? In the meanwhile, can anyone help?
~ Bring a Pen ~
mikey Wrote:OK thanks, I think I get it, but I get errors:
Code:
if (myArrayCount >= myArraySize) {
myArraySize *= 2;
laserCarbineVertices = realloc(laserCarbineVertices, sizeof(GLfloat) * myArraySize);
// ERROR: Passing argument 1 of realloc makes pointer form integer without a cast
}
laserCarbineVertices[myArrayCount] = xc;
// ERROR : Subscripted value is neither array nor pointer
laserCarbineVertices[myArrayCount+1] = yc;
// ERROR : Subscripted value is neither array nor pointer
laserCarbineVertices[myArrayCount+2] = zc;
// ERROR : Subscripted value is neither array nor pointer
So, this means the compiler thinks laserCarbineVertices is an integer, but...
mikey Wrote:...and...
Code:
GLfloat * laserCarbineVertices = NULL;
size_t myArraySize = 1;
size_t myArrayCount = 0;
laserCarbineVertices = malloc(sizeof(GLfloat) * myArraySize);
// Conflicting types for laserCarbineVertices
// initilizer element is not constant.
...this looks fine, and I don't see any obvious reason it would be failing. Does your code look exactly like what you pasted? I'd be suspicious of the line right before laserCarbineVertices = malloc(...), but it looks correct above.
_ibd_ Wrote:Is there any good reason why you are using C over C++?
C++ generally isn't an appropriate choice for a beginner. We've discussed this somewhat recently.
Quote:C++ generally isn't an appropriate choice for a beginner. We've discussed this somewhat recently.I know, I was just making sure. I think I'm going to stick with C for now.
The [IM(G)] tags didn't work, just go to the link below.
http://img38.imageshack.us/gal.php?g=picture2hae.png
~ Bring a Pen ~
Oh! Right, OK, functions can't be called at the top scope level. You'll need to move the "laserCarbineVertices = malloc(sizeof(GLfloat) * myArraySize);" line into a function somewhere.
ThemsAllTook Wrote:C++ generally isn't an appropriate choice for a beginner. We've discussed this somewhat recently.
I agree. But it's a small step from C to C++. You can still use 95% of the code you already have, but with C++ you can enjoy lots of more things. I would never bother with malloc to have resizable arrays when I have std:vectors... I think
this post by Najdorf summarizes it quite well and also describes what I do.
http://www.idevgames.com/forum/showpost....stcount=24
_ibd_ Wrote:But it's a small step from C to C++.
Do we really need to have this discussion every other week?
Right, Thanks AllThemsTook. Xcode proudly displays no errors, but my program loads into GDB. 
I think it's something wrong with:
Probably nobody wants to read all of it,
but here's the source anyway:

I think it's something wrong with:
Code:
while(!feof(objfile)) {Probably nobody wants to read all of it,
but here's the source anyway:
Code:
//////////////////////////////////////////////////////////////////////////////////
///////#///////#////////// //////////////////////////
/////////////////##/////// © Michael McKay //////////////////////////
//////############//////// Summer 2009 //////////////////////////
////////////////////////// //////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/* SUGGESTED NAMES */
// The Elite
// elite
// damage
// warzone
// unit 42
// War 2239
// July 2978
// VR
// SISA
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <GLUT/glut.h>
#include "sfxLite.h"
#include "function-dec.h"
#include <OpenGL/OpenGL.h>
#include <string.h>
//#include <gl\glaux.h>
#define DEG_TO_RAD 0.0174532925199f
#define ROT_VELOCITY 90.0f
#define MOVE_VELOCITY 25.0f
// models
float bulletz;
char bulletzstring[5];
int LaserSoundBufferID;
float bob;
float walk;
GLfloat heading = 0.0f, pitch = 0.0f, xp = 0.0f, yp = 20.0f, zp = 0.0f;
GLuint filter; // Which Filter To Use
GLuint fogMode[]= { GL_EXP, GL_EXP2, GL_LINEAR }; // Storage For Three Types Of Fog
GLuint fogfilter= 0; // Which Fog To Use
GLfloat fogColor[4]= {0.5f, 0.5f, 0.5f, 1.0f}; // Fog Color
float LightPos[4]={-5.0f,5.0f,10.0f,0.0f};
char fchar[100];
int numbervertices;
GLfloat * laserCarbineVertices = NULL;
size_t myArraySize = 1;
size_t myArrayCount = 0;
typedef enum
{
ENTER = 3,
TAB = 9,
RETURN = 13,
ESC = 27,
SPACE = 32,
DEL = 127,
UP_ARROW, DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW,
NUM_KEY_CODES
} KeyCode;
bool key[NUM_KEY_CODES], keyDown[NUM_KEY_CODES], keyUp[NUM_KEY_CODES];
int lastFrameTime = 0;
float dt;
int firing;
float recoil;
///// ###### TEXTURES
/// </TEXTURES>
int moX , moY;
// mouse
drawGLString(GLfloat x, GLfloat y, char *textString)
{
int le;
int qs;
glRasterPos2f(x, y);
le = (int) strlen(textString);
for (qs = 0; qs < le; qs++)
{
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, textString[qs]);
}
}
////###########################////###########################////###########################////###########################
////###########################////###########################////###########################////###########################
////###########################////###########################////###########################////###########################
////###########################////###########################////###########################////###########################
void resetKeyboardInput(void)
{
int i;
for (i = 0; i < NUM_KEY_CODES; i++)
{
keyDown[i] = false;
keyUp[i] = false;
}
}
void keyboard(unsigned char rawKeyCode, int x, int y)
{
if (rawKeyCode < NUM_KEY_CODES)
{
key[rawKeyCode] = true;
keyDown[rawKeyCode] = true;
}
}
void keyboardUp(unsigned char rawKeyCode, int x, int y)
{
if (rawKeyCode < NUM_KEY_CODES)
{
key[rawKeyCode] = false;
keyUp[rawKeyCode] = false;
}
}
void keyboardSpecial(int rawKeyCode, int x, int y)
{
switch (rawKeyCode)
{
case GLUT_KEY_LEFT:
key[LEFT_ARROW] = true;
keyDown[LEFT_ARROW] = true;
break;
case GLUT_KEY_UP:
key[UP_ARROW] = true;
keyDown[UP_ARROW] = true;
break;
case GLUT_KEY_RIGHT:
key[RIGHT_ARROW] = true;
keyDown[RIGHT_ARROW] = true;
break;
case GLUT_KEY_DOWN:
key[DOWN_ARROW] = true;
keyDown[DOWN_ARROW] = true;
break;
}
}
void keyboardSpecialUp(int rawKeyCode, int x, int y)
{
switch (rawKeyCode)
{
case GLUT_KEY_LEFT:
key[LEFT_ARROW] = false;
keyUp[LEFT_ARROW] = true;
break;
case GLUT_KEY_UP:
key[UP_ARROW] = false;
keyUp[UP_ARROW] = true;
break;
case GLUT_KEY_RIGHT:
key[RIGHT_ARROW] = false;
keyUp[RIGHT_ARROW] = true;
break;
case GLUT_KEY_DOWN:
key[DOWN_ARROW] = false;
keyUp[DOWN_ARROW] = true;
break;
}
}
void setOrthographicProjection() {
// switch to projection mode
glMatrixMode(GL_PROJECTION);
// save previous matrix which contains the
//settings for the perspective projection
glPushMatrix();
// reset matrix
glLoadIdentity();
// set a 2D orthographic projection
gluOrtho2D(0, 1680, 0, 1050);
// invert the y axis, down is positive
glScalef(1, -1, 1);
// mover the origin from the bottom left corner
// to the upper left corner
glTranslatef(0, -1050, 0);
glMatrixMode(GL_MODELVIEW);
}
void resetPerspectiveProjection() {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void getMouse( int x, int y)
{
pitch = ((1050 - y - 360));
if (pitch > 90.0f)
pitch = 89.0f;
if (pitch < -90.0f)
pitch = -90.0f;
heading = ((x*1));
pitch = pitch * -1;
moX = heading;
moY = pitch+360;
}
void shoot(int button, int state, int x, int y)
{
if ((state == GLUT_DOWN))
{
firing = 1;
}
else {
firing = 0;
}
}
void drawBullets() // The laser is just graphical, the 'hit' is code-worked.
{
glPushMatrix();
glDisable(GL_LIGHTING);
glDisable(GL_FOG);
//sfxBufferPlay(LaserSoundBufferID, 1.0f, 0.5f);
glLineWidth(5.0f);
glEnable(GL_LIGHT1);
glColor3f(1.0f,1.0f,1.0f);
glTranslatef(2.0f,-1.0f,bulletz);
glScalef(1.0f, 1.0f, -1.0f);
glutSolidCone(0.05, 5.0, 10, 10);
//////////////////////////////////
if (bulletz < -500)
bulletz = -15;
else
bulletz-= 30;
glDisable(GL_LIGHT1);
glEnable(GL_FOG);
glEnable(GL_LIGHTING);
glPopMatrix();
}
void drawHUD(void)
{
/*glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glOrtho(1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS);
glVertex2f(900.0f, 585.0f); // UL
glVertex2f(900.0f, 465.0f); // BL
glVertex2f(780.0f, 465.0f); // BR
glVertex2f(780.0f,585.0f);
glEnd();
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
*/
glPushMatrix();
glLoadIdentity();
setOrthographicProjection();
glLineWidth(3.0);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glColor3f(0.0f,1.0f,0.0f);
glLineWidth(1.0f);
glBegin(GL_LINES);
glVertex2f(840.0f,575.0f);
glVertex2f(840.0f,600.0f);
glVertex2f(840.0f,475.0f);
glVertex2f(840.0f,450.0f);
glVertex2f(890.0f,525.0f);
glVertex2f(915.0f,525.0f);
glVertex2f(790.0f,525.0f);
glVertex2f(765.0f,525.0f);
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(0.0f,0.9f,0.0f);
glVertex2f(0.0f,1050.0f);
glVertex2f(1680.0f,1050.0f);
// glColor3f(0.0f,0.1f,0.0f);
glVertex2f(1680.0f,950.0f);
glVertex2f(0.0f,950.0f);
glEnd();
glEnd();
glBegin(GL_TRIANGLES);
glVertex2f(moX,15.0f);
glVertex2f(moX+30,10.0f);
glVertex2f(moX-30,10.0f);
//
glVertex2f(15.0f,moY);
glVertex2f(10.0f,moY+30);
glVertex2f(10.0f,moY-30);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(30.0f,30.0f);
glVertex2f(1650.0f,30.0f);
glVertex2f(1650.0f,1020.0f);
glVertex2f(30.0f,1020.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0f,0.1f,0.0f);
glVertex2f(0.0f,1050.0f);
glVertex2f(1680.0f,1050.0f);
// glColor3f(0.0f,0.1f,0.0f);
glVertex2f(1680.0f,950.0f);
glVertex2f(0.0f,950.0f);
glEnd();
glColor3f(0.0f,1.0f,0.0f);
drawGLString(9.0f, 970.0f, "ERROR - ATMOSPHERIC PARTICLE DENSITY TOO GREAT...");
drawGLString(9.0f, 985.0f, "(c) BIOTECH SYSTEMS CORPORATION");
drawGLString(9.0f, 1000.0f, "INITIATING LASER ...");
/*
char moxString[5];
sprintf(moxString, "%d", moX);
drawGLString(moX+35, 15.0f,moxString);
char moyString[5];
sprintf(moyString, "%d", moY);
drawGLString(4.0f, moY+35 ,moyString);
char xpString[5];
sprintf(xpString, "xp = %d ", xp);
drawGLString(860, 550,xpString);
char zpString[5];
sprintf(zpString, "zp = %d ", zp);
drawGLString(860, 560,zpString);
*/
glEnd();
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glPopMatrix();
resetPerspectiveProjection();
}
void drawScene(void) // ########################### HOPEFULLY WE'LL REPLACE THIS WITH A MODEL LOADER. BUT FOR NOW...
{
glLineWidth(2.0f);
glBegin(GL_QUADS);
glColor3f(0.0f,0.0f,0.8f);
glTexCoord2d(0.0f,0.0f); glVertex3f( -100.0f, -1.0f, -100.0f);
glTexCoord2d(0.0f,1.0f); glVertex3f(-100.0f,-1.0f, 100.0f);
glTexCoord2d(1.0f,1.0f); glVertex3f( 100.0f,-1.0f, 100.0f);
glTexCoord2d(1.0f,0.0f); glVertex3f( 100.0f,-1.0f,-100.0f);
glEnd();
//
glTranslatef(0.0f,4.0f,0.0f);
//yay! FLATS ###
glColor3f(0.0f,0.2f,0.9f);
// Select Our Texture
glutSolidCube(10);
glScalef(1.0f,5.0f,1.0f);
//
glTranslatef(10.0f,4.0f,15.0f);
glutSolidCube(10);
//
glScalef(1.0f,1.0f,1.0f);
glTranslatef(20.0f,0.0f,45.0f);
glutSolidCube(10);
//
glScalef(1.0f,1.0f,1.0f);
glTranslatef(45.0f,0.0f,30.0f);
glutSolidCube(10);
}
void drawPlayer(void)
{
//glLoadIdentity();
glPushMatrix();
//glLoadIdentity();
//glTranslatef( xp,5.0f,zp+5);
//glRotatef(-pitch,1.0f,0.0f,0.0f);
if (firing == 1)
{
// player model changes when firing //
}
//glTranslatef(2.0f,-1.0f,0.0f );
//glRotatef(-heading,0.0f,1.0f,0.0f);
//glRotatef(pitch,0.0f ,0.0f,1.0f);
// WE"LL REPLACE THIS WITH AN .OBJ LOADER.
glDrawArrays(GL_QUADS, 0, 10);
glPopMatrix();
//glFlush();
//glLoadIdentity();
}
//###############################################################################################################################################################################################################################
//################### ########### ############################ #######################################################################################################################
//################################################################################### UPDATE #######################################################################################################################
//################################################################################### #######################################################################################################################
void update(void)
{
/*
if (key[LEFT_ARROW])
{
heading -= ROT_VELOCITY * dt;
while (heading < 0.0f)
heading += 360.0f;
}
if (key[RIGHT_ARROW])
{
heading += ROT_VELOCITY * dt;
while (heading > 360.0f)
heading -= 360.0f;
}
if (key[UP_ARROW])
{
pitch += ROT_VELOCITY * dt;
if (pitch > 45.0f)
pitch = 45.0f;
}
if (key[DOWN_ARROW])
{
pitch -= ROT_VELOCITY * dt;
if (pitch < -45.0f)
pitch = -45.0f;
}
*/
////
if (key['w'])
{
xp += cos(DEG_TO_RAD * (heading - 90.0f)) * MOVE_VELOCITY * dt;
zp += sin(DEG_TO_RAD * (heading - 90.0f)) * MOVE_VELOCITY * dt;
}
if (key['s'])
{
xp -= cos(DEG_TO_RAD * (heading - 90.0f)) * MOVE_VELOCITY * dt;
zp -= sin(DEG_TO_RAD * (heading - 90.0f)) * MOVE_VELOCITY * dt;
}
if (key['a'])
{
xp -= cos(DEG_TO_RAD * heading) * MOVE_VELOCITY * dt;
zp -= sin(DEG_TO_RAD * heading) * MOVE_VELOCITY * dt;
}
if (key['d'])
{
xp += cos(DEG_TO_RAD * heading) * MOVE_VELOCITY * dt;
zp += sin(DEG_TO_RAD * heading) * MOVE_VELOCITY * dt;
}
/*if (key['q'])
{
yp += MOVE_VELOCITY * dt;
}
if (key['z'])
{
yp -= MOVE_VELOCITY * dt;
}
*/
yp = 5.0f;
}
//###############################################################################################################################################################################################################################
//################################################################################### #######################################################################################################################
//################################################################################### DISPLAY #######################################################################################################################
//################################################################################### #######################################################################################################################
void display(void)
{
// calculate delta time
if (lastFrameTime == 0)
{
lastFrameTime = glutGet(GLUT_ELAPSED_TIME);
}
int now = glutGet(GLUT_ELAPSED_TIME);
int elapsedMilliseconds = now - lastFrameTime;
dt = elapsedMilliseconds / 1000.0f;
lastFrameTime = now;
update();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClearDepth(1.0f);
glFogi(GL_FOG_MODE, fogMode[fogfilter]); // Fog Mode
glFogfv(GL_FOG_COLOR, fogColor); // Set Fog Color
glFogf(GL_FOG_DENSITY, 0.02f); // How Dense Will The Fog Be
glHint(GL_FOG_HINT, GL_DONT_CARE); // Fog Hint Value
glFogf(GL_FOG_START, 1.0f); // Fog Start Depth
glFogf(GL_FOG_END, 5.0f); // Fog End Depth
glEnable(GL_FOG); // Enables GL_FOG
glEnable (GL_BLEND);
glEnable (GL_LINE_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glEnable(GL_COLOR_MATERIAL);
glDepthFunc(GL_LEQUAL);
glLoadIdentity();
glEnable(GL_LIGHT0);
GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glPushMatrix();
// virtual "camera" is first tranform(s) in scene
glRotatef(pitch, 1.0f, 0.0f, 0.0f);
glRotatef(heading, 0.0f, 1.0f, 0.0f);
glTranslatef(-xp, -yp, -zp);
drawScene();
glLoadIdentity();
drawPlayer();
drawHUD();
if (firing == 1)
drawBullets();
else {
bulletz = 0;
}
glutSwapBuffers();
resetKeyboardInput();
}
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 2.0f, 100000.0f);
glMatrixMode(GL_MODELVIEW);
}
void idle(void)
{
glutPostRedisplay();
}
//###############################################################################################################################################################################################################################
//################################################################################### #######################################################################################################################
//################################################################################### MAIN #######################################################################################################################
//################################################################################### #######################################################################################################################
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayString("double rgba depth=24");
glutInitWindowSize(640, 480);
glutCreateWindow("Reality 3D");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboardUp);
glutSpecialFunc(keyboardSpecial);
glutSpecialUpFunc(keyboardSpecialUp);
const GLint swapInterval = 1;
CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &swapInterval);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, laserCarbineVertices);
// m0dels
laserCarbineVertices = malloc(sizeof(GLfloat) * myArraySize);
FILE * objfile;
objfile = fopen("cube.obj" , "r");
numbervertices = 0;
while(!feof(objfile)) {
float xc;
float yc;
float zc;
if ( fscanf(objfile,"%s %f %f" , &fchar, &xc, &yc, &zc ) != 1 )
{
if (strcmp( &fchar,"v"))
{
// Add item to array
if (myArrayCount >= myArraySize) {
myArraySize *= 2;
laserCarbineVertices = realloc(laserCarbineVertices, sizeof(GLfloat) * myArraySize);
}
laserCarbineVertices[myArrayCount] = xc;
laserCarbineVertices[myArrayCount+1] = yc;
laserCarbineVertices[myArrayCount+2] = zc;
}
// printf("| %s |",&line);
numbervertices += 3;
//printf(" %s " , &line);
}
}
// mousey
HideCursor();
glutPassiveMotionFunc(getMouse);
glutMouseFunc(shoot);
glutMotionFunc(shoot);
sfxInit();
LaserSoundBufferID = sfxBufferLoadFromWAVFile("LaserShot.wav");
glutFullScreen();
glutMainLoop();
return EXIT_SUCCESS;
}~ Bring a Pen ~
mikey Wrote:Right, Thanks AllThemsTook. Xcode proudly displays no errors, but my program loads into GDB.
Why is that a bad thing?
Worlds at War (Current Project) - http://www.awkward-games.com/forum/
mikey Wrote:I think it's something wrong with:
Code:
while(!feof(objfile)) {
You're not checking the return value of fopen. If the file path you specified can't be opened, you'll get NULL back and then crash when you try to do stuff with it.
Quote:Why is that a bad thing?
erm, because it means my code is faulty (and gdb is quite hard to understand). Quote:You're not checking the return value of fopen. If the file path you specified can't be opened, you'll get NULL back and then crash when you try to do stuff with
Ahh, I see, but the file path seems fine. "cube.obj" resides in (main project folder)!
Am I missing something here?
~ Bring a Pen ~
mikey Wrote:ThemsAllTook Wrote:You're not checking the return value of fopen. If the file path you specified can't be opened, you'll get NULL back and then crash when you try to do stuff with
Ahh, I see, but the file path seems fine. "cube.obj" resides in (main project folder)!
Am I missing something here?
Yes, You are missing code that checks the return value of fopen.
Sir, e^iπ + 1 = 0, hence God exists; reply!
Oh yes, I forgot to post that:
Code:
FILE * objfile;
objfile = fopen("cube.obj" , "r");
numbervertices = 0;
if ( fopen("cube.obj" , "r") == NULL)
{
printf("ERROR, OBJ file path is faulty");
}
else {
while(!feof(objfile))
{
float xc;
float yc;
float zc;
if ( fscanf(objfile,"%s %f %f" , &fchar, &xc, &yc, &zc ) != 1 )
{
if (strcmp( &fchar,"v"))
{
// Add item to array
if (myArrayCount >= myArraySize) {
myArraySize *= 2;
laserCarbineVertices = realloc(laserCarbineVertices, sizeof(GLfloat) * myArraySize);
}
laserCarbineVertices[myArrayCount] = xc;
laserCarbineVertices[myArrayCount+1] = yc;
laserCarbineVertices[myArrayCount+2] = zc;
}
// printf("| %s |",&line);
numbervertices += 3;
//printf(" %s " , &line);
}
}
}~ Bring a Pen ~
mikey Wrote:Ahh, I see, but the file path seems fine. "cube.obj" resides in (main project folder)!
Am I missing something here?
OneSadCookie's done a nice writeup about this: http://blog.onesadcookie.com/2007/12/fin...files.html
There's a mistake in your code, it should be: if ( objfile == NULL), Anyway you haven't said if the program is finding the .obj file or not. (And you might rather use perror than ("ERROR, OBJ file path is faulty"))
Sir, e^iπ + 1 = 0, hence God exists; reply!

