Fixed texture rendering

This commit is contained in:
2016-05-23 15:34:11 +02:00
parent 8bd092c9e9
commit 24a9ccd7fa
3 changed files with 31 additions and 9 deletions
+27 -5
View File
@@ -5,6 +5,8 @@
#include <iostream>
#include <string>
GLuint imageIndex;
HeightMap::HeightMap(const std::string &file)
{
int bpp;
@@ -17,16 +19,31 @@ HeightMap::HeightMap(const std::string &file)
int offsets[4][2] = { { 0, 0 },{ 1, 0 },{ 1, 1 },{ 0, 1 } };
for (int i = 0; i < 4; i++)
{
float height = ((float)imgData[((h + offsets[i][0]) + (w + offsets[i][1]) * width) * 4]);
height = (height / 256.0f) * 100.0f;
float y = ((float)imgData[((h + offsets[i][0]) + (w + offsets[i][1]) * width) * 4]);
y = (y / 256.0f) * 100.0f;
vertices.push_back(Vertex{ (float)(h + offsets[i][0]), height, (float)(w + offsets[i][1]),
vertices.push_back(Vertex{ (float)(h + offsets[i][0]), y, (float)(w + offsets[i][1]),
0, 1, 0,
h*0.1f, (w*0.f) + offsets[i][1] });
(h + offsets[i][0]) / (float)height, (w + offsets[i][1]) / (float)width } );
}
}
}
glGenTextures(1, &imageIndex);
glBindTexture(GL_TEXTURE_2D, imageIndex);
glTexImage2D(GL_TEXTURE_2D,
0, //level
GL_RGBA, //internal format
width, //width
height, //height
0, //border
GL_RGBA, //data format
GL_UNSIGNED_BYTE, //data type
imgData); //data
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
stbi_image_free(imgData);
}
@@ -36,16 +53,21 @@ HeightMap::~HeightMap()
void HeightMap::Draw()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, imageIndex);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//glEnableClientState(GL_COLOR_ARRAY);
//glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 0);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 6);
//glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)cubeVertices.data()) + 3);
//glColorPointer(4, GL_FLOAT, sizeof(Vertex), ((float*)cubeVertices.data()) + 6);
glDrawArrays(GL_QUADS, 0, vertices.size());
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//glDisableClientState(GL_COLOR_ARRAY);
//glDisableClientState(GL_NORMAL_ARRAY);
}
+4 -4
View File
@@ -74,7 +74,7 @@ void display()
float pos[4] = { 0.5, 1, -1, 0 };
glLightfv(GL_LIGHT0, GL_POSITION, pos);
/*
glColor3f(0.1f, 1.0f, 0.2f);
glBegin(GL_QUADS);
glVertex3f(-15, -1, -15);
@@ -83,7 +83,6 @@ void display()
glVertex3f(-15, -1, 15);
glEnd();
/*
for (int x = -10; x <= 10; x += 5)
{
for (int y = -10; y <= 10; y += 5)
@@ -101,6 +100,7 @@ void display()
*/
glColor3f(1.0f, 1.0f, 1.0f);
heightmap->Draw();
glutSwapBuffers();
@@ -176,7 +176,7 @@ int main(int argc, char* argv[])
memset(keys, 0, sizeof(keys));
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
//glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glutIdleFunc(idle);
@@ -186,7 +186,7 @@ int main(int argc, char* argv[])
glutKeyboardUpFunc(keyboardUp);
glutPassiveMotionFunc(mousePassiveMotion);
heightmap = new HeightMap("worlds/HMCSHeightmap.gif");
heightmap = new HeightMap("worlds/hell.png");
cubeVertices.push_back(Vertex{ -1, -1, -1, 0,0,1, 1,1,1,1 });
cubeVertices.push_back(Vertex{ -1, 1, -1, 0,0,1, 1,1,1,1 });
Binary file not shown.

After

Width:  |  Height:  |  Size: 257 KiB