現在地 >> メニュー >> サンプルコード::OpenGL >> 画像読み込みと点


問題


画像を読み込んで、それをglVertexに色をつけ点を打つことで
表示せよ。

画像

 >>画像読み込みと点2

答え


画像読み込みはOpenCVを使用。
アスペクト比は適当。

#include <cv.h>
#include <highgui.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <iostream>

void display();
void reshape(int w, int h);
void timer(int value);

void DRAW_XYZ();
void DRAW_POINT();

using namespace std;

IplImage *image1;
void LoadGLTextures()
{

image1=cvLoadImage("test2.jpg",CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
if(image1==NULL)
{
printf("can't load image \n");
std::exit(0);
}

cvCvtColor(image1, image1, CV_BGR2RGB);
cvFlip(image1,NULL,0);

}


inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(200,200);
}

inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(10,timer,0);
}

inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
LoadGLTextures();
}


int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
glutMainLoop();

return 0;
}

/********[ここからコールバック]****************************************/
void display()
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_DEPTH_TEST);
DRAW_XYZ();


glPushMatrix();
glTranslated(0,0,-10);

DRAW_POINT();
glPopMatrix();

glDisable(GL_DEPTH_TEST);

glutSwapBuffers();



}


void reshape(int w, int h)
{
glViewport(0, 0, w, h);

glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
gluLookAt(10.0, 10.0, 50.0, 10.0,10.0, 19.0, 0.0, 1.0, 0.0);
}

void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(1,timer,0); //タイマー関数
}


/**********[ここから各種関数]***********************/

void DRAW_XYZ()
{
glBegin(GL_LINES);

glColor3d(0,1,0);//x
glVertex2d(-100,0);
glVertex2d(100, 0);

glColor3d(1,0,0);//y
glVertex2d(0,0);
glVertex2d(0,100);

glColor3d(0,0,1);//z
glVertex3d(0,0,-100);
glVertex3d(0,0, 100);
glEnd();

}

void DRAW_POINT()
{
unsigned char r,g,b;
glPointSize(1);

glBegin(GL_POINTS);

for(int j = 0; j<image1->height;j++){
for(int i = 0; i<image1->widthStep;i+=3)
{
r = image1->imageData[j*image1->widthStep + i];
g = image1->imageData[j*image1->widthStep + i+1];
b = image1->imageData[j*image1->widthStep + i+2];

glColor3ub(r,g,b);
glVertex2d*1;

}
}


glEnd();
}


メモ


display-listでメモリにためた方がよいかも。

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






▲よろしければ広告のクリックもお願いします


▲ランキングに参加しました

管理人/副管理人のみ編集できます