void RenderSystem::render(std::vector<Entity *> *entityArray)
{
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

    for (std::vector<Entity *>::iterator iterator = entityArray->begin(); iterator != entityArray->end(); iterator++)
    {
        Entity*entity = *iterator;
        
        if (entity->getVertexBuffer() != NULL) {
            
        glUseProgram(entity->getVertexBuffer()->getShader()->getProgramHandle());
        glBindBuffer(GL_ARRAY_BUFFER, entity->getVertexBuffer()->getVertexBufferID());
        glLoadIdentity();
            
            gluLookAt(_currentCamera->getPosition().x,
                      _currentCamera->getPosition().y,
                      _currentCamera->getPosition().z,
                      _currentCamera->getEyeVec().x,
                      _currentCamera->getEyeVec().y,
                      _currentCamera->getEyeVec().z,
                      _currentCamera->getUpVec().x,
                      _currentCamera->getUpVec().y,
                      _currentCamera->getUpVec().z);
            
            
            
            glTranslatef(entity->getPosition().x, entity->getPosition().y, entity->getPosition().z);
            if (entity->getPosition().x >= 0.9 && entity->getVelocity().x > 0.0f) {
                entity->setVelocity(makeVec3((-1 * entity->getVelocity().x), entity->getVelocity().y, entity->getVelocity().z));
            }
            if (entity->getPosition().x <= -0.9 && entity->getVelocity().x < 0.0f) {
                entity->setVelocity(makeVec3((-1 * entity->getVelocity().x), entity->getVelocity().y, entity->getVelocity().z));
            }
            if (entity->getPosition().x >= 0.9 && entity->getVelocity().x > 0.0f) {
                entity->setVelocity(makeVec3((-1 * entity->getVelocity().x), entity->getVelocity().y, entity->getVelocity().z));
            }
            if (entity->getPosition().y >= 0.9 && entity->getVelocity().y > 0.0f) {
                entity->setVelocity(makeVec3( entity->getVelocity().x, (-1 *entity->getVelocity().y), entity->getVelocity().z));
            }
            if (entity->getPosition().y <= -0.9 && entity->getVelocity().y < 0.0f) {
                entity->setVelocity(makeVec3( entity->getVelocity().x, (-1 *entity->getVelocity().y), entity->getVelocity().z));
            }
            
            glRotatef(entity->getRotation().x, 0.0f, 0.0f, 1.0f);
            glRotatef(entity->getRotation().y, 0.0f, 1.0f, 0.0f);
            glRotatef(entity->getRotation().z, 1.0f, 0.0f, 0.0f);
            
            glScalef(entity->getScale().x, entity->getScale().y, entity->getScale().z);
            
            glUniform4f((entity->getVertexBuffer()->getShader())->get_uColor(),
                        (entity->getVertexBuffer()->getShaderData())->get_uColorValue().x,
                        (entity->getVertexBuffer()->getShaderData())->get_uColorValue().y,
                        (entity->getVertexBuffer()->getShaderData())->get_uColorValue().z,
                        (entity->getVertexBuffer()->getShaderData())->get_uColorValue().w);
            
            glUniform3f((entity->getVertexBuffer()->getShader())->get_uLightPosition(),
                        (entity->getVertexBuffer()->getShaderData())->get_uLightPosition().x,
                        (entity->getVertexBuffer()->getShaderData())->get_uLightPosition().y,
                        (entity->getVertexBuffer()->getShaderData())->get_uLightPosition().z);
            
            entity->getVertexBuffer()->configureVertexAttributes();
            entity->getVertexBuffer()->renderVertexBuffer();
            //glfwSwapBuffers(_window);
            //glfwPollEvents();
            
        }
        
    }
    glfwSwapBuffers(_window);
    glfwPollEvents();
    //glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

 
}
ResourceManager::ResourceManager()
{
    _shaderArray = new std::vector<ShaderInterface *>();
    _shaderData = new std::vector<ShaderData*>();
    ShaderInterface* shader = new ShaderInterface("ColorShader.vsh", "ColorShader.fsh");
    _shaderArray->push_back(shader);
    
    ShaderInterface* lightShader = new ShaderInterface("SimpleLightShader.vsh", "SimpleLightShader.fsh");
    _shaderArray->push_back(lightShader);
    
    
    ShaderData* green = new ShaderData(makeVec4(.20f, .90f, 0.20f, 1.0f), makeVec3(0.0f, 0.50f, 1.0f));
    _shaderData->push_back(green);
    
    ShaderData* blue = new ShaderData(makeVec4(0.4f, 0.3f, 1.0f, 1.0f), makeVec3(0.0f, .50f, 1.0f));
    _shaderData->push_back(blue);
    
    ShaderData* purple = new ShaderData(makeVec4(.6f, 0.0f, .7f, 1.0f), makeVec3(0.0f, 0.50f, 1.0f));
    _shaderData->push_back(purple);
    
    ShaderData* orange = new ShaderData(makeVec4(1.0f, .50f, 0.0f, 1.0f),makeVec3(0.0f, 0.50f, 1.0f));
    _shaderData->push_back(orange);
    
    ShaderData* red = new ShaderData(makeVec4(1.0f, .3f, .3f, 1.0f), makeVec3(0.0f, 0.50f, 1.0f));
    _shaderData->push_back(red);

    
    
    _vertexBufferArray = new std::vector<VertexBuffer*>();
    
   VertexBuffer *square0 = new VertexBuffer(PlaneVertices,
                                            sizeof(PlaneVertices),
                                            GL_QUADS,
                                            4,
                                            sizeof(VertexDataPosn),
                                            _shaderArray->at(1),
                                            _shaderData->at(0),
                                            NULL,
                                            NULL);
    
    _vertexBufferArray->push_back(square0);
    
    VertexBuffer *square1 = new VertexBuffer(PlaneVertices,
                                             sizeof(PlaneVertices),
                                             GL_QUADS,
                                             4,
                                             sizeof(VertexDataPosn),
                                             _shaderArray->at(1),
                                             _shaderData->at(1),
                                             NULL,
                                             NULL);
    
    _vertexBufferArray->push_back(square1);

    
    VertexBuffer *square2 = new VertexBuffer(PlaneVertices,
                                             sizeof(PlaneVertices),
                                             GL_QUADS,
                                             4,
                                             sizeof(VertexDataPosn),
                                             _shaderArray->at(1),
                                             _shaderData->at(2),
                                             NULL,
                                             NULL);
    
    _vertexBufferArray->push_back(square2);

    VertexBuffer *square3 = new VertexBuffer(PlaneVertices,
                                             sizeof(PlaneVertices),
                                             GL_QUADS,
                                             4,
                                             sizeof(VertexDataPosn),
                                             _shaderArray->at(1),
                                             _shaderData->at(3),
                                             NULL,
                                             NULL);
    
    _vertexBufferArray->push_back(square3);

    VertexBuffer *square4 = new VertexBuffer(PlaneVertices,
                                             sizeof(PlaneVertices),
                                             GL_QUADS,
                                             4,
                                             sizeof(VertexDataPosn),
                                             _shaderArray->at(1),
                                             _shaderData->at(4),
                                             NULL,
                                             NULL);
    
    _vertexBufferArray->push_back(square4);

    
    
}
Exemple #3
0
int main(void)
{
	srand(time(NULL));
	//Set the error callback  
	glfwSetErrorCallback(error_callback);

	//Initialize GLFW  
	if (!glfwInit())
	{
		exit(EXIT_FAILURE);
	}

	//Set the GLFW window creation hints - these are optional  
	//glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //Request a specific OpenGL version  
	//glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //Request a specific OpenGL version  
	//glfwWindowHint(GLFW_SAMPLES, 4); //Request 4x antialiasing  
	//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);  

	//Declare a window object  
	GLFWwindow* window;

	//Create a window and create its OpenGL context  
	window = glfwCreateWindow(800, 600, "Test Window", NULL, NULL);

	//If the window couldn't be created  
	if (!window)
	{
		fprintf(stderr, "Failed to open GLFW window.\n");
		glfwTerminate();
		exit(EXIT_FAILURE);
	}

	//This function makes the context of the specified window current on the calling thread.   
	glfwMakeContextCurrent(window);

	//Sets the key callback  
	glfwSetKeyCallback(window, key_callback);

	//Set cursor pos callback
	glfwSetCursorPosCallback(window, cursor_position_callback);

	//Initialize GLEW  
	GLenum err = glewInit();

	//If GLEW hasn't initialized  
	if (err != GLEW_OK)
	{
		fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
		return -1;
	}

	//Set a background color  
	glClearColor(0.5f, 0.5f, .05f, 0.0f);
	glfwSwapInterval(1);

	//Initialization
	chars[0].x1 = -.25f;
	chars[0].x2 = .25f;
	chars[0].y1 = -.25f;
	chars[0].y2 = .25f;
	chars[0].rotation = 0.0f;

	chars[1].x1 = -.25f;
	chars[1].x2 = .25f;
	chars[1].y1 = -.25f;
	chars[1].y2 = .25f;
	chars[1].rotation = 0.0f;

	//New Drawing Test
	// Create Vertex Array Object
	GLuint vao;
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	// Create a Vertex Buffer Object and copy the vertex data to it
	GLuint vbo;
	glGenBuffers(1, &vbo);

	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	GLuint ebo;
	glGenBuffers(1, &ebo);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);

	// Create and compile the vertex shader
	GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertexShader, 1, &vertexSource, NULL);
	glCompileShader(vertexShader);

	// Create and compile the fragment shader
	GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
	glCompileShader(fragmentShader);

	// Link the vertex and fragment shader into a shader program
	GLuint shaderProgram = glCreateProgram();
	glAttachShader(shaderProgram, vertexShader);
	glAttachShader(shaderProgram, fragmentShader);
	glBindFragDataLocation(shaderProgram, 0, "outColor"); //Unnecsary for now
	glLinkProgram(shaderProgram);
	glUseProgram(shaderProgram);

	// Specify the layout of the vertex data
	GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
	glEnableVertexAttribArray(posAttrib);
	glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), 0);

	// Get the location of the color uniform
	//GLint uniColor = glGetUniformLocation(shaderProgram, "triangleColor");
	GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
	glEnableVertexAttribArray(colAttrib);
	glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));

	//Texture coordinates
	GLint texAttrib = glGetAttribLocation(shaderProgram, "texcoord");
	glEnableVertexAttribArray(texAttrib);
	glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(5 * sizeof(GLfloat)));
	
	// Load textures
	GLuint textures[2];
	glGenTextures(2, textures);

	int width, height;
	unsigned char* image;

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, textures[0]);
	image = SOIL_load_image("sample.png", &width, &height, 0, SOIL_LOAD_RGB);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
	SOIL_free_image_data(image);
	glUniform1i(glGetUniformLocation(shaderProgram, "texKitten"), 0);
	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, textures[1]);
	image = SOIL_load_image("sample2.png", &width, &height, 0, SOIL_LOAD_RGB);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
	SOIL_free_image_data(image);
	glUniform1i(glGetUniformLocation(shaderProgram, "texPuppy"), 1);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	
	//Transformations
	
	//printf("X: %f\nY: %f\nZ: %f\n", trans.arr[0], trans.arr[5], trans.arr[10]);
	GLint uniModel = glGetUniformLocation(shaderProgram, "model");

	//
	mat4* view = lookAtMat4(makeVec3(1.2f, 1.2f, 1.2f),
		makeVec3(0.0f, 0.0f, 0.0f),
		makeVec3(0.0f, 0.0f, 1.0f));
	GLint uniView = glGetUniformLocation(shaderProgram, "view");
	glUniformMatrix4fv(uniView, 1, GL_FALSE, view);

	mat4* proj = perspective(45.0f, 800.0f / 600.0f, 1.0f, 10.0f);
	GLint uniProj = glGetUniformLocation(shaderProgram, "proj");
	glUniformMatrix4fv(uniProj, 1, GL_FALSE, proj);
	
	GLint uniTime = glGetUniformLocation(shaderProgram, "time");

	//GLfloat test = *(GLfloat*)malloc(sizeof(GLfloat));
	//test = 45.0f;
	//printf("%f : %f\n", degToRadCos(test), degToRadSin(test));
	//printVec3(normalizeNewVec3(makeVec3(3, 2, 7)));
	//printVec3(normalizeNewVec3(makeVec3(11, 4, 1)));

	//Main Loop  
	do
	{
		//float ratio;
		//int width, height;
		//glfwGetFramebufferSize(window, &width, &height);
		//ratio = width / (float)height;

		//glViewport(0, 0, width, height);

		//Clear color buffer  
		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		//glClearColor((float)rand() / (float)RAND_MAX, (float)rand() / (float)RAND_MAX, (float)rand() / (float)RAND_MAX, 1.0f);
		
		movement(&chars[0]);
		mat4* model;
		model = rotateMat4(glfwGetTime() * 180.0f, makeVec3(0.0f, 0.0f, 1.0f));
		glUniformMatrix4fv(uniModel, 1, GL_FALSE, model);
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

		//Swap buffers  
		glfwSwapBuffers(window);
		//Get and organize events, like keyboard and mouse input, window resizing, etc...  
		glfwPollEvents();

	} //Check if the ESC key had been pressed or if the window had been closed  
	while (!glfwWindowShouldClose(window));

	//Close OpenGL window and terminate GLFW  
	glfwDestroyWindow(window);
	//Finalize and clean up GLFW  
	glfwTerminate();

	exit(EXIT_SUCCESS);
}
Exemple #4
0
void INode::renderTree(Context& ctx) const {
  for (size_t i = 0; i < _states.size(); i++) {
    // start point
    vec3 startpoint = ctx.getCurrentOrigin();

    // calculate end point of joint
    vec3 endpoint = _states[i]->getEndpoint(ctx);

    // draw the link
    //glBegin(GL_LINES);
    //  glColor3d(1.0, 1.0, 1.0);
    //  glVertex3d(startpoint[0], startpoint[1], startpoint[2]);
    //  glVertex3d(endpoint[0], endpoint[1], endpoint[2]);
    //glEnd();

    // calculate orthonormal basis for cylinder on joint
    vec3 u, v, n;
    _states[i]->getBasis(ctx, u, v, n);

    // check if basis is really orthonormal
    assert(double_equals(dot(u, v), 0));
    assert(double_equals(dot(u, n), 0));
    assert(double_equals(dot(v, n), 0));

    assert(double_equals(norm(u, 2), 1));
    assert(double_equals(norm(v, 2), 1));
    assert(double_equals(norm(n, 2), 1));

    //cout << "pos:" << endl << pos << endl;

    //cout << "u:" << endl << u << endl;
    //cout << "v:" << endl << v << endl;
    //cout << "n:" << endl << n << endl;

    vec3 x = makeVec3(1, 0, 0); 
    vec3 y = makeVec3(0, 1, 0);
    vec3 z = makeVec3(0, 0, 1);

    double ux = dot(x, u);
    double uy = dot(y, u);
    double uz = dot(z, u);

    double vx = dot(x, v);
    double vy = dot(y, v);
    double vz = dot(z, v);

    double nx = dot(x, n);
    double ny = dot(y, n);
    double nz = dot(z, n);

    // change of orthonormal basis from uvn -> xyz
    GLdouble m[16];
    m[0]  = ux;
    m[1]  = uy;
    m[2]  = uz;
    m[3]  = 0;
    
    m[4]  = vx; 
    m[5]  = vy;
    m[6]  = vz;
    m[7]  = 0;

    m[8]  = nx;
    m[9]  = ny;
    m[10] = nz;
    m[11] = 0;

    m[12] = 0; m[13] = 0; m[14] = 0; m[15] = 1;

    mat44 A; 
    A << ux << vx << nx << 0 << endr 
      << uy << vy << ny << 0 << endr
      << uz << vz << nz << 0 << endr 
      << 0  << 0  << 0  << 1 << endr;

    //if (!double_equals(det(A), 1))
    //  cout << "A is: " << endl << A << endl;

    //cout << "det(A): " << det(A) << endl;
    const double dA = det(A);
    if (!double_equals(dA, 1)) {
      cerr << "ERROR: det(A) = " << dA << endl; 
      throw runtime_error("determinant not 1 for rotation matrix");
    }


    //cout << "--" << endl;
    //for (int iii = 0; iii < 16; iii++) {
    //  cout << m[iii] << endl;
    //}
    //cout << "--" << endl;

    if (isRootNode())
      glColor3d(0.0, 0.0, 0.8);
    else if (isFixed())
      glColor3d(0.0, 1.0, 1.0); 
    else
      glColor3d(0.0, 0.0, 1.0);

    glPushMatrix();
      glTranslated(startpoint[0], startpoint[1], startpoint[2]);
      if (isRootNode())
        glutSolidSphere(0.1, 20, 20);
      else
        glutSolidSphere(0.08, 20, 20);
    glPopMatrix();

    GLUquadricObj *quadric = gluNewQuadric();

    glPushMatrix();
      glColor3d(0.0, 1.0, 0.0);
      glTranslated(startpoint[0], startpoint[1], startpoint[2]);
      glMultMatrixd(m);
      gluCylinder(quadric, 0.05, 0.05, _states[i]->getLength(), 32, 32);
    glPopMatrix();

    gluDeleteQuadric(quadric);

    // recurse into child
    _states[i]->pushContext(ctx);
      _kids[i]->renderTree(ctx);
    ctx.popContext();
  }
}