opengl 空心圆
时间: 2023-12-09 08:06:50 浏览: 44
以下是使用OpenGL绘制空心圆的代码示例:
```c++
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 360; i++) {
float theta = i * 3.1415926 / 180;
glVertex2f(0.5*cos(theta), 0.5*sin(theta));
}
glEnd();
glFlush();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400); glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
```
相关问题
Java中OpenGL绘制平面空心圆
在Java中使用OpenGL绘制平面空心圆可以通过以下步骤实现:
1. 创建OpenGL窗口和上下文,初始化OpenGL环境。
2. 在绘制之前,设置绘制属性,例如绘制颜色、线宽等。
3. 在绘制之前,设置投影矩阵和模型视图矩阵,以确定绘制的位置和大小。
4. 使用OpenGL提供的绘制函数绘制平面空心圆,例如使用glBegin(GL_LINE_LOOP)函数绘制圆形的边缘线。
以下是一份基本的Java代码,可以用来绘制平面空心圆:
```java
import java.awt.*;
import java.awt.event.*;
import java.nio.FloatBuffer;
import javax.swing.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
public class Circle implements GLEventListener {
private GLU glu = new GLU();
public static void main(String[] args) {
JFrame frame = new JFrame("Circle");
GLCanvas canvas = new GLCanvas();
canvas.addGLEventListener(new Circle());
frame.add(canvas);
frame.setSize(300, 300);
final Animator animator = new Animator(canvas);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
new Thread(new Runnable() {
public void run() {
animator.stop();
System.exit(0);
}
}).start();
}
});
frame.setVisible(true);
animator.start();
}
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL.GL_FLAT);
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(0.0, width, 0.0, height);
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glLineWidth(2.0f);
double r = 100.0;
double cx = 150.0;
double cy = 150.0;
double theta = 0.0;
double dtheta = Math.PI / 20.0;
gl.glBegin(GL.GL_LINE_LOOP);
for (int i = 0; i < 40; i++) {
double x = r * Math.cos(theta) + cx;
double y = r * Math.sin(theta) + cy;
gl.glVertex2d(x, y);
theta += dtheta;
}
gl.glEnd();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
}
```
在这个例子中,我们使用了OpenGL的基本函数来绘制平面空心圆。我们首先在init()方法中设置绘制属性,然后在reshape()方法中设置投影矩阵和模型视图矩阵,最后在display()方法中使用glBegin()和glEnd()函数绘制圆形的边缘线。
OpenGL画圆球代码
是使用OpenGL画圆球的代码:
```python
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import math
def drawSphere(radius, slices, stacks):
for i in range(stacks):
lat0 = math.pi * (-0.5 + float(float(i - 1) / float(stacks)))
z0 = radius * math.sin(lat0)
zr0 = radius * math.cos(lat0)
lat1 = math.pi * (-0.5 + float(float(i) / float(stacks)))
z1 = radius * math.sin(lat1)
zr1 = radius * math.cos(lat1)
glBegin(GL_QUAD_STRIP)
for j in range(slices):
lng = 2 * math.pi * float(float(j - 1) / float(slices))
x = math.cos(lng)
y = math.sin(lng)
glNormal3f(x * zr0, y * zr0, z0)
glVertex3f(x * zr0, y * zr0, z0)
glNormal3f(x * zr1, y * zr1, z1)
glVertex3f(x * zr1, y * zr1, z1)
glEnd()
def draw():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0)
glColor3f(1, 1, 1)
drawSphere(1, 50, 50)
glutSwapBuffers()
if __name__ == '__main__':
glutInit()
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutInitWindowSize(800, 600)
glutCreateWindow("OpenGL Sphere")
glEnable(GL_DEPTH_TEST)
glutDisplayFunc(draw)
glutMainLoop()
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)