opengl을 이용하여 그래프 그리는 프로그램을 만들고 있는데요....
글쓴이: cambel / 작성시간: 목, 2014/10/16 - 11:54오후
아래 첨부 파일과 같이 수식을 입력하면
화면에는 그래프 창과 콘솔창이 뜹니다.
그리고 그래프 창에서 '+' 또는 '-'를 누르면 샘플링의 수가 변해서
그래프의 모양이 변해야 하는데 그대로 입니다.
어떻게 해야 모양이 바뀔까요?
void KeyboardFunc(unsigned char key, int x, int y)
아마도 위의 함수를 수정해야 할거 같은데....
잘 모르겠습니다. ㅠㅠ
답변 좀 부탁드릴게요...
감사합니다.
#include <stdlib.h>
#include <stdio.h>
#include <gl/glut.h>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;
int width = 600, height = 600;
int num_sample = 100;
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_ftc();
int degree = 0;
vector <double> Coefficient_List;
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(width, height); //Create an application window of 600 x 600
glutInitWindowPosition(0, 0);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("hw5");
glutDisplayFunc(Render);
Input_ftc();
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, x = 10, y = -10, y = 10, z = -10, z = 10
}
void Render()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f); //Initialize its background to white color
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 (Coefficient_List.size() > 0)
{
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < num_sample; ++i)
{
double x = -10.0 + (20.0 * (double)i / (double)num_sample);
double y = 0.0;
for (int j = 0; j <= degree; ++j)
{
y = y + (Coefficient_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)
{
switch (key)
{
case 'n':
case 'N':
Coefficient_List.clear();
Input_ftc();
break;
case '+':
num_sample++;
break;
case '-':
if (Coefficient_List.size() < 0)
{
num_sample = 0;
}
else
{
num_sample--;
break;
}
}
glutPostRedisplay();
}
void Input_ftc()
{
double co_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", &co_tmp);
Coefficient_List.push_back(co_tmp);
}
printf("f(x) = ");
for (int i = 0; i <= degree; i++)
{
if (i == 0)
{
if (i == (degree))
printf("%lf \n", Coefficient_List[i]);
else
printf("%lf + ", Coefficient_List[i]);
}
else
{
if (i == (degree))
printf("%lfx^%d \n", Coefficient_List[i], i);
else
printf("%lfx^%d + ", Coefficient_List[i], i);
}
}
}File attachments:
| 첨부 | 파일 크기 |
|---|---|
| 160.17 KB |
Forums:


댓글 달기