2012-03-14 2 views
1

360도주기 완료시 색상이 바뀌는 사각형 (같은 크기)의 나선형을 만들어야합니다. 예를 들어 3 색 나선형을 만들고 싶습니다. 첫 번째 부분은 파란색 일 수 있습니다. 첫 번째주기가 지나면 사각형이 녹색으로 바뀌고 마지막으로 빨간색이됩니다. 다음의 시각화의 나는 무엇을 찾고 있어요 :OpenGL에서 사각형의 나선형을 만드는 방법은 무엇입니까? (JAVA)

import javax.media.opengl.GL; 
import javax.media.opengl.GLAutoDrawable; 
import javax.media.opengl.GLEventListener; 
import javax.media.opengl.glu.GLU; 


public class SecondGLEventListener implements GLEventListener { 

    /** 
    * Interface to the GLU library. 
    */ 
    private GLU glu; 

    /** 
    * Take care of initialization here. 
    */ 
    public void init(GLAutoDrawable drawable) { 
     GL gl = drawable.getGL(); 
     glu = new GLU(); 

     gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 

     gl.glViewport(0, 0, 500, 300); 
     gl.glMatrixMode(GL.GL_PROJECTION); 
     gl.glLoadIdentity(); 
     glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0); 
    } 
    /** 
    * Take care of drawing here. 
    */ 
    public void display(GLAutoDrawable drawable) 
    { 

     GL gl = drawable.getGL(); 

     gl.glClear(GL.GL_COLOR_BUFFER_BIT); 

     for (int i=0; i<50; i++) 
     { 
      gl.glColor3f(0.0f,0.0f,1.0f); 
      gl.glPointSize(3); 
      gl.glBegin(GL.GL_POINTS); 
      //some code for the spiral thing. 
      gl.glEnd(); 
     } 

    } 
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, 
    int height) {} 

    public void displayChanged(GLAutoDrawable drawable, 
    boolean modeChanged, boolean deviceChanged) {} 

} 

하는 당신이 방법으로 어떤 제안이 있습니까 :

square spiral

지금까지합니다 (JPanel의 꼬추의 제외) 내 코드는 다음과 같다 이걸 구현할 수 있을까요?

+0

오! 이건 숙제 야! –

+0

예 여기에도 같은 숙제가 있습니다 : http://stackoverflow.com/questions/26784258/creating-a-multicoloured-spiral-in-opengl-java – gouessej

답변

4

함께 twotwo을 넣어 :

#include <GL/glut.h> 
#include <cmath> 

void glSpiral(float a, float b, float thetaStart, float thetaEnd, unsigned int samples = 200) 
{ 
    glBegin(GL_QUADS); 

    const float sz = 1.0f; 

    float dt = (thetaEnd - thetaStart)/(float)samples; 

    for(unsigned int i = 0; i <= samples; ++i) 
    { 
     // archimedean spiral 
     float theta = thetaStart + (i * dt); 
     float r = a + b * theta; 

     // polar to cartesian 
     float x = r * cos(theta); 
     float y = r * sin(theta); 

     glVertex2f(x - sz, y - sz); 
     glVertex2f(x + sz, y - sz); 
     glVertex2f(x + sz, y + sz); 
     glVertex2f(x - sz, y + sz); 
    } 

    glEnd(); 
} 

size_t win_w = 0; 
size_t win_h = 0; 
double aspect_ratio = 0; 
void display(void) 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrtho(-100*aspect_ratio, 100*aspect_ratio, -100, 100, -1, 1); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    const float PI = 3.14159; 
    for(unsigned int i = 0; i < 6; i++) 
    { 
     switch(i) 
     { 
     case 0: glColor3ub(255,0,0);  break; 
     case 1: glColor3ub(0,255,0);  break; 
     case 2: glColor3ub(0,0,255);  break; 
     case 3: glColor3ub(255,255,0);  break; 
     case 4: glColor3ub(0,255,255);  break; 
     case 5: glColor3ub(255,255,255); break; 
     default: glColor3ub(128,128,128); break; 
     } 

     float beg = (i+0)*2*PI; 
     float end = (i+1)*2*PI; 
     glSpiral(0, 3, beg, end, 80); 
    } 

    glFlush(); 
    glutSwapBuffers(); 
} 

void reshape(int w, int h) 
{ 
    win_w = w; 
    win_h = h; 
    aspect_ratio = (double)win_w/(double)win_h; 
    glViewport(0, 0, w, h); 
} 

int main(int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); 

    glutInitWindowSize(800,600); 
    glutCreateWindow("Spiral"); 

    glutDisplayFunc(display); 
    glutReshapeFunc(reshape); 
    glutMainLoop(); 
    return 0; 
} 

screenshot 그것은 C에있어 ++ 및 GLUT하지만 자바 코어 로직을 포팅하는 것은 쉬워야한다. 사각 나선형에 대한

+0

전체 나선형을 어떻게 움직일 수 있습니까? 패널 모서리 중 하나로 번역하고 싶습니다. –

+0

'glPushMatrix(); glTranslatef (xpos, ypos, 0); drawSpiral(); glPopMatrix();' – genpfault

-1

이 필요한 경우 :

import java.awt.*; 
public class Spiral { 
    public static void main(String[] args) { 
     DrawingPanel panel = new DrawingPanel(170, 170); 
     Graphics g = panel.getGraphics(); 
     for (int i = 0; i < 8; i++) { 
     g.drawLine(  10*i, 10 + 10*i, 160 - 10*i, 10 + 10*i); // top 
     g.drawLine(160 - 10*i, 10 + 10*i, 160 - 10*i, 160 - 10*i); // right 
     g.drawLine(10 + 10*i, 160 - 10*i, 160 - 10*i, 160 - 10*i); // bottom 
     g.drawLine(10 + 10*i, 20 + 10*i, 10 + 10*i, 160 - 10*i); // left 
     } 
    } 
} 
관련 문제