PDA

View Full Version : Black window



Srcee
06-01-2011, 09:34 AM
This is my program:


#include <GL/glut.h>
#include <math.h>
#include <stdio.h> // Header file for standard file i/o.
#include <stdlib.h> // Header file for malloc/free.
#include "utils.c"


const int chekor=9;
float agolX=0;
float agolY=0;
float agolZ=0;


float transZ=70; // tekovna pozicija (oddalechenost)
float rotateA=0.0; // tekoven agol na rotacija
float rotateAspeed=0.01f; // prirast na agolot na rotacija (brzina)
int polygonMode=GL_FILL;
static GLuint texName3;
static GLuint texName1;
float svX=0, svY=0, svZ=10;

void kocka (void) // ova e objektot koj go pretstavuva svetloto
{
int i, j;

glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
glFrontFace(GL_CW);

glColor3f(0,0,0);
glBegin(GL_QUADS);
glNormal3f(0,0,1); // za sekoj poedinechen poligon mora da se navede vektor
// koj e normalen na ramninata na poligonot, za da se znae
// vo koj pravec se odbiva svetloto
glVertex3f(-10,-10,0);
glVertex3f(10,-10,0);
glVertex3f(10,10,0);
glVertex3f(-10,10,0);

glNormal3f(0,0,-1);
glVertex3f(-10,-10,13);
glVertex3f(-10,10,13);
glVertex3f(10,10,13);
glVertex3f(10,-10,13);

glNormal3f(0,1,0);
glVertex3f(-10,-10,0);
glVertex3f(-10,-10,13);
glVertex3f(10,-10,13);
glVertex3f(10,-10,0);

glNormal3f(-1,0,0);
glVertex3f(10,10,0);
glVertex3f(10,-10,0);
glVertex3f(10,-10,13);
glVertex3f(10,10,13);

glNormal3f(0,-1,0);
glVertex3f(10,10,0);
glVertex3f(10,10,13);
glVertex3f(-10,10,13);
glVertex3f(-10,10,0);

glNormal3f(1,0,0);
glVertex3f(-10,10,0);
glVertex3f(-10,10,13);
glVertex3f(-10,-10,13);
glVertex3f(-10,-10,0);
glEnd();
}




void display(void)
{


//Mestenje pogled
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,300,transZ,0,0,0,0,1,0);
glRotatef(rotateA,0,1,0);

glPushMatrix();
glTranslatef(svX,svY,svZ);
glScalef(0.2,0.2,0.2);
kocka();
glPopMatrix();


//podloga
glPopMatrix();
//glTranslatef(50,0,-20);
glScalef(500,500,500);
glBindTexture(GL_TEXTURE_2D, texName3);
glRotatef(90,0,2,0);//glTranslatef(0,0,0);
glBegin(GL_POLYGON);
glTexCoord2f(0, 0.0); glVertex3f (1, 0,-1);
glTexCoord2f(0, 1.0); glVertex3f (1, 0,1);
glTexCoord2f(1, 1.0); glVertex3f (-1, 0,1);
glTexCoord2f(1, 0.0); glVertex3f (-1, 0,-1);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glPushMatrix();


// repozicioniranje na svetloto
GLfloat svetlo0_position[]= {svX,svY,svZ,1};
glLightfv(GL_LIGHT0,GL_POSITION,svetlo0_position);
// crveno svetlo
GLfloat svetlo1_position[]= {-34,27,2.5,1};
glLightfv(GL_LIGHT1,GL_POSITION,svetlo1_position);


//zlaten materijal za bukvite
float mat_ambient []={0.24725f, 0.2245f, 0.0645f, 1.0};
float mat_diffuse []={0.34615f, 0.3143f, 0.0903f, 1.0};
float mat_specular[]={0.797357f, 0.723991f, 0.208006f, 1.0};
float mat_shininess = 83.2;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
glFrontFace(GL_CW);


glPolygonMode(GL_FRONT_AND_BACK,polygonMode);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glEnable(GL_TEXTURE_2D);//bez ovie dva reda podlogata na buk-
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);//vite nema da fati
glPushMatrix();
glScalef(1,1,5);

glPopMatrix();
glPushMatrix();
glTranslatef(0,-11,-10);
glScalef(5,0.1,5);

glPopMatrix();
glFlush();
glutSwapBuffers();
}

