gpt4 book ai didi

c++ - 如何使用 C++ 在 OpenGL_POINTS 函数中绘制正弦波

转载 作者:太空狗 更新时间:2023-10-29 23:40:30 25 4
gpt4 key购买 nike

Image of a sine wave drawn over 2PI with red dots我应该使用 OpenGL_POINTS 绘制一个正弦波(如图中的那个)。然而,在代码中完成我的循环后,我一直只得到一个波浪点。

这是我的代码。

#include "stdafx.h"
#include <iostream>
#include <gl\GLUT.h>
#include <math.h>

using namespace std;

void RenderSineWave()
{
int i;
float x,y;
glClearColor(0.0, 0.0, 0.0, 1.0); // clear background with black
glClear(GL_COLOR_BUFFER_BIT);

glPointSize(10);
glColor3f(1.0,0.0,0.0);


for(i=0;i<361;i=i+5)
{

x = (float)i;
y = 100.0 * sin(i *(6.284/360.0));
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
glFlush();
glutPostRedisplay();
}

}

void main(int argc, char** argv)
{
glutInit(&argc,argv);
glutCreateWindow("SineWave.cpp");
glutDisplayFunc(RenderSineWave);
glutMainLoop();

}

最佳答案

尝试设置一个合理的投影矩阵:

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

using namespace std;

void RenderSineWave()
{
glClearColor(0.0, 0.0, 0.0, 1.0); // clear background with black
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
double ar = w / h;
glOrtho( -360 * ar, 360 * ar, -120, 120, -1, 1 );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glPointSize(10);
glColor3f(1.0,0.0,0.0);

glBegin(GL_POINTS);
for(int i=0;i<361;i=i+5)
{
float x = (float)i;
float y = 100.0 * sin(i *(6.284/360.0));
glVertex2f(x,y);
}
glEnd();

glutSwapBuffers();
}

int main(int argc, char** argv)
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "SineWave.cpp" );
glutDisplayFunc( RenderSineWave );
glutMainLoop();
return 0;
}

关于c++ - 如何使用 C++ 在 OpenGL_POINTS 函数中绘制正弦波,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19386318/

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