Related
I can't see a cube on the screen.I located it with glTranslatef. I got it to stop somewhere with LoadIdentity. I wanted to get a camera by changing the Matrix Modes, but the cube does not appear.
Code:
#include <Windows.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#include <GLFW/glfw3.h>
#include <cstdio>
int width = 1280;
int height = 720;
GLfloat vertices[] = {
-1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1,
1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1,
-1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1,
-1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1
};
GLfloat colors[] =
{
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0
};
void keyboard(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS)
if (key == GLFW_KEY_ESCAPE)
glfwSetWindowShouldClose(window, GL_TRUE);
}
void drawCube() {
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colors);
glDrawArrays(GL_QUADS, 0, 24);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(width, height, "C++ OpenGL Test Area", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, keyboard);
glEnable(GL_DEPTH_TEST);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
glViewport(0, 0, width, height);
/* Render here */
glClearColor(0.0, 192/256, 1, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt(1, 1, 1, 0, 0, 0, 0, 1, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, 5);
drawCube();
glFlush();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
I thought it was because I couldn't draw the cube. I checked the codes again, but there is no problem drawing the cube.
The cube is not in the viewing volume. If you do not specify a projection matrix, the viewing volume is a unique cube around the camera position, aligned along the line of sight. Any geometry that is not in the viewing volume is clipped. You must set up a projection matrix. The projection matrix defines the volume in the scene that is projected onto the viewport. An orthographic projection matrix can be set with glOrtho. A perspective projection matrix can be set with glFrustum or gluPerspective. The perspective projection matrix defines a Viewing frustum. e.g.:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = (float)width / (float)height;
gluPerspective(90.0, aspect, 0.1, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(4, 4, 4, 0, 0, 0, 0, 1, 0);
drawCube();
In the code below, I don't understand why some faces have their normals reversed.
The triangles looks ordered in the anti-clockwise direction, but some faces remain black.
When I modify the fragment shader with color = -vnormal; the two black faces are rendered correctly, but not the others, obviously.
Thanks for any help given
// minimalist but fonctional code using glew, glfw, glm
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shaders.h"
GLuint myVAO;
void createCube() {
// v6----- v5
// /| /|
// v1------v0|
// | | | |
// | |v7---|-|v4
// |/ |/
// v2------v3
const GLfloat cube_vertices[] = {
1, 1, 1, -1, 1, 1, -1,-1, 1, // v0-v1-v2 (front)
-1,-1, 1, 1,-1, 1, 1, 1, 1, // v2-v3-v0
1, 1, 1, 1,-1, 1, 1,-1,-1, // v0-v3-v4 (right)
1,-1,-1, 1, 1,-1, 1, 1, 1, // v4-v5-v0
1, 1, 1, 1, 1,-1, -1, 1,-1, // v0-v5-v6 (top)
-1, 1,-1, -1, 1, 1, 1, 1, 1, // v6-v1-v0
-1, 1, 1, -1, 1,-1, -1,-1,-1, // v1-v6-v7 (left)
-1,-1,-1, -1,-1, 1, -1, 1, 1, // v7-v2-v1
-1,-1,-1, 1,-1,-1, 1,-1, 1, // v7-v4-v3 (bottom)
1,-1, 1, -1,-1, 1, -1,-1,-1, // v3-v2-v7
1,-1,-1, -1,-1,-1, -1, 1,-1, // v4-v7-v6 (back)
-1, 1,-1, 1, 1,-1, 1,-1,-1 }; // v6-v5-v4
// normal array
const GLfloat cube_normalsI[] = {
0, 0, 1, 0, 0, 1, 0, 0, 1, // v0-v1-v2 (front)
0, 0, 1, 0, 0, 1, 0, 0, 1, // v2-v3-v0
1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v3-v4 (right)
1, 0, 0, 1, 0, 0, 1, 0, 0, // v4-v5-v0
0, 1, 0, 0, 1, 0, 0, 1, 0, // v0-v5-v6 (top)
0, 1, 0, 0, 1, 0, 0, 1, 0, // v6-v1-v0
-1, 0, 0, -1, 0, 0, -1, 0, 0, // v1-v6-v7 (left)
-1, 0, 0, -1, 0, 0, -1, 0, 0, // v7-v2-v1
0,-1, 0, 0,-1, 0, 0,-1, 0, // v7-v4-v3 (bottom)
0,-1, 0, 0,-1, 0, 0,-1, 0, // v3-v2-v7
0, 0,-1, 0, 0,-1, 0, 0,-1, // v4-v7-v6 (back)
0, 0,-1, 0, 0,-1, 0, 0,-1 }; // v6-v5-v4
// Upload per-vertex positions
GLuint positionVBO = 0;
glGenBuffers(1, &positionVBO);
glBindBuffer(GL_ARRAY_BUFFER, positionVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices) * sizeof(GLfloat), cube_vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Upload per-vertex normals
GLuint normalVBO = 0;
glGenBuffers(1, &normalVBO);
glBindBuffer(GL_ARRAY_BUFFER, normalVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_normalsI) * sizeof(GLfloat), cube_normalsI, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Hook up vertex/normals buffers to a "vertex array object" (VAO)
glGenVertexArrays(1, &myVAO);
glBindVertexArray(myVAO);
// Attach position buffer as attribute 0
glBindBuffer(GL_ARRAY_BUFFER, positionVBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Attach normal buffer as attribute 1
glBindBuffer(GL_ARRAY_BUFFER, normalVBO);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, 0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
int main(int argc, char** argv) {
glfwInit();
GLFWwindow* window = glfwCreateWindow(768, 768, "", NULL, NULL);
glfwMakeContextCurrent(window);
glewInit();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE); // same problem with glEnable(GL_FRONT_AND_BACK);
glClearColor(0.8f, 0.7f, 0.5f, 1.0f);
unsigned int program = shaders::CreateShader("simple.vert", "simple.frag");
createCube();
while (glfwWindowShouldClose(window) == GL_FALSE) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0, 0.0, -4.0));
glm::mat4 view = glm::lookAt(glm::vec3(-2.0, -2.0, 0.0), glm::vec3(0.0, 0.0, -4.0), glm::vec3(0.0, 1.0, 0.0));
glm::mat4 projection = glm::perspective(45.0f, 1.0f, 0.1f, 10.0f);
glm::mat4 mvp = projection * view * model;
glUseProgram(program);
GLuint imvp = glGetUniformLocation(program, "mvp");
glUniformMatrix4fv(imvp, 1, false, glm::value_ptr(mvp));
glBindVertexArray(myVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glUseProgram(0);
glfwSwapBuffers(window);
}
}
The vertex shader:
#version 330 core
layout (location = 0) in vec3 in_position;
layout (location = 1) in vec3 in_normal;
uniform mat4 mvp;
out vec3 vnormal;
void main() {
vnormal = in_normal;
gl_Position = mvp * vec4(in_position,1);
}
The fragment shader:
#version 330 core
in vec3 vnormal;
out vec3 color;
void main() {
color= vnormal;
}
Output colors get clamped to the 0.0-1.0 range.
So your negative normals like -1, 0, 0 end up as RGB(0,0,0) in the color buffer.
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).
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.