Hi I am having so much trouble in applying post effects when you have 3 cameras active in one frame.
First camera - which renders game play section of the game.
Second Camera - GUI layer.
Third Camera - Back Ground and animations.
Here I am trying to apply post processing effects like blur and bloom on the first camera, Which works fine but here comes the issue while post effects are enabled GUI, Back Ground animations are not visible but i am sure they are drawn.
And if I give all the cameras as input render targets then i can see all the objects in that scene.
Not sure why this is happening can anyone please explain me what could be the reason thank you.
Here I am Adding the Opengl Call log to see what exactly i am doing.
Problem is at Stats_layer and HudPage_layer they are not displayed after the post effects are enabled.
Marker: SceneBegin: ClearCamera
glBindFramebufferEXT(GL_FRAMEBUFFER, 2)
glClearColor(0, 0, 0, 1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, {1})
glMatrixMode(GL_MODELVIEW)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
glMatrixMode(GL_PROJECTION)
glLoadMatrixf({2.7457316, 0, 0, 0} {0, 0, 1.0001999, 1} {0, 1.8304877, 0, 0} {0, 0, -0.20001999, 0})
glMatrixMode(GL_MODELVIEW)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
Marker: RenderGroup: ClearCamera - Opaque
Marker: RenderGroup: ClearCamera - Immediate
Marker: RenderGroup: ClearCamera - Transparent
Marker: RenderGroup: ClearCamera - Overlay
Marker: PostProcess: PMaterial_GodRays_RadialBlur__2
glBindFramebufferEXT(GL_FRAMEBUFFER, 2)
glMatrixMode(GL_MODELVIEW)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
glMatrixMode(GL_PROJECTION)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
glMatrixMode(GL_MODELVIEW)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
glColor4f(0, 0, 0, 1)
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, {0, 0, 0, 1})
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, {0, 0, 0, 1})
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, {0, 0, 0, 1})
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0)
glActiveTexture(GL_TEXTURE0)
glDisable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, 0)
glMatrixMode(GL_TEXTURE)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
glUniform2fv(0, 1, {0.5, 0.49836433})
glUniform1fv(1, 1, {0.050000001})
glActiveTexture(GL_TEXTURE1)
glBindTexture(GL_TEXTURE_2D, 88) [Context 1 - Texture 88: Context 1 - Texture 88]
glUniform1i(2, 1)
glBindBuffer(GL_ARRAY_BUFFER, 163)
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointer(3, GL_FLOAT, 32, 0x00000000)
glEnableClientState(GL_NORMAL_ARRAY)
glNormalPointer(GL_FLOAT, 32, 0x0000000C)
glClientActiveTexture(GL_TEXTURE0)
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
glTexCoordPointer(2, GL_FLOAT, 32, 0x00000018)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, FALSE, 32, 0x00000000)
glEnableVertexAttribArray(1)
glVertexAttribPointer(1, 2, GL_FLOAT, FALSE, 32, 0x00000018)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 164)
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0x00000000)
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
glClientActiveTexture(GL_TEXTURE0)
glDisableClientState(GL_TEXTURE_COORD_ARRAY)
glDisableVertexAttribArray(0)
glDisableVertexAttribArray(1)
Marker: SceneBegin: ClearCamera
glBindFramebufferEXT(GL_FRAMEBUFFER, 3)
glClearColor(0, 0, 0, 1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, {1})
glMatrixMode(GL_MODELVIEW)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
glMatrixMode(GL_PROJECTION)
glLoadMatrixf({2.7457316, 0, 0, 0} {0, 0, 1.0001999, 1} {0, 1.8304877, 0, 0} {0, 0, -0.20001999, 0})
glMatrixMode(GL_MODELVIEW)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
Marker: RenderGroup: ClearCamera - Opaque
Marker: RenderGroup: ClearCamera - Immediate
Marker: RenderGroup: ClearCamera - Transparent
Marker: RenderGroup: ClearCamera - Overlay
Marker: PostProcess: PMaterial_GodRays_RadialBlur__3
glBindFramebufferEXT(GL_FRAMEBUFFER, 3)
glMatrixMode(GL_MODELVIEW)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
glMatrixMode(GL_PROJECTION)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
glMatrixMode(GL_MODELVIEW)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
glColor4f(0, 0, 0, 1)
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, {0, 0, 0, 1})
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, {0, 0, 0, 1})
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, {0, 0, 0, 1})
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0)
glActiveTexture(GL_TEXTURE0)
glDisable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, 0)
glMatrixMode(GL_TEXTURE)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
glUniform2fv(0, 1, {0.5, 0.49836433})
glUniform1fv(1, 1, {0.1})
glActiveTexture(GL_TEXTURE1)
glBindTexture(GL_TEXTURE_2D, 87) [Context 1 - Texture 87: Context 1 - Texture 87]
glUniform1i(2, 1)
glBindBuffer(GL_ARRAY_BUFFER, 163)
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointer(3, GL_FLOAT, 32, 0x00000000)
glEnableClientState(GL_NORMAL_ARRAY)
glNormalPointer(GL_FLOAT, 32, 0x0000000C)
glClientActiveTexture(GL_TEXTURE0)
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
glTexCoordPointer(2, GL_FLOAT, 32, 0x00000018)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, FALSE, 32, 0x00000000)
glEnableVertexAttribArray(1)
glVertexAttribPointer(1, 2, GL_FLOAT, FALSE, 32, 0x00000018)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 164)
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0x00000000)
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
glClientActiveTexture(GL_TEXTURE0)
glDisableClientState(GL_TEXTURE_COORD_ARRAY)
glDisableVertexAttribArray(0)
glDisableVertexAttribArray(1)
Marker: SceneBegin: STATS_LAYER
wglMakeCurrent(0x8C0119F2, 0x00010000)
glBindFramebufferEXT(GL_FRAMEBUFFER, 0)
glViewport(0, 0, 640, 960)
glScissor(0, 0, 640, 960)
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, {1})
glMatrixMode(GL_MODELVIEW)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
glMatrixMode(GL_PROJECTION)
glLoadMatrixf({1, 0, 0, 0} {0, 0, 0.0066666668, 0} {0, 0.66666669, 0, 0} {0, 0, -0.33333334, 1})
glMatrixMode(GL_MODELVIEW)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
Marker: RenderGroup: STATS_LAYER - Opaque
Marker: RenderGroup: STATS_LAYER - Immediate
Marker: RenderGroup: STATS_LAYER - Transparent
Marker: RenderGroup: STATS_LAYER - Overlay
Marker: SceneBegin: HUDPAGE_LAYER
wglMakeCurrent(0x8C0119F2, 0x00010000)
glBindFramebufferEXT(GL_FRAMEBUFFER, 0)
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, {1})
glMatrixMode(GL_MODELVIEW)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
glMatrixMode(GL_PROJECTION)
glLoadMatrixf({1, 0, 0, 0} {0, 0, 0.0066666668, 0} {0, 0.66666669, 0, 0} {0, 0, -0.33333334, 1})
glMatrixMode(GL_MODELVIEW)
glLoadMatrixf({1, 0, 0, 0} {0, 1, 0, 0} {0, 0, 1, 0} {0, 0, 0, 1})
Marker: RenderGroup: HUDPAGE_LAYER - Opaque
Marker: RenderGroup: HUDPAGE_LAYER - Immediate
Marker: RenderGroup: HUDPAGE_LAYER - Transparent
The Problem with the above question is with the way i am drawing objects once/before the post process is applied.
For example if i have 2 passes:
Opaque.
Transparent.
then i should be drawing Opaque objects in Opaque pass and transparent ones in the later one.
But instead for some reason(Noob me!) I am drawing opaque objects in a transparent pass.
I had to do a lot of debugging to found the root issue.
I guess if we want to do graphics we have put things in a proper order before you start adding effects and stuff.
Learnt a lesson though(Hope i could post an answer with the code soon by making a sample project explaining the situation).
Related
// Vertices information
GLfloat vertices[] = { 1, 1, 1, -1, 1, 1, -1,-1, 1, 1,-1, 1, // (front)
1, 1, 1, 1,-1, 1, 1,-1,-1, 1, 1,-1, // (right)
1, 1, 1, 1, 1,-1, -1, 1,-1, -1, 1, 1, // (top)
-1, 1, 1, -1, 1,-1, -1,-1,-1, -1,-1, 1, // (left)
-1,-1,-1, 1,-1,-1, 1,-1, 1, -1,-1, 1, // (bottom)
1,-1,-1, -1,-1,-1, -1, 1,-1, 1, 1,-1 }; // (back)
// Normal information
GLfloat normals[72] = { 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // (front)
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // (right)
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // (top)
-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // (left)
0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, // (bottom)
0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1 }; // (back)
// drawing cube
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glNormalPointer(GL_FLOAT, 0, normals);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
glPopMatrix();
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
I want to make sure there is no color difference between the two sides.
Is there a solution for this?
The issue is causes, because you've activated the flat shading mode (see Detecting legacy OpenGL and glShadeModel).
Thus the light is computed for the vertices of the triangle primitive, but the color of the triangle is determined by the Provoking vertex.
Switch to the the smooth (GL_SMOOTH) shading model and ensure that the light is diffuse (GL_DIFFUSE parameter - see glLight):
glShadeModel(GL_SMOOTH);
See the difference between shading mode GL_FLAT (left) and GL_SMOOTH (right):
Another option would be to render GL_QUADS primitives instead of GL_TRIANGLES primitives:
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, indices)
glDrawArrays(GL_QUADS, 0, 24);
Again, GL_FLAT (left) and GL_SMOOTH (right):
I am currently trying to program Pacman in C++ using OpenGL. I am currently trying to create a display list to draw the maze. My thoughts were to create a square tile and then simply display said tile if matrix indicates a wall segment at some position. My code for this is:
const unsigned int FREE = 0;
const unsigned int WALL = 1;
const unsigned int GHOST_WALL = 2;
const unsigned int matrix[22][19] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 0, 1, 1, 2, 1, 1, 0, 1, 0, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1},
{1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1},
{1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1},
{1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
GLuint tile_handle = 0;
GLuint maze_handle = 0;
/*
* Initialize maze by precompiling its components
*/
void maze_init() {
// request a single display list handle for the tile
tile_handle = glGenLists(1);
glNewList(tile_handle, GL_COMPILE);
glColor3f(0, 0, 0.7f); // Blue
glBegin(GL_QUADS);
glVertex2i(0,0);
glVertex2i(1,0);
glVertex2i(1,1);
glVertex2i(0,1);
glEnd();
glEndList();
// request a single display list handle for the entire maze
maze_handle = glGenLists(1);
glNewList(maze_handle, GL_COMPILE);
glColor3f(0, 0, 0.7f); // Blue
for(int row = 0; row < 22; row--) {
for(int col = 0; col < 19; col++) {
if(matrix[21-row][col] == WALL) {
glPushMatrix();
glScaled(30, 30, 1);
glTranslated(col, row, 0);
glCallList(tile_handle);
glPopMatrix();
}
}
}
glEndList();
}
I then use maze_init() in my main initialisation method and use the call glCallList(maze_handle); in my main display function. This compiles fine but when I try to run it, it gives me a Bus error: 10 error.
Now I tried the same without the loops in the definition of glNewList(maze_handle, GL_COMPILE); and it ran fine. My question is therefore this: why do the loops (or the conditional statement) inside the call list create a bus error?
I tried a simple loop inside glNewList(maze_handle, GL_COMPILE); that made calls to DISTINCT display lists and it ran fine. So is the problem here that I make a repeated call to the same display list? For some reason I just can't seem to find the problem here ...
I am experimenting with doing bicubic interpolation of some gridded data using Eigen, and I can't figure out how to reshape the 16x1 column vector of coefficients into a 4x4 matrix. Ideally I would like to do something along the lines of https://bitbucket.org/eigen/eigen/pull-request/41/reshape/diff without any copying, but I can't make heads or tails of the docs. Alternatively, a map would be fine as well, but I can't figure out how to use a map on an already existing matrix.
More here: http://en.wikipedia.org/wiki/Bicubic_interpolation
/// The inverse of the A matrix for the bicubic interpolation
/// (http://en.wikipedia.org/wiki/Bicubic_interpolation)
static const double Ainv_data[16*16] = {
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-3, 3, 0, 0, -2, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, -2, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, -3, 3, 0, 0, -2, -1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 2, -2, 0, 0, 1, 1, 0, 0,
-3, 0, 3, 0, 0, 0, 0, 0, -2, 0, -1, 0, 0, 0, 0, 0,
0, 0, 0, 0, -3, 0, 3, 0, 0, 0, 0, 0, -2, 0, -1, 0,
9, -9, -9, 9, 6, 3, -6, -3, 6, -6, 3, -3, 4, 2, 2, 1,
-6, 6, 6, -6, -3, -3, 3, 3, -4, 4, -2, 2, -2, -2, -1, -1,
2, 0, -2, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 2, 0, -2, 0, 0, 0, 0, 0, 1, 0, 1, 0,
-6, 6, 6, -6, -4, -2, 4, 2, -3, 3, -3, 3, -2, -1, -2, -1,
4, -4, -4, 4, 2, 2, -2, -2, 2, -2, 2, -2, 1, 1, 1, 1};
Eigen::Matrix<double, 16, 16> Ainv(Ainv_data);
Eigen::Matrix<double, 16, 1> f;
f.setRandom();
Eigen::Matrix<double, 16, 1> alpha = Ainv*f;
// This next line works, but it is making a copy, right?
Eigen::Matrix<double, 4, 4> a(alpha.data());
The last line is indeed doing a copy, so you can use a Map as follow:
Map<Matrix4d,Eigen::Aligned> a(alpha.data());
a behaves like a Matrix4d and it is read-write. The Eigen::Aligned flag tells Eigen that the pointer you pass to Map is properly aligned for vectorization. The only difference with a pure Matrix4d is that the C++ type is not the same.
So I have a 2D array which acts as a map for my tiles to be drawn.
int sMap[12][20] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 2, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1},
{1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 2, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 1, 0, 1, 1, 2, 2, 2, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
Once my tiles have been loaded in, I use this function() to place the tiles:
for (int y = 0; y < 12; y++){
for (int x = 0; x < 20; x++){
if (sMap[y][x] == 1)
glBindTexture( GL_TEXTURE_2D, brick1);
else if (sMap[y][x] == 2)
glBindTexture( GL_TEXTURE_2D, brick2);
else
glBindTexture( GL_TEXTURE_2D, wall );
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x + offsetx), float(MAP_SIZEY - (y + offsety)), 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(float(x + 1 + offsetx), float(MAP_SIZEY - (y + offsety)), 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(float(x + 1 + offsetx), float(MAP_SIZEY - (y + 1 + offsety)), 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(float(x + offsetx), float(MAP_SIZEY - (y + 1 + offsety)), 0.0f);
glEnd();
}
}
I think I may have confused myself with the coordinate system of the tiles because when I draw a basic OpenGL square which acts as a sprite, I just get a black screen upon running the program.
I'm unsure whether this means the scale of the sprite to the tiles is wrong, or whether the sprite and tiles are on different Zplanes...
I would appreciate if someone could explain the coordinate system in case I don't understand it as much as I thought and also advise me how to draw an OpenGLsquare on the same coordinates.
Currently I have this to draw my basic sprite:
struct RECT{float x, y, w, h;};
RECT sprite = {0, 0, 10, 10};
void drawSprite (RECT rect){
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(rect.x, rect.y, 0.0);
glVertex3f(rect.x, rect.y+rect.h, 0.0);
glVertex3f(rect.x+rect.w, rect.y+rect.h, 0.0);
glVertex3f(rect.x+rect.w, rect.y, 0.0);
glEnd();
}
EDIT:
resize screen:
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,20.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
draw scene:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(10.0f, 8.0f, 20.0f, 10.0f, 8.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(5.0f,4.0f,0.0f);
draw_tiles();
draw_sprite();
In the draw_tiles function it looks like you might be passing incorrect coordinates - maybe you should be multiplying the x and y values by your tile size.
Also try turning off depth testing and backface culling to help resolve your black screen problem.
glDisable( GL_DEPTH_TEST );
glDisable( GL_CULL_FACE );
I have created a 2D array that represents a map for my tiles to be placed:
int sMap[12][20] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 2, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1},
{1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 2, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 1, 0, 1, 1, 2, 2, 2, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
Once my tiles have been loaded in, I use this function() to place the tiles:
for (int y = 0; y < 12; y++){ //for (int y = 11; y >= 0; y--){
for (int x = 0; x < 20; x++){ //for (int x = 19; x >= 0; x--){
if (sMap[y][x] == 1)
glBindTexture( GL_TEXTURE_2D, brick1);
else if (sMap[y][x] == 2)
glBindTexture( GL_TEXTURE_2D, brick2);
else
glBindTexture( GL_TEXTURE_2D, wall );
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x), float(y), 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(float(x + 1), float(y), 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(float(x + 1), float(y + 1), 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(float(x), float(y + 1), 0.0f);
glEnd();
}
}
For some reason, my Map is inverted... (So the bottom line of digits in the map above is the top line of tiles on screen and the second line up on the map is the second line of tiles down on screen).
Its my understanding, you iterate through selecting each digit and assign the correct tile. I've tried changing the x/y's but that rotates the map on screen so its 12 tiles on the x and 20 tiles on the y.
I want my tiles on screen to represent the map above. Any ideas how to fix this? Thanks.
Problem is in the fact that OpenGL actually has the coordinate system that point (0, 0) is in the lower-left part of the viewport. So if you want to represent that matrix in the code the same way as it should draw, you need to calculate y coordinate for drawing as <height of viewport> - (calculated y). How to get that height - it's up to you, probably you defined it earlier in the code.
For instance, float(y + 1 + offsety) will become float(HEIGHT - (y + 1 + offsety)).
EDIT
Oh, I just realized - you have that loop, so, you can put
for (int y = 11; y >= 0; y--){
instead existing start of outer loop and that's it. But, anyway, it's good to know that it is NOT accidentally inverted as you thought - it's just up to coordinate system.