From e8c9945e13fca88a6f8232838682df0654437f3e Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 5 Jul 2012 23:47:29 +0200 Subject: Fixed bug with texturing - moved creation-time tex params to Gfx::TextureCreateParams - fixed bug with texture creation - added simple test for multitexturing --- src/graphics/opengl/test/CMakeLists.txt | 31 +++ src/graphics/opengl/test/tex1.png | Bin 0 -> 151263 bytes src/graphics/opengl/test/tex2.png | Bin 0 -> 57503 bytes src/graphics/opengl/test/texture_test.cpp | 365 ++++++++++++++++++++++++++++++ 4 files changed, 396 insertions(+) create mode 100644 src/graphics/opengl/test/CMakeLists.txt create mode 100644 src/graphics/opengl/test/tex1.png create mode 100644 src/graphics/opengl/test/tex2.png create mode 100644 src/graphics/opengl/test/texture_test.cpp (limited to 'src/graphics/opengl/test') diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt new file mode 100644 index 0000000..ce2a83f --- /dev/null +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -0,0 +1,31 @@ +cmake_minimum_required(VERSION 2.8) + +find_package(OpenGL REQUIRED) +find_package(SDL REQUIRED) +find_package(SDL_image REQUIRED) +find_package(PNG REQUIRED) + +set(CMAKE_BUILD_TYPE debug) +set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0") + + +set(SOURCES +../gldevice.cpp +../../common/device.cpp +../../../common/logger.cpp +../../../common/image.cpp +texture_test.cpp +) + +include_directories(../../../) + +set(LIBS +${SDL_LIBRARY} +${SDLIMAGE_LIBRARY} +${OPENGL_LIBRARY} +${PNG_LIBRARIES} +) + +add_executable(texture_test ${SOURCES}) + +target_link_libraries(texture_test ${LIBS}) diff --git a/src/graphics/opengl/test/tex1.png b/src/graphics/opengl/test/tex1.png new file mode 100644 index 0000000..46c68a0 Binary files /dev/null and b/src/graphics/opengl/test/tex1.png differ diff --git a/src/graphics/opengl/test/tex2.png b/src/graphics/opengl/test/tex2.png new file mode 100644 index 0000000..ebdae0d Binary files /dev/null and b/src/graphics/opengl/test/tex2.png differ diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp new file mode 100644 index 0000000..022cf87 --- /dev/null +++ b/src/graphics/opengl/test/texture_test.cpp @@ -0,0 +1,365 @@ +#include "common/logger.h" +#include "common/image.h" +#include "graphics/opengl/gldevice.h" +#include "math/geometry.h" + +#include +#include +#include + +#include + + +#define DEV 1 + + +void Init(Gfx::CGLDevice *device) +{ + device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, false); + device->SetShadeModel(Gfx::SHADE_SMOOTH); + + CImage img1; + if (! img1.Load("tex1.png")) + { + std::string err = img1.GetError(); + GetLogger()->Error("texture 1 not loaded, error: %d!\n", err.c_str()); + } + CImage img2; + if (! img2.Load("tex2.png")) + { + std::string err = img2.GetError(); + GetLogger()->Error("texture 2 not loaded, error: %d!\n", err.c_str()); + } + + Gfx::TextureCreateParams tex1CreateParams; + tex1CreateParams.alpha = true; + tex1CreateParams.mipmap = true; + tex1CreateParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; + tex1CreateParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; + tex1CreateParams.wrapT = Gfx::TEX_WRAP_CLAMP; + + Gfx::TextureCreateParams tex2CreateParams; + tex2CreateParams.alpha = true; + tex2CreateParams.mipmap = true; + tex2CreateParams.minFilter = Gfx::TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST; + tex2CreateParams.magFilter = Gfx::TEX_MAG_FILTER_NEAREST; + tex2CreateParams.wrapS = Gfx::TEX_WRAP_CLAMP; + + Gfx::Texture* tex1 = device->CreateTexture(&img1, tex1CreateParams); + Gfx::Texture* tex2 = device->CreateTexture(&img2, tex2CreateParams); + + device->SetTexture(0, tex1); + device->SetTexture(1, tex2); + + Gfx::TextureParams tex1Params; + tex1Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + tex1Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + device->SetTextureParams(0, tex1Params); + + Gfx::TextureParams tex2Params; + tex2Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + tex2Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + device->SetTextureParams(1, tex2Params); + + device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); +} + +void Render(Gfx::CGLDevice *device) +{ + device->BeginScene(); + + glFlush(); + + Math::Matrix ortho; + Math::LoadOrthoProjectionMatrix(ortho, -10, 10, -10, 10); + device->SetTransform(Gfx::TRANSFORM_PROJECTION, ortho); + + Math::Matrix id; + id.LoadIdentity(); + + device->SetTransform(Gfx::TRANSFORM_WORLD, id); + device->SetTransform(Gfx::TRANSFORM_VIEW, id); + + static Gfx::VertexTex2 quad[] = + { + Gfx::VertexTex2(Math::Vector(-2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 1.0f), Math::Point(0.0f, 1.0f)), + Gfx::VertexTex2(Math::Vector( 2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 1.0f), Math::Point(1.0f, 1.0f)), + Gfx::VertexTex2(Math::Vector( 2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 0.0f), Math::Point(1.0f, 0.0f)), + + Gfx::VertexTex2(Math::Vector( 2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 0.0f), Math::Point(1.0f, 0.0f)), + Gfx::VertexTex2(Math::Vector(-2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f)), + Gfx::VertexTex2(Math::Vector(-2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 1.0f), Math::Point(0.0f, 1.0f)), + }; + + Math::Matrix t; + Math::LoadTranslationMatrix(t, Math::Vector(-4.0f, 4.0f, 0.0f)); + device->SetTransform(Gfx::TRANSFORM_VIEW, t); + + device->SetTextureEnabled(0, true); + device->SetTextureEnabled(1, false); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); + + Math::LoadTranslationMatrix(t, Math::Vector( 4.0f, 4.0f, 0.0f)); + device->SetTransform(Gfx::TRANSFORM_VIEW, t); + + device->SetTextureEnabled(0, false); + device->SetTextureEnabled(1, true); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); + + device->SetTextureEnabled(0, true); + device->SetTextureEnabled(1, true); + + Math::LoadTranslationMatrix(t, Math::Vector( 0.0f, -4.0f, 0.0f)); + device->SetTransform(Gfx::TRANSFORM_VIEW, t); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); + + device->EndScene(); +} + +void InitGL() +{ + CImage img1; + if (! img1.Load("tex1.png")) + { + std::string err = img1.GetError(); + GetLogger()->Error("texture 1 not loaded, error: %d!\n", err.c_str()); + } + CImage img2; + if (! img2.Load("tex2.png")) + { + std::string err = img2.GetError(); + GetLogger()->Error("texture 2 not loaded, error: %d!\n", err.c_str()); + } + + unsigned int textureHandle1 = 0; + + glActiveTexture(GL_TEXTURE0_ARB); + glEnable(GL_TEXTURE_2D); + + glPixelStorei(GL_UNPACK_ALIGNMENT,1); + + glGenTextures(1, &textureHandle1); + glBindTexture(GL_TEXTURE_2D, textureHandle1); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img1.GetData()->surface->w, img1.GetData()->surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img1.GetData()->surface->pixels); + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); + + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_REPLACE); + glTexEnvi(GL_TEXTURE_2D, GL_SRC0_RGB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_REPLACE); + glTexEnvi(GL_TEXTURE_2D, GL_SRC0_ALPHA, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + + + + unsigned int textureHandle2 = 0; + + glActiveTexture(GL_TEXTURE1_ARB); + glEnable(GL_TEXTURE_2D); + + glPixelStorei(GL_UNPACK_ALIGNMENT,1); + + glGenTextures(1, &textureHandle2); + glBindTexture(GL_TEXTURE_2D, textureHandle2); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img2.GetData()->surface->w, img2.GetData()->surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img2.GetData()->surface->pixels); + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); + + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_MODULATE); + glTexEnvi(GL_TEXTURE_2D, GL_SRC0_RGB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_MODULATE); + glTexEnvi(GL_TEXTURE_2D, GL_SRC0_ALPHA, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + + + + glMatrixMode(GL_PROJECTION); + glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, -1.0f, 1.0f); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + +void RenderGL() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glLoadIdentity(); + + glColor3f(1.0f, 1.0f, 1.0f); + + glPushMatrix(); + glTranslatef(-4.0f, 4.0f, 0.0f); + + glActiveTextureARB(GL_TEXTURE0_ARB); + glEnable(GL_TEXTURE_2D); + glActiveTextureARB(GL_TEXTURE1_ARB); + glDisable(GL_TEXTURE_2D); + + glBegin(GL_QUADS); + { + glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); + glVertex2f(-2.0f, -2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); + glVertex2f(2.0f, -2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); + glVertex2f(2.0f, 2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); + glVertex2f(-2.0f, 2.0f); + } + glEnd(); + + glPopMatrix(); + glPushMatrix(); + glTranslatef( 4.0f, 4.0f, 0.0f); + + glActiveTextureARB(GL_TEXTURE0_ARB); + glDisable(GL_TEXTURE_2D); + glActiveTextureARB(GL_TEXTURE1_ARB); + glEnable(GL_TEXTURE_2D); + + glBegin(GL_QUADS); + { + glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); + glVertex2f(-2.0f, -2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); + glVertex2f(2.0f, -2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); + glVertex2f(2.0f, 2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); + glVertex2f(-2.0f, 2.0f); + } + glEnd(); + + glPopMatrix(); + glPushMatrix(); + glTranslatef( 0.0f, -4.0f, 0.0f); + + glActiveTextureARB(GL_TEXTURE0_ARB); + glEnable(GL_TEXTURE_2D); + glActiveTextureARB(GL_TEXTURE1_ARB); + glEnable(GL_TEXTURE_2D); + + glBegin(GL_QUADS); + { + glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); + glVertex2f(-2.0f, -2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); + glVertex2f(2.0f, -2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); + glVertex2f(2.0f, 2.0f); + glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); + glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); + glVertex2f(-2.0f, 2.0f); + } + glEnd(); + + glPopMatrix(); + + glFlush(); +} + + +int main() +{ + CLogger(); + + // Without any error checking, for simplicity + + SDL_Init(SDL_INIT_VIDEO); + + IMG_Init(IMG_INIT_PNG); + + const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); + + Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE; + + if (videoInfo->hw_available) + videoFlags |= SDL_HWSURFACE; + else + videoFlags |= SDL_SWSURFACE; + + if (videoInfo->blit_hw) + videoFlags |= SDL_HWACCEL; + + + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + SDL_Surface *surface = SDL_SetVideoMode(800, 600, 32, videoFlags); + + + SDL_WM_SetCaption("Texture Test", "Texture Test"); + + + #if DEV + Gfx::CGLDevice *device = new Gfx::CGLDevice(); + device->Create(); + + Init(device); + #else + InitGL(); + #endif + + bool done = false; + while (! done) + { + #if DEV + Render(device); + #else + RenderGL(); + #endif + + SDL_GL_SwapBuffers(); + + SDL_Event event; + SDL_PollEvent(&event); + if (event.type == SDL_QUIT) + done = true; + + usleep(50000); + } + + #if DEV + device->Destroy(); + #endif + + SDL_FreeSurface(surface); + + IMG_Quit(); + + SDL_Quit(); + + return 0; +} -- cgit v1.2.3-1-g7c22 From 32043605153543bd72eb012ff310367299ad4e8f Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Fri, 6 Jul 2012 19:00:22 +0200 Subject: Refactoring in math & texture modules - moved texture-related structs to texture.h & code to texture.cpp - cleaned up texture test code - added Math:: namespace qualifiers to math modules for clarity --- src/graphics/opengl/test/texture_test.cpp | 186 +----------------------------- 1 file changed, 1 insertion(+), 185 deletions(-) (limited to 'src/graphics/opengl/test') diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp index 022cf87..764e127 100644 --- a/src/graphics/opengl/test/texture_test.cpp +++ b/src/graphics/opengl/test/texture_test.cpp @@ -7,11 +7,6 @@ #include #include -#include - - -#define DEV 1 - void Init(Gfx::CGLDevice *device) { @@ -68,8 +63,6 @@ void Render(Gfx::CGLDevice *device) { device->BeginScene(); - glFlush(); - Math::Matrix ortho; Math::LoadOrthoProjectionMatrix(ortho, -10, 10, -10, 10); device->SetTransform(Gfx::TRANSFORM_PROJECTION, ortho); @@ -119,172 +112,6 @@ void Render(Gfx::CGLDevice *device) device->EndScene(); } -void InitGL() -{ - CImage img1; - if (! img1.Load("tex1.png")) - { - std::string err = img1.GetError(); - GetLogger()->Error("texture 1 not loaded, error: %d!\n", err.c_str()); - } - CImage img2; - if (! img2.Load("tex2.png")) - { - std::string err = img2.GetError(); - GetLogger()->Error("texture 2 not loaded, error: %d!\n", err.c_str()); - } - - unsigned int textureHandle1 = 0; - - glActiveTexture(GL_TEXTURE0_ARB); - glEnable(GL_TEXTURE_2D); - - glPixelStorei(GL_UNPACK_ALIGNMENT,1); - - glGenTextures(1, &textureHandle1); - glBindTexture(GL_TEXTURE_2D, textureHandle1); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img1.GetData()->surface->w, img1.GetData()->surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img1.GetData()->surface->pixels); - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_REPLACE); - glTexEnvi(GL_TEXTURE_2D, GL_SRC0_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_RGB, GL_SRC_COLOR); - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_REPLACE); - glTexEnvi(GL_TEXTURE_2D, GL_SRC0_ALPHA, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - - - - unsigned int textureHandle2 = 0; - - glActiveTexture(GL_TEXTURE1_ARB); - glEnable(GL_TEXTURE_2D); - - glPixelStorei(GL_UNPACK_ALIGNMENT,1); - - glGenTextures(1, &textureHandle2); - glBindTexture(GL_TEXTURE_2D, textureHandle2); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img2.GetData()->surface->w, img2.GetData()->surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img2.GetData()->surface->pixels); - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_RGB, GL_MODULATE); - glTexEnvi(GL_TEXTURE_2D, GL_SRC0_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_RGB, GL_SRC_COLOR); - glTexEnvi(GL_TEXTURE_2D, GL_COMBINE_ALPHA, GL_MODULATE); - glTexEnvi(GL_TEXTURE_2D, GL_SRC0_ALPHA, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_2D, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - - - - glMatrixMode(GL_PROJECTION); - glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, -1.0f, 1.0f); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); -} - -void RenderGL() -{ - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glLoadIdentity(); - - glColor3f(1.0f, 1.0f, 1.0f); - - glPushMatrix(); - glTranslatef(-4.0f, 4.0f, 0.0f); - - glActiveTextureARB(GL_TEXTURE0_ARB); - glEnable(GL_TEXTURE_2D); - glActiveTextureARB(GL_TEXTURE1_ARB); - glDisable(GL_TEXTURE_2D); - - glBegin(GL_QUADS); - { - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); - glVertex2f(-2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); - glVertex2f(2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); - glVertex2f(2.0f, 2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); - glVertex2f(-2.0f, 2.0f); - } - glEnd(); - - glPopMatrix(); - glPushMatrix(); - glTranslatef( 4.0f, 4.0f, 0.0f); - - glActiveTextureARB(GL_TEXTURE0_ARB); - glDisable(GL_TEXTURE_2D); - glActiveTextureARB(GL_TEXTURE1_ARB); - glEnable(GL_TEXTURE_2D); - - glBegin(GL_QUADS); - { - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); - glVertex2f(-2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); - glVertex2f(2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); - glVertex2f(2.0f, 2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); - glVertex2f(-2.0f, 2.0f); - } - glEnd(); - - glPopMatrix(); - glPushMatrix(); - glTranslatef( 0.0f, -4.0f, 0.0f); - - glActiveTextureARB(GL_TEXTURE0_ARB); - glEnable(GL_TEXTURE_2D); - glActiveTextureARB(GL_TEXTURE1_ARB); - glEnable(GL_TEXTURE_2D); - - glBegin(GL_QUADS); - { - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 1.0f); - glVertex2f(-2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 1.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 1.0f); - glVertex2f(2.0f, -2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 1.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 1.0f, 0.0f); - glVertex2f(2.0f, 2.0f); - glMultiTexCoord2f(GL_TEXTURE0_ARB, 0.0f, 0.0f); - glMultiTexCoord2f(GL_TEXTURE1_ARB, 0.0f, 0.0f); - glVertex2f(-2.0f, 2.0f); - } - glEnd(); - - glPopMatrix(); - - glFlush(); -} - - int main() { CLogger(); @@ -322,24 +149,15 @@ int main() SDL_WM_SetCaption("Texture Test", "Texture Test"); - - #if DEV Gfx::CGLDevice *device = new Gfx::CGLDevice(); device->Create(); Init(device); - #else - InitGL(); - #endif bool done = false; while (! done) { - #if DEV Render(device); - #else - RenderGL(); - #endif SDL_GL_SwapBuffers(); @@ -348,12 +166,10 @@ int main() if (event.type == SDL_QUIT) done = true; - usleep(50000); + usleep(10000); } - #if DEV device->Destroy(); - #endif SDL_FreeSurface(surface); -- cgit v1.2.3-1-g7c22 From 54f4da87923465a5387e2e854b58616647deb7af Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sun, 15 Jul 2012 19:17:49 +0200 Subject: Fix in model loading; simple model viewer - fixed model loading code - added simple model viewer (model_test) in src/graphics/opengl/test - added system time stamp code - split the code in app/system modules to separate headers - added debug messages in model loading - minor fixes in OpenGL engine --- src/graphics/opengl/test/CMakeLists.txt | 42 +++- src/graphics/opengl/test/README.txt | 7 + src/graphics/opengl/test/model_test.cpp | 385 ++++++++++++++++++++++++++++++ src/graphics/opengl/test/texture_test.cpp | 1 + 4 files changed, 430 insertions(+), 5 deletions(-) create mode 100644 src/graphics/opengl/test/README.txt create mode 100644 src/graphics/opengl/test/model_test.cpp (limited to 'src/graphics/opengl/test') diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt index ce2a83f..36bd738 100644 --- a/src/graphics/opengl/test/CMakeLists.txt +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -8,16 +8,46 @@ find_package(PNG REQUIRED) set(CMAKE_BUILD_TYPE debug) set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0") - -set(SOURCES +if (${CMAKE_SYSTEM_NAME} MATCHES "Windows") + set(PLATFORM_WINDOWS 1) + set(PLATFORM_LINUX 0) + set(PLATFORM_OTHER 0) +elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") + set(PLATFORM_WINDOWS 0) + set(PLATFORM_LINUX 1) + set(PLATFORM_OTHER 0) +else() + set(PLATFORM_WINDOWS 0) + set(PLATFORM_LINUX 0) + set(PLATFORM_OTHER 1) +endif() + +configure_file(../../../common/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/common/config.h) + + +set(TEXTURE_SOURCES ../gldevice.cpp ../../common/device.cpp +../../common/texture.cpp ../../../common/logger.cpp ../../../common/image.cpp texture_test.cpp ) -include_directories(../../../) +set(MODEL_SOURCES +../gldevice.cpp +../../common/device.cpp +../../common/modelfile.cpp +../../common/texture.cpp +../../../common/logger.cpp +../../../common/image.cpp +../../../common/iman.cpp +../../../common/stringutils.cpp +../../../app/system.cpp +model_test.cpp +) + +include_directories(../../../ ${CMAKE_CURRENT_BINARY_DIR}) set(LIBS ${SDL_LIBRARY} @@ -26,6 +56,8 @@ ${OPENGL_LIBRARY} ${PNG_LIBRARIES} ) -add_executable(texture_test ${SOURCES}) - +add_executable(texture_test ${TEXTURE_SOURCES}) target_link_libraries(texture_test ${LIBS}) + +add_executable(model_test ${MODEL_SOURCES}) +target_link_libraries(model_test ${LIBS}) diff --git a/src/graphics/opengl/test/README.txt b/src/graphics/opengl/test/README.txt new file mode 100644 index 0000000..c309f23 --- /dev/null +++ b/src/graphics/opengl/test/README.txt @@ -0,0 +1,7 @@ +Test programs for OpenGL engine: + - texture_test -> multitexturing test with 2 textures (included as files: ./tex1.png, ./tex2.png) + - model_test -> simple model viewer to test model loading + usage: ./model_test {dxf|mod} model_file + second argument is the loaded format (DXF or Colobot .mod files) + requires ./tex folder (or symlink) with Colobot textures + viewer is controlled from keyboard - the bindings can be found in code diff --git a/src/graphics/opengl/test/model_test.cpp b/src/graphics/opengl/test/model_test.cpp new file mode 100644 index 0000000..b73dc71 --- /dev/null +++ b/src/graphics/opengl/test/model_test.cpp @@ -0,0 +1,385 @@ +#include "app/system.h" +#include "common/logger.h" +#include "common/image.h" +#include "common/iman.h" +#include "graphics/common/modelfile.h" +#include "graphics/opengl/gldevice.h" +#include "math/geometry.h" + +#include +#include +#include + +#include +#include + +enum KeySlots +{ + K_RotXUp, + K_RotXDown, + K_RotYLeft, + K_RotYRight, + K_Forward, + K_Back, + K_Left, + K_Right, + K_Up, + K_Down, + K_Count +}; +bool KEYMAP[K_Count] = { false }; + +Math::Vector TRANSLATION(0.0f, 0.0f, 30.0f); +Math::Vector ROTATION; + +const int FRAME_DELAY = 5000; + +std::map TEXS; + +SystemTimeStamp *PREV_TIME = NULL, *CURR_TIME = NULL; + +Gfx::Texture* GetTexture(const std::string &name) +{ + std::map::iterator it = TEXS.find(name); + if (it == TEXS.end()) + return NULL; + + return (*it).second; +} + +void LoadTexture(Gfx::CGLDevice *device, const std::string &name) +{ + if (name.empty()) + return; + + if (GetTexture(name) != NULL) + return; + + Gfx::Texture *tex = NULL; + + CImage img; + if (! img.Load(std::string("tex/") + name)) + { + std::string err = img.GetError(); + GetLogger()->Error("Texture not loaded, error: %s!\n", err.c_str()); + } + else + { + Gfx::TextureCreateParams texCreateParams; + texCreateParams.alpha = false; + texCreateParams.mipmap = true; + texCreateParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; + texCreateParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; + texCreateParams.wrapT = Gfx::TEX_WRAP_CLAMP; + + tex = device->CreateTexture(&img, texCreateParams); + } + + TEXS[name] = tex; +} + +void Init(Gfx::CGLDevice *device, Gfx::CModelFile *model) +{ + std::vector &triangles = model->GetTriangles(); + + for (int i = 0; i < (int) triangles.size(); ++i) + { + LoadTexture(device, triangles[i].tex1Name); + LoadTexture(device, triangles[i].tex2Name); + } + + device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); + device->SetRenderState(Gfx::RENDER_STATE_LIGHTING, true); + device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, true); + device->SetShadeModel(Gfx::SHADE_SMOOTH); + + Gfx::Light light; + light.type = Gfx::LIGHT_DIRECTIONAL; + light.ambient = Gfx::Color(0.4f, 0.4f, 0.4f, 0.0f); + light.diffuse = Gfx::Color(0.8f, 0.8f, 0.8f, 0.0f); + light.specular = Gfx::Color(0.2f, 0.2f, 0.2f, 0.0f); + light.position = Math::Vector(0.0f, 0.0f, -1.0f); + light.direction = Math::Vector(0.0f, 0.0f, 1.0f); + + device->SetGlobalAmbient(Gfx::Color(0.5f, 0.5f, 0.5f, 0.0f)); + device->SetLight(0, light); + device->SetLightEnabled(0, true); +} + +void Render(Gfx::CGLDevice *device, Gfx::CModelFile *modelFile) +{ + device->BeginScene(); + + Math::Matrix persp; + Math::LoadProjectionMatrix(persp, Math::PI / 4.0f, (600.0f) / (800.0f), 0.1f, 100.0f); + device->SetTransform(Gfx::TRANSFORM_PROJECTION, persp); + + Math::Matrix id; + id.LoadIdentity(); + device->SetTransform(Gfx::TRANSFORM_WORLD, id); + + Math::Matrix viewMat; + Math::LoadTranslationMatrix(viewMat, TRANSLATION); + Math::Matrix rot; + Math::LoadRotationXZYMatrix(rot, ROTATION); + viewMat = Math::MultiplyMatrices(viewMat, rot); + device->SetTransform(Gfx::TRANSFORM_VIEW, viewMat); + + std::vector &triangles = modelFile->GetTriangles(); + + Gfx::VertexTex2 tri[3]; + + for (int i = 0; i < (int) triangles.size(); ++i) + { + device->SetTexture(0, GetTexture(triangles[i].tex1Name)); + device->SetTexture(1, GetTexture(triangles[i].tex2Name)); + device->SetTextureEnabled(0, true); + device->SetTextureEnabled(1, true); + + Gfx::TextureParams tex1Params; + tex1Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + tex1Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + device->SetTextureParams(0, tex1Params); + + Gfx::TextureParams tex2Params; + tex2Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + tex2Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + device->SetTextureParams(1, tex2Params); + + device->SetMaterial(triangles[i].material); + + tri[0] = triangles[i].p1; + tri[1] = triangles[i].p2; + tri[2] = triangles[i].p3; + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, tri, 3); + } + + device->EndScene(); +} + +void Update() +{ + const float ROT_SPEED = 80.0f * Math::DEG_TO_RAD; // rad / sec + const float TRANS_SPEED = 3.0f; // units / sec + + GetCurrentTimeStamp(CURR_TIME); + float timeDiff = TimeStampDiff(PREV_TIME, CURR_TIME, STU_SEC); + CopyTimeStamp(PREV_TIME, CURR_TIME); + + if (KEYMAP[K_RotYLeft]) + ROTATION.y -= ROT_SPEED * timeDiff; + if (KEYMAP[K_RotYRight]) + ROTATION.y += ROT_SPEED * timeDiff; + if (KEYMAP[K_RotXDown]) + ROTATION.x -= ROT_SPEED * timeDiff; + if (KEYMAP[K_RotXUp]) + ROTATION.x += ROT_SPEED * timeDiff; + + if (KEYMAP[K_Forward]) + TRANSLATION.z -= TRANS_SPEED * timeDiff; + if (KEYMAP[K_Back]) + TRANSLATION.z += TRANS_SPEED * timeDiff; + if (KEYMAP[K_Left]) + TRANSLATION.x += TRANS_SPEED * timeDiff; + if (KEYMAP[K_Right]) + TRANSLATION.x -= TRANS_SPEED * timeDiff; + if (KEYMAP[K_Up]) + TRANSLATION.y += TRANS_SPEED * timeDiff; + if (KEYMAP[K_Down]) + TRANSLATION.y -= TRANS_SPEED * timeDiff; +} + +void KeyboardDown(SDLKey key) +{ + switch (key) + { + case SDLK_LEFT: + KEYMAP[K_RotYLeft] = true; + break; + case SDLK_RIGHT: + KEYMAP[K_RotYRight] = true; + break; + case SDLK_UP: + KEYMAP[K_RotXUp] = true; + break; + case SDLK_DOWN: + KEYMAP[K_RotXDown] = true; + break; + case SDLK_w: + KEYMAP[K_Forward] = true; + break; + case SDLK_s: + KEYMAP[K_Back] = true; + break; + case SDLK_a: + KEYMAP[K_Left] = true; + break; + case SDLK_d: + KEYMAP[K_Right] = true; + break; + case SDLK_z: + KEYMAP[K_Down] = true; + break; + case SDLK_x: + KEYMAP[K_Up] = true; + break; + default: + break; + } +} + +void KeyboardUp(SDLKey key) +{ + switch (key) + { + case SDLK_LEFT: + KEYMAP[K_RotYLeft] = false; + break; + case SDLK_RIGHT: + KEYMAP[K_RotYRight] = false; + break; + case SDLK_UP: + KEYMAP[K_RotXUp] = false; + break; + case SDLK_DOWN: + KEYMAP[K_RotXDown] = false; + break; + case SDLK_w: + KEYMAP[K_Forward] = false; + break; + case SDLK_s: + KEYMAP[K_Back] = false; + break; + case SDLK_a: + KEYMAP[K_Left] = false; + break; + case SDLK_d: + KEYMAP[K_Right] = false; + break; + case SDLK_z: + KEYMAP[K_Down] = false; + break; + case SDLK_x: + KEYMAP[K_Up] = false; + break; + default: + break; + } +} + +int main(int argc, char *argv[]) +{ + CLogger logger; + + PREV_TIME = CreateTimeStamp(); + CURR_TIME = CreateTimeStamp(); + + GetCurrentTimeStamp(PREV_TIME); + GetCurrentTimeStamp(CURR_TIME); + + if (argc != 3) + { + std::cerr << "Usage: " << argv[0] << "{mod|dxf} model_file" << std::endl; + return 1; + } + + CInstanceManager iMan; + + Gfx::CModelFile *modelFile = new Gfx::CModelFile(&iMan); + if (std::string(argv[1]) == "mod") + { + if (! modelFile->ReadModel(argv[2], false, false)) + { + std::cerr << "Error reading MOD: " << modelFile->GetError() << std::endl; + return 1; + } + } + else if (std::string(argv[1]) == "dxf") + { + if (! modelFile->ReadDXF(argv[2], 0.0f, 0.0f)) + { + std::cerr << "Error reading DXF: " << modelFile->GetError() << std::endl; + return 1; + } + } + else + { + std::cerr << "Usage: " << argv[0] << "{mod|dxf} model_file" << std::endl; + return 1; + } + + // Without any error checking, for simplicity + + SDL_Init(SDL_INIT_VIDEO); + + IMG_Init(IMG_INIT_PNG); + + const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); + + Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE; + + if (videoInfo->hw_available) + videoFlags |= SDL_HWSURFACE; + else + videoFlags |= SDL_SWSURFACE; + + if (videoInfo->blit_hw) + videoFlags |= SDL_HWACCEL; + + + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + SDL_Surface *surface = SDL_SetVideoMode(800, 600, 32, videoFlags); + + + SDL_WM_SetCaption("Model Test", "Model Test"); + + Gfx::CGLDevice *device = new Gfx::CGLDevice(); + device->Create(); + + Init(device, modelFile); + + bool done = false; + while (! done) + { + Render(device, modelFile); + Update(); + + SDL_GL_SwapBuffers(); + + SDL_Event event; + SDL_PollEvent(&event); + if (event.type == SDL_QUIT) + done = true; + else if (event.type == SDL_KEYDOWN) + KeyboardDown(event.key.keysym.sym); + else if (event.type == SDL_KEYUP) + KeyboardUp(event.key.keysym.sym); + + usleep(FRAME_DELAY); + } + + delete modelFile; + + device->Destroy(); + delete device; + + SDL_FreeSurface(surface); + + IMG_Quit(); + + SDL_Quit(); + + DestroyTimeStamp(PREV_TIME); + DestroyTimeStamp(CURR_TIME); + + return 0; +} diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp index 764e127..79518f8 100644 --- a/src/graphics/opengl/test/texture_test.cpp +++ b/src/graphics/opengl/test/texture_test.cpp @@ -170,6 +170,7 @@ int main() } device->Destroy(); + delete device; SDL_FreeSurface(surface); -- cgit v1.2.3-1-g7c22 From 68a7bafe37adef0e5ef12c2d0e8461a21e05363b Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Mon, 16 Jul 2012 19:17:26 +0200 Subject: Fixes in texture loading - added other texture formats: BGR and BGRA - fixed texture loading in model viewer - moved code from texture.cpp module to texture.h --- src/graphics/opengl/test/CMakeLists.txt | 6 ++++-- src/graphics/opengl/test/model_test.cpp | 5 ++++- src/graphics/opengl/test/texture_test.cpp | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) (limited to 'src/graphics/opengl/test') diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt index 36bd738..4242c77 100644 --- a/src/graphics/opengl/test/CMakeLists.txt +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -8,6 +8,8 @@ find_package(PNG REQUIRED) set(CMAKE_BUILD_TYPE debug) set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0") +set(ADD_LIBS "") + if (${CMAKE_SYSTEM_NAME} MATCHES "Windows") set(PLATFORM_WINDOWS 1) set(PLATFORM_LINUX 0) @@ -16,6 +18,7 @@ elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") set(PLATFORM_WINDOWS 0) set(PLATFORM_LINUX 1) set(PLATFORM_OTHER 0) + set(ADD_LIBS "-lrt") else() set(PLATFORM_WINDOWS 0) set(PLATFORM_LINUX 0) @@ -28,7 +31,6 @@ configure_file(../../../common/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/common set(TEXTURE_SOURCES ../gldevice.cpp ../../common/device.cpp -../../common/texture.cpp ../../../common/logger.cpp ../../../common/image.cpp texture_test.cpp @@ -38,7 +40,6 @@ set(MODEL_SOURCES ../gldevice.cpp ../../common/device.cpp ../../common/modelfile.cpp -../../common/texture.cpp ../../../common/logger.cpp ../../../common/image.cpp ../../../common/iman.cpp @@ -54,6 +55,7 @@ ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY} ${OPENGL_LIBRARY} ${PNG_LIBRARIES} +${ADD_LIBS} ) add_executable(texture_test ${TEXTURE_SOURCES}) diff --git a/src/graphics/opengl/test/model_test.cpp b/src/graphics/opengl/test/model_test.cpp index b73dc71..88404b7 100644 --- a/src/graphics/opengl/test/model_test.cpp +++ b/src/graphics/opengl/test/model_test.cpp @@ -66,8 +66,11 @@ void LoadTexture(Gfx::CGLDevice *device, const std::string &name) else { Gfx::TextureCreateParams texCreateParams; - texCreateParams.alpha = false; texCreateParams.mipmap = true; + if (img.GetData()->surface->format->Amask == 0) + texCreateParams.format = Gfx::TEX_IMG_BGR; + else + texCreateParams.format = Gfx::TEX_IMG_BGRA; texCreateParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; texCreateParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; texCreateParams.wrapT = Gfx::TEX_WRAP_CLAMP; diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp index 79518f8..03847fc 100644 --- a/src/graphics/opengl/test/texture_test.cpp +++ b/src/graphics/opengl/test/texture_test.cpp @@ -27,15 +27,15 @@ void Init(Gfx::CGLDevice *device) } Gfx::TextureCreateParams tex1CreateParams; - tex1CreateParams.alpha = true; tex1CreateParams.mipmap = true; + tex1CreateParams.format = Gfx::TEX_IMG_RGBA; tex1CreateParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; tex1CreateParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; tex1CreateParams.wrapT = Gfx::TEX_WRAP_CLAMP; Gfx::TextureCreateParams tex2CreateParams; - tex2CreateParams.alpha = true; tex2CreateParams.mipmap = true; + tex2CreateParams.format = Gfx::TEX_IMG_RGBA; tex2CreateParams.minFilter = Gfx::TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST; tex2CreateParams.magFilter = Gfx::TEX_MAG_FILTER_NEAREST; tex2CreateParams.wrapS = Gfx::TEX_WRAP_CLAMP; -- cgit v1.2.3-1-g7c22 From f364f378cf497faf61d78aadd8f1aebce678c0ec Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 18 Jul 2012 19:08:34 +0200 Subject: Fixed OpenGL transformations - fixed wrong order of transformations - added transform_test --- src/graphics/opengl/test/CMakeLists.txt | 13 ++ src/graphics/opengl/test/README.txt | 1 + src/graphics/opengl/test/transform_test.cpp | 339 ++++++++++++++++++++++++++++ 3 files changed, 353 insertions(+) create mode 100644 src/graphics/opengl/test/transform_test.cpp (limited to 'src/graphics/opengl/test') diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt index 4242c77..58c4714 100644 --- a/src/graphics/opengl/test/CMakeLists.txt +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -48,6 +48,16 @@ set(MODEL_SOURCES model_test.cpp ) +set(TRANSFORM_SOURCES +../gldevice.cpp +../../common/device.cpp +../../../common/logger.cpp +../../../common/image.cpp +../../../common/iman.cpp +../../../app/system.cpp +transform_test.cpp +) + include_directories(../../../ ${CMAKE_CURRENT_BINARY_DIR}) set(LIBS @@ -63,3 +73,6 @@ target_link_libraries(texture_test ${LIBS}) add_executable(model_test ${MODEL_SOURCES}) target_link_libraries(model_test ${LIBS}) + +add_executable(transform_test ${TRANSFORM_SOURCES}) +target_link_libraries(transform_test ${LIBS}) diff --git a/src/graphics/opengl/test/README.txt b/src/graphics/opengl/test/README.txt index c309f23..fe6f1d7 100644 --- a/src/graphics/opengl/test/README.txt +++ b/src/graphics/opengl/test/README.txt @@ -5,3 +5,4 @@ Test programs for OpenGL engine: second argument is the loaded format (DXF or Colobot .mod files) requires ./tex folder (or symlink) with Colobot textures viewer is controlled from keyboard - the bindings can be found in code + - transform_test -> simple "walk around" test for world & view transformations diff --git a/src/graphics/opengl/test/transform_test.cpp b/src/graphics/opengl/test/transform_test.cpp new file mode 100644 index 0000000..5982c4e --- /dev/null +++ b/src/graphics/opengl/test/transform_test.cpp @@ -0,0 +1,339 @@ +#include "app/system.h" +#include "common/logger.h" +#include "common/image.h" +#include "common/iman.h" +#include "graphics/opengl/gldevice.h" +#include "math/geometry.h" + +#include +#include +#include + +#include +#include + +enum KeySlots +{ + K_Forward, + K_Back, + K_Left, + K_Right, + K_Up, + K_Down, + K_Count +}; +bool KEYMAP[K_Count] = { false }; + +Math::Point MOUSE_POS_BASE; + +Math::Vector TRANSLATION(0.0f, 2.0f, 0.0f); +Math::Vector ROTATION, ROTATION_BASE; + +const int FRAME_DELAY = 5000; + +SystemTimeStamp *PREV_TIME = NULL, *CURR_TIME = NULL; + +void Init(Gfx::CGLDevice *device) +{ + device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, true); + device->SetShadeModel(Gfx::SHADE_SMOOTH); +} + +void Render(Gfx::CGLDevice *device) +{ + device->BeginScene(); + + Math::Matrix persp; + Math::LoadProjectionMatrix(persp, Math::PI / 4.0f, (600.0f) / (800.0f), 0.1f, 100.0f); + device->SetTransform(Gfx::TRANSFORM_PROJECTION, persp); + + + Math::Matrix viewMat; + Math::Matrix mat; + + viewMat.LoadIdentity(); + + Math::LoadRotationXMatrix(mat, -ROTATION.x); + viewMat = Math::MultiplyMatrices(viewMat, mat); + + Math::LoadRotationYMatrix(mat, -ROTATION.y); + viewMat = Math::MultiplyMatrices(viewMat, mat); + + Math::LoadTranslationMatrix(mat, -TRANSLATION); + viewMat = Math::MultiplyMatrices(viewMat, mat); + + device->SetTransform(Gfx::TRANSFORM_VIEW, viewMat); + + + Math::Matrix worldMat; + worldMat.LoadIdentity(); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + Gfx::VertexCol line[2] = { Gfx::VertexCol() }; + + for (int x = -40; x <= 40; ++x) + { + line[0].color = Gfx::Color(0.7f + x / 120.0f, 0.0f, 0.0f); + line[0].coord.z = -40; + line[0].coord.x = x; + line[1].color = Gfx::Color(0.7f + x / 120.0f, 0.0f, 0.0f); + line[1].coord.z = 40; + line[1].coord.x = x; + device->DrawPrimitive(Gfx::PRIMITIVE_LINES, line, 2); + } + + for (int z = -40; z <= 40; ++z) + { + line[0].color = Gfx::Color(0.0f, 0.7f + z / 120.0f, 0.0f); + line[0].coord.z = z; + line[0].coord.x = -40; + line[1].color = Gfx::Color(0.0f, 0.7f + z / 120.0f, 0.0f); + line[1].coord.z = z; + line[1].coord.x = 40; + device->DrawPrimitive(Gfx::PRIMITIVE_LINES, line, 2); + } + + + Gfx::VertexCol quad[6] = { Gfx::VertexCol() }; + + for (int i = 0; i < 6; ++i) + quad[i].color = Gfx::Color(1.0f, 1.0f, 0.0f); + + quad[0].coord = Math::Vector(-1.0f, -1.0f, 0.0f); + quad[1].coord = Math::Vector( 1.0f, -1.0f, 0.0f); + quad[2].coord = Math::Vector( 1.0f, 1.0f, 0.0f); + quad[3].coord = Math::Vector( 1.0f, 1.0f, 0.0f); + quad[4].coord = Math::Vector(-1.0f, 1.0f, 0.0f); + quad[5].coord = Math::Vector(-1.0f, -1.0f, 0.0f); + + Math::LoadTranslationMatrix(worldMat, Math::Vector(40.0f, 2.0f, 40.0f)); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); + + for (int i = 0; i < 6; ++i) + quad[i].color = Gfx::Color(0.0f, 1.0f, 1.0f); + + Math::LoadTranslationMatrix(worldMat, Math::Vector(-40.0f, 2.0f, -40.0f)); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); + + for (int i = 0; i < 6; ++i) + quad[i].color = Gfx::Color(1.0f, 0.0f, 1.0f); + + Math::LoadTranslationMatrix(worldMat, Math::Vector(0.0f, 10.0f, 0.0f)); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); + + device->EndScene(); +} + +void Update() +{ + const float TRANS_SPEED = 6.0f; // units / sec + + GetCurrentTimeStamp(CURR_TIME); + float timeDiff = TimeStampDiff(PREV_TIME, CURR_TIME, STU_SEC); + CopyTimeStamp(PREV_TIME, CURR_TIME); + + Math::Vector incTrans; + + if (KEYMAP[K_Forward]) + incTrans.z = +TRANS_SPEED * timeDiff; + if (KEYMAP[K_Back]) + incTrans.z = -TRANS_SPEED * timeDiff; + if (KEYMAP[K_Right]) + incTrans.x = +TRANS_SPEED * timeDiff; + if (KEYMAP[K_Left]) + incTrans.x = -TRANS_SPEED * timeDiff; + if (KEYMAP[K_Up]) + incTrans.y = +TRANS_SPEED * timeDiff; + if (KEYMAP[K_Down]) + incTrans.y = -TRANS_SPEED * timeDiff; + + Math::Point rotTrans = Math::RotatePoint(-ROTATION.y, Math::Point(incTrans.x, incTrans.z)); + incTrans.x = rotTrans.x; + incTrans.z = rotTrans.y; + TRANSLATION += incTrans; +} + +void KeyboardDown(SDLKey key) +{ + switch (key) + { + case SDLK_w: + KEYMAP[K_Forward] = true; + break; + case SDLK_s: + KEYMAP[K_Back] = true; + break; + case SDLK_d: + KEYMAP[K_Right] = true; + break; + case SDLK_a: + KEYMAP[K_Left] = true; + break; + case SDLK_z: + KEYMAP[K_Down] = true; + break; + case SDLK_x: + KEYMAP[K_Up] = true; + break; + default: + break; + } +} + +void KeyboardUp(SDLKey key) +{ + switch (key) + { + case SDLK_w: + KEYMAP[K_Forward] = false; + break; + case SDLK_s: + KEYMAP[K_Back] = false; + break; + case SDLK_d: + KEYMAP[K_Right] = false; + break; + case SDLK_a: + KEYMAP[K_Left] = false; + break; + case SDLK_z: + KEYMAP[K_Down] = false; + break; + case SDLK_x: + KEYMAP[K_Up] = false; + break; + default: + break; + } +} + +void MouseMove(int x, int y) +{ + Math::Point currentPos((float)x, (float)y); + + static bool first = true; + if (first || (x < 10) || (y < 10) || (x > 790) || (y > 590)) + { + SDL_WarpMouse(400, 300); + MOUSE_POS_BASE.x = 400; + MOUSE_POS_BASE.y = 300; + ROTATION_BASE = ROTATION; + first = false; + return; + } + + ROTATION.y = ROTATION_BASE.y + ((float) (x - MOUSE_POS_BASE.x) / 800.0f) * Math::PI; + ROTATION.x = ROTATION_BASE.x + ((float) (y - MOUSE_POS_BASE.y) / 600.0f) * Math::PI; +} + +int main(int argc, char *argv[]) +{ + CLogger logger; + + PREV_TIME = CreateTimeStamp(); + CURR_TIME = CreateTimeStamp(); + + GetCurrentTimeStamp(PREV_TIME); + GetCurrentTimeStamp(CURR_TIME); + + CInstanceManager iMan; + + // Without any error checking, for simplicity + + SDL_Init(SDL_INIT_VIDEO); + + IMG_Init(IMG_INIT_PNG); + + const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); + + Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE; + + if (videoInfo->hw_available) + videoFlags |= SDL_HWSURFACE; + else + videoFlags |= SDL_SWSURFACE; + + if (videoInfo->blit_hw) + videoFlags |= SDL_HWACCEL; + + + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + SDL_Surface *surface = SDL_SetVideoMode(800, 600, 32, videoFlags); + + + SDL_WM_SetCaption("Transform Test", "Transform Test"); + + //SDL_WM_GrabInput(SDL_GRAB_ON); + SDL_ShowCursor(SDL_DISABLE); + + Gfx::CGLDevice *device = new Gfx::CGLDevice(); + device->Create(); + + Init(device); + + bool done = false; + while (! done) + { + Render(device); + Update(); + + SDL_GL_SwapBuffers(); + + SDL_Event event; + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) + { + break; + done = true; + } + else if (event.type == SDL_KEYDOWN) + { + if (event.key.keysym.sym == SDLK_q) + { + done = true; + break; + } + else + KeyboardDown(event.key.keysym.sym); + } + else if (event.type == SDL_KEYUP) + KeyboardUp(event.key.keysym.sym); + else if (event.type == SDL_MOUSEMOTION) + MouseMove(event.motion.x, event.motion.y); + } + + usleep(FRAME_DELAY); + } + + //SDL_WM_GrabInput(SDL_GRAB_OFF); + SDL_ShowCursor(SDL_ENABLE); + + device->Destroy(); + delete device; + + SDL_FreeSurface(surface); + + IMG_Quit(); + + SDL_Quit(); + + DestroyTimeStamp(PREV_TIME); + DestroyTimeStamp(CURR_TIME); + + return 0; +} -- cgit v1.2.3-1-g7c22 From 8797569d33c4917eb8f8a1dc2341aac7b5815315 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Sun, 22 Jul 2012 22:05:12 +0200 Subject: Texture & mouse functions; refactoring & fixes - cleaned up and added documentation to engine.h - refactored CEngine interface and associated structs - added mouse handling functions in CApplication & CEngine - fixed bugs in projection matrix setting - changed texture loading & handling - added const-values in CDevice & CGLDevice - changed event management in CApplication - other minor changes & bugfixes --- src/graphics/opengl/test/model_test.cpp | 16 ++++++++-------- src/graphics/opengl/test/texture_test.cpp | 12 ++++++------ src/graphics/opengl/test/transform_test.cpp | 6 ++++-- 3 files changed, 18 insertions(+), 16 deletions(-) (limited to 'src/graphics/opengl/test') diff --git a/src/graphics/opengl/test/model_test.cpp b/src/graphics/opengl/test/model_test.cpp index 88404b7..3aad1b6 100644 --- a/src/graphics/opengl/test/model_test.cpp +++ b/src/graphics/opengl/test/model_test.cpp @@ -34,15 +34,15 @@ Math::Vector ROTATION; const int FRAME_DELAY = 5000; -std::map TEXS; +std::map TEXS; SystemTimeStamp *PREV_TIME = NULL, *CURR_TIME = NULL; -Gfx::Texture* GetTexture(const std::string &name) +Gfx::Texture GetTexture(const std::string &name) { - std::map::iterator it = TEXS.find(name); + std::map::iterator it = TEXS.find(name); if (it == TEXS.end()) - return NULL; + return Gfx::Texture(); return (*it).second; } @@ -52,10 +52,10 @@ void LoadTexture(Gfx::CGLDevice *device, const std::string &name) if (name.empty()) return; - if (GetTexture(name) != NULL) - return; + Gfx::Texture tex = GetTexture(name); - Gfx::Texture *tex = NULL; + if (tex.valid) + return; CImage img; if (! img.Load(std::string("tex/") + name)) @@ -114,7 +114,7 @@ void Render(Gfx::CGLDevice *device, Gfx::CModelFile *modelFile) device->BeginScene(); Math::Matrix persp; - Math::LoadProjectionMatrix(persp, Math::PI / 4.0f, (600.0f) / (800.0f), 0.1f, 100.0f); + Math::LoadProjectionMatrix(persp, Math::PI / 4.0f, (800.0f) / (600.0f), 0.1f, 100.0f); device->SetTransform(Gfx::TRANSFORM_PROJECTION, persp); Math::Matrix id; diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp index 03847fc..66ed770 100644 --- a/src/graphics/opengl/test/texture_test.cpp +++ b/src/graphics/opengl/test/texture_test.cpp @@ -40,8 +40,8 @@ void Init(Gfx::CGLDevice *device) tex2CreateParams.magFilter = Gfx::TEX_MAG_FILTER_NEAREST; tex2CreateParams.wrapS = Gfx::TEX_WRAP_CLAMP; - Gfx::Texture* tex1 = device->CreateTexture(&img1, tex1CreateParams); - Gfx::Texture* tex2 = device->CreateTexture(&img2, tex2CreateParams); + Gfx::Texture tex1 = device->CreateTexture(&img1, tex1CreateParams); + Gfx::Texture tex2 = device->CreateTexture(&img2, tex2CreateParams); device->SetTexture(0, tex1); device->SetTexture(1, tex2); @@ -75,13 +75,13 @@ void Render(Gfx::CGLDevice *device) static Gfx::VertexTex2 quad[] = { - Gfx::VertexTex2(Math::Vector(-2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 1.0f), Math::Point(0.0f, 1.0f)), - Gfx::VertexTex2(Math::Vector( 2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 1.0f), Math::Point(1.0f, 1.0f)), + Gfx::VertexTex2(Math::Vector(-2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f)), Gfx::VertexTex2(Math::Vector( 2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 0.0f), Math::Point(1.0f, 0.0f)), + Gfx::VertexTex2(Math::Vector( 2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 1.0f), Math::Point(1.0f, 1.0f)), - Gfx::VertexTex2(Math::Vector( 2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 0.0f), Math::Point(1.0f, 0.0f)), - Gfx::VertexTex2(Math::Vector(-2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f)), + Gfx::VertexTex2(Math::Vector( 2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(1.0f, 1.0f), Math::Point(1.0f, 1.0f)), Gfx::VertexTex2(Math::Vector(-2.0f, -2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 1.0f), Math::Point(0.0f, 1.0f)), + Gfx::VertexTex2(Math::Vector(-2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f)), }; Math::Matrix t; diff --git a/src/graphics/opengl/test/transform_test.cpp b/src/graphics/opengl/test/transform_test.cpp index 5982c4e..4decaa0 100644 --- a/src/graphics/opengl/test/transform_test.cpp +++ b/src/graphics/opengl/test/transform_test.cpp @@ -38,13 +38,13 @@ void Init(Gfx::CGLDevice *device) device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, true); device->SetShadeModel(Gfx::SHADE_SMOOTH); } - +#include void Render(Gfx::CGLDevice *device) { device->BeginScene(); Math::Matrix persp; - Math::LoadProjectionMatrix(persp, Math::PI / 4.0f, (600.0f) / (800.0f), 0.1f, 100.0f); + Math::LoadProjectionMatrix(persp, Math::PI / 4.0f, (800.0f) / (600.0f), 0.1f, 100.0f); device->SetTransform(Gfx::TRANSFORM_PROJECTION, persp); @@ -71,6 +71,8 @@ void Render(Gfx::CGLDevice *device) Gfx::VertexCol line[2] = { Gfx::VertexCol() }; + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + for (int x = -40; x <= 40; ++x) { line[0].color = Gfx::Color(0.7f + x / 120.0f, 0.0f, 0.0f); -- cgit v1.2.3-1-g7c22 From 9d592045317ca66be415e60ba4c2db90b5d7ad3e Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Mon, 23 Jul 2012 21:41:27 +0200 Subject: Cursor drawing - fixed cursor drawing in CEngine - changed event loop to generate more events --- src/graphics/opengl/test/transform_test.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/graphics/opengl/test') diff --git a/src/graphics/opengl/test/transform_test.cpp b/src/graphics/opengl/test/transform_test.cpp index 4decaa0..83819b8 100644 --- a/src/graphics/opengl/test/transform_test.cpp +++ b/src/graphics/opengl/test/transform_test.cpp @@ -38,7 +38,7 @@ void Init(Gfx::CGLDevice *device) device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, true); device->SetShadeModel(Gfx::SHADE_SMOOTH); } -#include + void Render(Gfx::CGLDevice *device) { device->BeginScene(); @@ -71,8 +71,6 @@ void Render(Gfx::CGLDevice *device) Gfx::VertexCol line[2] = { Gfx::VertexCol() }; - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); - for (int x = -40; x <= 40; ++x) { line[0].color = Gfx::Color(0.7f + x / 120.0f, 0.0f, 0.0f); -- cgit v1.2.3-1-g7c22 From 42963b341f3fbc055c494a0dc0c97d395fa69562 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 25 Jul 2012 00:27:01 +0200 Subject: Refactoring of texture code - refactored texture structs & functions - added note about OpenGL extensions - removed device.cpp as unnecessary - minor changes in CEngine --- src/graphics/opengl/test/model_test.cpp | 11 --------- src/graphics/opengl/test/texture_test.cpp | 37 ++++++++++++++++--------------- 2 files changed, 19 insertions(+), 29 deletions(-) (limited to 'src/graphics/opengl/test') diff --git a/src/graphics/opengl/test/model_test.cpp b/src/graphics/opengl/test/model_test.cpp index 3aad1b6..3e3d100 100644 --- a/src/graphics/opengl/test/model_test.cpp +++ b/src/graphics/opengl/test/model_test.cpp @@ -73,7 +73,6 @@ void LoadTexture(Gfx::CGLDevice *device, const std::string &name) texCreateParams.format = Gfx::TEX_IMG_BGRA; texCreateParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; texCreateParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; - texCreateParams.wrapT = Gfx::TEX_WRAP_CLAMP; tex = device->CreateTexture(&img, texCreateParams); } @@ -139,16 +138,6 @@ void Render(Gfx::CGLDevice *device, Gfx::CModelFile *modelFile) device->SetTextureEnabled(0, true); device->SetTextureEnabled(1, true); - Gfx::TextureParams tex1Params; - tex1Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - tex1Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; - device->SetTextureParams(0, tex1Params); - - Gfx::TextureParams tex2Params; - tex2Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - tex2Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; - device->SetTextureParams(1, tex2Params); - device->SetMaterial(triangles[i].material); tri[0] = triangles[i].p1; diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp index 66ed770..aa9817e 100644 --- a/src/graphics/opengl/test/texture_test.cpp +++ b/src/graphics/opengl/test/texture_test.cpp @@ -10,9 +10,14 @@ void Init(Gfx::CGLDevice *device) { - device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, false); device->SetShadeModel(Gfx::SHADE_SMOOTH); + device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, false); + device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); + + device->SetTextureEnabled(0, true); + device->SetTextureEnabled(1, true); + CImage img1; if (! img1.Load("tex1.png")) { @@ -31,32 +36,18 @@ void Init(Gfx::CGLDevice *device) tex1CreateParams.format = Gfx::TEX_IMG_RGBA; tex1CreateParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; tex1CreateParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; - tex1CreateParams.wrapT = Gfx::TEX_WRAP_CLAMP; Gfx::TextureCreateParams tex2CreateParams; tex2CreateParams.mipmap = true; tex2CreateParams.format = Gfx::TEX_IMG_RGBA; tex2CreateParams.minFilter = Gfx::TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST; tex2CreateParams.magFilter = Gfx::TEX_MAG_FILTER_NEAREST; - tex2CreateParams.wrapS = Gfx::TEX_WRAP_CLAMP; Gfx::Texture tex1 = device->CreateTexture(&img1, tex1CreateParams); Gfx::Texture tex2 = device->CreateTexture(&img2, tex2CreateParams); device->SetTexture(0, tex1); device->SetTexture(1, tex2); - - Gfx::TextureParams tex1Params; - tex1Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - tex1Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; - device->SetTextureParams(0, tex1Params); - - Gfx::TextureParams tex2Params; - tex2Params.alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - tex2Params.colorOperation = Gfx::TEX_MIX_OPER_MODULATE; - device->SetTextureParams(1, tex2Params); - - device->SetRenderState(Gfx::RENDER_STATE_TEXTURING, true); } void Render(Gfx::CGLDevice *device) @@ -84,6 +75,16 @@ void Render(Gfx::CGLDevice *device) Gfx::VertexTex2(Math::Vector(-2.0f, 2.0f, 0.0f), Math::Vector(), Math::Point(0.0f, 0.0f), Math::Point(0.0f, 0.0f)), }; + Gfx::TextureStageParams tex1StageParams; + tex1StageParams.colorOperation = Gfx::TEX_MIX_OPER_DEFAULT; + tex1StageParams.alphaOperation = Gfx::TEX_MIX_OPER_DEFAULT; + device->SetTextureStageParams(0, tex1StageParams); + + Gfx::TextureStageParams tex2StageParams; + tex2StageParams.colorOperation = Gfx::TEX_MIX_OPER_DEFAULT; + tex2StageParams.alphaOperation = Gfx::TEX_MIX_OPER_DEFAULT; + device->SetTextureStageParams(1, tex2StageParams); + Math::Matrix t; Math::LoadTranslationMatrix(t, Math::Vector(-4.0f, 4.0f, 0.0f)); device->SetTransform(Gfx::TRANSFORM_VIEW, t); @@ -101,12 +102,12 @@ void Render(Gfx::CGLDevice *device) device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); - device->SetTextureEnabled(0, true); - device->SetTextureEnabled(1, true); - Math::LoadTranslationMatrix(t, Math::Vector( 0.0f, -4.0f, 0.0f)); device->SetTransform(Gfx::TRANSFORM_VIEW, t); + device->SetTextureEnabled(0, true); + device->SetTextureEnabled(1, true); + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); device->EndScene(); -- cgit v1.2.3-1-g7c22 From bc1c9b5284c10d8aafc04bcd6c3597b3626a782f Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Wed, 25 Jul 2012 20:27:13 +0200 Subject: Fixed bug in texturing --- src/graphics/opengl/test/CMakeLists.txt | 3 --- src/graphics/opengl/test/texture_test.cpp | 10 ++++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) (limited to 'src/graphics/opengl/test') diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt index 58c4714..f49fbac 100644 --- a/src/graphics/opengl/test/CMakeLists.txt +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -30,7 +30,6 @@ configure_file(../../../common/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/common set(TEXTURE_SOURCES ../gldevice.cpp -../../common/device.cpp ../../../common/logger.cpp ../../../common/image.cpp texture_test.cpp @@ -38,7 +37,6 @@ texture_test.cpp set(MODEL_SOURCES ../gldevice.cpp -../../common/device.cpp ../../common/modelfile.cpp ../../../common/logger.cpp ../../../common/image.cpp @@ -50,7 +48,6 @@ model_test.cpp set(TRANSFORM_SOURCES ../gldevice.cpp -../../common/device.cpp ../../../common/logger.cpp ../../../common/image.cpp ../../../common/iman.cpp diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp index aa9817e..c3c568b 100644 --- a/src/graphics/opengl/test/texture_test.cpp +++ b/src/graphics/opengl/test/texture_test.cpp @@ -108,6 +108,16 @@ void Render(Gfx::CGLDevice *device) device->SetTextureEnabled(0, true); device->SetTextureEnabled(1, true); + tex1StageParams.colorOperation = Gfx::TEX_MIX_OPER_DEFAULT; + tex1StageParams.alphaOperation = Gfx::TEX_MIX_OPER_DEFAULT; + device->SetTextureStageParams(0, tex1StageParams); + + tex2StageParams.colorOperation = Gfx::TEX_MIX_OPER_ADD; + tex2StageParams.colorArg1 = Gfx::TEX_MIX_ARG_COMPUTED_COLOR; + tex2StageParams.colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; + tex2StageParams.alphaOperation = Gfx::TEX_MIX_OPER_DEFAULT; + device->SetTextureStageParams(1, tex2StageParams); + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLES, quad, 6); device->EndScene(); -- cgit v1.2.3-1-g7c22 From d1fe0d2dcc30a76055d6423805f8e0108b4b64be Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 26 Jul 2012 19:05:09 +0200 Subject: Lighting - fixed problems with lighting - added light_test --- src/graphics/opengl/test/CMakeLists.txt | 12 + src/graphics/opengl/test/light_test.cpp | 437 ++++++++++++++++++++++++++++++++ 2 files changed, 449 insertions(+) create mode 100644 src/graphics/opengl/test/light_test.cpp (limited to 'src/graphics/opengl/test') diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt index f49fbac..793e858 100644 --- a/src/graphics/opengl/test/CMakeLists.txt +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -55,6 +55,15 @@ set(TRANSFORM_SOURCES transform_test.cpp ) +set(LIGHT_SOURCES +../gldevice.cpp +../../../common/logger.cpp +../../../common/image.cpp +../../../common/iman.cpp +../../../app/system.cpp +light_test.cpp +) + include_directories(../../../ ${CMAKE_CURRENT_BINARY_DIR}) set(LIBS @@ -73,3 +82,6 @@ target_link_libraries(model_test ${LIBS}) add_executable(transform_test ${TRANSFORM_SOURCES}) target_link_libraries(transform_test ${LIBS}) + +add_executable(light_test ${LIGHT_SOURCES}) +target_link_libraries(light_test ${LIBS}) \ No newline at end of file diff --git a/src/graphics/opengl/test/light_test.cpp b/src/graphics/opengl/test/light_test.cpp new file mode 100644 index 0000000..80fa911 --- /dev/null +++ b/src/graphics/opengl/test/light_test.cpp @@ -0,0 +1,437 @@ +#include "app/system.h" +#include "common/logger.h" +#include "common/image.h" +#include "common/iman.h" +#include "graphics/opengl/gldevice.h" +#include "math/geometry.h" + +#include +#include +#include + +#include +#include + +enum KeySlots +{ + K_Forward, + K_Back, + K_Left, + K_Right, + K_Up, + K_Down, + K_Count +}; +bool KEYMAP[K_Count] = { false }; + +Math::Point MOUSE_POS_BASE; + +Math::Vector TRANSLATION(0.0f, 2.0f, 0.0f); +Math::Vector ROTATION, ROTATION_BASE; + +float CUBE_ORBIT = 0.0f; + +const int FRAME_DELAY = 5000; + +SystemTimeStamp *PREV_TIME = NULL, *CURR_TIME = NULL; + +void Init(Gfx::CGLDevice *device) +{ + device->SetRenderState(Gfx::RENDER_STATE_DEPTH_TEST, true); + device->SetShadeModel(Gfx::SHADE_SMOOTH); +} + +void Render(Gfx::CGLDevice *device) +{ + device->BeginScene(); + + /* Unlit part of scene */ + + device->SetRenderState(Gfx::RENDER_STATE_LIGHTING, false); + device->SetRenderState(Gfx::RENDER_STATE_CULLING, false); // Double-sided drawing + + Math::Matrix persp; + Math::LoadProjectionMatrix(persp, Math::PI / 4.0f, (800.0f) / (600.0f), 0.1f, 100.0f); + device->SetTransform(Gfx::TRANSFORM_PROJECTION, persp); + + + Math::Matrix viewMat; + Math::Matrix mat; + + viewMat.LoadIdentity(); + + Math::LoadRotationXMatrix(mat, -ROTATION.x); + viewMat = Math::MultiplyMatrices(viewMat, mat); + + Math::LoadRotationYMatrix(mat, -ROTATION.y); + viewMat = Math::MultiplyMatrices(viewMat, mat); + + Math::LoadTranslationMatrix(mat, -TRANSLATION); + viewMat = Math::MultiplyMatrices(viewMat, mat); + + device->SetTransform(Gfx::TRANSFORM_VIEW, viewMat); + + Math::Matrix worldMat; + worldMat.LoadIdentity(); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + Gfx::VertexCol line[2] = { Gfx::VertexCol() }; + + for (int x = -40; x <= 40; ++x) + { + line[0].color = Gfx::Color(0.7f + x / 120.0f, 0.0f, 0.0f); + line[0].coord.z = -40; + line[0].coord.x = x; + line[1].color = Gfx::Color(0.7f + x / 120.0f, 0.0f, 0.0f); + line[1].coord.z = 40; + line[1].coord.x = x; + device->DrawPrimitive(Gfx::PRIMITIVE_LINES, line, 2); + } + + for (int z = -40; z <= 40; ++z) + { + line[0].color = Gfx::Color(0.0f, 0.7f + z / 120.0f, 0.0f); + line[0].coord.z = z; + line[0].coord.x = -40; + line[1].color = Gfx::Color(0.0f, 0.7f + z / 120.0f, 0.0f); + line[1].coord.z = z; + line[1].coord.x = 40; + device->DrawPrimitive(Gfx::PRIMITIVE_LINES, line, 2); + } + + + Gfx::VertexCol quad[6] = { Gfx::VertexCol() }; + + quad[0].coord = Math::Vector(-1.0f, -1.0f, 0.0f); + quad[1].coord = Math::Vector( 1.0f, -1.0f, 0.0f); + quad[2].coord = Math::Vector(-1.0f, 1.0f, 0.0f); + quad[3].coord = Math::Vector( 1.0f, 1.0f, 0.0f); + + for (int i = 0; i < 6; ++i) + quad[i].color = Gfx::Color(1.0f, 1.0f, 0.0f); + + Math::LoadTranslationMatrix(worldMat, Math::Vector(40.0f, 2.0f, 40.0f)); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, quad, 4); + + for (int i = 0; i < 6; ++i) + quad[i].color = Gfx::Color(0.0f, 1.0f, 1.0f); + + Math::LoadTranslationMatrix(worldMat, Math::Vector(-40.0f, 2.0f, -40.0f)); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, quad, 4); + + for (int i = 0; i < 6; ++i) + quad[i].color = Gfx::Color(1.0f, 0.0f, 1.0f); + + Math::LoadTranslationMatrix(worldMat, Math::Vector(10.0f, 4.5f, 5.0f)); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, quad, 4); + + /* Moving lit cube */ + device->SetRenderState(Gfx::RENDER_STATE_LIGHTING, true); + device->SetRenderState(Gfx::RENDER_STATE_CULLING, true); // Culling (CCW faces) + + device->SetGlobalAmbient(Gfx::Color(0.4f, 0.4f, 0.4f)); + + Gfx::Light light1; + light1.type = Gfx::LIGHT_POINT; + light1.position = Math::Vector(10.0f, 4.5f, 5.0f); + light1.ambient = Gfx::Color(0.2f, 0.2f, 0.2f); + light1.diffuse = Gfx::Color(1.0f, 0.1f, 0.1f); + light1.specular = Gfx::Color(0.0f, 0.0f, 0.0f); + device->SetLight(0, light1); + device->SetLightEnabled(0, true); + + /*Gfx::Light light2; + device->SetLight(1, light2); + device->SetLightEnabled(1, true);*/ + + Gfx::Material material; + material.ambient = Gfx::Color(0.3f, 0.3f, 0.3f); + material.diffuse = Gfx::Color(0.8f, 0.7f, 0.6f); + material.specular = Gfx::Color(0.0f, 0.0f, 0.0f); + device->SetMaterial(material); + + const Gfx::Vertex cube[6][4] = + { + { + // Front + Gfx::Vertex(Math::Vector(-1.0f, -1.0f, -1.0f), Math::Vector( 0.0f, 0.0f, -1.0f)), + Gfx::Vertex(Math::Vector( 1.0f, -1.0f, -1.0f), Math::Vector( 0.0f, 0.0f, -1.0f)), + Gfx::Vertex(Math::Vector(-1.0f, 1.0f, -1.0f), Math::Vector( 0.0f, 0.0f, -1.0f)), + Gfx::Vertex(Math::Vector( 1.0f, 1.0f, -1.0f), Math::Vector( 0.0f, 0.0f, -1.0f)) + }, + + { + // Back + Gfx::Vertex(Math::Vector( 1.0f, -1.0f, 1.0f), Math::Vector( 0.0f, 0.0f, 1.0f)), + Gfx::Vertex(Math::Vector(-1.0f, -1.0f, 1.0f), Math::Vector( 0.0f, 0.0f, 1.0f)), + Gfx::Vertex(Math::Vector( 1.0f, 1.0f, 1.0f), Math::Vector( 0.0f, 0.0f, 1.0f)), + Gfx::Vertex(Math::Vector(-1.0f, 1.0f, 1.0f), Math::Vector( 0.0f, 0.0f, 1.0f)) + }, + + { + // Top + Gfx::Vertex(Math::Vector(-1.0f, 1.0f, -1.0f), Math::Vector( 0.0f, 1.0f, 0.0f)), + Gfx::Vertex(Math::Vector( 1.0f, 1.0f, -1.0f), Math::Vector( 0.0f, 1.0f, 0.0f)), + Gfx::Vertex(Math::Vector(-1.0f, 1.0f, 1.0f), Math::Vector( 0.0f, 1.0f, 0.0f)), + Gfx::Vertex(Math::Vector( 1.0f, 1.0f, 1.0f), Math::Vector( 0.0f, 1.0f, 0.0f)) + }, + + { + // Bottom + Gfx::Vertex(Math::Vector(-1.0f, -1.0f, 1.0f), Math::Vector( 0.0f, -1.0f, 0.0f)), + Gfx::Vertex(Math::Vector( 1.0f, -1.0f, 1.0f), Math::Vector( 0.0f, -1.0f, 0.0f)), + Gfx::Vertex(Math::Vector(-1.0f, -1.0f, -1.0f), Math::Vector( 0.0f, -1.0f, 0.0f)), + Gfx::Vertex(Math::Vector( 1.0f, -1.0f, -1.0f), Math::Vector( 0.0f, -1.0f, 0.0f)) + }, + + { + // Left + Gfx::Vertex(Math::Vector(-1.0f, -1.0f, 1.0f), Math::Vector(-1.0f, 0.0f, 0.0f)), + Gfx::Vertex(Math::Vector(-1.0f, -1.0f, -1.0f), Math::Vector(-1.0f, 0.0f, 0.0f)), + Gfx::Vertex(Math::Vector(-1.0f, 1.0f, 1.0f), Math::Vector(-1.0f, 0.0f, 0.0f)), + Gfx::Vertex(Math::Vector(-1.0f, 1.0f, -1.0f), Math::Vector(-1.0f, 0.0f, 0.0f)) + }, + + { + // Right + Gfx::Vertex(Math::Vector( 1.0f, -1.0f, -1.0f), Math::Vector( 1.0f, 0.0f, 0.0f)), + Gfx::Vertex(Math::Vector( 1.0f, -1.0f, 1.0f), Math::Vector( 1.0f, 0.0f, 0.0f)), + Gfx::Vertex(Math::Vector( 1.0f, 1.0f, -1.0f), Math::Vector( 1.0f, 0.0f, 0.0f)), + Gfx::Vertex(Math::Vector( 1.0f, 1.0f, 1.0f), Math::Vector( 1.0f, 0.0f, 0.0f)) + } + }; + + Math::Matrix cubeTrans; + Math::LoadTranslationMatrix(cubeTrans, Math::Vector(10.0f, 2.0f, 5.0f)); + Math::Matrix cubeRot; + Math::LoadRotationMatrix(cubeRot, Math::Vector(0.0f, 1.0f, 0.0f), CUBE_ORBIT); + Math::Matrix cubeRotInv; + Math::LoadRotationMatrix(cubeRotInv, Math::Vector(0.0f, 1.0f, 0.0f), -CUBE_ORBIT); + Math::Matrix cubeTransRad; + Math::LoadTranslationMatrix(cubeTransRad, Math::Vector(0.0f, 0.0f, 6.0f)); + worldMat = Math::MultiplyMatrices(cubeTransRad, cubeRotInv); + worldMat = Math::MultiplyMatrices(cubeRot, worldMat); + worldMat = Math::MultiplyMatrices(cubeTrans, worldMat); + device->SetTransform(Gfx::TRANSFORM_WORLD, worldMat); + + for (int i = 0; i < 6; ++i) + device->DrawPrimitive(Gfx::PRIMITIVE_TRIANGLE_STRIP, cube[i], 4); + + device->EndScene(); +} + +void Update() +{ + const float TRANS_SPEED = 6.0f; // units / sec + + GetCurrentTimeStamp(CURR_TIME); + float timeDiff = TimeStampDiff(PREV_TIME, CURR_TIME, STU_SEC); + CopyTimeStamp(PREV_TIME, CURR_TIME); + + CUBE_ORBIT += timeDiff * (Math::PI / 4.0f); + + Math::Vector incTrans; + + if (KEYMAP[K_Forward]) + incTrans.z = +TRANS_SPEED * timeDiff; + if (KEYMAP[K_Back]) + incTrans.z = -TRANS_SPEED * timeDiff; + if (KEYMAP[K_Right]) + incTrans.x = +TRANS_SPEED * timeDiff; + if (KEYMAP[K_Left]) + incTrans.x = -TRANS_SPEED * timeDiff; + if (KEYMAP[K_Up]) + incTrans.y = +TRANS_SPEED * timeDiff; + if (KEYMAP[K_Down]) + incTrans.y = -TRANS_SPEED * timeDiff; + + Math::Point rotTrans = Math::RotatePoint(-ROTATION.y, Math::Point(incTrans.x, incTrans.z)); + incTrans.x = rotTrans.x; + incTrans.z = rotTrans.y; + TRANSLATION += incTrans; +} + +void KeyboardDown(SDLKey key) +{ + switch (key) + { + case SDLK_w: + KEYMAP[K_Forward] = true; + break; + case SDLK_s: + KEYMAP[K_Back] = true; + break; + case SDLK_d: + KEYMAP[K_Right] = true; + break; + case SDLK_a: + KEYMAP[K_Left] = true; + break; + case SDLK_z: + KEYMAP[K_Down] = true; + break; + case SDLK_x: + KEYMAP[K_Up] = true; + break; + default: + break; + } +} + +void KeyboardUp(SDLKey key) +{ + switch (key) + { + case SDLK_w: + KEYMAP[K_Forward] = false; + break; + case SDLK_s: + KEYMAP[K_Back] = false; + break; + case SDLK_d: + KEYMAP[K_Right] = false; + break; + case SDLK_a: + KEYMAP[K_Left] = false; + break; + case SDLK_z: + KEYMAP[K_Down] = false; + break; + case SDLK_x: + KEYMAP[K_Up] = false; + break; + default: + break; + } +} + +void MouseMove(int x, int y) +{ + Math::Point currentPos((float)x, (float)y); + + static bool first = true; + if (first || (x < 10) || (y < 10) || (x > 790) || (y > 590)) + { + SDL_WarpMouse(400, 300); + MOUSE_POS_BASE.x = 400; + MOUSE_POS_BASE.y = 300; + ROTATION_BASE = ROTATION; + first = false; + return; + } + + ROTATION.y = ROTATION_BASE.y + ((float) (x - MOUSE_POS_BASE.x) / 800.0f) * Math::PI; + ROTATION.x = ROTATION_BASE.x + ((float) (y - MOUSE_POS_BASE.y) / 600.0f) * Math::PI; +} + +int main(int argc, char *argv[]) +{ + CLogger logger; + + PREV_TIME = CreateTimeStamp(); + CURR_TIME = CreateTimeStamp(); + + GetCurrentTimeStamp(PREV_TIME); + GetCurrentTimeStamp(CURR_TIME); + + CInstanceManager iMan; + + // Without any error checking, for simplicity + + SDL_Init(SDL_INIT_VIDEO); + + IMG_Init(IMG_INIT_PNG); + + const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); + + Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE; + + if (videoInfo->hw_available) + videoFlags |= SDL_HWSURFACE; + else + videoFlags |= SDL_SWSURFACE; + + if (videoInfo->blit_hw) + videoFlags |= SDL_HWACCEL; + + + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + SDL_Surface *surface = SDL_SetVideoMode(800, 600, 32, videoFlags); + + + SDL_WM_SetCaption("Light Test", "Light Test"); + + //SDL_WM_GrabInput(SDL_GRAB_ON); + SDL_ShowCursor(SDL_DISABLE); + + Gfx::CGLDevice *device = new Gfx::CGLDevice(); + device->Create(); + + Init(device); + + bool done = false; + while (! done) + { + Render(device); + Update(); + + SDL_GL_SwapBuffers(); + + SDL_Event event; + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) + { + break; + done = true; + } + else if (event.type == SDL_KEYDOWN) + { + if (event.key.keysym.sym == SDLK_q) + { + done = true; + break; + } + else + KeyboardDown(event.key.keysym.sym); + } + else if (event.type == SDL_KEYUP) + KeyboardUp(event.key.keysym.sym); + else if (event.type == SDL_MOUSEMOTION) + MouseMove(event.motion.x, event.motion.y); + } + + usleep(FRAME_DELAY); + } + + //SDL_WM_GrabInput(SDL_GRAB_OFF); + SDL_ShowCursor(SDL_ENABLE); + + device->Destroy(); + delete device; + + SDL_FreeSurface(surface); + + IMG_Quit(); + + SDL_Quit(); + + DestroyTimeStamp(PREV_TIME); + DestroyTimeStamp(CURR_TIME); + + return 0; +} -- cgit v1.2.3-1-g7c22 From 045f17a274c0cd41aebd34d5759f7fe791b680e4 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 26 Jul 2012 21:35:04 +0200 Subject: Dynamic light manager - rewrote old CLight as CLightManager --- src/graphics/opengl/test/README.txt | 1 + 1 file changed, 1 insertion(+) (limited to 'src/graphics/opengl/test') diff --git a/src/graphics/opengl/test/README.txt b/src/graphics/opengl/test/README.txt index fe6f1d7..c618415 100644 --- a/src/graphics/opengl/test/README.txt +++ b/src/graphics/opengl/test/README.txt @@ -6,3 +6,4 @@ Test programs for OpenGL engine: requires ./tex folder (or symlink) with Colobot textures viewer is controlled from keyboard - the bindings can be found in code - transform_test -> simple "walk around" test for world & view transformations + - light test -> test for lighting -- cgit v1.2.3-1-g7c22 From 4ddcd9f810fa588ccf90442f7b4e5ddf385e85f2 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 26 Jul 2012 22:26:19 +0200 Subject: Change of paths in src/graphics - moved abstract core to src/graphics/core - moved proper graphics engine to src/graphics/engine --- src/graphics/opengl/test/CMakeLists.txt | 4 ++-- src/graphics/opengl/test/model_test.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/graphics/opengl/test') diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt index 793e858..8ed7364 100644 --- a/src/graphics/opengl/test/CMakeLists.txt +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -37,7 +37,7 @@ texture_test.cpp set(MODEL_SOURCES ../gldevice.cpp -../../common/modelfile.cpp +../../engine/modelfile.cpp ../../../common/logger.cpp ../../../common/image.cpp ../../../common/iman.cpp @@ -84,4 +84,4 @@ add_executable(transform_test ${TRANSFORM_SOURCES}) target_link_libraries(transform_test ${LIBS}) add_executable(light_test ${LIGHT_SOURCES}) -target_link_libraries(light_test ${LIBS}) \ No newline at end of file +target_link_libraries(light_test ${LIBS}) diff --git a/src/graphics/opengl/test/model_test.cpp b/src/graphics/opengl/test/model_test.cpp index 3e3d100..3e8efe6 100644 --- a/src/graphics/opengl/test/model_test.cpp +++ b/src/graphics/opengl/test/model_test.cpp @@ -2,7 +2,7 @@ #include "common/logger.h" #include "common/image.h" #include "common/iman.h" -#include "graphics/common/modelfile.h" +#include "graphics/engine/modelfile.h" #include "graphics/opengl/gldevice.h" #include "math/geometry.h" -- cgit v1.2.3-1-g7c22