Example #1
0
// This function does any needed initialization on the rendering
// context.
void SetupRC(void)
  {
  // Background
  glClearColor(0.0f, 0.0f, 0.0f, 1.0f );

  // A number of shipping drivers are not conformant to the current OpenGL
  // spec and require this. NVidia... in particular. The OpenGL specification
  // states that this is always "on", in fact you can't enable or disable it
  // anymore. Adding this lines "fixes" this on non-conformant drivers, but
  // be aware, if you have a pure core (and working correctly) GL context,
  //you should not do this
  glEnable(GL_POINT_SPRITE);

  GLfloat fColors[4][4] = {{ 1.0f, 1.0f, 1.0f, 1.0f}, // White
                             { 0.67f, 0.68f, 0.82f, 1.0f}, // Blue Stars
                             { 1.0f, 0.5f, 0.5f, 1.0f}, // Reddish
                           { 1.0f, 0.82f, 0.65f, 1.0f}}; // Orange


    // Randomly place the stars in their initial positions, and pick a random color
    starsBatch.Begin(GL_POINTS, NUM_STARS);
    for(int i = 0; i < NUM_STARS; i++)
        {
    int iColor = 0;		// All stars start as white

    // One in five will be blue
        if(rand() % 5 == 1)
      iColor = 1;

    // One in 50 red
    if(rand() % 50 == 1)
      iColor = 2;

    // One in 100 is amber
    if(rand() % 100 == 1)
      iColor = 3;


    starsBatch.Color4fv(fColors[iColor]);

    M3DVector3f vPosition;
    vPosition[0] = float(3000 - (rand() % 6000)) * 0.1f;
    vPosition[1] = float(3000 - (rand() % 6000)) * 0.1f;
    vPosition[2] = -float(rand() % 1000)-1.0f;  // -1 to -1000.0f

    starsBatch.Vertex3fv(vPosition);
    }
    starsBatch.End();


    starFieldShader = gltLoadShaderPairWithAttributes(
                          "vertexSpace.shader","fragmentSpace.shader", 2,
                                    GLT_ATTRIBUTE_VERTEX,"vVertex",
                                    GLT_ATTRIBUTE_COLOR, "vColor");

  locMVP = glGetUniformLocation(starFieldShader, "mvpMatrix");
  locTexture = glGetUniformLocation(starFieldShader, "starImage");
    locTimeStamp = glGetUniformLocation(starFieldShader, "timeStamp");

  glGenTextures(1, &starTexture);
  glBindTexture(GL_TEXTURE_2D, starTexture);
  LoadTGATexture("star.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);
    }
Example #2
0
void MakePyramid(GLBatch& pyramidBatch)
    {
	pyramidBatch.Begin(GL_TRIANGLES, 18, 1);
    
	// Bottom of pyramid
	pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f);
	pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
	pyramidBatch.Vertex3f(-1.0f, -1.0f, -1.0f);
    
	pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f);
	pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
	pyramidBatch.Vertex3f(1.0f, -1.0f, -1.0f);
    
	pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f);
	pyramidBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
	pyramidBatch.Vertex3f(1.0f, -1.0f, 1.0f);
    
	pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f);
	pyramidBatch.MultiTexCoord2f(0, 0.0f, 1.0f);
	pyramidBatch.Vertex3f(-1.0f, -1.0f, 1.0f);
    
	pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f);
	pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
	pyramidBatch.Vertex3f(-1.0f, -1.0f, -1.0f);
    
	pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f);
	pyramidBatch.MultiTexCoord2f(0, 1.0f, 1.0f);
	pyramidBatch.Vertex3f(1.0f, -1.0f, 1.0f);
    
	
	M3DVector3f vApex = { 0.0f, 1.0f, 0.0f };
	M3DVector3f vFrontLeft = { -1.0f, -1.0f, 1.0f };
	M3DVector3f vFrontRight = { 1.0f, -1.0f, 1.0f };
	M3DVector3f vBackLeft = { -1.0f, -1.0f, -1.0f };
	M3DVector3f vBackRight = { 1.0f, -1.0f, -1.0f };
	M3DVector3f n;
	
	// Front of Pyramid
	m3dFindNormal(n, vApex, vFrontLeft, vFrontRight);
	pyramidBatch.Normal3fv(n);
	pyramidBatch.MultiTexCoord2f(0, 0.5f, 1.0f);
	pyramidBatch.Vertex3fv(vApex);		// Apex
    
	pyramidBatch.Normal3fv(n);
	pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
	pyramidBatch.Vertex3fv(vFrontLeft);		// Front left corner
    
	pyramidBatch.Normal3fv(n);
	pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
	pyramidBatch.Vertex3fv(vFrontRight);		// Front right corner
    
    
	m3dFindNormal(n, vApex, vBackLeft, vFrontLeft);
	pyramidBatch.Normal3fv(n);
	pyramidBatch.MultiTexCoord2f(0, 0.5f, 1.0f);
	pyramidBatch.Vertex3fv(vApex);		// Apex
    
	pyramidBatch.Normal3fv(n);
	pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
	pyramidBatch.Vertex3fv(vBackLeft);		// Back left corner
	
	pyramidBatch.Normal3fv(n);
	pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
	pyramidBatch.Vertex3fv(vFrontLeft);		// Front left corner
    
	m3dFindNormal(n, vApex, vFrontRight, vBackRight);
	pyramidBatch.Normal3fv(n);
	pyramidBatch.MultiTexCoord2f(0, 0.5f, 1.0f);
	pyramidBatch.Vertex3fv(vApex);				// Apex
    
	pyramidBatch.Normal3fv(n);
	pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
	pyramidBatch.Vertex3fv(vFrontRight);		// Front right corner
    
	pyramidBatch.Normal3fv(n);
	pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
	pyramidBatch.Vertex3fv(vBackRight);			// Back right cornder
    
    
	m3dFindNormal(n, vApex, vBackRight, vBackLeft);
	pyramidBatch.Normal3fv(n);
	pyramidBatch.MultiTexCoord2f(0, 0.5f, 1.0f);
	pyramidBatch.Vertex3fv(vApex);		// Apex
    
	pyramidBatch.Normal3fv(n);
	pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
	pyramidBatch.Vertex3fv(vBackRight);		// Back right cornder

	pyramidBatch.Normal3fv(n);
	pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f);
	pyramidBatch.Vertex3fv(vBackLeft);		// Back left corner

	pyramidBatch.End();
	}
