Exemple #1
0
void Draw_Manager::Rectangle2D(Vector2f pos1, Vector2f pos2, Vector4f color) {
	//FIXME: what I was supposed to fix here
	drawVBO();

	glDisable(GL_TEXTURE_2D);
	VBO.Add(Vector3f(pos1.x,	pos1.y,	0),	Vector2f(0,0));
	VBO.Add(Vector3f(pos2.x,	pos1.y,	0),	Vector2f(0,0));
	VBO.Add(Vector3f(pos2.x,	pos2.y,	0),	Vector2f(0,0));
			
	VBO.Add(Vector3f(pos1.x,	pos1.y,	0),	Vector2f(0,0));
	VBO.Add(Vector3f(pos2.x,	pos2.y,	0),	Vector2f(0,0));
	VBO.Add(Vector3f(pos1.x,	pos2.y,	0),	Vector2f(0,0));

	glColor4f(color.x,color.y,color.z,color.w);
	drawVBO();

	glEnable(GL_TEXTURE_2D);
}
Exemple #2
0
int main(void)
{
    GLFWwindow* window;
    
    /* Initialize the library */
    if (!glfwInit())
        return -1;
    
    //使用OpenGL 3.2
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    
    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);
    
    initTriangle();
    initShader();

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClearColor(0.0f,0.0f,0.0f,0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        drawVBO();
        
        /* Swap front and back buffers */
        glfwSwapBuffers(window);
        
        /* Poll for and process events */
        glfwPollEvents();
    }
    
    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}