void init (void)
{
glClearColor(0.2f, 0.2f, 0.2f, 0.2f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glFrustum(-1, 1, -1, 1, 1.3, 200);
glFrustum(-1, 1, -1, 1, 56, 0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);

// globalno svetlo
GLfloat global_ambient[]= {0.1,0.1,0.1,0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

// Svetlo 1
GLfloat svetlo0_ambient[]= {0.3,0.3,0.3,0};
GLfloat svetlo0_diffuse[]= {1.9,1.9,1.9,0};
GLfloat svetlo0_specular[]= {1,1,1,1};
glLightfv(GL_LIGHT0,GL_AMBIENT,svetlo0_ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,svetlo0_diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,svetlo0_specular);
glEnable(GL_LIGHT0);
GLfloat light1_ambient[] = { 1.0, 0.0, 0.0, 0 };
GLfloat light1_diffuse[] = { 1.9, 0, 0, 0 };
GLfloat light1_specular[] = {3,3,3, 1};
//GLfloat light1_position[] = {17.0,24.0,2.5,1.0};
GLfloat spot_direction[] = { 0.0, -1.0, 0.0 };

glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
//glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
//glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5);
//glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5);
//glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2);

glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 30.0);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction);
//glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);
glEnable(GL_LIGHT1);
//podloga
Image *image1;

image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
exit(0);
}

if (!ImageLoad("back.bmp", image1)) {
exit(1);
}

glGenTextures(1, &amp;texName3);
glBindTexture(GL_TEXTURE_2D, texName3);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY,
0, GL_RGB, GL_UNSIGNED_BYTE, image1->data);



}

void keyboard(unsigned char key, int x, int y)
{
printf("%d %f %f %f %f \n", key, svX, svY, svZ, transZ);
switch (key) {
case 27:
exit(0);
break;
case 'W':
transZ+=0.5f;
glutPostRedisplay(); // obnovi go prikazot
break;
case 'S':
transZ-=0.5f;
if (transZ<0) transZ=0;
glutPostRedisplay(); // obnovi go prikazot
break;
case 'w':
transZ+=0.1f;
glutPostRedisplay(); // obnovi go prikazot
break;
case 's':
transZ-=0.1f;
if (transZ<0) transZ=0;
glutPostRedisplay(); // obnovi go prikazot
break;
case 'A':
rotateAspeed+=1.0f;
glutPostRedisplay(); // obnovi go prikazot
break;
case 'a':
rotateAspeed+=0.1f;
glutPostRedisplay(); // obnovi go prikazot
break;
case 'D':
rotateAspeed-=1.0f;
glutPostRedisplay(); // obnovi go prikazot
break;
case 'd':
rotateAspeed-=0.1f;
glutPostRedisplay(); // obnovi go prikazot
break;
case '1':
polygonMode=GL_LINE;
glutPostRedisplay(); // obnovi go prikazot
break;
case '2':
polygonMode=GL_FILL;
glutPostRedisplay(); // obnovi go prikazot
break;
case 'i':
svY+=1.0f;
glutPostRedisplay(); // obnovi go prikazot
break;
case 'k':
svY-=1.0f;
glutPostRedisplay(); // obnovi go prikazot
break;
case 'j':
svX-=1.0f;
glutPostRedisplay(); // obnovi go prikazot
break;
case 'l':
svX+=1.0f;
glutPostRedisplay(); // obnovi go prikazot
break;
case 'y':
svZ+=1.0f;
glutPostRedisplay(); // obnovi go prikazot
break;
case 'h':
svZ-=1.0f;
glutPostRedisplay(); // obnovi go prikazot
break;

}
}

void idle(void)
{
rotateA+=rotateAspeed;
glutPostRedisplay();
}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (600, 480);
glutInitWindowPosition (10, 10);
glutCreateWindow ("Ispit");
init ();
glutDisplayFunc(display);
glutIdleFunc(idle);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}


But all I get is a black window. Where is the mistake?

carsten neumann
06-01-2011, 10:23 AM
Please, if you want to make it easy for people to help you, consider these:

- place code inside [ code] [ /code] tags (remove leading space inside the brackets).
- remove dead code (commented out or #if 0 #endif sections)
- reduce the program to be as small as possible (e.g. removing unnecessary state setting), but still shows your problem
- English comments

Doing that you may already be able to spot that you do a glClear towards the end of your display function, wiping out all that was drawn before. ;)

Dan Bartlett
06-01-2011, 10:34 AM
Also, your glPushMatrix/glPopMatrix calls don't seem to match up properly, in one place you have:

//podloga
glPopMatrix();
...
glPushMatrix();
which should be

//podloga
glPushMatrix();
...
glPopMatrix();
And then you have some code that does nothing useful at all, just backs up the model view matrix, modifies it but doesn't use it, then restores it to it's original value again:

glPushMatrix();
glScalef(1,1,5);

glPopMatrix();
glPushMatrix();
glTranslatef(0,-11,-10);
glScalef(5,0.1,5);

glPopMatrix();

Srcee
06-01-2011, 11:23 AM
Tnx a lot, you really helped!