예제 #1
0
int CALLBACK WinMain(
	__in  HINSTANCE hInstance,
	__in  HINSTANCE hPrevInstance,
	__in  LPSTR lpCmdLine,
	__in  int nCmdShow
	)
#endif
{
	if (!glfwInit())
		return 0;

	GLFWwindow* window = glfwCreateWindow(640, 480, "Window Title", NULL, NULL);
	glfwMakeContextCurrent(window);
	glewExperimental = GL_TRUE;
	if (glewInit() != GLEW_OK)
		return -1;


	GLuint vertexBuffer;
	glGenBuffers(1, &vertexBuffer);

	Shader* vertexShader = new Shader("Content\\Shaders\\vertexShader.vs", GL_VERTEX_SHADER);
	Shader* fragmentShader = new Shader("Content\\Shaders\\fragmentShader.fs", GL_FRAGMENT_SHADER);
	ShaderProgram* program = new ShaderProgram();
	program->AttachShader(vertexShader);
	program->AttachShader(fragmentShader);

	program->BindFragDataLocation(0, "outColour");
	program->Link();
	program->Use();

	GLint posAttrib = program->GetAttribLocation("position");
	
	GLuint vao;
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

	GLuint vbo;
	glGenBuffers(1, &vbo);

	float vertices[] = {
		0.0f, 0.5f,
		0.5f, -0.5f,
		-0.5f, -0.5f
	};

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

	glEnableVertexAttribArray(posAttrib);
	glVertexAttribPointer
		(posAttrib, // Ref input
		2, // Number of values for input  
		GL_FLOAT,  // Type of each component
		GL_FALSE, // Whether to normalise
		0,  // Stride
		0); // Offset

	

	

	

	while (!glfwWindowShouldClose(window))
	{
		glClearColor(0.f, 0.f, 0.f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		glDrawArrays(GL_TRIANGLES, 0, 3);

		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	glfwDestroyWindow(window);
	return 0;
}
예제 #2
0
void SORA_test_draw2(int w, int h) {
  static bool init = false;
  static ShaderProgram shader;
  static TextureManager tex_mgr;
  
  if(init == false) {
    init = true;
    //create shader
    //2d shader
    std::string app_vert_path = sora::Filesystem::GetAppPath("shader/simple.vs");
    std::string app_frag_path = sora::Filesystem::GetAppPath("shader/simple.fs");
    sora::MemoryFile vert_file(app_vert_path);
    sora::MemoryFile frag_file(app_frag_path);
    vert_file.Open();
    frag_file.Open();
    const char *vert_src = (const char*)(vert_file.start);
    const char *frag_src = (const char*)(frag_file.start);
    bool prog_result = shader.Init(vert_src, frag_src);
    if(prog_result == false) {
      LOGE("Could not create program.");
    }

    {
      std::string tex_path = sora::Filesystem::GetAppPath("texture/sora.png");
      sora::MemoryFile tex_file(tex_path);
      tex_file.Open();
      Texture tex("sora");
      tex.SetData(sora::Texture::kFilePNG, tex_file.start, tex_file.end);
      tex_mgr.Add(tex);
    }

  }
  static float a = 0;
  a += 0.1f;

  glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
  glClear(GL_COLOR_BUFFER_BIT);
  glViewport(0, 0, w, h);

  SR_CHECK_ERROR("Render 2d start");
  //draw 2d something
  glm::mat4 world_mat(1.0f);

  int pos_loc = shader.GetAttribLocation("a_position");
  int tex_loc = shader.GetAttribLocation("a_texcoord");
  glEnableVertexAttribArray(pos_loc);
  glEnableVertexAttribArray(tex_loc);
  
  int mvp_loc = shader.GetUniformLocation("u_worldViewProjection");

  float vertex[] = {
    -0.5, -0.5, 0,
    0.5, -0.5, 0,
    0, 0.5, 0,
  };
  float texcoord[] = {
    0, 0,
    1, 0,
    0.5, 1,
  };


  glUseProgram(shader.prog);

  Texture *tex = tex_mgr.Get_ptr(string("sora"));
  glBindTexture(GL_TEXTURE_2D, tex->handle());

  glUniformMatrix4fv(mvp_loc, 1, GL_FALSE, glm::value_ptr(world_mat));
  glVertexAttribPointer(pos_loc, 3, GL_FLOAT, GL_FALSE, 0, vertex);
  glVertexAttribPointer(tex_loc, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
  glDrawArrays(GL_TRIANGLE_FAN, 0, 3);
  SR_CHECK_ERROR("glDrawArrays");
}