Exemple #1
0
// RENDERS AN OBJECT USING A SKYBOX AS THE TEXTURE SOURCE
void displayBoxFun(GLuint shaderProg) {
  Matrix4f viewMat, projMat, modelMat, m;
  // set up the mode to wireframe

  // setting up the transformaiton of the object from model coord. system to world coord.
  modelMat = Matrix4f::identity();
  //modelMat = Matrix4f::translation(0,0,0);

  modelMat = Matrix4f::scale(50,50,50);

  // ROATE THE OBJECT AROUND THE CAMERA VECTOR
  // CAN ADD ROTATIONS AROUND THE PRIMARY AXIS
  modelMat = modelMat * Matrix4f::rotateVector(cam.getLookAtVector(),angle,1);

  // setting up the viewpoint transformation
  viewMat = cam.getViewMatrix(NULL);

  // setting up the projection transformation
  projMat= cam.getProjectionMatrix(NULL);

  // putting it all together
  m = projMat * viewMat * modelMat;

  // tell openfl which shader program to use
  glUseProgram(shaderProg);

  // transfer the modelViewProjection  matrix to the shader
  GLuint locMat= 0;
  locMat=glGetUniformLocation(shaderProg,  "modelViewProjMat");
  glUniformMatrix4fv(locMat,1,1,(float *)m.vm);

  // transfer the modelView matrix to the shader
  m = viewMat * modelMat;
  locMat=glGetUniformLocation(shaderProg,  "modelViewMat");
  glUniformMatrix4fv(locMat,1,1,(float *)m.vm);

  // transfer the model matrix to the shader
  m = modelMat;
  locMat=glGetUniformLocation(shaderProg,  "modelMat");
  glUniformMatrix4fv(locMat,1,1,(float *)m.vm);

  // load the camera position to the shader
  locMat=glGetUniformLocation(shaderProg,  "camPos");
  Vector3f camPos = cam.getPosition();
  glUniform3fv(locMat,1, (float *) &camPos);

  // load the refract flag to the shader
  locMat=glGetUniformLocation(shaderProg,  "refractFlag");
  glUniform1i(locMat, refractFlag);

  glActiveTexture(GL_TEXTURE3);
  GLuint texCube = skybox.getTexHandle();
  glBindTexture(GL_TEXTURE_CUBE_MAP, texCube);
  GLuint samLoc = glGetUniformLocation(shaderProg, "texCube");
  glUniform1i(samLoc, 3);

  GLint ttt = 0;
  glGetUniformiv(shaderProg, samLoc, &ttt);

  // bind the buffers to the shaders
  GLuint positionLoc = glGetAttribLocation(shaderProg, "vertex_position");
  GLuint normalLoc = glGetAttribLocation(shaderProg, "vertex_normal");

  glEnableVertexAttribArray(positionLoc);
  glEnableVertexAttribArray(normalLoc);

  glBindBuffer(GL_ARRAY_BUFFER, vertex_handle);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangle_handle);

  // Tells OpenGL how to walk through the two VBOs
  struct sphereVertex v;
  int relAddress = (char *) v.pos - (char *) &v;
  glVertexAttribPointer(positionLoc,4,GL_FLOAT, GL_FALSE, sizeof(struct sphereVertex),(char*) NULL+relAddress);
  relAddress = (char *) v.normal - (char *) &v;
  glVertexAttribPointer(normalLoc,4,GL_FLOAT, GL_FALSE, sizeof(struct sphereVertex),(char*) NULL+relAddress);

  // draw the triangles
  glDrawElements(GL_TRIANGLES, numTriangles*3, GL_UNSIGNED_INT, (char*) NULL+0);

  // glUseProgram(0);
  // glUseProgram(shaderProg);

  // draw the second sphere

  modelMat = Matrix4f::identity();
  //modelMat = Matrix4f::translation(0,0,0);

  modelMat = Matrix4f::scale(50,50,50) * Matrix4f::translation(2, 0, 0);

  // ROATE THE OBJECT AROUND THE CAMERA VECTOR
  // CAN ADD ROTATIONS AROUND THE PRIMARY AXIS
  modelMat = modelMat * Matrix4f::rotateVector(cam.getLookAtVector(),angle,1);

  // setting up the viewpoint transformation
  viewMat = cam.getViewMatrix(NULL);

  // setting up the projection transformation
  projMat= cam.getProjectionMatrix(NULL);

  // putting it all together
  m = projMat * viewMat * modelMat;

  locMat= 0;
  locMat=glGetUniformLocation(shaderProg,  "modelViewProjMat");
  glUniformMatrix4fv(locMat,1,1,(float *)m.vm);

  // transfer the modelView matrix to the shader
  m = viewMat * modelMat;
  locMat=glGetUniformLocation(shaderProg,  "modelViewMat");
  glUniformMatrix4fv(locMat,1,1,(float *)m.vm);

  // transfer the model matrix to the shader
  m = modelMat;
  locMat=glGetUniformLocation(shaderProg,  "modelMat");
  glUniformMatrix4fv(locMat,1,1,(float *)m.vm);

  glActiveTexture(GL_TEXTURE3);
  GLuint texCube2 = skybox2.getTexHandle();
  glBindTexture(GL_TEXTURE_CUBE_MAP, texCube2);
  GLuint samLoc2 = glGetUniformLocation(shaderProg, "texCube");
  glUniform1i(samLoc2, 3);

  GLint ttt2 = 0;
  glGetUniformiv(shaderProg, samLoc, &ttt2);

  glBindBuffer(GL_ARRAY_BUFFER, vertex_handle2);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, triangle_handle2);

  // Tells OpenGL how to walk through the two VBOs
  // struct sphereVertex v;
  relAddress = (char *) v.pos - (char *) &v;
  glVertexAttribPointer(positionLoc,4,GL_FLOAT, GL_FALSE, sizeof(struct sphereVertex),(char*) NULL+relAddress);
  relAddress = (char *) v.normal - (char *) &v;
  glVertexAttribPointer(normalLoc,4,GL_FLOAT, GL_FALSE, sizeof(struct sphereVertex),(char*) NULL+relAddress);

  // draw the triangles
  glDrawElements(GL_TRIANGLES, numTriangles*3, GL_UNSIGNED_INT, (char*) NULL+0);

  return;
}
Exemple #2
0
void SkyBox::displaySkybox(camera cam) {

  int t = 1;  // remove
  glDisable(GL_DEPTH_TEST);    // need depth test to correctly draw 3D objects

  Matrix4f viewMat, projMat, modelMat, m;

  // set local camera for the skybox
  camera c1 = cam;
  //c1.changeAbsPoition(0,0,0);
  //c1.changeLookAtVector(1, 1, 1);

  //glFrontFace(GL_CW);
  //glCullFace(FRONT);

  // set up the mode to wireframe
  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  //set up the mode to fill
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

  // get the transformation matrix
  modelMat = Matrix4f::translation(cam.getPosition());

  // setting up the viewpoint transformation
  viewMat = c1.getViewMatrix(NULL);

  // setting up the projection transformation  make sure that it is the same
  // projection as the display function!!!
  //projMat =   Matrix4f::symmetricPerspectiveProjectionMatrix(30, 1, 0.1, 500);
  projMat = cam.getProjectionMatrix(NULL);

  // frustum matrix
  //projMat = Matrix4f::frustumProjectionMatrix(-1,-1,1,1, 10,100);

  // putting it all together
  m = projMat * viewMat * modelMat;

  // load the program to the shader
  glUseProgram(shaderProg);

  viewMat.m = (float *) viewMat.vm;
  projMat.m = (float *) projMat.vm;

  // transfer the matrix to the shader
  GLuint locMat= 0;
  locMat=glGetUniformLocation(shaderProg,  "modelViewProjMat");
  glUniformMatrix4fv(locMat,1,1,(float *)m.vm);

  m = viewMat*modelMat;
  locMat=glGetUniformLocation(shaderProg,  "modelViewMat");
  glUniformMatrix4fv(locMat,1,1,(float *) m.vm);

  // set the time
  GLuint tLoc = glGetUniformLocation(shaderProg, "t");
  glUniform1i(tLoc, t);

  // set the time
  GLuint cameraPositiontLoc = glGetUniformLocation(shaderProg, "cameraPosition");
  glEnableVertexAttribArray(cameraPositiontLoc);

  Vector3f camPos = c1.getPosition();
  glUniform3f(cameraPositiontLoc, camPos.x, camPos.y, camPos.z);

  //glActiveTexture (GL_TEXTURE1);
  glActiveTexture(GL_TEXTURE3);
  glBindTexture(GL_TEXTURE_CUBE_MAP, texHandle);
  GLuint texLoc = glGetUniformLocation(shaderProg, "texCube");
    glUniform1i(texLoc, 3);
  GLint ttt = 0;
  glGetUniformiv(shaderProg, texLoc, &ttt);

  //glUniform1i(glGetUniformLocation(skyboxProg, "texCube"), texCube);
  //glDisable(GL_CULL_FACE);

  // bind the buffers to the shaders
  GLuint positionLoc = glGetAttribLocation(shaderProg, "vertex_position");
  //GLuint normalLoc = glGetAttribLocation(shaderProg, "vertex_normal");
  //GLuint texLoc = glGetAttribLocation(shaderProg, "texCoord");

  glEnableVertexAttribArray(positionLoc);
  //glEnableVertexAttribArray(normalLoc);
  //glEnableVertexAttribArray(texLoc);
  glBindBuffer(GL_ARRAY_BUFFER, vboCube);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboCube);


  // Tells OpenGL how to walk through the two VBOs
  glVertexAttribPointer(positionLoc,4,GL_FLOAT, GL_FALSE, 0,0);
  // draw the triangles
  glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, (char*) NULL+0);

  //glutSwapBuffers();
  glEnable(GL_DEPTH_TEST);    // need depth test to correctly draw 3D objects
  return;
}