c++ opengl을 이용해서 그래프 그리는 프로그램 만드는데...
창에서 '+' 또는 '-'를 누르면 샘플링의 수가 변해서 그래프의 모양이 변해야 하는데 그대로 입니다.
이걸 어떻게 해야 하나요~?
답변 부탑드립니다.
그리고 그래프를 눌러서 미분,적분 하는 기능도 넣도 싶은데 어떻게 해야 하는지를 모르겠어요~
opengl 초보자라서...
도와주세요~
답변 부탑드립니다.
#include
#include
#include
#include
#include
#include
using namespace std;
int width = 600, height = 600;
int num = 25;
void Render();
void Reshape(int w, int h);
void glutPostRedisply();
void SetupViewTransform();
void SetupVeiwVolume();
void glutKeyboardFunc();
void KeyboardFunc(unsigned char key, int x, int y);
void Input_get();
int degree = 0;
vector Coeff_List;
int main(int argc, char **argv)
{
Input_get();
glutInit(&argc, argv);
glutInitWindowSize(width, height); //600x600
glutInitWindowPosition(0, 0);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("graph program");
glutDisplayFunc(Render);
glutKeyboardFunc(KeyboardFunc);
glutMainLoop();
return 0;
}
void SetupVeiwTransform()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -10.0);
}
void SetupViewVolume()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0); //x = -10,10, y = -10,10, z = -10,10
}
void Render()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f); //배경화명 흰색
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SetupVeiwTransform();
SetupViewVolume();
glMatrixMode(GL_MODELVIEW);
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(-10.0, 0.0, 0.0);
glVertex3f(10.0, 0.0, 0.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(0.0, 10.0, 0.0);
glVertex3f(0.0, -10.0, 0.0);
glEnd();
for (int i = 0; i < 20; i++)
{
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(-0.1, -10.0 + i, 0.0);
glVertex3f(0.1, -10.0 + i, 0.0);
glEnd();
}
for (int i = 0; i < 20; i++)
{
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex3f(-10.0 + i, 0.1, 0.0);
glVertex3f(-10.0 + i, -0.1, 0.0);
glEnd();
}
if (Coeff_List.size() > 0)
{
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < num; ++i)
{
double x = -10.0 + (20.0 * (double)i / (double)num);
double y = 0.0;
for (int j = 0; j <= degree; ++j)
{
y = y + (Coeff_List[j] * pow(x, j));
}
glVertex3f(x, y, 0.0);
}
glEnd();
}
glutSwapBuffers();
}
void Reshape(int w, int h)
{
glViewport(0, 0, w, h);
width = w;
height = h;
}
void KeyboardFunc(unsigned char key, int x, int y)
{
if (key == '+')
{
num = num + 1;
Coeff_List.clear();
Input_get();
}
else if (key == '-')
{
num = num - 1;
Coeff_List.clear();
Input_get();
}
else if (key == 'n' || key == 'N')
{
Coeff_List.clear();
num = 25;
Input_get();
}
}
void Input_get()
{
double tmp = 0.0;
printf("Enter the degree (n) of f(x) : ");
scanf("%d", °ree);
printf("Enter the coefficients of f(x) : ");
for (int i = 0; i <= degree; i++)
{
scanf("%lf", &tmp);
Coeff_List.push_back(tmp);
}
printf("f(x) = ");
for (int i = 0; i <= degree; i++)
{
if (i == 0)
{
if (i == (degree))
printf("%lf \n", Coeff_List[i]);
else
printf("%lf + ", Coeff_List[i]);
}
else
{
if (i == (degree))
printf("%lfx^%d \n", Coeff_List[i], i);
else
printf("%lfx^%d + ", Coeff_List[i], i);
}
}
}
첨부 | 파일 크기 |
---|---|
![]() | 160.17 KB |
소스코드 올릴 때
소스코드 올릴 때 https://kldp.org/filter/tips 참고하셔요.
세벌 https://sebuls.blogspot.kr/
댓글 달기