opengl 도형옮기기 질문.
opengl초본대요.. 고수님들이라면 바로 보면 아실거라고 생각되네요.
마우스를 클릭하면 삼각형이나 사각형이 그려지고 다시 그려진 도형들을 클릭해서 드래깅하면 옮겨져야 합니다. 셀렉트나 피킹쓰지 않고 자리를 옮기도록 해야하는데 제 생각에 배열에 들어가있는 값들을 새로운 자리의 배열로 옮기면 된다고 생각하는데.. 어쨋든 잘 안돼네요. 좀 도와주세요~~~ 절박합니다 낼까지 인데..ㅠ_ㅠ
#include "stdafx.h"
#include
#include
#include
#include // Must come first to avoid redef error
#include
#define NUMBER_OF_POINT 1000
#include
//using namespace std;
// Constants and enums
enum buttonTypes {SQUARE_BUTTON_ID = 0, TRIANGLE_BUTTON_ID=1} buttonA;
enum colors { RED =0, GREEN=1, BLUE=2} buttonB;
const int SQUARE_WIDTH = 10;
const int TRIANGLE_WIDTH = 10;
const int WIN_WIDTH = 500;
const int WIN_HEIGHT = 500;
// These are the live variables controlled/modified by the GUI
int main_window;
int red = 0;
int green = 0;
int blue = 0;
GLUI *glui;
//GLint xa;
//GLint ya;
GLfloat myVertex[100][2];
GLfloat myVertexCol[100][3];
GLint count=0;
void buttonCB(int control)
{
switch(control)
{
case SQUARE_BUTTON_ID:
printf ("Square\n");
buttonA = SQUARE_BUTTON_ID;
break;
case TRIANGLE_BUTTON_ID:
printf("Triangle\n");
buttonA = TRIANGLE_BUTTON_ID;
break;
}
// This causes opengl to redraw
glutPostRedisplay();
}
void colorCB(int id)
{
switch(id)
{
case RED:
printf("Changing Red\n");
glColor3f(1.0f, 0.0f, 0.0f);
buttonB = RED;
break;
case GREEN:
printf("Changing Green\n");
glColor3f(0.0f, 1.0f, 0.0f);
buttonB = GREEN;
break;
case BLUE:
printf("Changing Blue\n");
buttonB = BLUE;
break;
}
glutPostRedisplay();
}
void drawSquare(int xa, int ya)
{
GLint index;
myVertexCol[count][3]=(1.0,0.0,0.0);
glColor3f(1, 0, 0);
ya=WIN_HEIGHT-ya;
//for (index=0;index < count;index++){
// if (abs(xa-myVertex
// printf("hh");
// }else
glBegin(GL_POLYGON);
glVertex2f(xa+SQUARE_WIDTH, ya+SQUARE_WIDTH);
glVertex2f(xa-SQUARE_WIDTH, ya+SQUARE_WIDTH);
glVertex2f(xa-SQUARE_WIDTH, ya-SQUARE_WIDTH);
glVertex2f(xa+SQUARE_WIDTH, ya-SQUARE_WIDTH);
//}
glEnd();
glFlush();
}
void drawTriangle(int xa, int ya)
{
glColor3f(1, 0, 0);
myVertexCol[count][3]=(1.0,0.0,0.0);
ya=WIN_HEIGHT-ya;
glBegin(GL_POLYGON);
glVertex2f(xa, ya);
glVertex2f(xa-SQUARE_WIDTH, ya-SQUARE_WIDTH);
glVertex2f(xa+SQUARE_WIDTH, ya-SQUARE_WIDTH);
//glVertex2f(xa+SQUARE_WIDTH, ya-SQUARE_WIDTH);
glEnd();
glFlush();
}
void drawLine()
{
// Code to draw a line
}
// The display function is called whenever OpenGL needs to refresh the image
void myGlutDisplay(void)
{
glutSwapBuffers();
}
// This is for double buffering...it's necessary for interactive applications.
// We'll learn more about it later
//Code goes in here!
void myGlutReshape(int x, int y)
{
// This will not allow them to resize the window! We'll change this
// later down the road
/*glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, x, 0.0, y);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,x,y);
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();*/
glutReshapeWindow(WIN_WIDTH, WIN_HEIGHT);
}
void myGlutMotion(int x, int y)
{
myGlutDisplay();
}
/*void myCol(int xa, int ya, int r, int g, int b)
{
GLint index, index1;
myVertexCol[count][3]=(r,g,b);
glColor3f(r+0.1,g,b);
myVertexCol[count][3]=(r+0.2,g,b);
ya=WIN_HEIGHT-ya;
}*/
void myGlutMouse (int button, int button_state, int x, int y)
{
//Callback for mouse presses
if(button == GLUT_LEFT_BUTTON && button_state == GLUT_DOWN)
{
GLint index,index1,index2,index3,index4;
myVertex[count][0]=x;
myVertex[count][1]=y;
for (index=0;index < count;index++){
if (abs(myVertex
{
switch (buttonA)
{
case SQUARE_BUTTON_ID:
drawSquare(myVertex[count][0],myVertex[count][1]);
break;
case TRIANGLE_BUTTON_ID:
drawTriangle(myVertex[count][0],myVertex[count][1]);
break;
}
}
if (abs(myVertex
{
//int oldx=x;
//int oldy=y;
/*if(button == GLUT_LEFT_BUTTON && button_state == GLUT_UP){
for(index1=oldx-5,index3=x-5;index1
for(index2=oldx-5,index4=x-5;index2
myVertex[index3][index4]=myVertex[oldx][oldy];
}
}*/
switch(buttonB){
case RED:
// myCol(myVertex[index1][index2]);
case BLUE:
// myCol(count);
case GREEN:
break;
// myCol(count);
}
}
}
count++;
myGlutDisplay();
}
}
void initScene()
{
///All initialization stuff goes here
/*glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0, 1.0, 1.0,1.0); //white background
glutReshapeFunc(myGlutReshape);*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, WIN_WIDTH, 0.0, WIN_HEIGHT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,WIN_WIDTH,WIN_HEIGHT);
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
int main(int argc, char **argv)
{
// setup glut
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE); //We'll discuss these later
glutInitWindowPosition( 50, 50 );
glutInitWindowSize( WIN_WIDTH , WIN_HEIGHT );
glMatrixMode(GL_PROJECTION);
//You'll need a handle for your main window for GLUI
main_window = glutCreateWindow( "Graph Maker" );
glutDisplayFunc( myGlutDisplay);
glutReshapeFunc( myGlutReshape );
glutMouseFunc( myGlutMouse );
glutMotionFunc( myGlutMotion);
// Initialize my Scene
initScene();
//Build the GUI
glui = GLUI_Master.create_glui( "Graph Maker GUI", 0, 600, 50 );
GLUI_Panel *componentPanel = glui->add_panel("Graph Components");
glui->add_button_to_panel(componentPanel, "Square", SQUARE_BUTTON_ID, buttonCB);
glui->add_button_to_panel(componentPanel, "Triangle", TRIANGLE_BUTTON_ID, buttonCB);
glui->add_separator();
GLUI_Panel *colorPanel = glui->add_panel("Color");
// These could be done with floats but I ran into some issues
GLUI_Spinner *redValue = glui->add_spinner_to_panel(colorPanel, "Red", 2, &red, RED, colorCB);
redValue->set_int_limits(0, 255);
GLUI_Spinner *greenValue = glui->add_spinner_to_panel(colorPanel, "Green", 2, &green,GREEN, colorCB);
greenValue->set_int_limits(0,255);
GLUI_Spinner *blueValue = glui->add_spinner_to_panel(colorPanel, "Blue", 2, &blue, BLUE, colorCB);
blueValue->set_int_limits(0.0, 255);
glui->set_main_gfx_window( main_window );
// We register the idle callback with GLUI, *not* with GLUT
//GLUI_Master.set_glutIdleFunc( myGlutIdle );
GLUI_Master.set_glutIdleFunc( NULL );
glutMainLoop();
return EXIT_SUCCESS;
}
댓글 달기