Example #3
0
void generateCuboidBatch(GLBatch& batch, float length, float width, float height)
{
    float halfLength = length / 2;
    float halfWidth = width / 2;
    float halfHeight = height / 2;

    M3DVector3f frontLeftBottom  = {-halfLength, -halfHeight,  halfWidth};
    M3DVector3f frontLeftTop     = {-halfLength,  halfHeight,  halfWidth};
    M3DVector3f frontRightBottom = { halfLength, -halfHeight,  halfWidth};
    M3DVector3f frontRightTop    = { halfLength,  halfHeight,  halfWidth};
    M3DVector3f backLeftBottom   = {-halfLength, -halfHeight, -halfWidth};
    M3DVector3f backLeftTop      = {-halfLength,  halfHeight, -halfWidth};
    M3DVector3f backRightBottom  = { halfLength, -halfHeight, -halfWidth};
    M3DVector3f backRightTop     = { halfLength,  halfHeight, -halfWidth};

    batch.Begin(GL_QUADS, 24, 1);
    
    // bottom face
    batch.Normal3f(0, -1, 0);
    batch.MultiTexCoord2f(0, 0, 1);
    batch.Vertex3fv(frontLeftBottom);
    batch.Normal3f(0, -1, 0);
    batch.MultiTexCoord2f(0, 0, 0);
    batch.Vertex3fv(backLeftBottom);
    batch.Normal3f(0, -1, 0);
    batch.MultiTexCoord2f(0, 1, 0);
    batch.Vertex3fv(backRightBottom);
    batch.Normal3f(0, -1, 0);
    batch.MultiTexCoord2f(0, 1, 1);
    batch.Vertex3fv(frontRightBottom);

    // up face
    batch.Normal3f(0, 1, 0);
    batch.MultiTexCoord2f(0, 0, 1);
    batch.Vertex3fv(frontLeftTop);
    batch.Normal3f(0, 1, 0);
    batch.MultiTexCoord2f(0, 1, 1);
    batch.Vertex3fv(frontRightTop);
    batch.Normal3f(0, 1, 0);
    batch.MultiTexCoord2f(0, 1, 0);
    batch.Vertex3fv(backRightTop);
    batch.Normal3f(0, 1, 0);
    batch.MultiTexCoord2f(0, 0, 0);
    batch.Vertex3fv(backLeftTop);

    // front face
    batch.Normal3f(0, 0, 1);
    batch.MultiTexCoord2f(0, 0, 3);
    batch.Vertex3fv(frontLeftTop);
    batch.Normal3f(0, 0, 1);
    batch.MultiTexCoord2f(0, 0, 0);
    batch.Vertex3fv(frontLeftBottom);
    batch.Normal3f(0, 0, 1);
    batch.MultiTexCoord2f(0, 3, 0);
    batch.Vertex3fv(frontRightBottom);
    batch.Normal3f(0, 0, 1);
    batch.MultiTexCoord2f(0, 3, 3);
    batch.Vertex3fv(frontRightTop);

    // back face
    batch.Normal3f(0, 0, -1);
    batch.MultiTexCoord2f(0, 0, 1);
    batch.Vertex3fv(backLeftTop);
    batch.Normal3f(0, 0, -1);
    batch.MultiTexCoord2f(0, 1, 1);
    batch.Vertex3fv(backRightTop);
    batch.Normal3f(0, 0, -1);
    batch.MultiTexCoord2f(0, 1, 0);
    batch.Vertex3fv(backRightBottom);
    batch.Normal3f(0, 0, -1);
    batch.MultiTexCoord2f(0, 0, 0);
    batch.Vertex3fv(backLeftBottom);

    // left face
    batch.Normal3f(-1, 0, 0);
    batch.MultiTexCoord2f(0, 0, 3);
    batch.Vertex3fv(backLeftTop);
    batch.Normal3f(-1, 0, 0);
    batch.MultiTexCoord2f(0, 0, 0);
    batch.Vertex3fv(backLeftBottom);
    batch.Normal3f(-1, 0, 0);
    batch.MultiTexCoord2f(0, 3, 0);
    batch.Vertex3fv(frontLeftBottom);
    batch.Normal3f(-1, 0, 0);
    batch.MultiTexCoord2f(0, 3, 3);
    batch.Vertex3fv(frontLeftTop);

    // right face
    batch.Normal3f(1, 0, 0);
    batch.MultiTexCoord2f(0, 0, 1);
    batch.Vertex3fv(backRightTop);
    batch.Normal3f(1, 0, 0);
    batch.MultiTexCoord2f(0, 1, 1);
    batch.Vertex3fv(frontRightTop);
    batch.Normal3f(1, 0, 0);
    batch.MultiTexCoord2f(0, 1, 0);
    batch.Vertex3fv(frontRightBottom);
    batch.Normal3f(1, 0, 0);
    batch.MultiTexCoord2f(0, 0, 0);
    batch.Vertex3fv(backRightBottom);

    batch.End();
}