Facebook Sharer
选择您要替换的背景颜色:
【农历新年】背景图片:
个性化设定
 注册  找回密码
查看: 1449|回复: 4
打印 上一主题 下一主题

【问题解决】OpenGL 和c++问题

[复制链接]

160

主题

1

好友

5万

积分

本站精忠 MVP

Rank: 20Rank: 20

跳转到指定楼层
1#
发表于 2011-4-8 06:53 PM |只看该作者 |倒序浏览
本帖最后由 kuang 于 2011-4-12 12:56 PM 编辑

1)为什么在void sinegraph function里面的loop只可以loop几次,不会一直loop?
2)为什么我按Y或y以外的字母他还是会继续的?
3)要怎样在createwindow之前先让user输入的东西也就是x3,然后才弹出graph的window,show graph出来?
4)还有什么要改进的吗?

有哪位大大知道如何解决?小弟先感谢各位。。。以下是我的coding

#include <iostream>
#include <cmath>
#include <gl\glut.h>
using namespace std;

#define SIZE 700
#define PI 3.14159265

void menu();
void sinegraph();
void cosinegraph();
void tangentgraph();
void drawline(float x1, float y1, float x2, float y2);
void selection();

int main(int argc, char ** argv)
{
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
        glutInitWindowSize(700,500);
        glutInitWindowPosition(650,100);
       
        menu();
        selection();

        glutMainLoop();

        return 0;
}
void menu()
{
        cout<<"******** Welcome to Trigonometric World ********"<<endl<<endl;
        cout<<"1. Sine Graph"<<endl;
        cout<<"2. Cosine Graph"<<endl;
        cout<<"3. Tangent Graph"<<endl;
        cout<<"4. Quit"<<endl<<endl;
        cout<<"************************************************"<<endl<<endl;

}
void sinegraph()
{
        float x1, x2, x3, y1, y2;
        char again;

        do
        {
        cout<<"Enter period(number of cycles) you want : ";
        cin>>x3;
        for (int i=-700;i<SIZE;i++)
        {
                x1=((float)i/SIZE);
                x2=(((float)(i+1))/SIZE);
                y1=sin(x1*x3*PI);
                y2=sin(x2*x3*PI);
                drawline(x1,y1,x2,y2);
        }
        glutSwapBuffers();
        cout<<"Do you want to continue to draw? (Y/N) ";
        cin>>again;
        }while(again=='Y' && again=='y');
       
}
void cosinegraph()
{
        float x1, x2, y1, y2;
        for (int i=-700;i<SIZE;i++)
        {
                x1=((float)i/SIZE);
                x2=(((float)(i+1))/SIZE);
                y1=cos(x1*2*PI);
                y2=cos(x2*2*PI);
                drawline(x1,y1,x2,y2);
        }
        glutSwapBuffers();
}
void tangentgraph()
{
        float x1, x2, y1, y2;
        for (int i=-700;i<SIZE;i++)
        {
                x1=((float)i/SIZE);
                x2=(((float)(i+1))/SIZE);
                y1=tan(x1*2*PI);
                y2=tan(x2*2*PI);
                drawline(x1,y1,x2,y2);
        }
        glutSwapBuffers();
}
void drawline(float x1, float y1, float x2, float y2)
{
        //x-axis
        glBegin(GL_LINES);
        glColor3f(1.0, 0, 0);
        glVertex2f(-10.0f, 0.0f);
        glVertex2f(10.0f, 0.0f);
        glEnd();

        //y-axis
        glBegin(GL_LINES);
        glColor3f(1.0, 0, 0);
        glVertex2f(0.0f, -10.0f);
        glVertex2f(0.0f, 10.0f);
        glEnd();

        //curve
        glBegin(GL_LINE_STRIP);
        glColor3f(1.0,1.0,1.0);
        glVertex2f(x1,y1);
        glVertex2f(x2,y2);
        glEnd();
}
void selection()
{
        int choice;
       
        cout<<"Enter the type of graph you want(1,2,3 or 4) : ";
        cin>>choice;
        switch(choice)
        {
        case 1: glutCreateWindow(" Sine Graph ");
                        glutDisplayFunc(sinegraph);
                        break;
        case 2:        glutCreateWindow(" Cosine Graph ");
                        glutDisplayFunc(cosinegraph);
                        break;
        case 3:        glutCreateWindow(" Tangent Graph ");
                        glutDisplayFunc(tangentgraph);
                        break;
        default:exit(0);
        }
}




收藏收藏0

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

2#
发表于 2011-4-9 02:20 AM |只看该作者
1)为什么在void sinegraph function里面的loop只可以loop几次,不会一直loop?
2)为什么我按Y或y以外的字 ...
kuang 发表于 2011-4-8 06:53 PM



while(again=='Y' & again=='y')
看看你這里甚么地方错了


回复

使用道具 举报

160

主题

1

好友

5万

积分

本站精忠 MVP

Rank: 20Rank: 20

3#
发表于 2011-4-9 03:24 AM |只看该作者
本帖最后由 kuang 于 2011-4-9 03:29 AM 编辑

回复 2# Super-Tomato

我有两个&的,copy过来就不见了==
放两个|| 他还是继续loop,真搞不懂到底怎么了


回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

4#
发表于 2011-4-9 12:27 PM |只看该作者
回复  Super-Tomato

我有两个&的,copy过来就不见了==
放两个|| 他还是继续loop,真搞不懂到底怎么了
kuang 发表于 2011-4-9 03:24 AM



&& <--- 我就是指這個錯誤
|| 還是不行那麽就代表錯誤不在這里,自己使用編程軟體的 debug 功能追蹤問題出在哪吧


回复

使用道具 举报

2

主题

0

好友

251

积分

支柱会员

Rank: 4Rank: 4Rank: 4Rank: 4

5#
发表于 2011-4-10 11:55 PM |只看该作者
问题应该在glutMainLoop();
这个function一旦triggered后就不会return,你的main console会hang掉。
你如果要loop的话需要multi-threading,不然要从OpenGL 的window 里执行的你IO function。


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-10-25 10:26 AM , Processed in 0.102397 second(s), 26 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Ultra High-performance Dedicated Server powered by iCore Technology Sdn. Bhd.
Domain Registration | Web Hosting | Email Hosting | Forum Hosting | ECShop Hosting | Dedicated Server | Colocation Services
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部