summaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-04-09 20:22:13 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2011-04-09 20:22:13 +0200
commit3817acdb49d775edb23fafbed70095be8f648ed6 (patch)
tree088d94ebd9f767e84263e1400db336930a7324cd /main.cpp
parente19c3838a4d89fb0640ed563ffdf58cd125cde7f (diff)
Terrain multitexturing.
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp88
1 files changed, 53 insertions, 35 deletions
diff --git a/main.cpp b/main.cpp
index 97abb46..b068cd5 100644
--- a/main.cpp
+++ b/main.cpp
@@ -20,6 +20,33 @@
FTFont *font;
+GLuint load_texture(const std::string filename) {
+ GLuint texture;
+ SDL_Surface *surface = IMG_Load(filename.c_str());
+ SDL_Surface *image = SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h, 32,
+ 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
+ SDL_Rect area;
+ area.x = area.y = 0;
+ area.w = surface->w;
+ area.h = surface->h;
+ SDL_BlitSurface(surface, &area, image, &area);
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+ glGenTextures(1, &texture);
+ glBindTexture(GL_TEXTURE_2D, texture);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ if(image->format->Amask) {
+ gluBuild2DMipmaps(GL_TEXTURE_2D, 4, image->w, image->h, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
+ } else {
+ gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->w, image->h, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);
+ }
+ SDL_FreeSurface(surface);
+ SDL_FreeSurface(image);
+
+ return texture;
+}
+
+
int main(int argc, char **argv) {
video::width = 1280;
video::height = 720;
@@ -28,40 +55,27 @@ int main(int argc, char **argv) {
Scene scene;
GLShaderProgram program;
- GLVertexShader terrain_vertex("terrain_vertex.glsl");
- GLFragmentShader terrain_fragment("terrain_fragment.glsl");
+ GLVertexShader terrain_vertex("shaders/terrain_vertex.glsl");
+ GLFragmentShader terrain_fragment("shaders/terrain_fragment.glsl");
program.attach(terrain_vertex);
program.attach(terrain_fragment);
program.link();
+ {
+ program.use();
+ GLint tex1loc = glGetUniformLocation(program.get_program(), "tex[1]");
+ glUniform1i(tex1loc, 1);
+ GLint tex2loc = glGetUniformLocation(program.get_program(), "tex[2]");
+ glUniform1i(tex2loc, 2);
+ glUseProgram(0);
+ }
+
font = new FTTextureFont("font.ttf");
font->FaceSize(10);
- GLuint grass_texture;
- {
- SDL_Surface *surface = IMG_Load("Grass0073_3_S.jpg");
- SDL_Surface *image = SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h, 32,
- 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
- SDL_Rect area;
- area.x = area.y = 0;
- area.w = surface->w;
- area.h = surface->h;
- SDL_BlitSurface(surface, &area, image, &area);
- //glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
- glGenTextures(1, &grass_texture);
- glBindTexture(GL_TEXTURE_2D, grass_texture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- if(image->format->Amask) {
- //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
- gluBuild2DMipmaps(GL_TEXTURE_2D, 4, image->w, image->h, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
- } else {
- //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->w, image->h, 0, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);
- gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->w, image->h, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);
- }
- SDL_FreeSurface(surface);
- SDL_FreeSurface(image);
- }
+ GLuint grass_texture = load_texture("textures/Grass0073_3_S.jpg");
+ GLuint rock_texture = load_texture("textures/RockJagged0010_2_S.jpg");
+ GLuint soil_texture = load_texture("textures/SoilSand0168_9_S.jpg");
//SDL_WM_GrabInput(SDL_GRAB_ON);
@@ -207,9 +221,17 @@ int main(int argc, char **argv) {
unsigned int chunks_rendered = 0;
if(terrain) {
program.use();
- glBindTexture(GL_TEXTURE_2D, grass_texture);
glEnable(GL_TEXTURE_2D);
- //for(int i = 0; i < scene.qt->chunk_count; i++) {
+
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, grass_texture);
+
+ glActiveTexture(GL_TEXTURE1);
+ glBindTexture(GL_TEXTURE_2D, rock_texture);
+
+ glActiveTexture(GL_TEXTURE2);
+ glBindTexture(GL_TEXTURE_2D, soil_texture);
+
std::queue<Quadtree::QuadChunk*> q;
q.push(scene.qt->root);
while(!q.empty()) {
@@ -222,25 +244,21 @@ int main(int argc, char **argv) {
} else if(!chunk->vbo_object)
continue;
chunks_rendered++;
- //std::cout << (boost::format("vertices: %d nodes: %d") % scene.qt->chunks[i]->vertices % scene.qt->chunks[i]->node_count).str() << std::endl;
- //glBindBuffer(GL_ARRAY_BUFFER, scene.qt->vbo_object);
- //glBindBuffer(GL_ARRAY_BUFFER, scene.qt->chunks[i]->vbo_object);
glBindBuffer(GL_ARRAY_BUFFER, chunk->vbo_object);
glVertexPointer(3, GL_FLOAT, 0, NULL);
- //glNormalPointer(GL_FLOAT, 0, (GLvoid*)(scene.qt->vertices*3*sizeof(float)));
glNormalPointer(GL_FLOAT, 0, (GLvoid*)(chunk->vertices*3*sizeof(float)));
- //glTexCoordPointer(2, GL_FLOAT, 0, (GLvoid*)(scene.qt->vertices*3*sizeof(float)*2));
glTexCoordPointer(2, GL_FLOAT, 0, (GLvoid*)(chunk->vertices*3*sizeof(float)*2));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- //glDrawArrays(GL_TRIANGLES, 0, scene.qt->vertices);
glDrawArrays(GL_TRIANGLES, 0, chunk->vertices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
glDisable(GL_TEXTURE_2D);
+ // set active to texture0 to avoid breaking the texture font
+ glActiveTexture(GL_TEXTURE0);
glUseProgram(0);
}
if(grid) {