Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Last active April 14, 2016 02:47
Show Gist options
  • Save alphaKAI/9887282 to your computer and use it in GitHub Desktop.
Save alphaKAI/9887282 to your computer and use it in GitHub Desktop.
D言語でGLFW使って逆方向に回る2つの三角形を描画させてみた : $ dmd two_triangles.d lib/libDerelictGL3.a lib/libDerelictUtil.a lib/libDerelictGLFW3.a -L/usr/lib/libdl.so
import derelict.opengl3.gl,
derelict.opengl3.gl3,
derelict.glfw3.glfw3;
class TestGl{
static int width = 800;
static int height = 600;
static GLFWwindow* window;
static const float Pi = 3.1415926535897932384626433832795;
this(){
DerelictGL.load();
DerelictGL3.load();
DerelictGLFW3.load();
if (!glfwInit())
throw new Error("GLFW Initialize failed.");
window = glfwCreateWindow(width, height, "GLFW3 and OpenGL Test", null, null);
if (!window){
glfwTerminate();
throw new Error("Falied to Create GLFW Window.");
}
glfwMakeContextCurrent(window);
}
void drawInit(){
float ratio;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / cast(float)height;
glViewport(0,0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1.0f, 1.0f, 1.0f, -1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void turnLeft(){
glRotatef(cast(float)glfwGetTime() * 100.0f, 0.0f, 0.0f, 1.0f);
}
// -X = 2X
void turnRight(){
glRotatef(cast(float)glfwGetTime() * -400.0f, 0.0f, 0.0f, 1.0f);
}
void drawTriangleRight(){
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.6f, -0.4f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.6f, -0.4f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.6f, 0.0f);
glEnd();
}
void drawTriangleLeft(){
import std.math;
glBegin(GL_POLYGON);
const int n = 3;
//n is number of sides
for(int i=0 ; i<n ; i++){
auto mycos = cos(i*2*Pi/n);
auto mysin = sin(i*2*Pi/n);
glColor3f(1.0 , 0.5 , 0.0);
glVertex2f(mycos, mysin);
}
glEnd();
}
void mainLoop(){
while(!checkEndKey(window)){
drawInit();
turnLeft();
drawTriangleLeft();
turnRight();
drawTriangleRight();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return;
}
bool checkEndKey(GLFWwindow* window){
int keyArray[] = [GLFW_KEY_ESCAPE];
foreach(k; keyArray){
if(glfwGetKey(window, k) == GLFW_PRESS)
return true;
}
return false;
}
}
void main(){
TestGl tl = new TestGl();
with(tl){
mainLoop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment