[問題] 一題opengl的程式
遇到的問題: (題意請描述清楚)
畫出碎形,原本是一個藍色正方型,之後將每個邊都切成三等份
然後一直重覆,依據輸入者輸入的數字,遞迴切那麼多次,不過最多只能三次
我遇到的問題是,跑不出來
連那個最外面的大正方型(就是完全沒有不需要進入遞迴的狀況)都無法出現
只出現藍色外框,沒有填入顏色
---------------
| | | |
| 白 |藍 | 白 |
|----| |----|
| |
| 藍 黃 藍 |
|---- -----|
| | | |
| 白 |藍 | 白 |
| | | |
---------------
希望得到的正確結果:
依據輸入者的輸入跑出碎形
---------------
| | | |
| 白 |藍 | 白 |
|----| |----|
| |
| 藍 黃 藍 |
|---- -----|
| | | |
| 白 |藍 | 白 |
| | | |
---------------
程式跑出來的錯誤結果:
沒有錯誤,一樣能跑,只是只出現藍框
開發平台: VC 2008
有問題的code: (請善用置底文標色功能)
// hw1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<GL/glut.h>
unsigned char bottom;
int countLeft;
int countTop;
int countCentral;
int countBottom;
int countRight;
class vertex(){
GLfloat x;
GLfloat y;
vertex(){x=0.0f; y=0.0f}; //constructor
};
void SetupRC(void){ //set up the reandering state
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
void RenderFirstScene(void){ //creat to draw scene
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 1.0f);
glRectf(20.0f, 230.0f, 230.0f, 20.0f);
glFlush(); //flush drawing commands
}
void Keyboard(unsigned char bottom){
switch(bottom){
case 1:
RenderRestScene(bottom);
break;
case 2:
RenderResScene(bottom);
break;
case 3:
RenderRestScene(bottom);
break;
default:
break:
}
}
void RenderRestScene(unsigned char bottom){
vertex a, b, c, d;
a.x=20.0f; a.y=20.0f;
b.x=230.0f; b.y=20.0f;
c.x=230.0f; c.y=230.0f;
d.x=20.0f; d.y=230.0f;
countLeft=(int) bottom;
countTop=(int) bottom;
countCentral=(int) bottom;
countBottom=(int) bottom;
countRight=(int) bottom;
EdgeCut(a, b, c, d);
}
void EdgeCut(vertex v1, vertex v2, vertex v3, vertex v4){
--countLeft;
--countTop;
--countCentral;
--countBottom;
--countRight;
vertex left1, left2, right1, right2, bottom1, bottom2,
top1, top2, central1, central2, central3, central4;
left1.x = v1.x; left1.y = v1.y+((v1.y+ v4.y)/3);
left2.x = v1.x; left2.y = v1.y+2*((v1.y+ v4.y)/3);
right1.x = v2.x; right1.y = v1.y+((v1.y+ v4.y)/3);
right2.x = v2.x; right2.y = v1.y+2*((v1.y+ v4.y)/3);
bottom1.x = v1.x+((v1.x+ v2.x)/3); bottom1.y = v1.y;
bottom2.x = v1.x+2*((v1.x+ v2.x)/3); bottom1.y = v1.y;
top1.x = v1.x+((v1.x+ v2.x)/3); top1.y = v4.y;
top2.x = v1.x+2*((v1.x+ v2.x)/3); top2.y = v4.y;
central1.x = v1.x+((v1.x+ v2.x)/3); central1.y = v1.y+((v1.y+ v4.y)/3);
central2.x = v1.x+2*((v1.x+ v2.x)/3); central1.y = v1.y+((v1.y+ v4.y)/3);
central3.x = v1.x+2*((v1.x+ v2.x)/3); central3.y = v1.y+2*((v1.y+ v4.y)/3);
central4.x = v1.x+((v1.x+ v2.x)/3); central4.y = v1.y+2*((v1.y+ v4.y)/3);
glColor3f(0.0f, 0.0f, 1.0f); //set current drawing color to blue
//draw a filled rectangle with current color
glRectf(v1.x, v1.y+2*((v1.y+ v4.y)/3), v1.x+((v1.x+ v2.x)/3), v1.y+((v1.y+
v4.y)/3)); //left
glRectf(v1.x+((v1.x+ v2.x)/3), v1.y+((v1.y+ v4.y)/3), v1.x+2*((v1.x+
v2.x)/3), v1.y); //bottom
glRectf(v1.x+2*((v1.x+ v2.x)/3), v1.y+2*((v1.y+ v4.y)/3), v2.x, v1.y+((v1.y+
v4.y)/3)); //right
glRectf(v1.x+((v1.x+ v2.x)/3), v4.y, v1.x+2*((v1.x+ v2.x)/3), v1.y+2*((v1.y+
v4.y)/3)); //top
glColor3f(1.0f, 1.0f, 0.0f); //set current drawing color to yellow
//draw a filled rectangle with current color
glRectf(v1.x+((v1.x+ v2.x)/3), v1.y+2*((v1.y+ v4.y)/3), v1.x+2*((v1.x+
v2.x)/3), v1.y+((v1.y+ v4.y)/3)); //central
glColor3f(1.0f, 1.0f, 0.0f); //set current drawing color to white
//draw a filled rectangle with current color
glRectf(v1.x, v4.y, v1.x+((v1.x+ v2.x)/3), v1.y+2*((v1.y+
v4.y)/3)); //upper left
glRectf(v1.x+2*((v1.x+ v2.x)/3), v4.y, v2.x, v1.y+2*((v1.y+
v4.y)/3)); //upper right
glRectf(v1.x, v1.y+((v1.y+ v4.y)/3), v1.x+((v1.x+ v2.x)/3), v1.y); //lower
left
glRectf(v1.x+2*((v1.x+ v2.x)/3), v1.y+((v1.y+ v4.y)/3), v2.x,
v2.y); //lower right
if(countLeft>0)
EdgeCut(left1, central1, central2, left2);
if(countTop>0)
EdgeCut(central4, central3, top2, top1);
if(countCentral>0)
EdgeCut(central1, central2, cnetral3, central4);
if(countBottom>0)
EdgeCut(bottom1, bottom2, central2, central1);
if(countRight>0)
EdgeCut(central2, right1, right2, central3);
}
void ChangeSize(GLsizei w, GLsizei h){ //called by GLUT library when the
windows has changed size
if(h==0) //prevent a divide by zero
h=1;
glViewport(0, 0, w, h); //set viewport to windows dimensions
glMatrixMode(GL_PROJECTION); //reset coordinate system
glLoadIdentity();
if(w<=h) //established clipping volumn(left, right, bottom, top,
near, far)
glOrtho(0.0f, 250.0f, 0.0f, 250.0f*h/w, 1.0, -1.0);
else
glOrtho(0.0f, 250.0f*w/h, 0.0f, 250.0f, 1.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(){ //main program entry point
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutCreateWindow("hw1");
glutDisplayFunc(RenderFirstScene);
glutKeyboardFunc(Keyboard);
gluDisplayFunct(RenderRestScene);
glutReshapeFunc(ChangeSize);
SetupRC();
glutMainLoop();
}
補充說明:
有點複雜看,不過希望大家能幫我
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 112.104.62.127
→
10/17 17:17, , 1F
10/17 17:17, 1F
→
10/17 17:30, , 2F
10/17 17:30, 2F
→
10/17 17:35, , 3F
10/17 17:35, 3F
→
10/17 17:37, , 4F
10/17 17:37, 4F
→
10/17 17:53, , 5F
10/17 17:53, 5F
→
10/17 17:53, , 6F
10/17 17:53, 6F
→
10/17 17:59, , 7F
10/17 17:59, 7F
→
10/17 17:59, , 8F
10/17 17:59, 8F
→
10/17 20:14, , 9F
10/17 20:14, 9F
→
10/17 20:36, , 10F
10/17 20:36, 10F
→
10/17 20:57, , 11F
10/17 20:57, 11F
→
10/18 12:44, , 12F
10/18 12:44, 12F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):
問題
0
12