opengl을 이용하여 그래프 그리는 프로그램을 만들고 있는데요....

cambel의 이미지

아래 첨부 파일과 같이 수식을 입력하면
화면에는 그래프 창과 콘솔창이 뜹니다.

그리고 그래프 창에서 '+' 또는 '-'를 누르면 샘플링의 수가 변해서
그래프의 모양이 변해야 하는데 그대로 입니다.

어떻게 해야 모양이 바뀔까요?

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", &degree);
	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: 
첨부파일 크기
Image icon 12.png160.17 KB

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.