dranec
dranec

Reputation: 57

Black screen while trying to display drawing on screen

I just started to learn openGL, and now im stuck on a problem, when I try to make a resizable window, the object is not showing on the window, and I cant figure it why. So if anyone can just take a quick look what its wrong it will be of a big help. Best regards.

#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>

void initLights();
void reshape(int width, int height);
void display();
void renderScene();
int w;
int h;
int main(int argc, char** argv) {
  glutInit(&argc, argv); //metodi se predaje dva argumenta - andresa varijable koja sadrži broj argumenata iz komandne linije, te polje tih argumenata
  glutInitDisplayMode(GLUT_DOUBLE);
   glEnable(GL_LIGHTING);
  glutInitWindowSize(w, h); //dimenzije prozora u pikselima
  glutInitWindowPosition(0, 0); //pozicija gornjeg lijevo kuta u kojem će se scena iscrtati, prvi argument je x, drugi y
  glutCreateWindow("Primjer 1"); //stvaranje prozora u kojem će se iscrtati scena ("Naziv prozora")
  glutDisplayFunc(display); //registracija metode display, vraća podatak tipa int koji se može koristiti kao identifikator prozora koji se kasnije koristi za uništavanje prozora
  glutReshapeFunc(reshape); //registracija metode reshape
  glutMainLoop(); //beskonačna petlja osluškivanja, generiranja događaja i pozivanje registriranih metoda koja obrađuje događaje
}
void initLights() {
  // set up light colors (ambient, diffuse, specular)
  GLfloat lightKa[] = {.5f, .5f, .5f, 1.0f}; // ambient light
  GLfloat lightKd[] = {.7f, .7f, .7f, 1.0f}; // diffuse light
  GLfloat lightKs[] = {1, 1, 1, 1}; // specular light
  glLightfv(GL_LIGHT0, GL_AMBIENT, lightKa);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, lightKd);
  glLightfv(GL_LIGHT0, GL_SPECULAR, lightKs);
  // position the light
  float lightPos[4] = {0, 0, 20, 1}; // positional light
  glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
  glEnable(GL_LIGHT0); // MUST enable each light source after configuration
}

void display() { //iscrtavanje scene, radi se pozivom metode glutDisplayFunc(display); kojoj kao argument predajemo pokazivač na samu metodu
  glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //brisanje površine platna, boja pozadine platna
  glClear(GL_COLOR_BUFFER_BIT); //popunjavanje čitave površine platna prethodno definiranom bojom
  glLoadIdentity();
  // crtanje scene:
  renderScene();
  glutSwapBuffers();
}

void toPerspective(){
  glDisable(GL_DEPTH_TEST); //isključivanje provjere z-spremnika koji se koristi u 3D sceni
  glViewport(0, 0,w, h); //definicija dijela prozora koji prikazuje samu scenu 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

 gluPerspective(60, (double)w/(double)h, 1, 256);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();



}

void reshape(int width, int height) { //funkcija koja se koristi za promjenu veličine prozora
w = width;
h = height;
toPerspective();
}

void renderScene() {
  glPointSize(6.0f);
  glColor3f(0.0f, 1.0f, 1.0f);

  glBegin(GL_POINTS);
  glVertex2i(50, 50);
  glVertex2i(150, 150);
  glVertex2i(2, 2);
  glVertex2i(4, 4);
  glEnd();

  glBegin(GL_LINE_STRIP);
  glVertex2i(50, 50);              
  glVertex2i(150, 150);             
  glVertex2i(50, 150);
  glVertex2i(50, 50);
  glEnd();
  glVertex2i(250, 250);             

}

Upvotes: 0

Views: 165

Answers (1)

BDL
BDL

Reputation: 22165

In your code, the camera is located at (0,0,0) due to the modelviewmatrix which is an identity matrix. Now the projection is constructed with a nearplane at 1, which means that your model, which is drawn at z=0 is outside the view frustum and gets culled away.

Another problem: You are calling glVertex2i at least once outside of glBegin/glEnd which results in an OpenGL error.

Regarding the coordinates: Note, that drawing in OpenGL does not happen in pixel coordinates unless you specify an appropriate projection to do so.

Upvotes: 1

Related Questions