gpt4 book ai didi

java - 如何在 OpenGL 中创建正方形的螺旋? (Java)

转载 作者:行者123 更新时间:2023-11-29 06:02:13 26 4
gpt4 key购买 nike

我需要创建一个正方形螺旋(相同大小),在完成 360 度循环后,它会改变颜色。例如,我想制作一个三色螺旋。第一部分可能是蓝色的,在第一个循环之后方 block 变成绿色,最后变成红色。这是我正在寻找的内容的可视化:

square spiral

到目前为止我的代码(JPanel 东西除外)如下:

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) {}

}

关于我如何实现它,您有什么建议吗?

最佳答案

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 中,但将核心逻辑移植到 Java 应该很容易。

关于java - 如何在 OpenGL 中创建正方形的螺旋? (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9703888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com