用opengl绘制图形
如果我把形状描述成半个封闭的圆,另一半与第一个相连,我怎么能画出像(sin)这样的形状呢。使用笛卡尔方法,这是我的尝试:用opengl绘制图形,opengl,Opengl,如果我把形状描述成半个封闭的圆,另一半与第一个相连,我怎么能画出像(sin)这样的形状呢。使用笛卡尔方法,这是我的尝试: #include <windows.h> #include <gl/Gl.h> #include <gl/glut.h> #include<math.h> #include<cstdlib> static void myDisplay() { glClear(GL_COLOR_BUF
#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include<math.h>
#include<cstdlib>
static void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glColor3f(1.0,0.0,0.0);
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
double xc=200, yc=200,r=100;
double x,y;
for (x = xc - r; x<= xc + r;x++)
{
y = sqrt((r*r)-((xc - x)*(xc - x)));
glVertex2d(x, yc + y);
}
for (x = xc +r ; x<= xc - r ; x++)
{
y = sqrt((r*r)-((xc - x)*(xc - x)));
glVertex2d(x , yc - y);
}
glEnd();
glFlush();
}
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0); // set white background color
glColor3f(0.0f, 0.0f, 0.0f); // set the drawing color
glPointSize(4.0); // a ‘dot’ is 4 by 4 pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void main(int argc, char** argv)
{
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
glutInitWindowSize(640,480); // set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow("Line Scan Conversion"); // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
myInit();
glutMainLoop(); // go into a perpetual loop
}
#包括
#包括
#包括
#包括
#包括
静态void myDisplay()
{
glClear(GL_颜色_缓冲区_位);//清除屏幕
GL3F(1.0,0.0,0.0);
GL3F(0.0,0.0,0.0);
glBegin(GL_线_环);
双xc=200,yc=200,r=100;
双x,y;
对于(x=xc-r;x只需直接对函数(sin()
)进行采样:
#包括
#包括
静态void myDisplay()
{
glClearColor(0,0,0,1);//设置白色背景色
glClear(GL_颜色_缓冲区_位);//清除屏幕
glMatrixMode(GL_投影);
glLoadIdentity();
格洛托(-0.5,7,-1.2,1.2,-1,1);
glMatrixMode(GLU模型视图);
glLoadIdentity();
//斧头
glColor3ub(255、255、255);
glBegin(GL_行);
glVertex2i(0,0);
glVertex2i(7,0);
glVertex2i(0,-1);
glVertex2i(0,1);
格伦德();
//sin()函数图
glColor3ub(255,0,0);
glBegin(GL_线_带);
常量无符号整数样本=100;
for(unsigned int i=0;i第二个for
循环中存在问题:
for(x=xc+r;x)您的代码中有什么问题?前半个圆的图形,但后半个圆的图形(旁边)第一个我不知道该怎么做,只需画出单独的半圆对象,然后将第二个半圆翻转过来。简单的正反圆函数就可以了。如果我理解正确…请理解我,我不能加载图像,因为我的名声很小,我想要的是像罪而不是罪,但类似的是罪把前半圆放起来必须闭合,下半圆也必须闭合,使用笛卡尔方法@genpfault第一个半圆绘图,但下半圆(旁边)第一个我不知道怎么做@ssell请为(x=0;x<2r;x++),而不是第一个和第二个循环,或者我会让你的绘图代码类似:for(x=0;x<2r;x++){y=sqrt((r*r)-(xc-x)*(xc-x));glVertex2d((xc-r)+x,yc+y);}
用于上半部分,然后用于(x=0;x<2r;x++){y=sqrt((r*r)-(xc-x)*(xc-x));glVertex2d((xc+r)+x,yc y);}
用于下半部分。
#include <GL/glut.h>
#include <cmath>
static void myDisplay()
{
glClearColor( 0, 0, 0, 1 ); // set white background color
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( -0.5, 7, -1.2, 1.2, -1, 1 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// axes
glColor3ub( 255, 255, 255 );
glBegin( GL_LINES );
glVertex2i( 0, 0 );
glVertex2i( 7, 0 );
glVertex2i( 0, -1 );
glVertex2i( 0, 1 );
glEnd();
// sin() function plot
glColor3ub( 255, 0, 0 );
glBegin(GL_LINE_STRIP);
const unsigned int samples = 100;
for( unsigned int i = 0; i <= samples; ++i )
{
const float pct = ( (float)i / samples );
const float x = 2 * 3.14159 * pct;
const float y = sin( x );
glVertex2f( x, y );
}
glEnd();
glutSwapBuffers();
}
void main(int argc, char** argv)
{
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // set display mode
glutInitWindowSize(600, 600); // set window size
glutCreateWindow("Line Scan Conversion"); // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
glutMainLoop(); // go into a perpetual loop
}