Jump to content
  • Advertisement
Sign in to follow this  
Tragamota

OpenGL Printing second texture OpenGL

This topic is 871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, 

 

I'm new on this forum. 

 

I'm making a memorise game, I already got the background printed. now i'am working on to display al the different cards but I'm stuck with printing the different triangles for the cards.

 

Sorry for my English it's not the best!

 

Hope you can help me!


	//Vertex array object
	GLuint vao;
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);
	
	//Point coordinates
	static const GLfloat vertices[] = {
		//background
		-1.0f,  1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Top left 
		 1.0f,  1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // Top right
		 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // Bottom right
		-1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,  // Bottom left

		//card 1
		-0.84f, 0.92f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Top-left
		-0.54f, 0.92f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // Top-right
		-0.54f, 0.52f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
		-0.84f, 0.52f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,  // Bottom-left
		
		//card 2
		-0.38f, 0.92f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Top-left
		-0.08f, 0.92f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // Top-right
		-0.08f, 0.52f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
		-0.38f, 0.52f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,  // Bottom-left

		//card 3
		0.08f, 0.92f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Top-left
		0.38f, 0.92f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // Top-right
		0.38f, 0.52f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
		0.08f, 0.52f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, // Bottom-left

		//card 4
		0.54f, 0.92f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Top-left
		0.84f, 0.92f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // Top-right
		0.84f, 0.52f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
		0.54f, 0.52f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, // Bottom-left
	};

	GLuint vertexbuffer;
	glGenBuffers(1, &vertexbuffer);
	glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

	GLuint element[] = {
		0,1,2,
		2,3,0,

		5,6,7,
		7,8,5,
	};

	GLuint ebo;
	glGenBuffers(1, &ebo);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(element), element, GL_STATIC_DRAW);
	
#include <stdio.h>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

#include <stdlib.h>
#include <string.h>

#include <GL/glew.h>

const GLchar* vertexSource =
"#version 150 core\n"
"in vec2 position;"
"in vec3 color;"
"in vec2 texcoord;"
"out vec3 Color;"
"out vec2 Texcoord;"
"void main()"
"{"
"    Color = color;"
"    Texcoord = texcoord;"
"    gl_Position = vec4(position, 0.0, 1.0);"
"}";
const GLchar* fragmentSource =
"#version 150 core\n"
"in vec3 Color;"
"in vec2 Texcoord;"
"out vec4 outColor;"
"uniform sampler2D background;"
"void main()"
"{"
"    outColor = texture(background, Texcoord) * vec4(Color, 1.0);"
"}";

//Copied this piece of code
void Shaders(){

	// Create and compile the vertex shader
	GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertexShader, 1, &vertexSource, NULL);
	glCompileShader(vertexShader);

	// Create and compile the fragment shader
	GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
	glCompileShader(fragmentShader);

	// Link the vertex and fragment shader into a shader program
	GLuint shaderProgram = glCreateProgram();
	glAttachShader(shaderProgram, vertexShader);
	glAttachShader(shaderProgram, fragmentShader);
	glBindFragDataLocation(shaderProgram, 0, "outColor");
	glLinkProgram(shaderProgram);
	glUseProgram(shaderProgram);

	GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
	glEnableVertexAttribArray(posAttrib);
	glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), 0);

	GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
	glEnableVertexAttribArray(colAttrib);
	glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));

	GLint texAttrib = glGetAttribLocation(shaderProgram, "texcoord");
	glEnableVertexAttribArray(texAttrib);
	glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(5 * sizeof(GLfloat)));
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <SOIL.h>

#include "backgroundImage.h"

void loadTextureBackground() {

	GLuint tex[2];
	glGenTextures(1, tex);
	glBindTexture(GL_TEXTURE_2D, tex[0]);
	int width, height;

	unsigned char* backgroundImage = SOIL_load_image("Honingbackground.png", &width, &height, 0, SOIL_LOAD_RGB);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, backgroundImage);
	SOIL_free_image_data(backgroundImage);

	glBindTexture(GL_TEXTURE_2D, tex[0]);
	unsigned char* QuestionImage = SOIL_load_image("Honingbackground.png", &width, &height, 0, SOIL_LOAD_RGB);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, backgroundImage);
	SOIL_free_image_data(QuestionImage);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}

Ian

Share this post


Link to post
Share on other sites
Advertisement

first of all you need to fix vertex data it seems it has way too much numbers in array than actual vertices, try setting 4 vertices for each card. this will give you 4 * 3 * 4 (cards)+ 4*3 (background) length of vertex array

 

 

additionally you didint show your drawing code (glDrawArrays or something liek that) and you didint show shader code. and you didint show us the output screen so we can see what problem you get.

Share this post


Link to post
Share on other sites

I notice you are defining your triangles in a clockwise direction. They may be getting culled if you have backface culling enabled. Disable that while you are testing. Of course that all depends on where you are facing and since you said you have the background working then I guess it is ok.

 

I think your problem might lie here:

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

 

You are saying that attribute 0 has 3 elements but from your shader it looks like it actually only has 2 elements so the 3 should be a 2. you also set the stride to be 0 which means it will assume each vertex  is 3 floats big but your vertices are 7*sizeof(float) big so put that in for stride.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!