点此下载源文件
/*
MainApp.hpp
*/
#ifndef MAINAPP_HPP
#define MAINAPP_HPP
#include <GL/gl.h>
#include <SDL/SDL.h>
#define ImageWidth 64
#define ImageHeight 64
class MainApp
{
public:
MainApp();
~MainApp();
void loop();
void quit();
private:
void initGL();
void initTexture();
void makeImage();
void destroyGL();
void onKeyDown(const SDL_Event &event);
void draw();
bool running;
GLubyte Image[ImageWidth][ImageHeight][3];
};
#endif
/*
MainApp.cpp
*/
#include <GL/gl.h>
#include <SDL/SDL.h>
#include "MainApp.hpp"
MainApp::MainApp():running(true)
{
initGL();
}
MainApp::~MainApp()
{
destroyGL();
}
void MainApp::initGL()
{
SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(600,300,16,SDL_OPENGL);
glClearColor(0,0,0,0);
initTexture();
}
/* 创建纹理 */
void MainApp::makeImage(void)
{
int i, j, r,g,b;
for (i = 0; i < ImageWidth; i++)
{
for (j = 0; j < ImageHeight; j++)
{
r=(i*j)%255;
g=(4*i)%255;
b=(4*j)%255;
Image[i][j][0] = (GLubyte) r;
Image[i][j][1] = (GLubyte) g;
Image[i][j][2] = (GLubyte) b;
}
}
}
void MainApp::initTexture()
{
makeImage();
/* 定义纹理 */
glTexImage2D(GL_TEXTURE_2D, 0, 3, ImageWidth,
ImageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,
&Image[0][0][0]);
/* 控制滤波 */
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
/* 说明映射方式*/
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
/* 启动纹理映射 */
glEnable(GL_TEXTURE_2D);
}
void MainApp::destroyGL()
{
SDL_Quit();
}
void MainApp::loop()
{
SDL_Event event;
while(running)
{
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
onKeyDown(event);
break;
}
}
SDL_Delay(50);
draw();
}
}
void MainApp::quit()
{
running=false;
}
void MainApp::onKeyDown(const SDL_Event &event)
{
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
quit();
}
}
void MainApp::draw()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(1,1,1);
glTexCoord2f(0,0);glVertex3f(-0.5,-0.5,-0.5);
glTexCoord2f(0,1);glVertex3f(-0.5,0.5,-0.5);
glTexCoord2f(1,1);glVertex3f(0.5,0.5,0.5);
glTexCoord2f(1,0);glVertex3f(0.5,-0.5,0.5);
glEnd();
SDL_GL_SwapBuffers();
}
int main(int argc,char* argv[])
{
MainApp app;
app.loop();
}
posted on 2006-10-17 18:42
四海 阅读(1388)
评论(0) 编辑 收藏 引用