AKA What am I doing wrong?
I've been messing around with OpenGL and I'm just trying to work on lighting a cube right now. I'm not sure if I'm understanding what I'm supposed to do correctly because when I move the camera around, the lighting on the cube changes.
For example:
Looking at the cube from the top down:
Looking at the cube from the side:
From searching around all of the answers that I've seen say that this happens when the normal isn't set correctly, but I think they are being set correctly, because when I print out all of the vertices along with their normals, this is the result (grouped by face, in the order they're drawn):
Position: 0 0 0 Normal: -1 0 0
Position: 0 30 0 Normal: -1 0 0
Position: 0 30 30 Normal: -1 0 0
Position: 0 0 30 Normal: -1 0 0
Position: 0 0 0 Normal: 0 1 0
Position: 0 0 30 Normal: 0 1 0
Position: 30 0 30 Normal: 0 1 0
Position: 30 0 0 Normal: 0 1 0
Position: 0 0 0 Normal: 0 0 -1
Position: 30 0 0 Normal: 0 0 -1
Position: 30 30 0 Normal: 0 0 -1
Position: 0 30 0 Normal: 0 0 -1
Position: 0 0 30 Normal: 0 0 1
Position: 0 30 30 Normal: 0 0 1
Position: 30 30 30 Normal: 0 0 1
Position: 30 0 30 Normal: 0 0 1
Position: 0 30 0 Normal: 0 -1 0
Position: 30 30 0 Normal: 0 -1 0
Position: 30 30 30 Normal: 0 -1 0
Position: 0 30 30 Normal: 0 -1 0
Position: 30 0 0 Normal: 1 0 0
Position: 30 0 30 Normal: 1 0 0
Position: 30 30 30 Normal: 1 0 0
Position: 30 30 0 Normal: 1 0 0
Here's also some of the code used for rendering in case the mistake is in there:
RenderEngine::RenderEngine(int width, int height) {
//initializing the window...
glClearDepth(1.f);
glClearColor(217.f / 256.f, 233.f / 256.f, 255.f / 256.f, 1.f);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glFrontFace(GL_CW);
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
//glEnable(GL_COLOR_MATERIAL);
GLfloat lightPos[] = { 0.f, -1.0f, 0.0f, 0.f };
GLfloat ambient[] = {0.3f,0.3f,0.3f,1.0f};
GLfloat diffuse[] = {0.7f,0.7f,0.7f,1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glEnable(GL_LIGHT0);
//more window related things
}
void RenderEngine::beginRender() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void RenderEngine::endRender() {
//window stuff
}
void RenderEngine::translatePlayer(const sf::Vector3f& position) {
glTranslatef(-(position.x + 0.5) * 30, -(position.y + 1.75) * 30, -(position.z + 0.5) * 30);
}
void RenderEngine::rotatePlayer(const sf::Vector3f& rotation) {
glRotatef(rotation.x, 1.f, 0.f, 0.f);
glRotatef(rotation.y, 0.f, 1.f, 0.f);
glRotatef(rotation.z, 0.f, 0.f, 1.f);
}
void RenderEngine::renderVertexArray(const std::vector<Vertex>& vertices) {
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].pos[0]);
glColorPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].color[0]);
glNormalPointer(GL_FLOAT, sizeof(Vertex), &vertices[0].normal[0]);
glDrawArrays(GL_QUADS, 0, vertices.size());
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
And the vertex object:
struct Vertex {
float pos[3];
float color[3];
float normal[3];
Vertex(float _pos[3], float _color[3], float _normal[3]) :
pos {_pos[0], _pos[1], _pos[2]},
color {_color[0], _color[1], _color[2]},
normal{_normal[0], _normal[1], _normal[2]} {}
Vertex() : pos{0,0,0}, color{0,0,0}, normal{0,0,0} {}
};
Please ignore all the random 30's. I'm aware that those are out of place and should not be done that way, but that's not the issue here.
When you call the following:
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
... then the passed lightPos is transformed with the current model-view matrix and then stored in camera coordinates. Thus, your light will move together with the camera. If you want it to be static, you have to execute the above line again after setting the model-view matrix.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
In opengl, I was attempting to write a program that would be able to draw multiple rectangles across the screen using triangles. Instead of writing down all of the vertices by hand, I wrote a nested for loop to generate the vertices. However, instead of drawing all the triangles, this program only outputs the last two triangles as a rectangle(see the pictures below). I'm sure that this way of generating triangles is hilariously bad and inefficient but that's not my main gripe with the output of this code.
Below is the nested for loop that adds the vertices to the array(be warned this code is absolutely disgusting)
float initTri1[] = { -1.5f, 0.5f, 0.0f, -1.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f };
float initTri2[] = { -1.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.5f, 0.0f };
float vertices[18*4];
float increment =0.0f;
// draws an amount of rectangles equal to the number after 18 in vertices
for (int j = 0; j < sizeof(vertices)/sizeof(vertices[0])/18; j++)
{
increment += 0.5f;
// draws triangle with hypotenuse on right side
for (int i = 0; i < 9; i++)
{
// shifts the value of the initial triangles x verticies by 0.5
if ((i + 1) % 3 != 1)
{
vertices[i+j*9] = initTri1[i];
}
// keeps the y and z values the same as the initial triangle.
else
{
vertices[i+j*9] = initTri1[i] + increment;
}
}
// sometimes draws the triangle with the hypotenuse on the left side
for (int i = 9; i < 18; i++)
{
// shifts the value of the initial triangles x verticies by 0.5
if ((i + 1) % 3 != 1)
{
vertices[i+j*9] = initTri2[i - 9];
}
// keeps the y and z values the same as the initial triangle.
else
{
vertices[i+j*9] = initTri2[i - 9] + increment;
}
}
}
Below are images of the outcome of generating: 2 triangles, 4 triangles, and 8 triangles respectively.
I didn't feel able to follow the index computations of OP. It was easier to try it out. The result looks wrong:
vertices[0]: -1, 0.5, 0
vertices[3]: -1, 0, 0
vertices[6]: -0.5, 0, 0
vertices[9]: -0.5, 0.5, 0
vertices[12]: -0.5, 0, 0
vertices[15]: 0, 0, 0
vertices[18]: 0, 0.5, 0
vertices[21]: 0, 0, 0
vertices[24]: 0.5, 0, 0
vertices[27]: 0.5, 0.5, 0
vertices[30]: 0.5, 0, 0
vertices[33]: 1, 0, 0
vertices[36]: 0.5, 0.5, 0
vertices[39]: 1, 0, 0
vertices[42]: 1, 0.5, 0
vertices[45]: 0, 8.40779e-45, 0
vertices[48]: 8.82286e-39, 0, 8.82332e-39
vertices[51]: 0, 5.87998e-39, 0
vertices[54]: 5.87998e-39, 0, 8.82286e-39
vertices[57]: 0, -4.13785e+09, 4.58841e-41
vertices[60]: 1.4013e-45, 0, 2.8026e-45
vertices[63]: 0, 8.82195e-39, 0
vertices[66]: 5.88135e-39, 0, 0
vertices[69]: 0, 0, 0
Demo on coliru
So, I just rewrote the loops instead of tediously debugging it. (That appeared the lesser evil to me.)
#include <iostream>
int main()
{
float initTri1[] = { -1.5f, 0.5f, 0.0f, -1.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f };
const size_t nTri1 = std::size(initTri1);
float initTri2[] = { -1.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.5f, 0.0f };
const size_t nTri2 = std::size(initTri2);
const size_t nRect = 4;
const size_t nVtcs = (nTri1 + nTri2) * nRect;
float vertices[nVtcs];
float increment = 0.0f;
for (size_t j = 0, k = 0; j < nRect; ++j) {
for (size_t i = 2; i < nTri1; i += 3) {
vertices[k++] = initTri1[i - 2] + increment;
vertices[k++] = initTri1[i - 1];
vertices[k++] = initTri1[i - 0];
}
for (size_t i = 2; i < nTri2; i += 3) {
vertices[k++] = initTri2[i - 2] + increment;
vertices[k++] = initTri2[i - 1];
vertices[k++] = initTri2[i - 0];
}
increment += 0.5f;
}
for (size_t k = 0; k < nVtcs; ++k) {
if (k % (nTri1 + nTri2) == 0) std::cout << '\n';
if (k % 3 == 0) {
std::cout << "vertices[" << k << "]: ";
}
std::cout << vertices[k];
std::cout << (k % 3 < 2 ? ", " : "\n");
}
}
Output:
vertices[0]: -1.5, 0.5, 0
vertices[3]: -1.5, 0, 0
vertices[6]: -1, 0, 0
vertices[9]: -1.5, 0.5, 0
vertices[12]: -1, 0, 0
vertices[15]: -1, 0.5, 0
vertices[18]: -1, 0.5, 0
vertices[21]: -1, 0, 0
vertices[24]: -0.5, 0, 0
vertices[27]: -1, 0.5, 0
vertices[30]: -0.5, 0, 0
vertices[33]: -0.5, 0.5, 0
vertices[36]: -0.5, 0.5, 0
vertices[39]: -0.5, 0, 0
vertices[42]: 0, 0, 0
vertices[45]: -0.5, 0.5, 0
vertices[48]: 0, 0, 0
vertices[51]: 0, 0.5, 0
vertices[54]: 0, 0.5, 0
vertices[57]: 0, 0, 0
vertices[60]: 0.5, 0, 0
vertices[63]: 0, 0.5, 0
vertices[66]: 0.5, 0, 0
vertices[69]: 0.5, 0.5, 0
Demo on coliru
The moral of the story:
Simpler code is faster to write.
Simpler code is running sooner.
Simpler code is maintenance friendly.
Profiling it, you might be surprised that simpler code might be even faster.
I am trying to implement the following transformation.
My original world-space coordinates are (2D) x=1586266800 and y=11812
I want:
the bottom left corner of the OpenGL image to represent coordinates (1586266800, 11800)
the top right corner of the OpenGL image to represent coordinates (1586267400, 11900)
In order to do that I plan to join three transformation matrices:
Translate to the origin of coordinates x=1586266800 and y=11800
Scale to have a width of 600 and a height of 100
Translate again -1.0f and -1.0f so the center of the OpenGL is at the bottom left.
I use the following transformation matrices:
Translation Matrix:
| 1 0 0 tx |
| 0 1 0 ty |
| 0 0 1 tz |
| 0 0 0 1 |
Scale Matrix:
| sx 0 0 0 |
| 0 sy 0 0 |
| 0 0 sz 0 |
| 0 0 0 1 |
In Octave I can implement the transformation as follows, multiplying three matrices:
>> candle
candle =
1586266800
11812
0
1
>> translation1
translation1 =
1 0 0 -1586266800
0 1 0 -11800
0 0 1 0
0 0 0 1
>> scale
scale =
0.00333333333333333 0 0 0
0 0.02 0 0
0 0 1 0
0 0 0 1
(where `0.0033333 = 2/600` and `0.02 = 2/100`)
>> translation2
translation2 =
1 0 0 -1
0 1 0 -1
0 0 1 0
0 0 0 1
>> translation2*scale*translation1*candle
ans =
-1
-0.759999999999991
0
1
Which translates the point to the right place in a -1.0f,1.0f OpenGL screen.
Now I am trying to replicate that in my Geometry shader, which receives the original world-space coordinates from the vertex shader.
I tried this:
#version 330 core
layout (points) in;
layout (line_strip, max_vertices = 12) out;
in uint gs_in_y[];
in uint gs_in_x[];
uniform uint xOrigin;
uniform uint xScaleWidth;
uniform uint yOrigin;
uniform uint yScaleWidth;
void main()
{
// TRANSLATION MATRIX
// [ 1 0 0 tx ]
// [ 0 1 0 ty ]
// [ 0 0 1 tz ]
// [ 0 0 0 1 ]
// mat3 m = mat3(
// 1.1, 2.1, 3.1, // first column (not row!)
// 1.2, 2.2, 3.2, // second column
// 1.3, 2.3, 3.3 // third column
// );
mat4 translation = mat4(
1.0f, 0, 0, -xOrigin,
0, 1.0f, 0, -yOrigin,
0, 0, 1.0f, 0,
0, 0, 0, 1.0f
);
// SCALE MATRIX
// [ sx 0 0 0 ]
// [ 0 sy 0 0 ]
// [ 0 0 sz 0 ]
// [ 0 0 0 1 ]
mat4 scale = mat4(
2.0/xScaleWidth, 0, 0, 0,
0, 2.0f/yScaleWidth, 0, 0,
0, 0, 1.0f, 0,
0, 0, 0, 1.0f
);
// FINAL TRANSLATION
mat4 translationGl = mat4(
1.0f, 0, 0, -1.0f,
0, 1.0f, 0, -1.0f,
0, 0, 1.0f, 0,
0, 0, 0, 1.0f
);
gl_Position = translationGl * scale * translation * vec4(gs_in_x[0], gs_in_y[0], 0.0, 1.0);
EmitVertex();
gl_Position = translationGl * scale * translation * vec4(gs_in_x[0]+30, gs_in_y[0], 0.0, 1.0);
EmitVertex();
EndPrimitive();
}
I am trying to calculate the inverse of the square matrix but It is not working. I checked previous posts but the logic is same but I haven't still found where is the problem. I also share Matlab result for example matrix.
program test
Implicit none
real,allocatable,dimension(:,:) :: A
real,allocatable,dimension(:) :: WORK
integer ,allocatable,dimension(:) :: ipiv
integer :: n,info,M
external SGETRF,SGETRI
M=8
allocate(A(M,M),WORK(M),IPIV(M))
A(1,:)=(/3.74E-4, 0.0, 0.0, 4.98E-5, 0.0, 0.0, 0.0, 0.0/)
A(2,:)=(/0.0 , 3.74E-4, 0.0, 0.0, 4.98E-5 ,0.0 ,0.0 ,0.0 /)
A(3,:)=(/0.0 , 0.0 ,3.74E-4, 0.0 ,0.0, 4.98E-5, 0.0 ,0.0/)
A(4,:)=(/4.98E-5 ,0.0 ,0.0 ,6.64e-6, 0.0 ,0.0, 0.0, 0.0 /)
A(5,:)=(/0.0 , 4.98E-5, 0.0, 0.0 ,6.64E-6 ,0.0 ,0.0 ,0.0 /)
A(6,:)=(/0.0, 0.0, 4.98E-5, 0.0 ,0.0, 6.64E-6, 0.0 ,0.0 /)
A(7,:)=(/0.0, 0.0 ,0.0, 0.0 ,0.0 ,0.0 ,1.49E-11, 0.0 /)
A(8,:)=(/0.0 ,0.0 ,0.0 ,0.0 ,0.0 ,0.0, 0.0 ,1.49E-11 /)
call SGETRF(M,M,A,M,IPIV,info)
if(info .eq. 0) then
Print *,'succeded'
else
Print *,'failed'
end if
call SGETRI(M,A,M,IPIV,WORK,M,info)
if(info .eq. 0) then
Print *,'succeded'
else
Print *,'failed'
end if
Print *,A
deallocate(A,IPIV,WORK)
end
!!!!! Matlab Result
!1.0e+10 *
! 0.0002 0 0 -0.0015 0 0 0 0
! 0 0.0002 0 0 -0.0015 0 0 0
! 0 0 0.0002 0 0 -0.0015 0 0
! -0.0015 0 0 0.0113 0 0 0 0
! 0 -0.0015 0 0 0.0113 0 0 0
! 0 0 -0.0015 0 0 0.0113 0 0
! 0 0 0 0 0 0 6.7114 0
! 0 0 0 0 0 0 0 6.7114
Your reals are only single precision. The lapack D prefix implies double precision. Two fixes:
Change your DGs to SGs
Keep your DGs and use double precision
I am currently working on Oculus Rift PC SDK. Was trying to start off with something simpler like Tiny Room Demo(DX11). Saw this tutorial online to load a 3D model into the scene from external file (Rastertek
Tutorial 7: 3D Model Rendering)
The way the Tiny Room Demo creates model is to hardcode the coordinates and renders it
TriangleSet walls;
walls.AddSolidColorBox(10.1f, 0.0f, 20.0f, 10.0f, 4.0f, -20.0f, 0xff808080); // Left Wall
walls.AddSolidColorBox(10.0f, -0.1f, 20.1f, -10.0f, 4.0f, 20.0f, 0xff808080); // Back Wall
walls.AddSolidColorBox(-10.0f, -0.1f, 20.0f, -10.1f, 4.0f, -20.0f, 0xff808080); // Right Wall
Add(
new Model(&walls, XMFLOAT3(0, 0, 0), XMFLOAT4(0, 0, 0, 1),
new Material(
new Texture(false, 256, 256, Texture::AUTO_WALL)
)
)
);
void AddSolidColorBox(float x1, float y1, float z1, float x2, float y2, float z2, uint32_t c)
{
AddQuad(Vertex(XMFLOAT3(x1, y2, z1), ModifyColor(c, XMFLOAT3(x1, y2, z1)), z1, x1),
Vertex(XMFLOAT3(x2, y2, z1), ModifyColor(c, XMFLOAT3(x2, y2, z1)), z1, x2),
Vertex(XMFLOAT3(x1, y2, z2), ModifyColor(c, XMFLOAT3(x1, y2, z2)), z2, x1),
Vertex(XMFLOAT3(x2, y2, z2), ModifyColor(c, XMFLOAT3(x2, y2, z2)), z2, x2));
...}
AddQuad(Vertex v0, Vertex v1, Vertex v2, Vertex v3) { AddTriangle(v0, v1, v2); AddTriangle(v3, v2, v1); }
void AddTriangle(Vertex v0, Vertex v1, Vertex v2)
{
VALIDATE(numVertices <= (maxBuffer - 3), "Insufficient triangle set");
for (int i = 0; i < 3; i++) Indices[numIndices++] = short(numVertices + i);
Vertices[numVertices++] = v0;
Vertices[numVertices++] = v1;
Vertices[numVertices++] = v2;
}
Tried to load the model into the scene using a function from the tutorial
TriangleSet models;
models.LoadModel("F:\\cube.txt");
Add(
new OBJModel(&models, XMFLOAT3(0, 0, 0), XMFLOAT4(0, 0, 0, 1),
new OBJMaterial(
new Texture(false, 256, 256, Texture::AUTO_WHITE)
//new Texture(DirectX, L"wallpaper.jpg")
)
)
); //3D Model
void LoadModel(char* filename)
{
ifstream fin;
char input;
// Open the model file.
fin.open(filename);
// Read up to the value of vertex count.
fin.get(input);
while (input != ':')
{
fin.get(input);
}
// Read in the vertex count.
m_vertexCount = 0;
fin >> m_vertexCount;
// Read up to the beginning of the data.
fin.get(input);
while (input != ':')
{
fin.get(input);
}
fin.get(input);
fin.get(input);
// Read in the vertex data.
for (int i = 0; i<m_vertexCount; i++)
{
Indices[numIndices++] = short(numVertices + i);
//numVertices++; deleted
fin >> Vertices[numVertices].Pos.x >> Vertices[numVertices].Pos.y >> Vertices[numVertices].Pos.z;
fin >> Vertices[numVertices].U >> Vertices[numVertices].V;
fin >> Normals[numVertices].Norm.x >> Normals[numVertices].Norm.y >> Normals[numVertices].Norm.z;
Vertices[numVertices].C = ModifyColor(0xffffffff, Vertices[numVertices].Pos);
numVertices+=1; //new statement
}
// Close the model file.
fin.close();
}
I did not use the normal as from the tutorial it was meant for the texture of the object. Instead I defined the color to be solid yellow. Tried to keep the structure of loading the model as similar to Tiny Room Demo as possible.
I have used the same model, material and texture (vertex shader and pixel shader) as how Tiny Room Demo does. However what was rendered onto the scene did not appear as what it is supposed to be.
Did a step by step debugging to see if the coordinates were correctly loading into the Vertices[numVertices]. Seems like there is no issue. The file I tried to load was cube.txt
Vertex Count: 36
Data:
-1.0 1.0 -1.0 0.0 0.0 0.0 0.0 -1.0
1.0 1.0 -1.0 1.0 0.0 0.0 0.0 -1.0
-1.0 -1.0 -1.0 0.0 1.0 0.0 0.0 -1.0
-1.0 -1.0 -1.0 0.0 1.0 0.0 0.0 -1.0
1.0 1.0 -1.0 1.0 0.0 0.0 0.0 -1.0
1.0 -1.0 -1.0 1.0 1.0 0.0 0.0 -1.0
1.0 1.0 -1.0 0.0 0.0 1.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 1.0 0.0 0.0
1.0 -1.0 -1.0 0.0 1.0 1.0 0.0 0.0
1.0 -1.0 -1.0 0.0 1.0 1.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0 1.0 0.0 0.0
1.0 -1.0 1.0 1.0 1.0 1.0 0.0 0.0
1.0 1.0 1.0 0.0 0.0 0.0 0.0 1.0
-1.0 1.0 1.0 1.0 0.0 0.0 0.0 1.0
1.0 -1.0 1.0 0.0 1.0 0.0 0.0 1.0
1.0 -1.0 1.0 0.0 1.0 0.0 0.0 1.0
-1.0 1.0 1.0 1.0 0.0 0.0 0.0 1.0
-1.0 -1.0 1.0 1.0 1.0 0.0 0.0 1.0
...
What was suppose to show up (except without the texture)
3D cube
What actually showed up was just fragments of triangle
TinyRoomDemo + 3D cube
Unsure what went wrong. Please do advice! Thank you very much :)
Vertex and Index buffer
struct OBJModel
{
XMFLOAT3 Pos;
XMFLOAT4 Rot;
OBJMaterial * Fill;
DataBuffer * VertexBuffer;
DataBuffer * IndexBuffer;
int NumIndices;
OBJModel() : Fill(nullptr), VertexBuffer(nullptr), IndexBuffer(nullptr) {};
void Init(TriangleSet * t)
{
NumIndices = t->numIndices;
VertexBuffer = new DataBuffer(DIRECTX.Device, D3D11_BIND_VERTEX_BUFFER, &t->Vertices[0], t->numVertices * sizeof(Vertex));
IndexBuffer = new DataBuffer(DIRECTX.Device, D3D11_BIND_INDEX_BUFFER, &t->Indices[0], t->numIndices * sizeof(short));
}
...
DIRECTX.Context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
---------------------------------------------------------------------------------------------------------------------------------
06/06/2017 edited:
3D Model data:
Vertex Count: 798
Data:
28.3005 0.415886 -45.8282 0.7216 0.720211 0 0 -1
28.3005 -0.809079 -45.8282 0.732222 0.720211 0 0 -1
-27.7441 -0.809079 -45.8282 0.732222 0.847836 0 0 -1
28.3005 0.415886 68.1056 0.459891 0.720286 0 1 -0
28.3005 0.415886 -45.8282 0.719341 0.720286 0 1 -0
-27.7441 0.415886 -45.8282 0.719341 0.847911 0 1 -0
28.3005 -0.809079 68.1056 0.721603 0.720211 0 0 1
28.3005 0.415886 68.1056 0.732225 0.720211 0 0 1
-27.7441 0.415886 68.1056 0.732225 0.847836 0 0 1
28.3005 -0.809079 -45.8282 0.459891 0.720298 0 -1 -0
28.3005 -0.809079 68.1056 0.719341 0.720298 0 -1 -0
-27.7441 -0.809079 68.1056 0.719341 0.847923 0 -1 -0
28.3005 0.415886 68.1056 0.719341 0.70683 1 0 -0
...
From the data u provided for the house, it seems like 1 triangle is facing in one way and second is facing the opposite way.
Use rasterizer without back culling to draw this object
Hello People of Stackoverflowaria,
My if functions are not working. It's a logical error. Can someone spot the error? I've been looking through the code for ages... also, any way to improve my formatting / where I put my variables would be cool.
The logical error is all based around bgtoggle. The if statements are pointed out by the comments.
Thanks in advance!
Luca
Here's main.cpp:
#include "SDL.h"
#include "SDL_opengl.h"
#include <iostream>
void drawBox( int xpos , int ypos , int bwidth , int bheight );
void drawCrossHair();
int width = 400;
int height = 800;
bool bgtoggle = false;
int main(int argc, char* args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
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_BUFFER_SIZE, 32 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_WM_SetCaption( "Our First Game" , NULL );
SDL_SetVideoMode( width , height, 32 , SDL_OPENGL );
glClearColor( 1 , 1 , 1, 1 );
glViewport( 0,0 , width,height );
glShadeModel( GL_SMOOTH );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
bool isRunning = true;
SDL_Event event;
while ( isRunning )
{
while ( SDL_PollEvent( &event ) )
{
if ( event.type == SDL_QUIT )
isRunning = false;
if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE )
isRunning = false;
//THIS IF STATEMANT
if ( bgtoggle == false && event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_b )
{
glClearColor( 0 , 0 , 1 , 1 );
bgtoggle = true;
}
// AND THIS IF STATEMENT
if ( bgtoggle == true && event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_b) {
{
glClearColor( 1 , 1 , 1 , 1 );
bgtoggle = false;
}
}
glClear( GL_COLOR_BUFFER_BIT );
glPushMatrix();
glOrtho( 0 , width , height , 0 , -1 , 1 );
glBegin(GL_QUADS);
glColor4ub( 255 , 80 , 80 , 255 );
glVertex2f( 5 , 5 );
glVertex2f( width - 5 , 5 );
glColor4ub( 0 , 0 , 255 , 255 );
glVertex2f( width - 5 , height - 5 );
glVertex2f( 5 , height - 5 );
glEnd();
drawCrossHair();
glPopMatrix();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
void drawCrossHair()
{
glBegin( GL_LINES );
glColor4ub( 0 , 0 , 0 , 255);
glVertex2f( width / 2 - 5 , height / 2 );
glVertex2f( width / 2 - 15 , height / 2 );
glVertex2f( width / 2 + 5, height / 2 );
glVertex2f( width / 2 + 15 , height / 2 );
glVertex2f( width / 2 , height / 2 - 5 );
glVertex2f( width / 2 , height / 2 - 15 );
glVertex2f( width / 2 , height / 2 + 5 );
glVertex2f( width / 2 , height / 2 + 15 );
glEnd();
glBegin( GL_QUADS );
glVertex2f( width / 2 - 1 , height / 2 + 1 );
glVertex2f( width / 2 + 1 , height / 2 + 1 );
glVertex2f( width / 2 + 1 , height / 2 - 1 );
glVertex2f( width / 2 - 1 , height / 2 - 1 );
glEnd();
glBegin( GL_POINTS );
glVertex2f( width / 2 , height / 2 );
glEnd();
}
This is very simple. In your first if you set bgtoggle to true. This makes the condition for the second if true, which sets bgtoggle to false again.
You'd better do:
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_b )
bgtoggle = ! bgtoggle;
if(bgtoggle)
glClearColor( 0 , 0 , 1 , 1 );
else
glClearColor( 1 , 1 , 1 , 1 );