void drawPoints(){



	//New shader code here
	for (int i=0; i<6; i++){
		setColor(i);
		glUseProgram(shaderProg);
		int plotAxes = i;
		GLint axes = glGetUniformLocation(shaderProg, "axes"); 
	  	glUniform1i(axes, plotAxes);
	
		drawVBO();
		glUseProgram(0);
	}
	
}
Exemple #4
0
void display() {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

  /* camera transform */
	glTranslatef(0, 0, -camera.zoom);
	glRotatef(camera.rot.x, 1, 0, 0);
	glRotatef(camera.rot.y, 0, 1, 0);

  drawAxes(0.5);

  /* set the light position  */
  if(options[OPT_LIGHTING]) {
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
    glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
    glMaterialf(GL_FRONT, GL_SHININESS, shininess);
    glEnable(GL_LIGHTING);
  } else {
    glDisable(GL_LIGHTING);
  }

  if(options[OPT_SMOOTH_SHADE])
    glShadeModel(GL_SMOOTH);
  else
    glShadeModel(GL_FLAT);

  if(options[OPT_DRAW_NORMALS])
    drawNormals();
  
  if(options[OPT_SHADER]) {
    glUseProgram(currentShader);
    if (currentShader != 0 && (loc = glGetUniformLocation(currentShader, "GeomGen")) != -1)
      glUniform1i(loc, options[OPT_SHADER_GEN]);
  }

  if(options[OPT_WIREFRAME])
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  else
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

  if(options[OPT_BUMP_MAP]) {
    glUseProgram(bumpShader);
    if (bumpShader != 0 && (loc = glGetUniformLocation(bumpShader, "LightPosition")) != -1)
      glUniform3fv(loc, 1, lightPos);
    if (bumpShader != 0 && (loc = glGetUniformLocation(bumpShader, "SurfaceColor")) != -1)
      glUniform3f(loc, 1.0, 0.0, 0.0);
    if (bumpShader != 0 && (loc = glGetUniformLocation(bumpShader, "BumpDensity")) != -1)
      glUniform1f(loc, 4);
    if (bumpShader != 0 && (loc = glGetUniformLocation(bumpShader, "BumpSize")) != -1)
      glUniform1f(loc, 0.15);
    if (bumpShader != 0 && (loc = glGetUniformLocation(bumpShader, "SpecularFactor")) != -1)
      glUniform1f(loc, 0.5);
    if (bumpShader != 0 && (loc = glGetUniformLocation(bumpShader, "R")) != -1)
      glUniform1f(loc, R);
    if (bumpShader != 0 && (loc = glGetUniformLocation(bumpShader, "r")) != -1)
      glUniform1f(loc, r);
  
    if (bumpShader != 0 && (loc = glGetUniformLocation(bumpShader, "Time")) != -1) {
      double tt = 1.0;
      if(options[OPT_ANIM]) {
        double ipart, fpart;
        fpart = modf(thisTime * 0.001f, &ipart);
        if((int)ipart % 2 == 0)
          // using slightly less fpart smoothes animation
          tt = 1.1 - fpart; 
        else
          tt = fpart;
      }
      glUniform1f(loc, tt);
    }

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, geom_uv);
    glDrawElements(GL_QUAD_STRIP, n_geom_indices, GL_UNSIGNED_INT, geom_indices);
    glDisableClientState(GL_VERTEX_ARRAY);
  } else if(options[OPT_SHADER_GEN]) {
    /* pass in radii as uniforms */
    if (currentShader != 0 && (loc = glGetUniformLocation(currentShader, "R")) != -1)
      glUniform1f(loc, R);
    if (currentShader != 0 && (loc = glGetUniformLocation(currentShader, "r")) != -1)
      glUniform1f(loc, r);
    if (currentShader != 0 && (loc = glGetUniformLocation(currentShader, "GenMode")) != -1)
      glUniform1i(loc, gMode);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, geom_uv);
    glDrawElements(GL_QUAD_STRIP, n_geom_indices, GL_UNSIGNED_INT, geom_indices);
    glDisableClientState(GL_VERTEX_ARRAY);
  } else {
    if(options[OPT_IMMEDIATE]) {
      drawImmediate();
    } else {
      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_NORMAL_ARRAY);
      glNormalPointer(GL_FLOAT, 0, geom_norms);

      if(options[OPT_VBO])
        drawVBO();
      else
        drawVertexArray();

      glDisableClientState(GL_NORMAL_ARRAY);
      glDisableClientState(GL_VERTEX_ARRAY);
    }
  }

  /* Render the hud */
	glDisable(GL_DEPTH_TEST);
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(0, 1, 0, 1, -1, 1);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	glUseProgram(0);
	glDisable(GL_LIGHTING);
	glTranslatef(0.0, 0.0, -0.5);
	drawHud();
	glEnable(GL_LIGHTING);
	
	glMatrixMode(GL_PROJECTION);
	glPopMatrix(); 
	glMatrixMode(GL_MODELVIEW);
	glEnable(GL_DEPTH_TEST);

	glutSwapBuffers();
  int err;
	if ((err = glGetError()) != GL_NO_ERROR)
		fprintf(stderr, "%s\n", gluErrorString(err));
}
Exemple #5
0
void Draw_Manager::Sprite3D(TexID texID, Vector3f pos, float scale, int align, float rotation) {
	texture_entry* texEntry = Graphics.GetTextureEntryByID(texID);
	GraphicsID newGraphicsID = Graphics.GetGraphicsEntryByID(texID);
	if (texID == BAD_ID) return;// texEntry;

	if (newGraphicsID != currentGraphicsID) {
		drawVBO();

		//Switch current OpenGL texture
		OGLTexture textureOGLID = Graphics.GetOGLTextureByID(texID);
		glBindTexture(GL_TEXTURE_2D, textureOGLID);

		currentGraphicsID = newGraphicsID;
		//logWrite("GID %d",currentGraphicsID);

		curTextureBinds++;
	}

	if (VBO.vertexCount+6 > VBO.vertexAllocCount) {
		drawVBO();
	}

	float w = texEntry->Width*scale;
	float h = texEntry->Height*scale;

	if (rotation == 0.0f) {
		if (align == 0) {
			VBO.Add(Vector3f(pos.x+w,pos.y+h,pos.z),	Vector2f(texEntry->U2,texEntry->V2));
			VBO.Add(Vector3f(pos.x+w,pos.y,  pos.z),	Vector2f(texEntry->U2,texEntry->V1));
			VBO.Add(Vector3f(pos.x,  pos.y,  pos.z),	Vector2f(texEntry->U1,texEntry->V1));
			
			VBO.Add(Vector3f(pos.x+w,pos.y+h,pos.z),	Vector2f(texEntry->U2,texEntry->V2));
			VBO.Add(Vector3f(pos.x,  pos.y,  pos.z),	Vector2f(texEntry->U1,texEntry->V1));
			VBO.Add(Vector3f(pos.x,  pos.y+h,pos.z),	Vector2f(texEntry->U1,texEntry->V2));
		} else {
			VBO.Add(Vector3f(pos.x+w/2,pos.y+h/2,pos.z),Vector2f(texEntry->U2,texEntry->V2));
			VBO.Add(Vector3f(pos.x+w/2,pos.y-h/2,pos.z),Vector2f(texEntry->U2,texEntry->V1));
			VBO.Add(Vector3f(pos.x-w/2,pos.y-h/2,pos.z),Vector2f(texEntry->U1,texEntry->V1));
			
			VBO.Add(Vector3f(pos.x+w/2,pos.y+h/2,pos.z),Vector2f(texEntry->U2,texEntry->V2));
			VBO.Add(Vector3f(pos.x-w/2,pos.y-h/2,pos.z),Vector2f(texEntry->U1,texEntry->V1));
			VBO.Add(Vector3f(pos.x-w/2,pos.y+h/2,pos.z),Vector2f(texEntry->U1,texEntry->V2));
		}
	} else {
		Vector3f center = Vector3f(pos.x,pos.y,pos.z);
		Vector3f v1 = Vector3f(pos.x-w/2,pos.y-h/2,pos.z);
		Vector3f v2 = Vector3f(pos.x+w/2,pos.y-h/2,pos.z);
		Vector3f v3 = Vector3f(pos.x+w/2,pos.y+h/2,pos.z);
		Vector3f v4 = Vector3f(pos.x-w/2,pos.y+h/2,pos.z);

		v1.Rotate_Z(rotation*RAD_TO_DEG,center);
		v2.Rotate_Z(rotation*RAD_TO_DEG,center);
		v3.Rotate_Z(rotation*RAD_TO_DEG,center);
		v4.Rotate_Z(rotation*RAD_TO_DEG,center);

		VBO.Add(v3,	Vector2f(texEntry->U2,texEntry->V2));
		VBO.Add(v2,	Vector2f(texEntry->U2,texEntry->V1));
		VBO.Add(v1,	Vector2f(texEntry->U1,texEntry->V1));
		
		VBO.Add(v3,	Vector2f(texEntry->U2,texEntry->V2));
		VBO.Add(v1,	Vector2f(texEntry->U1,texEntry->V1));
		VBO.Add(v4,	Vector2f(texEntry->U1,texEntry->V2));
	}

	curPolies += 6;
	curSprites++;

	//return texEntry;
}
Exemple #6
0
//Draw sprites which are yet to be drawn
void Draw_Manager::FlushSprites() {
	drawVBO();
	//currentGraphicsID = BAD_ID;
}