PDA

View Full Version : help with drawing...



chign
05-13-2010, 03:08 AM
well what i want to do is this.. read from a file characters, and when i read a "2" draw a 3dcube.. my code is:


#include <stdlib.h>
#include <glut.h>
#include <math.h>
#include <stdio.h>
GLsizei width=640;
GLsizei height=480;
#define MAX_X 100
#define MAX_Y 100
int AR_GRAM;//apothikefsi ari8mo grammon
int AR_STIL;
GLsizei xwmin=-50;
GLsizei xwmax=50;
GLsizei ywmin=-50;
GLsizei ywmax=50;
int h;
int pos1,pos2;//thesi tou xaraktira
int fan1,fan2;//thesi toy fantasmatos
int num;
#define PI 3.14159
#define circlePoints 128
char direct[4]={'w','a','s','d'};
char x[MAX_X][MAX_Y];


void readtxt() {
FILE *fp;
int w=0,r=0,e;
fp=fopen("C:\\opengl\\map.txt", "r");
while ( !feof(fp)) {
e=getc(fp);
if (e!='\n')
{
x[w][r++]=e;
if (AR_STIL<r) AR_STIL=r;
}
else
{
r=0;
w++;
}
}
AR_STIL=AR_STIL-1;
AR_GRAM=w;
fclose(fp);
}

void drawcube(int a1,int a2, int b1, int b2) {//a->cnt, b->temp

glColor3f(1,0,0);
glPushMatrix();
glTranslatef( (GLfloat) b1+a1/2, (GLfloat) b2+a2/2, 0);
glScalef(2.0,2.0,1.0);
if (a1<a2)
glutSolidCube(a1);
else
glutSolidCube(a2);
glColor3f(1,0,1);
if (a1<a2)
glutWireCube(a1);
else
glutWireCube(a1);
glPopMatrix();

}


void display()
{
int i,j;
GLsizei temp1,temp2=-50,cnt1,cnt2;
glClearColor(1,1,1,0);
glClear(GL_COLOR_BUFFER_BIT);
cnt1=(2*xwmax/AR_STIL);
cnt2=(2*ywmax/(AR_GRAM+1));
for (i=AR_GRAM;i>=0;i--) {//gia na sxediastei opws to vlepoume stin pista
temp1=-50;//na arxizoun apo panw oi 8eseis (0,0) ktl
for (j=0;j<AR_STIL;j++) {
if (x[i][j]=='*') {
printf("asd\n");
drawcube(cnt1,cnt2,temp1,temp2);
}
else if (x[i][j]=='2'){
glColor3f(0,1,0);
//drawcube(cnt1,cnt2,temp1,temp2);
}
else if (x[i][j]=='#') {
glColor3f(0,0,1);
//drawsphere(cnt1,cnt2,temp1,temp2);
fan1=i;//apo8ikefsi 8eseon fantasmatos
fan2=j;
}
else if (x[i][j]=='@') {
glColor3f(1,1,0);
//drawcycle(cnt1,cnt2,temp1,temp2);
pos1=i;//apo8ikefsi 8eseon irwa
pos2=j;
}
else if (x[i][j]=='1') {
glColor3f(1,0,1);
//drawdot(cnt1,cnt2,temp1,temp2);
}
temp1=temp1+cnt1;
}
temp2=temp2+cnt2;
}
glFlush();
}


int main (int argc, char** argv)
{
glutInit(&amp;argc,argv);
glutInitWindowPosition(50,50);
glutInitWindowSize(width,height);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutCreateWindow("attempt1");

glMatrixMode(GL_PROJECTION);
glOrtho(-80,80,-80,80,0,100);

glMatrixMode(GL_MODELVIEW);
gluLookAt(-30,-30,40,0,0,0,0,1,0);

readtxt();
glutDisplayFunc(display);
glutMainLoop();

return 0;
}




i have read the file correctly.. but i do not draw the correctly.. ignore the comments and the other cases-- they are for the next stage..

any help??

the file is:


****------*--*
**-----------*
*------#-----*
*------------*
**************